DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
config_reference.hpp
Go to the documentation of this file.
1/*
2 * config_reference.hpp
3 *
4 * Created on: Feb 1, 2016
5 * Author: Leonidas Georgopoulos
6 */
7
8#ifndef DBE_CONFIG_REFERENCE_HPP_
9#define DBE_CONFIG_REFERENCE_HPP_
10
11#include "dbe/Exceptions.hpp"
13
15
16#include <vector>
17#include <memory>
18
19#ifndef DONOT_THROW_ON_NULL_CONFIGOBJECT_REFERENCE
20#define DEBUG_THROW_ON_NULL_CONFIGOBJECT_REFERENCE \
21 if(check_null and rval.is_deleted()) \
22 throw daq::dbe::null_config_reference_access(ERS_HERE);
23#else
24#define DEBUG_THROW_ON_NULL_CONFIGOBJECT_REFERENCE
25#endif
26
27namespace dbe
28{
29namespace inner
30{
31class dbcontroller;
32
33namespace configobject
34{
35
36//------------------------------------------------------------------------------------------
37class oref;
38
42class wref
43{
46 : ref(o)
47 {
48 }
49
50 wref(wref const &) = delete;
51 wref operator=(wref const &) = delete;
52
53 friend class oref;
54};
55//------------------------------------------------------------------------------------------
56
57//------------------------------------------------------------------------------------------
58struct refhasher;
59
60class tref;
61class vref;
62template<typename > class gref;
63template<typename > class aref;
67class oref
68{
69 friend class tref;
70 friend class vref;
71 friend struct refhasher;
72 template<typename > friend class aref;
73 template<typename > friend class gref;
75 friend bool operator ==(dbe::cokey const & left, dbe::cokey const & right);
76
79
85 oref(dunedaq::conffwk::ConfigObject const & o) noexcept(true)
86 : ref
87 { o },
88 this_last_full_name(o.full_name())
89 {
90 }
91
92 std::string full_name() const
93 {
95 }
96
97 void rename(std::string const & newname)
98 {
99 ref.ref.rename(newname);
100 this_last_full_name = newname;
101 }
102
108 explicit operator dunedaq::conffwk::ConfigObject&() noexcept
109 {
110 return ref.ref;
111 }
112
113 explicit operator dunedaq::conffwk::ConfigObject const &() const noexcept
114 {
115 return ref.ref;
116 }
117
118 // non-copyable
119 oref(oref const &) = delete;
120 oref operator=(oref const &) = delete;
121
122 public:
123 operator dbe::cokey() const
124 {
125 return
126 { ref.ref.UID(),ref.ref.class_name()};
127 }
128};
129//------------------------------------------------------------------------------------------
130
131//------------------------------------------------------------------------------------------
132template<typename T> class ref_interface;
133
134template<typename T, typename U> class authorized_getter
135{
136 private:
138
139 public:
141 : that(caller)
142 {
143 }
144
145 U operator()(std::string const & key);
146};
147
148template<typename T> class ref_interface
149{
150 private:
151 explicit operator dunedaq::conffwk::ConfigObject &() noexcept
152 {
153 return static_cast<T*>(this)->ref();
154 }
155
156 explicit operator dunedaq::conffwk::ConfigObject &() const noexcept
157 {
158 return static_cast<T const *>(this)->ref();
159 }
160
161 protected:
168 template<typename U> U getdirect(std::string const & key) const
169 {
170 U value;
171 try
172 {
173 static_cast<dunedaq::conffwk::ConfigObject &>(*const_cast<ref_interface<T>*>(this)).get(key, value);
174 }
175 catch (dunedaq::conffwk::Exception const & e)
176 {
177 // TODO handle retrieval errors here
178 }
179 return value;
180 }
181
182 template<typename X, typename U> friend class authorized_getter;
183
184 public:
185
186 bool is_null() const
187 {
188 return static_cast<dunedaq::conffwk::ConfigObject &>(static_cast<T const *>(this)->ref(false)).is_deleted();
189 }
190
191 std::string UID() const
192 {
193 return static_cast<dunedaq::conffwk::ConfigObject &>(*this).UID();
194 }
195
196 std::string class_name() const
197 {
198 return static_cast<dunedaq::conffwk::ConfigObject &>(*this).class_name();
199 }
200
201 std::string full_name() const
202 {
203 return static_cast<dunedaq::conffwk::ConfigObject &>(*this).full_name();
204 }
205
206 std::string contained_in() const
207 {
208 return static_cast<dunedaq::conffwk::ConfigObject &>(*this).contained_in();
209 }
210
217 template<typename U> U get(std::string const & key)
218 {
219 authorized_getter<T, U> guard(this);
220 return guard(key);
221 }
222
229 template<typename U> void get(std::string const & key, U & val)
230 {
231 val = get<U>(key);
232 }
233
241 std::vector<T> referenced_by(std::string const & name = "*", bool check_composite_only =
242 true) const;
243
244 void set_obj_null(std::string const & name, bool is_simple, bool skip_non_null_check = false)
245 {
246 if(is_simple)
247 {
248 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_obj(name, nullptr, skip_non_null_check);
249 } else
250 {
251 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_objs(name, {}, skip_non_null_check);
252 }
253 }
254
255 void set_obj(std::string const & name, T const & other,
256 bool skip_non_null_check = false)
257 {
258 static_cast<dunedaq::conffwk::ConfigObject &> ( *this ).set_obj ( name,
259 &static_cast<dunedaq::conffwk::ConfigObject &> ( other ),
260 skip_non_null_check);
261 }
262
263 void set_objs(std::string const & name, std::vector<T> const & others,
264 bool skip_non_null_check = false)
265 {
266 std::vector<dunedaq::conffwk::ConfigObject const *> configobjects;
267
268 std::transform(std::begin(others), std::end(others),
269 std::back_inserter(configobjects), [](T const & aref )
270 {
271 return &static_cast<dunedaq::conffwk::ConfigObject const &> ( aref );
272 } );
273
274 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_objs(name, configobjects, skip_non_null_check);
275 }
276
277 template<typename U> void set_by_val(std::string const & name, U val)
278 {
279 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_by_val(name, val);
280 }
281
282 template<typename U> void set_by_ref(std::string const & name, U & val)
283 {
284 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_by_ref(name, val);
285 }
286
287 void set_enum(std::string const & name, std::string const & val)
288 {
289 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_enum(name, val);
290 }
291
292 void set_class(std::string const & name, std::string const & val)
293 {
294 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_class(name, val);
295 }
296
297 void set_date(std::string const & name, std::string const & val)
298 {
299 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_date(name, val);
300 }
301
302 void set_time(std::string const & name, std::string const & val)
303 {
304 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_time(name, val);
305 }
306
307 void set_enum(std::string const & name, const std::vector<std::string>& value)
308 {
309 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_enum(name, value);
310 }
311
312 void set_class(std::string const & name, const std::vector<std::string>& value)
313 {
314 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_class(name, value);
315 }
316
317 void set_date(std::string const & name, const std::vector<std::string>& value)
318 {
319 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_date(name, value);
320 }
321
322 void set_time(std::string const & name, const std::vector<std::string>& value)
323 {
324 static_cast<dunedaq::conffwk::ConfigObject &>(*this).set_time(name, value);
325 }
326
327 void move(std::string const & at)
328 {
329 static_cast<dunedaq::conffwk::ConfigObject &>(*this).move(at);
330 }
331
336 void print_ref(std::ostream& s,
338 conf,
339 const std::string& prefix = "",
340 bool show_contained_in = false
341 ) const
342 {
343 static_cast<dunedaq::conffwk::ConfigObject &>(*this).print_ref(s, conf, prefix, show_contained_in);
344 }
345
346 friend std::ostream & operator<<(std::ostream & os,
347 dbe::inner::configobject::tref const & atref);
348
351 friend class dbe::inner::configobject::aref<T>;
352};
353//------------------------------------------------------------------------------------------
354
358template<typename T> class authorized_getter<T, configobject::tref>
359{
360 private:
362
363 public:
365 : that(caller)
366 {
367 }
368
369 configobject::tref operator()(std::string const & key);
370
371};
372
376template<typename T> class authorized_getter<T, std::vector<configobject::tref>>
377{
378 private:
380
381 public:
383 : that(caller)
384 {
385 }
386
387 std::vector<configobject::tref> operator()(std::string const & key);
388
389};
390
394template<typename T> class authorized_getter<T, dunedaq::conffwk::ConfigObject>
395{
396 private:
398
399 public:
401 : that(caller)
402 {
403 }
404
405 dunedaq::conffwk::ConfigObject operator()(std::string const & key) = delete;
406
407};
408
412template<typename T> class authorized_getter<T, std::vector<dunedaq::conffwk::ConfigObject>>
413{
414 private:
416
417 public:
419 : that(caller)
420 {
421 }
422
423 std::vector<dunedaq::conffwk::ConfigObject> operator()(std::string const & key) = delete;
424
425};
426
427template<typename T, typename U> U authorized_getter<T, U>::operator()(
428 std::string const & key)
429{
430 return this->that->template getdirect<U>(key);
431}
432
433//------------------------------------------------------------------------------------------
434
435//------------------------------------------------------------------------------------------
441{
442 size_t operator()(dbe::cokey const & o) const
443 {
444 return std::hash<std::string>()(o.this_class + o.this_name);
445 }
446};
447//------------------------------------------------------------------------------------------
448
449//------------------------------------------------------------------------------------------
450class tref:
451 public ref_interface<tref>
452{
453 private:
454 std::shared_ptr<oref> refered;
455
456 dunedaq::conffwk::ConfigObject & ref(bool check_null = true) const
457 {
460 return rval;
461 }
462
463 tref(std::shared_ptr<oref> other)
464 : refered(other)
465 {
466 }
467
468 friend class ref_interface<tref> ;
470};
471//------------------------------------------------------------------------------------------
472
473//------------------------------------------------------------------------------------------
477class vref:
478 public ref_interface<vref>
479{
480 private:
481 std::weak_ptr<oref> refered;
482
483 dunedaq::conffwk::ConfigObject & ref(bool check_null = false) const
484 {
485 std::shared_ptr<oref> return_val =
486 refered.expired() ?
487 std::shared_ptr<oref>(new oref(dunedaq::conffwk::ConfigObject())) : refered.lock();
488 dunedaq::conffwk::ConfigObject & rval = static_cast<dunedaq::conffwk::ConfigObject &>(*return_val);
490 return rval;
491 }
492
493 vref(std::weak_ptr<oref> other)
494 : refered(other)
495 {
496 }
497
498 friend class ref_interface<vref> ;
500};
501//------------------------------------------------------------------------------------------
502
503//------------------------------------------------------------------------------------------
504template<typename T> inline std::ostream & operator<<(std::ostream & os,
505 ref_interface<T> const & atref)
506{
507 return os << static_cast<T const &>(atref).ref();
508}
509//------------------------------------------------------------------------------------------
510
511//------------------------------------------------------------------------------------------
512template<typename T>
513inline bool operator==(ref_interface<T> const & l,
514 ref_interface<T> const & r)
515{
516 return l.UID() == r.UID() and l.class_name() == r.class_name();
517}
518//------------------------------------------------------------------------------------------
519
520}
521}
522}
523
524#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)
dunedaq::conffwk::ConfigObject & ref(bool check_null=true) const
std::shared_ptr< oref > refered
dunedaq::conffwk::ConfigObject & ref(bool check_null=false) const
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.
void rename(const std::string &new_id)
Rename object.
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
inner::configobject::aref< config_object_linked< std::string > > aref
Definition tref.hpp:32
Including Qt Headers.
size_t operator()(dbe::cokey const &o) const