DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
ConfigObject.cpp
Go to the documentation of this file.
1#include <vector>
2#include <iostream>
3
8#include "conffwk/Schema.hpp"
9
10namespace dunedaq {
11namespace conffwk {
12
14 m_impl(nullptr)
15{
16}
17
19 m_impl(other.m_impl)
20{
21}
22
24 m_impl(impl)
25{
26}
27
29{
30}
31
34{
35 if(this != &other) {
36 m_impl = other.m_impl;
37 }
38
39 return *this;
40}
41
44{
45 if(m_impl != impl) {
46 m_impl = impl;
47 }
48
49 return *this;
50}
51
52bool
53ConfigObject::operator==(const ConfigObject& other) const noexcept
54{
55 if(this == &other || m_impl == other.m_impl) return true; // the objects or implementations are the same
56 if(!m_impl || !other.m_impl) return false; // only one of objects has no implementation
57 return ((UID() == other.UID()) && (class_name() == other.class_name()));
58}
59
60void
61ConfigObject::print_ptr(std::ostream& s) const noexcept
62{
63 if(m_impl)
64 {
65 if(m_impl->is_deleted())
66 {
67 s << "(deleted object " << full_name() << ')';
68 }
69 else
70 {
71 s << full_name();
72 }
73 }
74 else
75 {
76 s << "(null)";
77 }
78}
79
85
86void
87ConfigObject::rename(const std::string& new_id)
88{
89 get_configuration()->rename_object(*this, new_id);
91}
92
93void
95{
96 db->action_on_update(*this, name);
97}
98
99std::ostream&
100operator<<(std::ostream& s, const ConfigObject * obj)
101{
102 obj->print_ptr(s);
103 return s;
104}
105
106std::ostream&
107operator<<(std::ostream& s, const ConfigObject & obj)
108{
109 obj.print_ptr(s);
110 return s;
111}
112
113inline static void
114print_sep(const char sep, std::ostream& s)
115{
116 if (sep)
117 s << sep;
118}
119
120template<class T>
121 void
122 print_val(const T &val, std::ostream &s)
123 {
124 s << val;
125 }
126
127template<>
128 void
129 print_val<uint8_t>(const uint8_t &val, std::ostream &s)
130 {
131 s << static_cast<uint16_t>(val);
132 }
133
134template<>
135 void
136 print_val<int8_t>(const int8_t &val, std::ostream &s)
137 {
138 s << static_cast<int16_t>(val);
139 }
140
141template<class T>
142 void
143 print_value(const ConfigObject& const_obj, const std::string& name, const bool ismv, const char sep, std::ostream& s)
144 {
145 ConfigObject& obj = const_cast<ConfigObject&>(const_obj);
146
147 try
148 {
149 if (ismv)
150 {
151 std::vector<T> value;
152
153 obj.get(name, value);
154
155 print_sep('(', s);
156
157 for (unsigned int i = 0; i < value.size(); ++i)
158 {
159 if (i != 0)
160 s << ", ";
161
162 print_sep(sep, s);
163 print_val<T>(value[i],s);
164 print_sep(sep, s);
165 }
166
167 print_sep(')', s);
168 }
169 else
170 {
171 T value;
172
173 obj.get(name, value);
174
175 print_sep(sep, s);
176 print_val<T>(value,s);
177 print_sep(sep, s);
178 }
179 }
180 catch (ers::Issue & ex)
181 {
182 s << "[bad_object] (could not get value of \'" << name << "\' of object \'" << &obj << "\': " << ex << ')';
183 }
184 }
185
186// workaround to avoid annoying warning about comparing this with nullptr
187inline bool
189{
190 return (o == nullptr || o->is_null());
191}
192
193void
194ConfigObject::print_ref(std::ostream& s, Configuration& conffwk, const std::string& prefix, bool show_contained_in) const noexcept
195{
196 static bool expand_aggregation = (getenv("TDAQ_CONFFWK_PRINT_EXPAND_AGGREGATIONS")); // FIXME tdaq-09-05-00 => add new parameter to conffwk and add fuse
197
198 // check if it is not a reference to 0
199 if (is_null_obj(this))
200 {
201 s << prefix << "(null)";
202 return;
203 }
204
205 // print out object-id and class-name
206 s
207 << prefix << "Object:\n"
208 << prefix << " id: \'" << UID() << "\', class name: \'" << class_name() << "\'\n";
209
210 if (show_contained_in)
211 s << prefix << " contained in: \'" << contained_in() << "\'\n";
212
213 try
214 {
215 const dunedaq::conffwk::class_t& cd(conffwk.get_class_info(class_name()));
216
217 // print attributes
218 for (const auto& i : cd.p_attributes)
219 {
220 const std::string& aname(i.p_name); // attribute name
221 const bool ismv(i.p_is_multi_value); // attribute is multi-value
222
223 s << prefix << " " << aname << ": ";
224
225 switch (i.p_type)
226 {
232 print_value<std::string>(*this, aname, ismv, '\"', s); break;
233 case dunedaq::conffwk::bool_type: print_value<bool>(*this, aname, ismv, 0, s); break;
234 case dunedaq::conffwk::u8_type: print_value<uint8_t>(*this, aname, ismv, 0, s); break;
235 case dunedaq::conffwk::s8_type: print_value<int8_t>(*this, aname, ismv, 0, s); break;
236 case dunedaq::conffwk::u16_type: print_value<uint16_t>(*this, aname, ismv, 0, s); break;
237 case dunedaq::conffwk::s16_type: print_value<int16_t>(*this, aname, ismv, 0, s); break;
238 case dunedaq::conffwk::u32_type: print_value<uint32_t>(*this, aname, ismv, 0, s); break;
239 case dunedaq::conffwk::s32_type: print_value<int32_t>(*this, aname, ismv, 0, s); break;
240 case dunedaq::conffwk::u64_type: print_value<uint64_t>(*this, aname, ismv, 0, s); break;
241 case dunedaq::conffwk::s64_type: print_value<int64_t>(*this, aname, ismv, 0, s); break;
242 case dunedaq::conffwk::float_type: print_value<float>(*this, aname, ismv, 0, s); break;
243 case dunedaq::conffwk::double_type: print_value<double>(*this, aname, ismv, 0, s); break;
244 default: s << "*** bad type ***";
245 }
246
247 s << std::endl;
248 }
249
250 // print relationships
251 for (const auto& i : cd.p_relationships)
252 {
253 s << prefix << " " << i.p_name << ':';
254 if (expand_aggregation == false || i.p_is_aggregation == false)
255 {
256 s << ' ';
257 print_value<ConfigObject>(*this, i.p_name, (i.p_cardinality == dunedaq::conffwk::zero_or_many) || (i.p_cardinality == dunedaq::conffwk::one_or_many), '\"', s);
258 s << std::endl;
259 }
260 else
261 {
262 s << std::endl;
263 std::string prefix2(prefix + " ");
264 ConfigObject& obj = const_cast<ConfigObject&>(*this);
265 if ((i.p_cardinality == dunedaq::conffwk::zero_or_many) || (i.p_cardinality == dunedaq::conffwk::one_or_many))
266 {
267 std::vector<ConfigObject> value;
268 obj.get(i.p_name, value);
269 if (value.empty())
270 s << prefix2 << "(null)\n";
271 else
272 for (const auto& x : value)
273 x.print_ref(s, conffwk, prefix2, show_contained_in);
274 }
275 else
276 {
277 ConfigObject value;
278 obj.get(i.p_name, value);
279 if (value.is_null())
280 s << prefix2 << "(null)\n";
281 else
282 value.print_ref(s, conffwk, prefix2, show_contained_in);
283 }
284 }
285 }
286 }
287 catch (dunedaq::conffwk::Exception& ex)
288 {
289 s << "cannot get schema description: caught dunedaq::conffwk::Exception exception" << std::endl;
290 std::cerr << "ERROR: " << ex << std::endl;
291 }
292
293}
294
295
297ConfigObject::get_obj_pybind(const std::string& attrname) {
298
299 ConfigObject* newobject = new ConfigObject;
300
301 if (newobject) {
302 this->get(attrname, *newobject);
303
304 if (newobject->is_null()) {
305 delete newobject;
306 return nullptr;
307 }
308 }
309
310 return newobject;
311}
312
313} // namespace conffwk
314} // namespace dunedaq
Implements database objects.
Represents database objects.
ConfigObject * get_obj_pybind(const std::string &attrname)
ConfigObject() noexcept
Default constructor.
bool operator==(const ConfigObject &other) const noexcept
Compare two objects.
void print_ptr(std::ostream &s) const noexcept
Print object's pointer in format 'obj-id@class-name'.
Configuration * get_configuration() const
Get pointer to configuration object.
void action_on_object_update(Configuration *db, const std::string &name)
void print_ref(std::ostream &s, Configuration &conf, const std::string &prefix="", bool show_contained_in=false) const noexcept
Print details of object's attributes and relationships.
void get(const std::string &name, T &value)
Get value of object's attribute or relationship.
ConfigObject & operator=(const ConfigObject &other) noexcept
Assignment operator.
bool is_null() const noexcept
Check if object's implementation points to null.
void rename(const std::string &new_id)
Rename object.
~ConfigObject() noexcept
Destructor.
Configuration * m_conf
Configuration pointer is needed for notification on changes, e.g. in case of subscription or an objec...
Defines base class for cache of template objects.
void action_on_update(const ConfigObject &obj, const std::string &name)
void rename_object(ConfigObject &obj, const std::string &new_id)
Cache of template object of given type.
Base class for any user define issue.
Definition Issue.hpp:69
conffwk entry point
void print_val(const T &val, std::ostream &s)
static void print_sep(const char sep, std::ostream &s)
void print_val< uint8_t >(const uint8_t &val, std::ostream &s)
bool is_null_obj(const ConfigObject *o)
void print_value(const ConfigObject &const_obj, const std::string &name, const bool ismv, const char sep, std::ostream &s)
void print_val< int8_t >(const int8_t &val, std::ostream &s)
std::ostream & operator<<(std::ostream &, const ConfigurationChange &)
Including Qt Headers.
const std::vector< attribute_t > p_attributes
Definition Schema.hpp:163
const std::vector< relationship_t > p_relationships
Definition Schema.hpp:164