Line data Source code
1 : //
2 : // DUNE DAQ modification notice:
3 : // This file has been modified from the original ATLAS oksconfig source for the DUNE DAQ project.
4 : // Fork baseline commit: oksconfig-03-02-00 (2021-04-20).
5 : // Renamed since fork: no.
6 : //
7 :
8 : #include "ers/ers.hpp"
9 :
10 : #include "oks/file.hpp"
11 : #include "oks/relationship.hpp"
12 :
13 : #include "conffwk/ConfigObject.hpp"
14 :
15 : #include "oksconflibs/OksConfigObject.hpp"
16 : #include "oksconflibs/OksConfiguration.hpp"
17 :
18 : #include "logging/Logging.hpp"
19 :
20 : using namespace dunedaq;
21 : using namespace dunedaq::oks;
22 : using namespace dunedaq::oksconflibs;
23 :
24 : static std::string
25 0 : mk_error_text(const char* op, const char* what, const std::string& name, const OksObject* o, const char* error)
26 : {
27 0 : std::ostringstream text;
28 0 : text << "failed to " << op << ' ' << what << " \"" << name << "\" of object " << o;
29 0 : if (error)
30 0 : text << ": " << error;
31 0 : return text.str();
32 0 : }
33 :
34 : static std::string
35 0 : mk_get_error_text(const char* what, const std::string& name, const OksObject* o, const char* error = nullptr)
36 : {
37 0 : return mk_error_text("read", what, name, o, error);
38 : }
39 :
40 : static std::string
41 0 : mk_set_error_text(const char* what, const std::string& name, const OksObject* o, const char* error = nullptr)
42 : {
43 0 : return mk_error_text("set value", what, name, o, error);
44 : }
45 :
46 : OksData::Type
47 0 : OksConfigObject::get_type(const std::string& name) const
48 : {
49 0 : try {
50 0 : return m_obj->GetAttributeValue(name)->type;
51 0 : } catch (oks::exception& ex) {
52 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_get_error_text("attribute", name, m_obj).c_str(), ex);
53 0 : }
54 : }
55 :
56 : // helper function to extract value out of OKS database
57 : template<class T>
58 : void
59 1559 : OksConfigObject::get_value(const std::string& name, T& value)
60 : {
61 1559 : std::lock_guard<std::mutex> scoped_lock(m_mutex);
62 :
63 : try {
64 1559 : throw_if_deleted();
65 1559 : OksData* data(m_obj->GetAttributeValue(name));
66 1559 : value = *reinterpret_cast<T*>(&data->data); // really ugly stuff here, but should come out right
67 0 : } catch (oks::exception& ex) {
68 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_get_error_text("attribute", name, m_obj).c_str(), ex);
69 : }
70 1559 : }
71 :
72 : // helper function to extract vector of values out of OKS database
73 : template<class T>
74 : void
75 0 : OksConfigObject::get_vector(const std::string& name, std::vector<T>& value)
76 : {
77 0 : OksData* data = 0;
78 :
79 0 : std::lock_guard<std::mutex> scoped_lock(m_mutex);
80 :
81 : try {
82 0 : throw_if_deleted();
83 0 : data = m_obj->GetAttributeValue(name);
84 0 : } catch (oks::exception& ex) {
85 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_get_error_text("attribute", name, m_obj).c_str(), ex);
86 : }
87 :
88 0 : if (data->type != OksData::list_type) {
89 0 : std::ostringstream text;
90 0 : text << "attribute \"" << name << "\" of object " << m_obj << " is not of a multi-value";
91 0 : throw dunedaq::conffwk::Generic(ERS_HERE, text.str().c_str());
92 0 : }
93 :
94 0 : value.clear();
95 0 : for (const auto& it : *data->data.LIST) {
96 0 : value.push_back(*reinterpret_cast<T*>(&it->data));
97 : }
98 0 : }
99 :
100 1301 : OksConfigObject::OksConfigObject(OksObject* obj, dunedaq::conffwk::ConfigurationImpl* impl) noexcept
101 : : ConfigObjectImpl(impl, obj->GetId())
102 1301 : , m_obj(obj)
103 : {
104 1301 : }
105 :
106 2602 : OksConfigObject::~OksConfigObject() noexcept {}
107 :
108 : const std::string
109 0 : OksConfigObject::contained_in() const
110 : {
111 0 : std::lock_guard<std::mutex> scoped_lock(m_mutex);
112 0 : throw_if_deleted();
113 0 : return m_obj->get_file()->get_full_file_name();
114 0 : }
115 :
116 : void
117 83 : OksConfigObject::get(const std::string& name, bool& value)
118 : {
119 83 : get_value(name, value);
120 83 : }
121 :
122 : void
123 16 : OksConfigObject::get(const std::string& name, uint8_t& value)
124 : {
125 16 : get_value(name, value);
126 16 : }
127 :
128 : void
129 0 : OksConfigObject::get(const std::string& name, int8_t& value)
130 : {
131 0 : get_value(name, value);
132 0 : }
133 :
134 : void
135 37 : OksConfigObject::get(const std::string& name, uint16_t& value)
136 : {
137 37 : get_value(name, value);
138 37 : }
139 :
140 : void
141 0 : OksConfigObject::get(const std::string& name, int16_t& value)
142 : {
143 0 : get_value(name, value);
144 0 : }
145 :
146 : void
147 1259 : OksConfigObject::get(const std::string& name, uint32_t& value)
148 : {
149 1259 : get_value(name, value);
150 1259 : }
151 :
152 : void
153 148 : OksConfigObject::get(const std::string& name, int32_t& value)
154 : {
155 148 : get_value(name, value);
156 148 : }
157 :
158 : void
159 16 : OksConfigObject::get(const std::string& name, uint64_t& value)
160 : {
161 16 : get_value(name, value);
162 16 : }
163 :
164 : void
165 0 : OksConfigObject::get(const std::string& name, int64_t& value)
166 : {
167 0 : get_value(name, value);
168 0 : }
169 :
170 : void
171 0 : OksConfigObject::get(const std::string& name, float& value)
172 : {
173 0 : get_value(name, value);
174 0 : }
175 :
176 : void
177 0 : OksConfigObject::get(const std::string& name, double& value)
178 : {
179 0 : get_value(name, value);
180 0 : }
181 :
182 : void
183 1198 : OksConfigObject::get(const std::string& name, std::string& value)
184 : {
185 1198 : OksData* data = 0;
186 :
187 1198 : std::lock_guard<std::mutex> scoped_lock(m_mutex);
188 :
189 1198 : try {
190 1198 : throw_if_deleted();
191 1198 : data = m_obj->GetAttributeValue(name);
192 0 : } catch (oks::exception& ex) {
193 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_get_error_text("attribute", name, m_obj).c_str(), ex);
194 0 : }
195 :
196 1198 : if ((data->type == OksData::string_type) || (data->type == OksData::enum_type)) {
197 1198 : value = *data->data.STRING;
198 : return;
199 0 : } else if (data->type == OksData::date_type || data->type == OksData::time_type) {
200 0 : value = data->str();
201 0 : return;
202 0 : } else if (data->type == OksData::class_type) {
203 0 : value = data->data.CLASS->get_name();
204 : return;
205 : } else {
206 0 : std::ostringstream text;
207 0 : text << "read wrong attribute type instead of expected \'string\' for attribute \"" << name << "\" of object "
208 0 : << m_obj;
209 0 : throw dunedaq::conffwk::Generic(ERS_HERE, text.str().c_str());
210 0 : }
211 1198 : }
212 :
213 : void
214 743 : OksConfigObject::get(const std::string& name, dunedaq::conffwk::ConfigObject& value)
215 : {
216 743 : OksData* data = 0;
217 :
218 743 : std::lock_guard<std::mutex> scoped_lock(m_mutex);
219 :
220 743 : try {
221 743 : throw_if_deleted();
222 743 : data = m_obj->GetRelationshipValue(name);
223 0 : } catch (oks::exception& ex) {
224 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_get_error_text("relationship", name, m_obj).c_str(), ex);
225 0 : }
226 :
227 743 : if (data->type != OksData::object_type) {
228 0 : if (data->type == OksData::uid_type || data->type == OksData::uid2_type) {
229 0 : std::ostringstream text;
230 0 : text << "referenced object " << *data << " is not loaded";
231 0 : throw dunedaq::conffwk::Generic(ERS_HERE,
232 0 : mk_get_error_text("relationship", name, m_obj, text.str().c_str()).c_str());
233 0 : }
234 :
235 0 : std::ostringstream text;
236 0 : text << "bad value of relationship \"" << name << "\" of object " << m_obj << ": ";
237 :
238 0 : if (data->type == OksData::list_type) {
239 0 : text << "the relationship is multi-value";
240 : } else {
241 0 : text << "non-object type was read";
242 : }
243 0 : throw dunedaq::conffwk::Generic(ERS_HERE, text.str().c_str());
244 0 : }
245 :
246 743 : if (data->data.OBJECT != nullptr) {
247 659 : value = static_cast<OksConfiguration*>(m_impl)->new_object(data->data.OBJECT);
248 : } else {
249 84 : value = nullptr;
250 : }
251 743 : }
252 :
253 : void
254 0 : OksConfigObject::get(const std::string& name, std::vector<bool>& value)
255 : {
256 0 : get_vector(name, value);
257 0 : }
258 :
259 : void
260 0 : OksConfigObject::get(const std::string& name, std::vector<uint8_t>& value)
261 : {
262 0 : get_vector(name, value);
263 0 : }
264 :
265 : void
266 0 : OksConfigObject::get(const std::string& name, std::vector<int8_t>& value)
267 : {
268 0 : get_vector(name, value);
269 0 : }
270 :
271 : void
272 0 : OksConfigObject::get(const std::string& name, std::vector<uint16_t>& value)
273 : {
274 0 : get_vector(name, value);
275 0 : }
276 :
277 : void
278 0 : OksConfigObject::get(const std::string& name, std::vector<int16_t>& value)
279 : {
280 0 : get_vector(name, value);
281 0 : }
282 :
283 : void
284 0 : OksConfigObject::get(const std::string& name, std::vector<uint32_t>& value)
285 : {
286 0 : get_vector(name, value);
287 0 : }
288 :
289 : void
290 0 : OksConfigObject::get(const std::string& name, std::vector<int32_t>& value)
291 : {
292 0 : get_vector(name, value);
293 0 : }
294 :
295 : void
296 0 : OksConfigObject::get(const std::string& name, std::vector<uint64_t>& value)
297 : {
298 0 : get_vector(name, value);
299 0 : }
300 :
301 : void
302 0 : OksConfigObject::get(const std::string& name, std::vector<int64_t>& value)
303 : {
304 0 : get_vector(name, value);
305 0 : }
306 :
307 : void
308 0 : OksConfigObject::get(const std::string& name, std::vector<float>& value)
309 : {
310 0 : get_vector(name, value);
311 0 : }
312 :
313 : void
314 0 : OksConfigObject::get(const std::string& name, std::vector<double>& value)
315 : {
316 0 : get_vector(name, value);
317 0 : }
318 :
319 : void
320 61 : OksConfigObject::get(const std::string& name, std::vector<std::string>& value)
321 : {
322 61 : OksData* data = 0;
323 :
324 61 : std::lock_guard<std::mutex> scoped_lock(m_mutex);
325 :
326 61 : try {
327 61 : throw_if_deleted();
328 61 : data = m_obj->GetAttributeValue(name);
329 0 : } catch (oks::exception& ex) {
330 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_get_error_text("attribute", name, m_obj).c_str(), ex);
331 0 : }
332 :
333 61 : if (data->type != OksData::list_type) {
334 0 : std::ostringstream text;
335 0 : text << "attribute \"" << name << "\" of object " << m_obj << " is not of a multi-value";
336 0 : throw dunedaq::conffwk::Generic(ERS_HERE, text.str().c_str());
337 0 : }
338 :
339 61 : value.clear();
340 :
341 76 : for (const auto& it : *data->data.LIST) {
342 15 : if ((it->type == OksData::string_type) || (it->type == OksData::enum_type)) {
343 0 : value.emplace_back(*it->data.STRING);
344 15 : } else if (it->type == OksData::class_type) {
345 15 : value.emplace_back(it->data.CLASS->get_name());
346 0 : } else if (it->type == OksData::date_type || it->type == OksData::time_type) {
347 0 : value.emplace_back(it->str());
348 : } else {
349 0 : std::ostringstream text;
350 0 : text << "wrong type of attribute \"" << name << "\" of object " << m_obj << " (instead of expected string)";
351 0 : throw dunedaq::conffwk::Generic(ERS_HERE, text.str().c_str());
352 0 : }
353 : }
354 61 : }
355 :
356 : void
357 711 : OksConfigObject::get(const std::string& name, std::vector<dunedaq::conffwk::ConfigObject>& value)
358 : {
359 711 : OksData* data = 0;
360 :
361 711 : std::lock_guard<std::mutex> scoped_lock(m_mutex);
362 :
363 711 : try {
364 711 : throw_if_deleted();
365 711 : data = m_obj->GetRelationshipValue(name);
366 0 : } catch (oks::exception& ex) {
367 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_get_error_text("relationship", name, m_obj).c_str(), ex);
368 0 : }
369 :
370 711 : if (data->type != OksData::list_type) {
371 0 : std::ostringstream text;
372 0 : text << "the value of relationship \"" << name << "\" of object " << m_obj << " is not multi-value";
373 0 : throw dunedaq::conffwk::Generic(ERS_HERE, text.str().c_str());
374 0 : }
375 :
376 711 : value.clear();
377 :
378 1197 : for (const auto& it : *data->data.LIST) {
379 486 : if (it->type != OksData::object_type) {
380 0 : TLOG_DEBUG(1) << "object " << *it << " is not loaded (relationship \'" << name << "\' of object " << m_obj << ')';
381 486 : } else if (!it->data.OBJECT) {
382 0 : TLOG_DEBUG(1) << "skip (null) object in relationship \'" << name << "\' of object " << m_obj;
383 : } else {
384 486 : value.push_back(conffwk::ConfigObject(static_cast<OksConfiguration*>(m_impl)->new_object(it->data.OBJECT)));
385 : }
386 : }
387 711 : }
388 :
389 : bool
390 0 : OksConfigObject::rel(const std::string& name, std::vector<conffwk::ConfigObject>& value)
391 : {
392 0 : std::lock_guard<std::mutex> scoped_lock(m_mutex);
393 :
394 0 : try {
395 0 : throw_if_deleted();
396 :
397 0 : if (OksDataInfo* di = m_obj->GetClass()->get_data_info(name)) {
398 0 : if (di->relationship) {
399 0 : OksData* data = m_obj->GetRelationshipValue(di);
400 :
401 0 : if (data->type == OksData::object_type) {
402 0 : if (data->data.OBJECT != nullptr) {
403 0 : value.push_back(
404 0 : conffwk::ConfigObject(static_cast<OksConfiguration*>(m_impl)->new_object(data->data.OBJECT)));
405 : }
406 0 : } else if (data->type == OksData::list_type) {
407 0 : for (const auto& it : *data->data.LIST) {
408 0 : if (it->type == OksData::object_type) {
409 0 : value.push_back(
410 0 : conffwk::ConfigObject(static_cast<OksConfiguration*>(m_impl)->new_object(it->data.OBJECT)));
411 : }
412 : }
413 : }
414 :
415 0 : return true;
416 : }
417 : }
418 :
419 : return false;
420 0 : } catch (oks::exception& ex) {
421 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_get_error_text("relationship", name, m_obj).c_str(), ex);
422 0 : }
423 0 : }
424 :
425 : void
426 0 : OksConfigObject::referenced_by(std::vector<conffwk::ConfigObject>& value,
427 : const std::string& rname,
428 : bool check_composite_only,
429 : unsigned long /* rlevel */,
430 : const std::vector<std::string>* /* rclasses */) const
431 : {
432 0 : value.clear();
433 :
434 0 : std::lock_guard<std::mutex> scoped_lock(m_mutex);
435 0 : throw_if_deleted();
436 :
437 0 : if (check_composite_only) {
438 0 : bool any_name = (rname == "*");
439 :
440 0 : if (const std::list<OksRCR*>* rcr = m_obj->reverse_composite_rels()) {
441 0 : for (const auto& i : *rcr) {
442 0 : if (any_name || rname == i->relationship->get_name()) {
443 0 : value.push_back(conffwk::ConfigObject(static_cast<OksConfiguration*>(m_impl)->new_object(i->obj)));
444 : }
445 : }
446 : }
447 : } else {
448 0 : if (OksObject::FList* objs = m_obj->get_all_rels(rname)) {
449 0 : for (const auto& i : *objs) {
450 0 : value.push_back(conffwk::ConfigObject(static_cast<OksConfiguration*>(m_impl)->new_object(i)));
451 : }
452 :
453 0 : delete objs;
454 : }
455 : }
456 0 : }
457 :
458 : /////////////////////////////////////////////////////////////////////////////
459 : //
460 : // set functions
461 : //
462 : /////////////////////////////////////////////////////////////////////////////
463 :
464 : void
465 21 : OksConfigObject::set_attr_value(const std::string& name, OksData& data)
466 : {
467 21 : std::lock_guard<std::mutex> scoped_lock(m_mutex);
468 :
469 21 : try {
470 21 : throw_if_deleted();
471 : // test_checkout_needs();
472 21 : m_obj->SetAttributeValue(name, &data);
473 0 : } catch (dunedaq::conffwk::Generic& ex) {
474 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_set_error_text("attribute", name, m_obj).c_str(), ex);
475 0 : } catch (oks::exception& ex) {
476 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_set_error_text("attribute", name, m_obj).c_str(), ex);
477 0 : }
478 21 : }
479 :
480 : void
481 10 : OksConfigObject::set_rel_value(const std::string& name, OksData& data, bool skip_non_null_check)
482 : {
483 10 : std::lock_guard<std::mutex> scoped_lock(m_mutex);
484 :
485 10 : try {
486 10 : throw_if_deleted();
487 : // test_checkout_needs();
488 10 : m_obj->SetRelationshipValue(name, &data, skip_non_null_check);
489 0 : } catch (dunedaq::conffwk::Generic& ex) {
490 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_set_error_text("relationship", name, m_obj).c_str(), ex);
491 0 : } catch (oks::exception& ex) {
492 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_set_error_text("relationship", name, m_obj).c_str(), ex);
493 0 : }
494 10 : }
495 :
496 : template<class T>
497 : void
498 21 : OksConfigObject::set_value(const std::string& name, const T& value)
499 : {
500 21 : OksData d(value);
501 21 : set_attr_value(name, d);
502 21 : }
503 :
504 : template<class T>
505 : void
506 0 : OksConfigObject::set_vector(const std::string& name, const std::vector<T>& value)
507 : {
508 0 : OksData d(new OksData::List());
509 :
510 0 : for (const auto& i : value) {
511 0 : d.data.LIST->push_back(new OksData(i));
512 : }
513 :
514 0 : set_attr_value(name, d);
515 0 : }
516 :
517 : void
518 0 : OksConfigObject::set(const std::string& name, bool value)
519 : {
520 0 : set_value(name, value);
521 0 : }
522 :
523 : void
524 0 : OksConfigObject::set(const std::string& name, uint8_t value)
525 : {
526 0 : set_value(name, value);
527 0 : }
528 :
529 : void
530 0 : OksConfigObject::set(const std::string& name, int8_t value)
531 : {
532 0 : set_value(name, value);
533 0 : }
534 :
535 : void
536 0 : OksConfigObject::set(const std::string& name, uint16_t value)
537 : {
538 0 : set_value(name, value);
539 0 : }
540 :
541 : void
542 0 : OksConfigObject::set(const std::string& name, int16_t value)
543 : {
544 0 : set_value(name, value);
545 0 : }
546 :
547 : void
548 0 : OksConfigObject::set(const std::string& name, uint32_t value)
549 : {
550 0 : set_value(name, value);
551 0 : }
552 :
553 : void
554 4 : OksConfigObject::set(const std::string& name, int32_t value)
555 : {
556 4 : set_value(name, value);
557 4 : }
558 :
559 : void
560 2 : OksConfigObject::set(const std::string& name, uint64_t value)
561 : {
562 2 : set_value(name, value);
563 2 : }
564 :
565 : void
566 0 : OksConfigObject::set(const std::string& name, int64_t value)
567 : {
568 0 : set_value(name, value);
569 0 : }
570 :
571 : void
572 0 : OksConfigObject::set(const std::string& name, float value)
573 : {
574 0 : set_value(name, value);
575 0 : }
576 :
577 : void
578 0 : OksConfigObject::set(const std::string& name, double value)
579 : {
580 0 : set_value(name, value);
581 0 : }
582 :
583 : void
584 15 : OksConfigObject::set(const std::string& name, const std::string& value)
585 : {
586 15 : set_value(name, value);
587 15 : }
588 :
589 : void
590 0 : OksConfigObject::set_enum(const std::string& name, const std::string& value)
591 : {
592 0 : OksData d;
593 :
594 0 : {
595 0 : std::lock_guard<std::mutex> scoped_lock(m_mutex);
596 0 : throw_if_deleted();
597 :
598 0 : if (OksAttribute* a = m_obj->GetClass()->find_attribute(name)) {
599 0 : try {
600 0 : d.data.ENUMERATION = a->get_enum_value(value);
601 0 : d.type = OksData::enum_type;
602 0 : } catch (std::exception& ex) {
603 0 : throw(dunedaq::conffwk::Generic(ERS_HERE, mk_set_error_text("attribute", name, m_obj, ex.what()).c_str()));
604 0 : }
605 : } else {
606 0 : throw(
607 0 : dunedaq::conffwk::Generic(ERS_HERE, mk_set_error_text("attribute", name, m_obj, "no such attribute").c_str()));
608 : }
609 0 : }
610 :
611 0 : set_attr_value(name, d);
612 0 : }
613 :
614 : void
615 0 : OksConfigObject::set_class(const std::string& name, const std::string& value)
616 : {
617 0 : OksData d;
618 :
619 0 : d.data.CLASS = nullptr;
620 0 : d.type = OksData::class_type;
621 :
622 : // try to find any attribute in this class; throw exception "no such attribute", if the class has no attributes at all
623 0 : {
624 0 : std::lock_guard<std::mutex> scoped_lock(m_mutex);
625 0 : throw_if_deleted();
626 :
627 0 : const std::list<OksAttribute*>* attrs = m_obj->GetClass()->all_attributes();
628 :
629 0 : if (!attrs || attrs->empty()) {
630 0 : throw dunedaq::conffwk::Generic(ERS_HERE,
631 0 : mk_set_error_text("attribute", name, m_obj, "no such attribute").c_str());
632 : }
633 :
634 0 : try {
635 0 : d.SetValue(value.c_str(), attrs->front());
636 0 : } catch (oks::exception& ex) {
637 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_set_error_text("attribute", name, m_obj).c_str(), ex);
638 0 : }
639 0 : }
640 :
641 0 : set_attr_value(name, d);
642 0 : }
643 :
644 : void
645 0 : OksConfigObject::set_date(const std::string& name, const std::string& value)
646 : {
647 0 : OksData d;
648 0 : d.type = OksData::date_type;
649 :
650 0 : try {
651 0 : d.SetValue(value.c_str(), nullptr);
652 0 : } catch (oks::exception& ex) {
653 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_set_error_text("attribute", name, m_obj).c_str(), ex);
654 0 : }
655 :
656 0 : set_attr_value(name, d);
657 0 : }
658 :
659 : void
660 0 : OksConfigObject::set_time(const std::string& name, const std::string& value)
661 : {
662 0 : OksData d;
663 0 : d.type = OksData::time_type;
664 :
665 0 : try {
666 0 : d.SetValue(value.c_str(), nullptr);
667 0 : } catch (oks::exception& ex) {
668 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_set_error_text("attribute", name, m_obj).c_str(), ex);
669 0 : }
670 :
671 0 : set_attr_value(name, d);
672 0 : }
673 :
674 : void
675 0 : OksConfigObject::set(const std::string& name, const std::vector<bool>& value)
676 : {
677 0 : set_vector(name, value);
678 0 : }
679 :
680 : void
681 0 : OksConfigObject::set(const std::string& name, const std::vector<uint8_t>& value)
682 : {
683 0 : set_vector(name, value);
684 0 : }
685 :
686 : void
687 0 : OksConfigObject::set(const std::string& name, const std::vector<int8_t>& value)
688 : {
689 0 : set_vector(name, value);
690 0 : }
691 :
692 : void
693 0 : OksConfigObject::set(const std::string& name, const std::vector<uint16_t>& value)
694 : {
695 0 : set_vector(name, value);
696 0 : }
697 :
698 : void
699 0 : OksConfigObject::set(const std::string& name, const std::vector<int16_t>& value)
700 : {
701 0 : set_vector(name, value);
702 0 : }
703 :
704 : void
705 0 : OksConfigObject::set(const std::string& name, const std::vector<uint32_t>& value)
706 : {
707 0 : set_vector(name, value);
708 0 : }
709 :
710 : void
711 0 : OksConfigObject::set(const std::string& name, const std::vector<int32_t>& value)
712 : {
713 0 : set_vector(name, value);
714 0 : }
715 :
716 : void
717 0 : OksConfigObject::set(const std::string& name, const std::vector<uint64_t>& value)
718 : {
719 0 : set_vector(name, value);
720 0 : }
721 :
722 : void
723 0 : OksConfigObject::set(const std::string& name, const std::vector<int64_t>& value)
724 : {
725 0 : set_vector(name, value);
726 0 : }
727 :
728 : void
729 0 : OksConfigObject::set(const std::string& name, const std::vector<float>& value)
730 : {
731 0 : set_vector(name, value);
732 0 : }
733 :
734 : void
735 0 : OksConfigObject::set(const std::string& name, const std::vector<double>& value)
736 : {
737 0 : set_vector(name, value);
738 0 : }
739 :
740 : void
741 0 : OksConfigObject::set(const std::string& name, const std::vector<std::string>& value)
742 : {
743 0 : set_vector(name, value);
744 0 : }
745 :
746 : void
747 0 : OksConfigObject::set_enum(const std::string& name, const std::vector<std::string>& value)
748 : {
749 0 : OksData d(new OksData::List());
750 :
751 0 : {
752 0 : std::lock_guard<std::mutex> scoped_lock(m_mutex);
753 0 : throw_if_deleted();
754 :
755 0 : if (OksAttribute* a = m_obj->GetClass()->find_attribute(name)) {
756 0 : for (const auto& i : value) {
757 0 : OksData* d2 = new OksData();
758 0 : try {
759 0 : d2->data.ENUMERATION = a->get_enum_value(i); // new OksString(i);
760 0 : d2->type = OksData::enum_type;
761 0 : d.data.LIST->push_back(d2);
762 0 : } catch (std::exception& ex) {
763 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_set_error_text("attribute", name, m_obj).c_str(), ex);
764 0 : }
765 : }
766 : } else {
767 0 : throw dunedaq::conffwk::Generic(ERS_HERE,
768 0 : mk_set_error_text("attribute", name, m_obj, "no such attribute").c_str());
769 : }
770 0 : }
771 :
772 0 : set_attr_value(name, d);
773 0 : }
774 :
775 : void
776 0 : OksConfigObject::set_class(const std::string& name, const std::vector<std::string>& value)
777 : {
778 0 : OksData d(new OksData::List());
779 :
780 : // try to find any attribute in this class; throw exception "no such attribute", if the class has no attributes at all
781 :
782 0 : {
783 0 : std::lock_guard<std::mutex> scoped_lock(m_mutex);
784 0 : throw_if_deleted();
785 :
786 0 : const std::list<OksAttribute*>* attrs = m_obj->GetClass()->all_attributes();
787 :
788 0 : if (!attrs || attrs->empty()) {
789 0 : throw dunedaq::conffwk::Generic(ERS_HERE,
790 0 : mk_set_error_text("attribute", name, m_obj, "no such attribute").c_str());
791 : }
792 :
793 0 : for (const auto& i : value) {
794 0 : OksData* d2 = new OksData();
795 0 : d2->data.CLASS = 0;
796 0 : d2->type = OksData::class_type;
797 :
798 0 : try {
799 0 : d2->SetValue(i.c_str(), attrs->front());
800 0 : } catch (oks::exception& ex) {
801 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_set_error_text("attribute", name, m_obj).c_str(), ex);
802 0 : }
803 :
804 0 : d.data.LIST->push_back(d2);
805 : }
806 0 : }
807 :
808 0 : set_attr_value(name, d);
809 0 : }
810 :
811 : void
812 0 : OksConfigObject::set_date(const std::string& name, const std::vector<std::string>& value)
813 : {
814 0 : OksData d(new OksData::List());
815 :
816 0 : for (const auto& i : value) {
817 0 : OksData* d2 = new OksData();
818 0 : d2->type = OksData::date_type;
819 0 : try {
820 0 : d2->SetValue(i.c_str(), nullptr);
821 0 : } catch (oks::exception& ex) {
822 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_set_error_text("attribute", name, m_obj).c_str(), ex);
823 0 : }
824 0 : d.data.LIST->push_back(d2);
825 : }
826 :
827 0 : set_attr_value(name, d);
828 0 : }
829 :
830 : void
831 0 : OksConfigObject::set_time(const std::string& name, const std::vector<std::string>& value)
832 : {
833 0 : OksData d(new OksData::List());
834 :
835 0 : for (const auto& i : value) {
836 0 : OksData* d2 = new OksData();
837 0 : d2->type = OksData::time_type;
838 0 : try {
839 0 : d2->SetValue(i.c_str(), 0);
840 0 : } catch (oks::exception& ex) {
841 0 : throw dunedaq::conffwk::Generic(ERS_HERE, mk_set_error_text("attribute", name, m_obj).c_str(), ex);
842 0 : }
843 0 : d.data.LIST->push_back(d2);
844 : }
845 :
846 0 : set_attr_value(name, d);
847 0 : }
848 :
849 : void
850 1 : OksConfigObject::set(const std::string& name, const conffwk::ConfigObject* value, bool skip_non_null_check)
851 : {
852 2 : OksData d((value != nullptr) ? static_cast<const OksConfigObject*>(value->implementation())->m_obj
853 1 : : (OksObject*)nullptr);
854 1 : set_rel_value(name, d, skip_non_null_check);
855 1 : }
856 :
857 : void
858 9 : OksConfigObject::set(const std::string& name,
859 : const std::vector<const conffwk::ConfigObject*>& value,
860 : bool skip_non_null_check)
861 : {
862 9 : OksData d(new OksData::List());
863 :
864 30 : for (const auto& i : value) {
865 21 : d.data.LIST->push_back(new OksData(static_cast<const OksConfigObject*>(i->implementation())->m_obj));
866 : }
867 :
868 9 : set_rel_value(name, d, skip_non_null_check);
869 9 : }
870 :
871 : // void OksConfigObject::test_checkout_needs()
872 : //{
873 : //// static_cast<OksConfiguration *>(m_impl)->test_and_checkout(m_obj->get_file());
874 : //}
875 :
876 : void
877 0 : OksConfigObject::move(const std::string& at)
878 : {
879 0 : std::lock_guard<std::mutex> scoped_lock(m_mutex);
880 :
881 0 : throw_if_deleted();
882 :
883 0 : if (OksFile* new_file_h = m_obj->GetClass()->get_kernel()->find_data_file(at)) {
884 0 : OksFile* old_file_h = m_obj->get_file();
885 0 : if (old_file_h != new_file_h) {
886 0 : try {
887 : // static_cast<OksConfiguration *>(m_impl)->test_and_checkout(old_file_h);
888 : // static_cast<OksConfiguration *>(m_impl)->test_and_checkout(new_file_h);
889 0 : m_obj->set_file(new_file_h, false);
890 0 : } catch (oks::exception& ex) {
891 0 : throw dunedaq::conffwk::Generic(ERS_HERE, ex.what());
892 0 : }
893 : }
894 : } else {
895 0 : std::ostringstream text;
896 0 : text << "file \"" << at << "\" is not loaded";
897 0 : throw dunedaq::conffwk::Generic(ERS_HERE, text.str().c_str());
898 0 : }
899 0 : }
900 :
901 : void
902 0 : OksConfigObject::rename(const std::string& new_id)
903 : {
904 0 : try {
905 0 : throw_if_deleted();
906 :
907 : // std::set<OksFile *> files;
908 : //
909 : // files.insert(m_obj->get_file());
910 : //
911 : // if (OksObject::FList * objs = m_obj->get_all_rels())
912 : // {
913 : // for (auto & f : *objs)
914 : // {
915 : // files.insert(f->get_file());
916 : // }
917 : //
918 : // delete objs;
919 : // }
920 : //
921 : // for (auto & f : files)
922 : // {
923 : // static_cast<OksConfiguration *>(m_impl)->test_and_checkout(f);
924 : // }
925 :
926 0 : m_obj->set_id(new_id);
927 0 : } catch (oks::exception& ex) {
928 0 : std::ostringstream text;
929 0 : text << "cannot rename object " << m_obj << " to new name \'" << new_id << "\' because " << ex.what();
930 0 : throw dunedaq::conffwk::Generic(ERS_HERE, text.str().c_str());
931 0 : }
932 0 : }
933 :
934 : void
935 0 : OksConfigObject::reset()
936 : {
937 0 : if (OksClass* c = static_cast<OksConfiguration*>(m_impl)->m_kernel->find_class(*m_class_name)) {
938 0 : m_obj = c->get_object(m_id);
939 : } else {
940 0 : m_obj = nullptr;
941 : }
942 :
943 0 : m_state = (m_obj ? dunedaq::conffwk::Valid : dunedaq::conffwk::Deleted);
944 0 : }
|