DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
ConfigObject.cpp
Go to the documentation of this file.
1//
2// DUNE DAQ modification notice:
3// This file has been modified from the original ATLAS config source for the DUNE DAQ project.
4// Fork baseline commit: 67a24e731 (2022-10-27).
5// Renamed since fork: no.
6//
7
8#include <iostream>
9#include <vector>
10
15#include "conffwk/Schema.hpp"
16
17namespace dunedaq {
18namespace conffwk {
19
21 : m_impl(nullptr)
22{
23}
24
26 : m_impl(other.m_impl)
27{
28}
29
31 : m_impl(impl)
32{
33}
34
36
39{
40 if (this != &other) {
41 m_impl = other.m_impl;
42 }
43
44 return *this;
45}
46
49{
50 if (m_impl != impl) {
51 m_impl = impl;
52 }
53
54 return *this;
55}
56
57bool
58ConfigObject::operator==(const ConfigObject& other) const noexcept
59{
60 if (this == &other || m_impl == other.m_impl)
61 return true; // the objects or implementations are the same
62 if (!m_impl || !other.m_impl)
63 return false; // only one of objects has no implementation
64 return ((UID() == other.UID()) && (class_name() == other.class_name()));
65}
66
67void
68ConfigObject::print_ptr(std::ostream& s) const noexcept
69{
70 if (m_impl) {
71 if (m_impl->is_deleted()) {
72 s << "(deleted object " << full_name() << ')';
73 } else {
74 s << full_name();
75 }
76 } else {
77 s << "(null)";
78 }
79}
80
86
87void
88ConfigObject::rename(const std::string& new_id)
89{
90 get_configuration()->rename_object(*this, new_id);
92}
93
94void
96{
97 db->action_on_update(*this, name);
98}
99
100std::ostream&
101operator<<(std::ostream& s, const ConfigObject* obj)
102{
103 obj->print_ptr(s);
104 return s;
105}
106
107std::ostream&
108operator<<(std::ostream& s, const ConfigObject& obj)
109{
110 obj.print_ptr(s);
111 return s;
112}
113
114inline static void
115print_sep(const char sep, std::ostream& s)
116{
117 if (sep)
118 s << sep;
119}
120
121template<class T>
122void
123print_val(const T& val, std::ostream& s)
124{
125 s << val;
126}
127
128template<>
129void
130print_val<uint8_t>(const uint8_t& val, std::ostream& s)
131{
132 s << static_cast<uint16_t>(val);
133}
134
135template<>
136void
137print_val<int8_t>(const int8_t& val, std::ostream& s)
138{
139 s << static_cast<int16_t>(val);
140}
141
142template<class T>
143void
144print_value(const ConfigObject& const_obj, const std::string& name, const bool ismv, const char sep, std::ostream& s)
145{
146 ConfigObject& obj = const_cast<ConfigObject&>(const_obj);
147
148 try {
149 if (ismv) {
150 std::vector<T> value;
151
152 obj.get(name, value);
153
154 print_sep('(', s);
155
156 for (unsigned int i = 0; i < value.size(); ++i) {
157 if (i != 0)
158 s << ", ";
159
160 print_sep(sep, s);
161 print_val<T>(value[i], s);
162 print_sep(sep, s);
163 }
164
165 print_sep(')', s);
166 } else {
167 T value;
168
169 obj.get(name, value);
170
171 print_sep(sep, s);
172 print_val<T>(value, s);
173 print_sep(sep, s);
174 }
175 } catch (ers::Issue& ex) {
176 s << "[bad_object] (could not get value of \'" << name << "\' of object \'" << &obj << "\': " << ex << ')';
177 }
178}
179
180// workaround to avoid annoying warning about comparing this with nullptr
181inline bool
183{
184 return (o == nullptr || o->is_null());
185}
186
187void
190 const std::string& prefix,
191 bool show_contained_in) const noexcept
192{
193 static bool expand_aggregation = (getenv(
194 "TDAQ_CONFFWK_PRINT_EXPAND_AGGREGATIONS")); // FIXME tdaq-09-05-00 => add new parameter to conffwk and add fuse
195
196 // check if it is not a reference to 0
197 if (is_null_obj(this)) {
198 s << prefix << "(null)";
199 return;
200 }
201
202 // print out object-id and class-name
203 s << prefix << "Object:\n" << prefix << " id: \'" << UID() << "\', class name: \'" << class_name() << "\'\n";
204
205 if (show_contained_in)
206 s << prefix << " contained in: \'" << contained_in() << "\'\n";
207
208 try {
209 const dunedaq::conffwk::class_t& cd(conffwk.get_class_info(class_name()));
210
211 // print attributes
212 for (const auto& i : cd.p_attributes) {
213 const std::string& aname(i.p_name); // attribute name
214 const bool ismv(i.p_is_multi_value); // attribute is multi-value
215
216 s << prefix << " " << aname << ": ";
217
218 switch (i.p_type) {
224 print_value<std::string>(*this, aname, ismv, '\"', s);
225 break;
227 print_value<bool>(*this, aname, ismv, 0, s);
228 break;
230 print_value<uint8_t>(*this, aname, ismv, 0, s);
231 break;
233 print_value<int8_t>(*this, aname, ismv, 0, s);
234 break;
236 print_value<uint16_t>(*this, aname, ismv, 0, s);
237 break;
239 print_value<int16_t>(*this, aname, ismv, 0, s);
240 break;
242 print_value<uint32_t>(*this, aname, ismv, 0, s);
243 break;
245 print_value<int32_t>(*this, aname, ismv, 0, s);
246 break;
248 print_value<uint64_t>(*this, aname, ismv, 0, s);
249 break;
251 print_value<int64_t>(*this, aname, ismv, 0, s);
252 break;
254 print_value<float>(*this, aname, ismv, 0, s);
255 break;
257 print_value<double>(*this, aname, ismv, 0, s);
258 break;
259 default:
260 s << "*** bad type ***";
261 }
262
263 s << std::endl;
264 }
265
266 // print relationships
267 for (const auto& i : cd.p_relationships) {
268 s << prefix << " " << i.p_name << ':';
269 if (expand_aggregation == false || i.p_is_aggregation == false) {
270 s << ' ';
272 i.p_name,
273 (i.p_cardinality == dunedaq::conffwk::zero_or_many) ||
274 (i.p_cardinality == dunedaq::conffwk::one_or_many),
275 '\"',
276 s);
277 s << std::endl;
278 } else {
279 s << std::endl;
280 std::string prefix2(prefix + " ");
281 ConfigObject& obj = const_cast<ConfigObject&>(*this);
282 if ((i.p_cardinality == dunedaq::conffwk::zero_or_many) || (i.p_cardinality == dunedaq::conffwk::one_or_many)) {
283 std::vector<ConfigObject> value;
284 obj.get(i.p_name, value);
285 if (value.empty())
286 s << prefix2 << "(null)\n";
287 else
288 for (const auto& x : value)
289 x.print_ref(s, conffwk, prefix2, show_contained_in);
290 } else {
291 ConfigObject value;
292 obj.get(i.p_name, value);
293 if (value.is_null())
294 s << prefix2 << "(null)\n";
295 else
296 value.print_ref(s, conffwk, prefix2, show_contained_in);
297 }
298 }
299 }
300 } catch (dunedaq::conffwk::Exception& ex) {
301 s << "cannot get schema description: caught dunedaq::conffwk::Exception exception" << std::endl;
302 std::cerr << "ERROR: " << ex << std::endl;
303 }
304}
305
307ConfigObject::get_obj_pybind(const std::string& attrname)
308{
309
310 ConfigObject* newobject = new ConfigObject;
311
312 if (newobject) {
313 this->get(attrname, *newobject);
314
315 if (newobject->is_null()) {
316 delete newobject;
317 return nullptr;
318 }
319 }
320
321 return newobject;
322}
323
324} // namespace conffwk
325} // 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'.
const std::string & UID() const noexcept
Return object identity.
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.
const std::string full_name() const noexcept
Return full object name.
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.
const std::string & class_name() const noexcept
Return object's class name.
const std::string contained_in() const
Return the name of the database file this object belongs to.
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:76
conffwk entry point
void print_val(const T &val, std::ostream &s)
static void print_sep(const char sep, 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)
std::ostream & operator<<(std::ostream &, const ConfigurationChange &)
The DUNE-DAQ namespace.
msgpack::object obj
const std::vector< attribute_t > p_attributes
Definition Schema.hpp:161
const std::vector< relationship_t > p_relationships
Definition Schema.hpp:162