DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
config_reference.hpp
Go to the documentation of this file.
1// DUNE DAQ modification notice:
2// This file has been modified from the original ATLAS dbe source for the DUNE DAQ project.
3// Fork baseline commit: dbe-02-12-17 (2022-05-12).
4// Renamed since fork: yes (from dbe/config_reference.hpp to include/dbe/config_reference.hpp).
5
6/*
7 * config_reference.hpp
8 *
9 * Created on: Feb 1, 2016
10 * Author: Leonidas Georgopoulos
11 */
12
13#ifndef DBE_CONFIG_REFERENCE_HPP_
14#define DBE_CONFIG_REFERENCE_HPP_
15
16#include "dbe/Exceptions.hpp"
18
20
21#include <vector>
22#include <memory>
23
24#ifndef DONOT_THROW_ON_NULL_CONFIGOBJECT_REFERENCE
25#define DEBUG_THROW_ON_NULL_CONFIGOBJECT_REFERENCE \
26 if(check_null and rval.is_deleted()) \
27 throw daq::dbe::null_config_reference_access(ERS_HERE);
28#else
29#define DEBUG_THROW_ON_NULL_CONFIGOBJECT_REFERENCE
30#endif
31
32namespace dbe
33{
34namespace inner
35{
36class dbcontroller;
37
38namespace configobject
39{
40
41//------------------------------------------------------------------------------------------
42class oref;
43
47class wref
48{
51 : ref(o)
52 {
53 }
54
55 wref(wref const &) = delete;
56 wref operator=(wref const &) = delete;
57
58 friend class oref;
59};
60//------------------------------------------------------------------------------------------
61
62//------------------------------------------------------------------------------------------
63struct refhasher;
64
65class tref;
66class vref;
67template<typename > class gref;
68template<typename > class aref;
72class oref
73{
74 friend class tref;
75 friend class vref;
76 friend struct refhasher;
77 template<typename > friend class aref;
78 template<typename > friend class gref;
80 friend bool operator ==(dbe::cokey const & left, dbe::cokey const & right);
81
84
90 oref(dunedaq::conffwk::ConfigObject const & o) noexcept(true)
91 : ref
92 { o },
93 this_last_full_name(o.full_name())
94 {
95 }
96
97 std::string full_name() const
98 {
100 }
101
102 void rename(std::string const & newname)
103 {
104 ref.ref.rename(newname);
105 this_last_full_name = newname;
106 }
107
113 explicit operator dunedaq::conffwk::ConfigObject&() noexcept
114 {
115 return ref.ref;
116 }
117
118 explicit operator dunedaq::conffwk::ConfigObject const &() const noexcept
119 {
120 return ref.ref;
121 }
122
123 // non-copyable
124 oref(oref const &) = delete;
125 oref operator=(oref const &) = delete;
126
127 public:
128 operator dbe::cokey() const
129 {
130 return
131 { ref.ref.UID(),ref.ref.class_name()};
132 }
133};
134//------------------------------------------------------------------------------------------
135
136//------------------------------------------------------------------------------------------
137template<typename T> class ref_interface;
138
139template<typename T, typename U> class authorized_getter
140{
141 private:
143
144 public:
146 : that(caller)
147 {
148 }
149
150 U operator()(std::string const & key);
151};
152
153template<typename T> class ref_interface
154{
155 private:
156 explicit operator dunedaq::conffwk::ConfigObject &() noexcept
157 {
158 return static_cast<T*>(this)->ref();
159 }
160
161 explicit operator dunedaq::conffwk::ConfigObject &() const noexcept
162 {
163 return static_cast<T const *>(this)->ref();
164 }
165
166 protected:
173 template<typename U> U getdirect(std::string const & key) const
174 {
175 U value;
176 try
177 {
178 static_cast<dunedaq::conffwk::ConfigObject &>(*const_cast<ref_interface<T>*>(this)).get(key, value);
179 }
180 catch (dunedaq::conffwk::Exception const & e)
181 {
182 // TODO handle retrieval errors here
183 }
184 return value;
185 }
186
187 template<typename X, typename U> friend class authorized_getter;
188
189 public:
190
191 bool is_null() const
192 {
193 return static_cast<dunedaq::conffwk::ConfigObject &>(static_cast<T const *>(this)->ref(false)).is_deleted();
194 }
195
196 std::string UID() const
197 {
198 return static_cast<dunedaq::conffwk::ConfigObject &>(*this).UID();
199 }
200
201 std::string class_name() const
202 {
203 return static_cast<dunedaq::conffwk::ConfigObject &>(*this).class_name();
204 }
205
206 std::string full_name() const
207 {
208 return static_cast<dunedaq::conffwk::ConfigObject &>(*this).full_name();
209 }
210
211 std::string contained_in() const
212 {
213 return static_cast<dunedaq::conffwk::ConfigObject &>(*this).contained_in();
214 }
215
222 template<typename U> U get(std::string const & key)
223 {
224 authorized_getter<T, U> guard(this);
225 return guard(key);
226 }
227
234 template<typename U> void get(std::string const & key, U & val)
235 {
236 val = get<U>(key);
237 }
238
246 std::vector<T> referenced_by(std::string const & name = "*", bool check_composite_only =
247 true) const;
248
249 void set_obj_null(std::string const & name, bool is_simple, bool skip_non_null_check = false)
250 {
251 if(is_simple)
252 {
253 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_obj(name, nullptr, skip_non_null_check);
254 } else
255 {
256 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_objs(name, {}, skip_non_null_check);
257 }
258 }
259
260 void set_obj(std::string const & name, T const & other,
261 bool skip_non_null_check = false)
262 {
263 static_cast<dunedaq::conffwk::ConfigObject &> ( *this ).set_obj ( name,
264 &static_cast<dunedaq::conffwk::ConfigObject &> ( other ),
265 skip_non_null_check);
266 }
267
268 void set_objs(std::string const & name, std::vector<T> const & others,
269 bool skip_non_null_check = false)
270 {
271 std::vector<dunedaq::conffwk::ConfigObject const *> configobjects;
272
273 std::transform(std::begin(others), std::end(others),
274 std::back_inserter(configobjects), [](T const & aref )
275 {
276 return &static_cast<dunedaq::conffwk::ConfigObject const &> ( aref );
277 } );
278
279 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_objs(name, configobjects, skip_non_null_check);
280 }
281
282 template<typename U> void set_by_val(std::string const & name, U val)
283 {
284 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_by_val(name, val);
285 }
286
287 template<typename U> void set_by_ref(std::string const & name, U & val)
288 {
289 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_by_ref(name, val);
290 }
291
292 void set_enum(std::string const & name, std::string const & val)
293 {
294 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_enum(name, val);
295 }
296
297 void set_class(std::string const & name, std::string const & val)
298 {
299 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_class(name, val);
300 }
301
302 void set_date(std::string const & name, std::string const & val)
303 {
304 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_date(name, val);
305 }
306
307 void set_time(std::string const & name, std::string const & val)
308 {
309 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_time(name, val);
310 }
311
312 void set_enum(std::string const & name, const std::vector<std::string>& value)
313 {
314 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_enum(name, value);
315 }
316
317 void set_class(std::string const & name, const std::vector<std::string>& value)
318 {
319 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_class(name, value);
320 }
321
322 void set_date(std::string const & name, const std::vector<std::string>& value)
323 {
324 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_date(name, value);
325 }
326
327 void set_time(std::string const & name, const std::vector<std::string>& value)
328 {
329 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_time(name, value);
330 }
331
332 void move(std::string const & at)
333 {
334 static_cast<dunedaq::conffwk::ConfigObject &>(*this).move(at);
335 }
336
340
341 void print_ref(std::ostream& s,
343 conf,
344 const std::string& prefix = "",
345 bool show_contained_in = false
346 ) const
347 {
348 static_cast<dunedaq::conffwk::ConfigObject &>(*this).print_ref(s, conf, prefix, show_contained_in);
349 }
350
351 friend std::ostream & operator<<(std::ostream & os,
352 dbe::inner::configobject::tref const & atref);
353
356 friend class dbe::inner::configobject::aref<T>;
357};
358//------------------------------------------------------------------------------------------
359
363template<typename T> class authorized_getter<T, configobject::tref>
364{
365 private:
367
368 public:
370 : that(caller)
371 {
372 }
373
374 configobject::tref operator()(std::string const & key);
375
376};
377
381template<typename T> class authorized_getter<T, std::vector<configobject::tref>>
382{
383 private:
385
386 public:
388 : that(caller)
389 {
390 }
391
392 std::vector<configobject::tref> operator()(std::string const & key);
393
394};
395
399template<typename T> class authorized_getter<T, dunedaq::conffwk::ConfigObject>
400{
401 private:
403
404 public:
406 : that(caller)
407 {
408 }
409
410 dunedaq::conffwk::ConfigObject operator()(std::string const & key) = delete;
411
412};
413
417template<typename T> class authorized_getter<T, std::vector<dunedaq::conffwk::ConfigObject>>
418{
419 private:
421
422 public:
424 : that(caller)
425 {
426 }
427
428 std::vector<dunedaq::conffwk::ConfigObject> operator()(std::string const & key) = delete;
429
430};
431
432template<typename T, typename U> U authorized_getter<T, U>::operator()(
433 std::string const & key)
434{
435 return this->that->template getdirect<U>(key);
436}
437
438//------------------------------------------------------------------------------------------
439
440//------------------------------------------------------------------------------------------
446{
447 size_t operator()(dbe::cokey const & o) const
448 {
449 return std::hash<std::string>()(o.this_class + o.this_name);
450 }
451};
452//------------------------------------------------------------------------------------------
453
454//------------------------------------------------------------------------------------------
455class tref:
456 public ref_interface<tref>
457{
458 private:
459 std::shared_ptr<oref> refered;
460
461 dunedaq::conffwk::ConfigObject & ref(bool check_null = true) const
462 {
465 return rval;
466 }
467
468 tref(std::shared_ptr<oref> other)
469 : refered(other)
470 {
471 }
472
473 friend class ref_interface<tref> ;
475};
476//------------------------------------------------------------------------------------------
477
478//------------------------------------------------------------------------------------------
482class vref:
483 public ref_interface<vref>
484{
485 private:
486 std::weak_ptr<oref> refered;
487
488 dunedaq::conffwk::ConfigObject & ref(bool check_null = false) const
489 {
490 std::shared_ptr<oref> return_val =
491 refered.expired() ?
492 std::shared_ptr<oref>(new oref(dunedaq::conffwk::ConfigObject())) : refered.lock();
493 dunedaq::conffwk::ConfigObject & rval = static_cast<dunedaq::conffwk::ConfigObject &>(*return_val);
495 return rval;
496 }
497
498 vref(std::weak_ptr<oref> other)
499 : refered(other)
500 {
501 }
502
503 friend class ref_interface<vref> ;
505};
506//------------------------------------------------------------------------------------------
507
508//------------------------------------------------------------------------------------------
509template<typename T> inline std::ostream & operator<<(std::ostream & os,
510 ref_interface<T> const & atref)
511{
512 return os << static_cast<T const &>(atref).ref();
513}
514//------------------------------------------------------------------------------------------
515
516//------------------------------------------------------------------------------------------
517template<typename T>
518inline bool operator==(ref_interface<T> const & l,
519 ref_interface<T> const & r)
520{
521 return l.UID() == r.UID() and l.class_name() == r.class_name();
522}
523//------------------------------------------------------------------------------------------
524
525}
526}
527}
528
529#endif /* DBE_CONFIG_REFERENCE_HPP_ */
dunedaq::conffwk::ConfigObject operator()(std::string const &key)=delete
std::vector< configobject::tref > operator()(std::string const &key)
std::vector< dunedaq::conffwk::ConfigObject > operator()(std::string const &key)=delete
authorized_getter(ref_interface< T > const *caller)
friend bool operator==(dbe::cokey const &left, dbe::cokey const &right)
oref operator=(oref const &)=delete
oref(dunedaq::conffwk::ConfigObject const &o) noexcept(true)
void rename(std::string const &newname)
oref(oref const &)=delete
void set_enum(std::string const &name, std::string const &val)
void set_time(std::string const &name, const std::vector< std::string > &value)
friend class dbe::inner::configobject::vref
void set_objs(std::string const &name, std::vector< T > const &others, bool skip_non_null_check=false)
void set_class(std::string const &name, const std::vector< std::string > &value)
U getdirect(std::string const &key) const
void set_obj_null(std::string const &name, bool is_simple, bool skip_non_null_check=false)
void set_by_ref(std::string const &name, U &val)
void set_date(std::string const &name, const std::vector< std::string > &value)
void get(std::string const &key, U &val)
void set_class(std::string const &name, std::string const &val)
void print_ref(std::ostream &s, dunedaq::conffwk::Configuration &conf, const std::string &prefix="", bool show_contained_in=false) const
Print details of object's attributes and relationships.
void set_date(std::string const &name, std::string const &val)
void set_obj(std::string const &name, T const &other, bool skip_non_null_check=false)
std::vector< T > referenced_by(std::string const &name="*", bool check_composite_only=true) const
void set_enum(std::string const &name, const std::vector< std::string > &value)
friend std::ostream & operator<<(std::ostream &os, dbe::inner::configobject::tref const &atref)
void set_time(std::string const &name, std::string const &val)
friend class dbe::inner::configobject::tref
void set_by_val(std::string const &name, U val)
tref(std::shared_ptr< oref > other)
dunedaq::conffwk::ConfigObject & ref(bool check_null=true) const
std::shared_ptr< oref > refered
dunedaq::conffwk::ConfigObject & ref(bool check_null=false) const
vref(std::weak_ptr< oref > other)
wref(wref const &)=delete
wref(dunedaq::conffwk::ConfigObject const &o)
wref operator=(wref const &)=delete
dunedaq::conffwk::ConfigObject ref
static dbcontroller & ref()
Represents database objects.
void set_time(const std::string &name, const std::string &value)
Set attribute time value.
void set_by_val(const std::string &name, T value)
Set attribute value.
void set_date(const std::string &name, const std::string &value)
Set attribute date value.
void set_objs(const std::string &name, const std::vector< const ConfigObject * > &o, bool skip_non_null_check=false)
Set relationship multi-value.
void move(const std::string &at)
Move object to a different database.
const std::string & UID() const noexcept
Return object identity.
void set_class(const std::string &name, const std::string &value)
Set attribute class value.
void set_by_ref(const std::string &name, T &value)
Set attribute value.
bool is_deleted() const
Check if object was deleted.
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 set_enum(const std::string &name, const std::string &value)
Set attribute enumeration value.
const std::string full_name() const noexcept
Return full object name.
void set_obj(const std::string &name, const ConfigObject *o, bool skip_non_null_check=false)
Set relationship single-value.
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.
#define DEBUG_THROW_ON_NULL_CONFIGOBJECT_REFERENCE
bool operator==(ref_interface< T > const &l, ref_interface< T > const &r)
std::ostream & operator<<(std::ostream &os, ref_interface< T > const &atref)
Include QT Headers.
config_object_key cokey
Including Qt Headers.
Definition module.cpp:16
size_t operator()(dbe::cokey const &o) const