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 <vector>
9#include <iostream>
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{
37}
38
41{
42 if(this != &other) {
43 m_impl = other.m_impl;
44 }
45
46 return *this;
47}
48
51{
52 if(m_impl != impl) {
53 m_impl = impl;
54 }
55
56 return *this;
57}
58
59bool
60ConfigObject::operator==(const ConfigObject& other) const noexcept
61{
62 if(this == &other || m_impl == other.m_impl) return true; // the objects or implementations are the same
63 if(!m_impl || !other.m_impl) 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 {
72 if(m_impl->is_deleted())
73 {
74 s << "(deleted object " << full_name() << ')';
75 }
76 else
77 {
78 s << full_name();
79 }
80 }
81 else
82 {
83 s << "(null)";
84 }
85}
86
89{
90 return m_impl->m_impl->m_conf;
91}
92
93void
94ConfigObject::rename(const std::string& new_id)
95{
96 get_configuration()->rename_object(*this, new_id);
98}
99
100void
102{
103 db->action_on_update(*this, name);
104}
105
106std::ostream&
107operator<<(std::ostream& s, const ConfigObject * obj)
108{
109 obj->print_ptr(s);
110 return s;
111}
112
113std::ostream&
114operator<<(std::ostream& s, const ConfigObject & obj)
115{
116 obj.print_ptr(s);
117 return s;
118}
119
120inline static void
121print_sep(const char sep, std::ostream& s)
122{
123 if (sep)
124 s << sep;
125}
126
127template<class T>
128 void
129 print_val(const T &val, std::ostream &s)
130 {
131 s << val;
132 }
133
134template<>
135 void
136 print_val<uint8_t>(const uint8_t &val, std::ostream &s)
137 {
138 s << static_cast<uint16_t>(val);
139 }
140
141template<>
142 void
143 print_val<int8_t>(const int8_t &val, std::ostream &s)
144 {
145 s << static_cast<int16_t>(val);
146 }
147
148template<class T>
149 void
150 print_value(const ConfigObject& const_obj, const std::string& name, const bool ismv, const char sep, std::ostream& s)
151 {
152 ConfigObject& obj = const_cast<ConfigObject&>(const_obj);
153
154 try
155 {
156 if (ismv)
157 {
158 std::vector<T> value;
159
160 obj.get(name, value);
161
162 print_sep('(', s);
163
164 for (unsigned int i = 0; i < value.size(); ++i)
165 {
166 if (i != 0)
167 s << ", ";
168
169 print_sep(sep, s);
170 print_val<T>(value[i],s);
171 print_sep(sep, s);
172 }
173
174 print_sep(')', s);
175 }
176 else
177 {
178 T value;
179
180 obj.get(name, value);
181
182 print_sep(sep, s);
183 print_val<T>(value,s);
184 print_sep(sep, s);
185 }
186 }
187 catch (ers::Issue & ex)
188 {
189 s << "[bad_object] (could not get value of \'" << name << "\' of object \'" << &obj << "\': " << ex << ')';
190 }
191 }
192
193// workaround to avoid annoying warning about comparing this with nullptr
194inline bool
196{
197 return (o == nullptr || o->is_null());
198}
199
200void
201ConfigObject::print_ref(std::ostream& s, Configuration& conffwk, const std::string& prefix, bool show_contained_in) const noexcept
202{
203 static bool expand_aggregation = (getenv("TDAQ_CONFFWK_PRINT_EXPAND_AGGREGATIONS")); // FIXME tdaq-09-05-00 => add new parameter to conffwk and add fuse
204
205 // check if it is not a reference to 0
206 if (is_null_obj(this))
207 {
208 s << prefix << "(null)";
209 return;
210 }
211
212 // print out object-id and class-name
213 s
214 << prefix << "Object:\n"
215 << prefix << " id: \'" << UID() << "\', class name: \'" << class_name() << "\'\n";
216
217 if (show_contained_in)
218 s << prefix << " contained in: \'" << contained_in() << "\'\n";
219
220 try
221 {
222 const dunedaq::conffwk::class_t& cd(conffwk.get_class_info(class_name()));
223
224 // print attributes
225 for (const auto& i : cd.p_attributes)
226 {
227 const std::string& aname(i.p_name); // attribute name
228 const bool ismv(i.p_is_multi_value); // attribute is multi-value
229
230 s << prefix << " " << aname << ": ";
231
232 switch (i.p_type)
233 {
239 print_value<std::string>(*this, aname, ismv, '\"', s); break;
240 case dunedaq::conffwk::bool_type: print_value<bool>(*this, aname, ismv, 0, s); break;
241 case dunedaq::conffwk::u8_type: print_value<uint8_t>(*this, aname, ismv, 0, s); break;
242 case dunedaq::conffwk::s8_type: print_value<int8_t>(*this, aname, ismv, 0, s); break;
243 case dunedaq::conffwk::u16_type: print_value<uint16_t>(*this, aname, ismv, 0, s); break;
244 case dunedaq::conffwk::s16_type: print_value<int16_t>(*this, aname, ismv, 0, s); break;
245 case dunedaq::conffwk::u32_type: print_value<uint32_t>(*this, aname, ismv, 0, s); break;
246 case dunedaq::conffwk::s32_type: print_value<int32_t>(*this, aname, ismv, 0, s); break;
247 case dunedaq::conffwk::u64_type: print_value<uint64_t>(*this, aname, ismv, 0, s); break;
248 case dunedaq::conffwk::s64_type: print_value<int64_t>(*this, aname, ismv, 0, s); break;
249 case dunedaq::conffwk::float_type: print_value<float>(*this, aname, ismv, 0, s); break;
250 case dunedaq::conffwk::double_type: print_value<double>(*this, aname, ismv, 0, s); break;
251 default: s << "*** bad type ***";
252 }
253
254 s << std::endl;
255 }
256
257 // print relationships
258 for (const auto& i : cd.p_relationships)
259 {
260 s << prefix << " " << i.p_name << ':';
261 if (expand_aggregation == false || i.p_is_aggregation == false)
262 {
263 s << ' ';
264 print_value<ConfigObject>(*this, i.p_name, (i.p_cardinality == dunedaq::conffwk::zero_or_many) || (i.p_cardinality == dunedaq::conffwk::one_or_many), '\"', s);
265 s << std::endl;
266 }
267 else
268 {
269 s << std::endl;
270 std::string prefix2(prefix + " ");
271 ConfigObject& obj = const_cast<ConfigObject&>(*this);
272 if ((i.p_cardinality == dunedaq::conffwk::zero_or_many) || (i.p_cardinality == dunedaq::conffwk::one_or_many))
273 {
274 std::vector<ConfigObject> value;
275 obj.get(i.p_name, value);
276 if (value.empty())
277 s << prefix2 << "(null)\n";
278 else
279 for (const auto& x : value)
280 x.print_ref(s, conffwk, prefix2, show_contained_in);
281 }
282 else
283 {
284 ConfigObject value;
285 obj.get(i.p_name, value);
286 if (value.is_null())
287 s << prefix2 << "(null)\n";
288 else
289 value.print_ref(s, conffwk, prefix2, show_contained_in);
290 }
291 }
292 }
293 }
294 catch (dunedaq::conffwk::Exception& ex)
295 {
296 s << "cannot get schema description: caught dunedaq::conffwk::Exception exception" << std::endl;
297 std::cerr << "ERROR: " << ex << std::endl;
298 }
299
300}
301
302
304ConfigObject::get_obj_pybind(const std::string& attrname) {
305
306 ConfigObject* newobject = new ConfigObject;
307
308 if (newobject) {
309 this->get(attrname, *newobject);
310
311 if (newobject->is_null()) {
312 delete newobject;
313 return nullptr;
314 }
315 }
316
317 return newobject;
318}
319
320} // namespace conffwk
321} // 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.
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:80
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.
Definition module.cpp:16
msgpack::object obj
const std::vector< attribute_t > p_attributes
Definition Schema.hpp:170
const std::vector< relationship_t > p_relationships
Definition Schema.hpp:171