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 <memory>
22#include <vector>
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 {
33namespace inner {
34class dbcontroller;
35
36namespace configobject {
37
38//------------------------------------------------------------------------------------------
39class oref;
40
44class wref
45{
48 : ref(o)
49 {
50 }
51
52 wref(wref const&) = delete;
53 wref operator=(wref const&) = delete;
54
55 friend class oref;
56};
57//------------------------------------------------------------------------------------------
58
59//------------------------------------------------------------------------------------------
60struct refhasher;
61
62class tref;
63class vref;
64template<typename>
65class gref;
66template<typename>
67class aref;
71class oref
72{
73 friend class tref;
74 friend class vref;
75 friend struct refhasher;
76 template<typename>
77 friend class aref;
78 template<typename>
79 friend class gref;
81 friend bool operator==(dbe::cokey const& left, dbe::cokey const& right);
82
85
91 oref(dunedaq::conffwk::ConfigObject const& o) noexcept(true)
92 : ref{ o }
94 {
95 }
96
97 std::string full_name() const { return this_last_full_name; }
98
99 void rename(std::string const& newname)
100 {
101 ref.ref.rename(newname);
102 this_last_full_name = newname;
103 }
104
110 explicit operator dunedaq::conffwk::ConfigObject&() noexcept { return ref.ref; }
111
112 explicit operator dunedaq::conffwk::ConfigObject const&() const noexcept { return ref.ref; }
113
114 // non-copyable
115 oref(oref const&) = delete;
116 oref operator=(oref const&) = delete;
117
118public:
119 operator dbe::cokey() const { return { ref.ref.UID(), ref.ref.class_name() }; }
120};
121//------------------------------------------------------------------------------------------
122
123//------------------------------------------------------------------------------------------
124template<typename T>
125class ref_interface;
126
127template<typename T, typename U>
129{
130private:
132
133public:
135 : that(caller)
136 {
137 }
138
139 U operator()(std::string const& key);
140};
141
142template<typename T>
144{
145private:
146 explicit operator dunedaq::conffwk::ConfigObject&() noexcept { return static_cast<T*>(this)->ref(); }
147
148 explicit operator dunedaq::conffwk::ConfigObject&() const noexcept { return static_cast<T const*>(this)->ref(); }
149
150protected:
157 template<typename U>
158 U getdirect(std::string const& key) const
159 {
160 U value;
161 try {
162 static_cast<dunedaq::conffwk::ConfigObject&>(*const_cast<ref_interface<T>*>(this)).get(key, value);
163 } catch (dunedaq::conffwk::Exception const& e) {
164 // TODO handle retrieval errors here
165 }
166 return value;
167 }
168
169 template<typename X, typename U>
170 friend class authorized_getter;
171
172public:
173 bool is_null() const
174 {
175 return static_cast<dunedaq::conffwk::ConfigObject&>(static_cast<T const*>(this)->ref(false)).is_deleted();
176 }
177
178 std::string UID() const { return static_cast<dunedaq::conffwk::ConfigObject&>(*this).UID(); }
179
180 std::string class_name() const { return static_cast<dunedaq::conffwk::ConfigObject&>(*this).class_name(); }
181
182 std::string full_name() const { return static_cast<dunedaq::conffwk::ConfigObject&>(*this).full_name(); }
183
184 std::string contained_in() const { return static_cast<dunedaq::conffwk::ConfigObject&>(*this).contained_in(); }
185
192 template<typename U>
193 U get(std::string const& key)
194 {
195 authorized_getter<T, U> guard(this);
196 return guard(key);
197 }
198
205 template<typename U>
206 void get(std::string const& key, U& val)
207 {
208 val = get<U>(key);
209 }
210
218 std::vector<T> referenced_by(std::string const& name = "*", bool check_composite_only = true) const;
219
220 void set_obj_null(std::string const& name, bool is_simple, bool skip_non_null_check = false)
221 {
222 if (is_simple) {
223 static_cast<dunedaq::conffwk::ConfigObject&>(*this).set_obj(name, nullptr, skip_non_null_check);
224 } else {
225 static_cast<dunedaq::conffwk::ConfigObject&>(*this).set_objs(name, {}, skip_non_null_check);
226 }
227 }
228
229 void set_obj(std::string const& name, T const& other, bool skip_non_null_check = false)
230 {
231 static_cast<dunedaq::conffwk::ConfigObject&>(*this).set_obj(
232 name, &static_cast<dunedaq::conffwk::ConfigObject&>(other), skip_non_null_check);
233 }
234
235 void set_objs(std::string const& name, std::vector<T> const& others, bool skip_non_null_check = false)
236 {
237 std::vector<dunedaq::conffwk::ConfigObject const*> configobjects;
238
239 std::transform(std::begin(others), std::end(others), std::back_inserter(configobjects), [](T const& aref) {
240 return &static_cast<dunedaq::conffwk::ConfigObject const&>(aref);
241 });
242
243 static_cast<dunedaq::conffwk::ConfigObject&>(*this).set_objs(name, configobjects, skip_non_null_check);
244 }
245
246 template<typename U>
247 void set_by_val(std::string const& name, U val)
248 {
249 static_cast<dunedaq::conffwk::ConfigObject&>(*this).set_by_val(name, val);
250 }
251
252 template<typename U>
253 void set_by_ref(std::string const& name, U& val)
254 {
255 static_cast<dunedaq::conffwk::ConfigObject&>(*this).set_by_ref(name, val);
256 }
257
258 void set_enum(std::string const& name, std::string const& val)
259 {
260 static_cast<dunedaq::conffwk::ConfigObject&>(*this).set_enum(name, val);
261 }
262
263 void set_class(std::string const& name, std::string const& val)
264 {
265 static_cast<dunedaq::conffwk::ConfigObject&>(*this).set_class(name, val);
266 }
267
268 void set_date(std::string const& name, std::string const& val)
269 {
270 static_cast<dunedaq::conffwk::ConfigObject&>(*this).set_date(name, val);
271 }
272
273 void set_time(std::string const& name, std::string const& val)
274 {
275 static_cast<dunedaq::conffwk::ConfigObject&>(*this).set_time(name, val);
276 }
277
278 void set_enum(std::string const& name, const std::vector<std::string>& value)
279 {
280 static_cast<dunedaq::conffwk::ConfigObject&>(*this).set_enum(name, value);
281 }
282
283 void set_class(std::string const& name, const std::vector<std::string>& value)
284 {
285 static_cast<dunedaq::conffwk::ConfigObject&>(*this).set_class(name, value);
286 }
287
288 void set_date(std::string const& name, const std::vector<std::string>& value)
289 {
290 static_cast<dunedaq::conffwk::ConfigObject&>(*this).set_date(name, value);
291 }
292
293 void set_time(std::string const& name, const std::vector<std::string>& value)
294 {
295 static_cast<dunedaq::conffwk::ConfigObject&>(*this).set_time(name, value);
296 }
297
298 void move(std::string const& at) { static_cast<dunedaq::conffwk::ConfigObject&>(*this).move(at); }
299
303
305 std::ostream& s,
307 const std::string& prefix = "",
308 bool show_contained_in = false
309 ) const
310 {
311 static_cast<dunedaq::conffwk::ConfigObject&>(*this).print_ref(s, conf, prefix, show_contained_in);
312 }
313
314 friend std::ostream& operator<<(std::ostream& os, dbe::inner::configobject::tref const& atref);
315
318 friend class dbe::inner::configobject::aref<T>;
319};
320//------------------------------------------------------------------------------------------
321
325template<typename T>
327{
328private:
330
331public:
333 : that(caller)
334 {
335 }
336
337 configobject::tref operator()(std::string const& key);
338};
339
343template<typename T>
344class authorized_getter<T, std::vector<configobject::tref>>
345{
346private:
348
349public:
351 : that(caller)
352 {
353 }
354
355 std::vector<configobject::tref> operator()(std::string const& key);
356};
357
361template<typename T>
363{
364private:
366
367public:
369 : that(caller)
370 {
371 }
372
373 dunedaq::conffwk::ConfigObject operator()(std::string const& key) = delete;
374};
375
379template<typename T>
380class authorized_getter<T, std::vector<dunedaq::conffwk::ConfigObject>>
381{
382private:
384
385public:
387 : that(caller)
388 {
389 }
390
391 std::vector<dunedaq::conffwk::ConfigObject> operator()(std::string const& key) = delete;
392};
393
394template<typename T, typename U>
395U
397{
398 return this->that->template getdirect<U>(key);
399}
400
401//------------------------------------------------------------------------------------------
402
403//------------------------------------------------------------------------------------------
409{
410 size_t operator()(dbe::cokey const& o) const { return std::hash<std::string>()(o.this_class + o.this_name); }
411};
412//------------------------------------------------------------------------------------------
413
414//------------------------------------------------------------------------------------------
415class tref : public ref_interface<tref>
416{
417private:
418 std::shared_ptr<oref> refered;
419
420 dunedaq::conffwk::ConfigObject& ref(bool check_null = true) const
421 {
424 return rval;
425 }
426
427 tref(std::shared_ptr<oref> other)
428 : refered(other)
429 {
430 }
431
432 friend class ref_interface<tref>;
434};
435//------------------------------------------------------------------------------------------
436
437//------------------------------------------------------------------------------------------
441class vref : public ref_interface<vref>
442{
443private:
444 std::weak_ptr<oref> refered;
445
446 dunedaq::conffwk::ConfigObject& ref(bool check_null = false) const
447 {
448 std::shared_ptr<oref> return_val =
449 refered.expired() ? std::shared_ptr<oref>(new oref(dunedaq::conffwk::ConfigObject())) : refered.lock();
450 dunedaq::conffwk::ConfigObject& rval = static_cast<dunedaq::conffwk::ConfigObject&>(*return_val);
452 return rval;
453 }
454
455 vref(std::weak_ptr<oref> other)
456 : refered(other)
457 {
458 }
459
460 friend class ref_interface<vref>;
462};
463//------------------------------------------------------------------------------------------
464
465//------------------------------------------------------------------------------------------
466template<typename T>
467inline std::ostream&
468operator<<(std::ostream& os, ref_interface<T> const& atref)
469{
470 return os << static_cast<T const&>(atref).ref();
471}
472//------------------------------------------------------------------------------------------
473
474//------------------------------------------------------------------------------------------
475template<typename T>
476inline bool
478{
479 return l.UID() == r.UID() and l.class_name() == r.class_name();
480}
481//------------------------------------------------------------------------------------------
482
483}
484}
485}
486
487#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
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
The DUNE-DAQ namespace.
size_t operator()(dbe::cokey const &o) const