DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
DalObject.hpp
Go to the documentation of this file.
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: yes (from config/DalObject.h to include/conffwk/DalObject.hpp).
6//
7
14
15#ifndef CONFFWK_DAL_OBJECT_H_
16#define CONFFWK_DAL_OBJECT_H_
17
18#include <iostream>
19#include <memory>
20#include <mutex>
21#include <string>
22
25#include "conffwk/Change.hpp"
27#include "conffwk/Errors.hpp"
29
30namespace dunedaq {
31namespace conffwk {
32
33
50
52{
53
54 friend class Configuration;
55 friend class DalFactoryFunctions;
56 friend class DalRegistry;
57
58 friend std::ostream&
59 operator<<(std::ostream& s, const DalObject * obj);
60
61protected:
62
66
67 DalObject(DalRegistry& db, const ConfigObject& o) noexcept :
68 p_was_read(false), p_registry(db), p_obj(o), p_UID(p_obj.UID())
69 {
71 }
72
73 virtual ~DalObject()
74 {
75 ;
76 }
77
82
83 void clear() noexcept
84 {
85 p_obj._clear();
86 }
87
93
94 void check() const
95 {
96 p_obj.m_impl->throw_if_deleted();
97 }
98
99
105
106 bool is_deleted() const
107 {
108 return (p_obj.m_impl->is_deleted());
109 }
110
111
112protected:
113
115 mutable std::mutex m_mutex;
116
119
122
125
127 std::string p_UID;
128
129public:
130
134
135 const std::string& UID() const noexcept
136 {
137 return p_UID;
138 }
139
143
144 const std::string& class_name() const noexcept
145 {
146 return p_obj.class_name();
147 }
148
154
155 bool castable(const std::string& target) const noexcept
156 {
157 return p_registry.configuration().is_superclass_of(target, *p_obj.m_impl->m_class_name);
158 }
159
163
164 bool castable(const std::string * target) const noexcept
165 {
166 return p_registry.configuration().is_superclass_of(target, p_obj.m_impl->m_class_name);
167 }
168
177
178 template<class TARGET> const TARGET *
179 cast() const noexcept
180 {
181 // std::lock_guard<std::mutex> scoped_lock(m_mutex);
182 // return const_cast<Configuration&>(p_registry).cast<TARGET>(this);
183 return dynamic_cast<const TARGET*>(this);
184 }
185
186
190
191 std::string
192 full_name() const noexcept
193 {
194 std::lock_guard<std::mutex> scoped_lock(m_mutex);
195 return (p_UID + '@' + class_name());
196 }
197
198
204
205 const ConfigObject&
207 {
208 std::lock_guard<std::mutex> scoped_lock(m_mutex);
209 check();
210 return p_obj;
211 }
212
216
217 DalRegistry& registry() const noexcept
218 {
219 return p_registry;
220 }
221
225
226 Configuration& configuration() const noexcept
227 {
228 return p_registry.configuration();
229 }
230
231
238
239 void unread()
240 {
241 std::lock_guard<std::mutex> scoped_lock(m_mutex);
242 check();
243 p_was_read = false;
244 }
245
246
251
252 void set(const ConfigObject& o) noexcept
253 {
254 p_obj = o;
255 }
256
257
262
263 void move(const std::string& at)
264 {
265 p_obj.move(at);
266 }
267
272
273 void rename(const std::string& new_id)
274 {
275 p_obj.rename(new_id);
276 }
277
278 virtual std::vector<const DalObject *> get(const std::string& name, bool upcast_unregistered = true) const = 0;
279
280
281 // helper methods used by generated DALs
282
283public:
284
285
293
294 virtual void print(unsigned int offset, bool print_header, std::ostream& s) const = 0;
295
297 static void p_null(std::ostream& s);
298
300 static void p_rm(std::ostream& s);
301
303 static void p_error(std::ostream& s, dunedaq::conffwk::Exception& ex);
304
306 void p_hdr(std::ostream& s, unsigned int indent, const std::string& cl, const char * nm = nullptr) const;
307
309 std::ostream& print_object(std::ostream& s) const
310 {
311 if(DalObject::is_null(this))
312 {
314 }
315 else if(p_obj.m_impl->m_state != dunedaq::conffwk::Valid)
316 {
318 }
319 else
320 {
321 print(0, true, s);
322 }
323
324 return s;
325 }
326
328 void throw_init_ex(dunedaq::conffwk::Exception& ex);
329
331 static void throw_get_ex(const std::string& what, const std::string& class_name, const DalObject * obj);
332
334 static bool
335 is_null(const DalObject * ref) noexcept
336 {
337 return (ref == nullptr);
338 }
339
340
341
342// methods for configuration profiler
343
344protected:
345
349
350 void increment_created() noexcept
351 {
352 // ++(p_registry.p_number_of_template_object_created);
353 }
354
358
359 void increment_read() noexcept
360 {
361 // ++(p_registry.p_number_of_template_object_read);
362 }
363
364
365private:
366
367 // prevent copy constructor and operator=
368 DalObject(const DalObject&) = delete;
369 DalObject& operator=(const DalObject&) = delete;
370
371 // template<typename T>
372 // static void update(Configuration& db, const ConfigurationChange * change) noexcept
373 // {
374 // db.update<T>(change->get_modified_objs(), change->get_removed_objs(), change->get_created_objs());
375 // }
376
377 // template<typename T>
378 // static void change_id(CacheBase* x, const std::string& old_id, const std::string& new_id) noexcept
379 // {
380 // Configuration::_rename_object<T>(x, old_id, new_id);
381 // }
382
383 // template<typename T>
384 // static void unread(CacheBase* x) noexcept
385 // {
386 // Configuration::_unread_objects<T>(x);
387 // }
388
389 // template<typename T>
390 // static DalObject *
391 // create_instance(Configuration& db, ConfigObject& obj, const std::string& uid)
392 // {
393 // return db._make_instance<T>(obj, uid);
394 // }
395
396 // template<typename T>
397 // static DalObject *
398 // new_instance(Configuration& db, ConfigObject& obj)
399 // {
400 // return new T(db, obj);
401 // }
402
403protected:
404
409 virtual void init(bool init_children) = 0;
410
412 void check_init() const
413 {
414 if(!p_was_read)
415 {
416 std::lock_guard<std::mutex> scoped_lock(this->p_registry.m_mutex);
417 const_cast<DalObject*>(this)->init(false);
418 }
419 }
420
422 template<typename T>
423 void
424 _set_object(const std::string &name, const T *value)
425 {
426 std::lock_guard<std::mutex> scoped_lock(m_mutex);
427 check();
428 clear();
429 p_obj.set_obj(name, (value ? &value->config_object() : (ConfigObject*) nullptr));
430 }
431
433 template<typename T>
434 void
435 _set_objects(const std::string &name, const std::vector<const T*> &value)
436 {
437 std::lock_guard<std::mutex> scoped_lock(m_mutex);
438 check();
439 clear();
440 std::vector<const ConfigObject*> v;
441 for (auto &i : value)
442 v.push_back(&(i->config_object()));
443 p_obj.set_objs(name, v);
444 }
445
446
448 bool
449 get_rel_objects(const std::string& name, bool upcast_unregistered, std::vector<const DalObject*>& objs) const;
450
452 bool
453 get_algo_objects(const std::string& name, std::vector<const DalObject*>& objs) const;
454
455};
456
458
459std::ostream&
460operator<<(std::ostream&, const DalObject *);
461
463
464
465// template<class T>
466// DalFactoryFunctions::DalFactoryFunctions(boost::compute::identity<T>, const std::set<std::string> algorithms) :
467// m_update_fn(DalObject::update<T>),
468// m_creator_fn(DalObject::create_instance<T>),
469// m_instantiator_fn(DalObject::new_instance<T>),
470// {
471// ;
472// }
473
474
475// template<class T>
476// const T *
477// Configuration::create(const DalObject& at, const std::string& id, bool init_object)
478// {
479// return create<T>(at.config_object().contained_in(), id, init_object);
480// }
481
482// template<class T>
483// Configuration::Cache<T> *
484// Configuration::get_cache() noexcept
485// {
486// CacheBase*& c(m_cache_map[&T::s_class_name]);
487
488// if (c == nullptr)
489// c = new CacheBase(T::s_class_name, DalFactory::instance().functions(*this, T::s_class_name, true));
490
491// return static_cast<Cache<T>*>(c);
492// }
493
494// template<class TARGET, class SOURCE>
495// const TARGET *
496// Configuration::cast(const SOURCE *s) noexcept
497// {
498// if (s)
499// {
500// std::lock_guard<std::mutex> scoped_lock(m_tmpl_mutex);
501// ConfigObjectImpl * obj = s->p_obj.m_impl;
502
503// if (try_cast(&TARGET::s_class_name, obj->m_class_name) == true)
504// {
505// std::lock_guard<std::mutex> scoped_lock(obj->m_mutex);
506// if (obj->m_state == dunedaq::conffwk::Valid)
507// return _get<TARGET>(*const_cast<ConfigObject *>(&s->p_obj), s->UID());
508// }
509// }
510
511// return nullptr;
512// }
513
514} // namespace conffwk
515} // namespace dunedaq
516
517#endif // CONFFWK_DAL_OBJECT_H_
Represents database objects.
The base class for any generated DAL object.
Definition DalObject.hpp:52
virtual void print(unsigned int offset, bool print_header, std::ostream &s) const =0
std::string full_name() const noexcept
friend class DalFactoryFunctions
Definition DalObject.hpp:55
void increment_created() noexcept
void p_hdr(std::ostream &s, unsigned int indent, const std::string &cl, const char *nm=nullptr) const
print object headers
Definition DalObject.cpp:65
bool castable(const std::string &target) const noexcept
static bool is_null(const DalObject *ref) noexcept
check a pointer on DAL object is null
const TARGET * cast() const noexcept
Casts object to different class.
static void p_null(std::ostream &s)
print "(null)"
Definition DalObject.cpp:47
std::string p_UID
Is used for template objects (see dqm_conffwk).
static void p_rm(std::ostream &s)
print "(deleted object)"
Definition DalObject.cpp:53
virtual void init(bool init_children)=0
virtual std::vector< const DalObject * > get(const std::string &name, bool upcast_unregistered=true) const =0
DalRegistry & registry() const noexcept
void throw_init_ex(dunedaq::conffwk::Exception &ex)
throw object initialisation exception (i.e.
Definition DalObject.cpp:74
void increment_read() noexcept
const std::string & class_name() const noexcept
static void p_error(std::ostream &s, dunedaq::conffwk::Exception &ex)
print error text
Definition DalObject.cpp:59
DalObject & operator=(const DalObject &)=delete
bool get_rel_objects(const std::string &name, bool upcast_unregistered, std::vector< const DalObject * > &objs) const
Read relationship values as DAL objects using DAL factory.
Definition DalObject.cpp:8
DalObject(const DalObject &)=delete
std::ostream & print_object(std::ostream &s) const
print object details
void rename(const std::string &new_id)
static void throw_get_ex(const std::string &what, const std::string &class_name, const DalObject *obj)
throw exception in generated get method (i.e.
Definition DalObject.cpp:82
void _set_objects(const std::string &name, const std::vector< const T * > &value)
Helper method for generated set multi-value relationship methods.
Configuration & configuration() const noexcept
DalObject(DalRegistry &db, const ConfigObject &o) noexcept
Definition DalObject.hpp:67
void set(const ConfigObject &o) noexcept
bool castable(const std::string *target) const noexcept
const ConfigObject & config_object() const
const std::string & UID() const noexcept
std::mutex m_mutex
Used to protect changes of DAL object.
void _set_object(const std::string &name, const T *value)
Helper method for generated set single-value relationship methods.
DalRegistry & p_registry
Configuration object.
bool p_was_read
is true, if the object was read
bool get_algo_objects(const std::string &name, std::vector< const DalObject * > &objs) const
Run algorithm and return result as DAL objects using DAL factory.
Definition DalObject.cpp:25
ConfigObject p_obj
Config object used by given template object.
void check_init() const
Check and initialize object if necessary.
friend std::ostream & operator<<(std::ostream &s, const DalObject *obj)
Definition DalObject.cpp:91
void move(const std::string &at)
conffwk entry point
double offset
std::ostream & operator<<(std::ostream &, const ConfigurationChange &)
Including Qt Headers.
Definition module.cpp:16
msgpack::object obj
default char v[0]