ifw-daq 3.1.0
IFW Data Acquisition modules
Loading...
Searching...
No Matches
source.hpp
Go to the documentation of this file.
1/**
2 * @file
3 * @ingroup daq_common_libdaq
4 * @copyright 2022 ESO - European Southern Observatory
5 *
6 * @brief Declarations for `daq::Source` and related classes.
7 */
8#ifndef OCF_DAQ_SOURCE_HPP_
9#define OCF_DAQ_SOURCE_HPP_
10#include "config.hpp"
11
12#include <memory>
13#include <string>
14
15#include <Metadaqif.hpp>
16#include <Recif.hpp>
17#include <boost/signals2/signal.hpp>
18#include <fmt/ostream.h>
19
20#include "state.hpp"
21
22namespace daq {
23
24/**
25 * Simple class that holds the source and associated state.
26 *
27 * @ingroup daq_common_libdaq
28 */
29template <class T>
30struct Source {
31 using StateSignal = boost::signals2::signal<void(State, bool)>;
32
33 Source(T&) = delete;
34 Source(T&& s) : m_source(std::move(s)) {
35 }
36 friend std::ostream& operator<<(std::ostream& os, Source<T> const& s) {
37 os << s.GetSource() << ", state=" << s.GetState()
38 << ", error_flag=" << (s.GetErrorFlag() ? "true" : "false");
39 return os;
40 }
41
42 /**
43 * Connect `subscriber` that is invoked on state changes.
44 *
45 * Type requirements of `subscriber`:
46 * Signature void(State, bool)
47 */
48 template <class Subscriber>
49 boost::signals2::connection ConnectStateListener(Subscriber subscriber) {
50 return m_state_signal.connect(std::move(subscriber));
51 }
52
53 void SetState(State state, std::optional<bool> error_flag = {}) {
54 if (error_flag) {
55 m_error_flag = *error_flag;
56 }
57 m_state = state;
59 }
60
62 m_error_flag = false;
64 }
65
66 void SetErrorFlag() {
67 m_error_flag = true;
69 }
70
71 bool GetErrorFlag() const {
72 return m_error_flag;
73 }
74 State GetState() const {
75 return m_state;
76 }
77
78 T& GetSource() {
79 return m_source;
80 }
81
82 T const& GetSource() const {
83 return m_source;
84 }
85
86protected:
89 bool m_error_flag = false;
91};
92
93/**
94 * Keeps relevant state to be able to communicate with a primary data source.
95 *
96 * @ingroup daq_common_libdaq
97 */
99public:
100 using RrClient = recif::RecCmdsAsync;
101
102 /**
103 * @throws std::invalid_argument if name or client is invalid.
104 */
105 PrimSource(std::string name, std::shared_ptr<RrClient> client)
106 : m_name(std::move(name)), m_rr_client(std::move(client)) {
107 if (m_name.empty()) {
108 throw std::invalid_argument("'name' is invalid");
109 }
110 if (!m_rr_client) {
111 throw std::invalid_argument("'client' is invalid");
112 }
113 }
114 PrimSource(PrimSource&&) = default;
115 PrimSource(PrimSource const&) = default;
118
119 std::string const& GetName() const {
120 return m_name;
121 }
122
124 return *m_rr_client;
125 }
126
127 friend std::ostream& operator<<(std::ostream& os, PrimSource const& s) {
128 os << "PrimSource: " << s.m_name;
129 return os;
130 }
131
132private:
133 std::string m_name;
134 std::shared_ptr<RrClient> m_rr_client;
135};
136
137/**
138 * Keeps relevant state to be able to communicate with a primary data source.
139 *
140 * @ingroup daq_common_libdaq
141 */
143public:
144 using RrClient = metadaqif::MetaDaqAsync;
145
146 /**
147 * @throws std::invalid_argument if name or client is invalid.
148 */
149 MetaSource(std::string name, std::shared_ptr<RrClient> client)
150 : m_name(std::move(name)), m_rr_client(std::move(client)) {
151 if (m_name.empty()) {
152 throw std::invalid_argument("'name' is invalid");
153 }
154 if (!m_rr_client) {
155 throw std::invalid_argument("'client' is invalid");
156 }
157 }
158 MetaSource(MetaSource&&) = default;
159 MetaSource(MetaSource const&) = default;
160 MetaSource& operator=(MetaSource const&) = default;
162
163 std::string const& GetName() const {
164 return m_name;
165 }
166
168 return *m_rr_client;
169 }
170
171 friend std::ostream& operator<<(std::ostream& os, MetaSource const& s) {
172 os << "MetaSource: " << s.m_name;
173 return os;
174 }
175
176private:
177 std::string m_name;
178 std::shared_ptr<RrClient> m_rr_client;
179};
180
181using SourceVariant = std::variant<PrimSource, MetaSource>;
182
183/**
184 * Data acquisition sources.
185 */
187public:
188 DaqSources() = default;
189 explicit DaqSources(std::vector<PrimSource> prim_sources, std::vector<MetaSource> meta_sources)
190 : m_prim_sources(std::move(prim_sources)), m_meta_sources(std::move(meta_sources)) {
191 }
192 std::vector<PrimSource> const& GetPrimarySources() const noexcept {
193 return m_prim_sources;
194 }
195
196 std::vector<PrimSource>& GetPrimarySources() noexcept {
197 return m_prim_sources;
198 }
199
200 std::vector<MetaSource> const& GetMetadataSources() const noexcept {
201 return m_meta_sources;
202 }
203
204 std::vector<MetaSource>& GetMetadataSources() noexcept {
205 return m_meta_sources;
206 }
207
208private:
209 std::vector<PrimSource> m_prim_sources;
210 std::vector<MetaSource> m_meta_sources;
211};
212
213} // namespace daq
214
215template <class T>
216struct fmt::formatter<daq::Source<T>> : ostream_formatter {};
217
218template <>
219struct fmt::formatter<daq::PrimSource> : ostream_formatter {};
220
221template <>
222struct fmt::formatter<daq::MetaSource> : ostream_formatter {};
223
224#endif // #ifndef OCF_DAQ_SOURCE_HPP_
Data acquisition sources.
Definition: source.hpp:186
std::vector< MetaSource > & GetMetadataSources() noexcept
Definition: source.hpp:204
std::vector< MetaSource > const & GetMetadataSources() const noexcept
Definition: source.hpp:200
std::vector< PrimSource > & GetPrimarySources() noexcept
Definition: source.hpp:196
DaqSources()=default
DaqSources(std::vector< PrimSource > prim_sources, std::vector< MetaSource > meta_sources)
Definition: source.hpp:189
std::vector< PrimSource > const & GetPrimarySources() const noexcept
Definition: source.hpp:192
Keeps relevant state to be able to communicate with a primary data source.
Definition: source.hpp:142
MetaSource & operator=(MetaSource const &)=default
MetaSource & operator=(MetaSource &&)=default
std::string const & GetName() const
Definition: source.hpp:163
MetaSource(std::string name, std::shared_ptr< RrClient > client)
Definition: source.hpp:149
RrClient & GetRrClient()
Definition: source.hpp:167
MetaSource(MetaSource &&)=default
friend std::ostream & operator<<(std::ostream &os, MetaSource const &s)
Definition: source.hpp:171
metadaqif::MetaDaqAsync RrClient
Definition: source.hpp:144
MetaSource(MetaSource const &)=default
Keeps relevant state to be able to communicate with a primary data source.
Definition: source.hpp:98
PrimSource & operator=(PrimSource &&)=default
RrClient & GetRrClient()
Definition: source.hpp:123
PrimSource(PrimSource &&)=default
std::string const & GetName() const
Definition: source.hpp:119
recif::RecCmdsAsync RrClient
Definition: source.hpp:100
friend std::ostream & operator<<(std::ostream &os, PrimSource const &s)
Definition: source.hpp:127
PrimSource(std::string name, std::shared_ptr< RrClient > client)
Definition: source.hpp:105
PrimSource & operator=(PrimSource &)=default
PrimSource(PrimSource const &)=default
Declares daq::State and related functions.
std::variant< PrimSource, MetaSource > SourceVariant
Definition: source.hpp:181
State
Observable states of the data acquisition process.
Definition: state.hpp:41
@ NotStarted
Initial state of data acquisition.
Config class header file.
Simple class that holds the source and associated state.
Definition: source.hpp:30
boost::signals2::connection ConnectStateListener(Subscriber subscriber)
Connect subscriber that is invoked on state changes.
Definition: source.hpp:49
State m_state
Definition: source.hpp:88
bool m_error_flag
Definition: source.hpp:89
void SetErrorFlag()
Definition: source.hpp:66
Source(T &)=delete
Source(T &&s)
Definition: source.hpp:34
T const & GetSource() const
Definition: source.hpp:82
friend std::ostream & operator<<(std::ostream &os, Source< T > const &s)
Definition: source.hpp:36
boost::signals2::signal< void(State, bool)> StateSignal
Definition: source.hpp:31
State GetState() const
Definition: source.hpp:74
bool GetErrorFlag() const
Definition: source.hpp:71
T & GetSource()
Definition: source.hpp:78
StateSignal m_state_signal
Definition: source.hpp:90
void SetState(State state, std::optional< bool > error_flag={})
Definition: source.hpp:53
void ClearErrorFlag()
Definition: source.hpp:61