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