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
23#include "conffwk/Change.hpp"
28#include "conffwk/Errors.hpp"
29
30namespace dunedaq {
31namespace conffwk {
32
49
51{
52
53 friend class Configuration;
54 friend class DalFactoryFunctions;
55 friend class DalRegistry;
56
57 friend std::ostream& operator<<(std::ostream& s, const DalObject* obj);
58
59protected:
63
64 DalObject(DalRegistry& db, const ConfigObject& o) noexcept
65 : p_was_read(false)
66 , p_registry(db)
67 , p_obj(o)
68 , p_UID(p_obj.UID())
69 {
71 }
72
73 virtual ~DalObject() { ; }
74
79
80 void clear() noexcept { p_obj._clear(); }
81
87
88 void check() const { p_obj.m_impl->throw_if_deleted(); }
89
95
96 bool is_deleted() const { return (p_obj.m_impl->is_deleted()); }
97
98protected:
100 mutable std::mutex m_mutex;
101
104
107
110
112 std::string p_UID;
113
114public:
118
119 const std::string& UID() const noexcept { return p_UID; }
120
124
125 const std::string& class_name() const noexcept { return p_obj.class_name(); }
126
132
133 bool castable(const std::string& target) const noexcept
134 {
136 }
137
141
142 bool castable(const std::string* target) const noexcept
143 {
145 }
146
155
156 template<class TARGET>
157 const TARGET* cast() const noexcept
158 {
159 // std::lock_guard<std::mutex> scoped_lock(m_mutex);
160 // return const_cast<Configuration&>(p_registry).cast<TARGET>(this);
161 return dynamic_cast<const TARGET*>(this);
162 }
163
167
168 std::string full_name() const noexcept
169 {
170 std::lock_guard<std::mutex> scoped_lock(m_mutex);
171 return (p_UID + '@' + class_name());
172 }
173
179
181 {
182 std::lock_guard<std::mutex> scoped_lock(m_mutex);
183 check();
184 return p_obj;
185 }
186
190
191 DalRegistry& registry() const noexcept { return p_registry; }
192
196
197 Configuration& configuration() const noexcept { return p_registry.configuration(); }
198
205
206 void unread()
207 {
208 std::lock_guard<std::mutex> scoped_lock(m_mutex);
209 check();
210 p_was_read = false;
211 }
212
217
218 void set(const ConfigObject& o) noexcept { p_obj = o; }
219
224
225 void move(const std::string& at) { p_obj.move(at); }
226
231
232 void rename(const std::string& new_id) { p_obj.rename(new_id); }
233
234 virtual std::vector<const DalObject*> get(const std::string& name, bool upcast_unregistered = true) const = 0;
235
236 // helper methods used by generated DALs
237
238public:
246
247 virtual void print(unsigned int offset, bool print_header, std::ostream& s) const = 0;
248
250 static void p_null(std::ostream& s);
251
253 static void p_rm(std::ostream& s);
254
256 static void p_error(std::ostream& s, dunedaq::conffwk::Exception& ex);
257
259 void p_hdr(std::ostream& s, unsigned int indent, const std::string& cl, const char* nm = nullptr) const;
260
262 std::ostream& print_object(std::ostream& s) const
263 {
264 if (DalObject::is_null(this)) {
268 } else {
269 print(0, true, s);
270 }
271
272 return s;
273 }
274
276 void throw_init_ex(dunedaq::conffwk::Exception& ex);
277
279 static void throw_get_ex(const std::string& what, const std::string& class_name, const DalObject* obj);
280
282 static bool is_null(const DalObject* ref) noexcept { return (ref == nullptr); }
283
284 // methods for configuration profiler
285
286protected:
290
291 void increment_created() noexcept
292 {
293 // ++(p_registry.p_number_of_template_object_created);
294 }
295
299
300 void increment_read() noexcept
301 {
302 // ++(p_registry.p_number_of_template_object_read);
303 }
304
305private:
306 // prevent copy constructor and operator=
307 DalObject(const DalObject&) = delete;
308 DalObject& operator=(const DalObject&) = delete;
309
310 // template<typename T>
311 // static void update(Configuration& db, const ConfigurationChange * change) noexcept
312 // {
313 // db.update<T>(change->get_modified_objs(), change->get_removed_objs(), change->get_created_objs());
314 // }
315
316 // template<typename T>
317 // static void change_id(CacheBase* x, const std::string& old_id, const std::string& new_id) noexcept
318 // {
319 // Configuration::_rename_object<T>(x, old_id, new_id);
320 // }
321
322 // template<typename T>
323 // static void unread(CacheBase* x) noexcept
324 // {
325 // Configuration::_unread_objects<T>(x);
326 // }
327
328 // template<typename T>
329 // static DalObject *
330 // create_instance(Configuration& db, ConfigObject& obj, const std::string& uid)
331 // {
332 // return db._make_instance<T>(obj, uid);
333 // }
334
335 // template<typename T>
336 // static DalObject *
337 // new_instance(Configuration& db, ConfigObject& obj)
338 // {
339 // return new T(db, obj);
340 // }
341
342protected:
347 virtual void init(bool init_children) = 0;
348
350 void check_init() const
351 {
352 if (!p_was_read) {
353 std::lock_guard<std::mutex> scoped_lock(this->p_registry.m_mutex);
354 const_cast<DalObject*>(this)->init(false);
355 }
356 }
357
359 template<typename T>
360 void _set_object(const std::string& name, const T* value)
361 {
362 std::lock_guard<std::mutex> scoped_lock(m_mutex);
363 check();
364 clear();
365 p_obj.set_obj(name, (value ? &value->config_object() : (ConfigObject*)nullptr));
366 }
367
369 template<typename T>
370 void _set_objects(const std::string& name, const std::vector<const T*>& value)
371 {
372 std::lock_guard<std::mutex> scoped_lock(m_mutex);
373 check();
374 clear();
375 std::vector<const ConfigObject*> v;
376 for (auto& i : value)
377 v.push_back(&(i->config_object()));
378 p_obj.set_objs(name, v);
379 }
380
382 bool get_rel_objects(const std::string& name, bool upcast_unregistered, std::vector<const DalObject*>& objs) const;
383
385 bool get_algo_objects(const std::string& name, std::vector<const DalObject*>& objs) const;
386};
387
389
390std::ostream&
391operator<<(std::ostream&, const DalObject*);
392
394
395// template<class T>
396// DalFactoryFunctions::DalFactoryFunctions(boost::compute::identity<T>, const std::set<std::string> algorithms) :
397// m_update_fn(DalObject::update<T>),
398// m_creator_fn(DalObject::create_instance<T>),
399// m_instantiator_fn(DalObject::new_instance<T>),
400// {
401// ;
402// }
403
404// template<class T>
405// const T *
406// Configuration::create(const DalObject& at, const std::string& id, bool init_object)
407// {
408// return create<T>(at.config_object().contained_in(), id, init_object);
409// }
410
411// template<class T>
412// Configuration::Cache<T> *
413// Configuration::get_cache() noexcept
414// {
415// CacheBase*& c(m_cache_map[&T::s_class_name]);
416
417// if (c == nullptr)
418// c = new CacheBase(T::s_class_name, DalFactory::instance().functions(*this, T::s_class_name, true));
419
420// return static_cast<Cache<T>*>(c);
421// }
422
423// template<class TARGET, class SOURCE>
424// const TARGET *
425// Configuration::cast(const SOURCE *s) noexcept
426// {
427// if (s)
428// {
429// std::lock_guard<std::mutex> scoped_lock(m_tmpl_mutex);
430// ConfigObjectImpl * obj = s->p_obj.m_impl;
431
432// if (try_cast(&TARGET::s_class_name, obj->m_class_name) == true)
433// {
434// std::lock_guard<std::mutex> scoped_lock(obj->m_mutex);
435// if (obj->m_state == dunedaq::conffwk::Valid)
436// return _get<TARGET>(*const_cast<ConfigObject *>(&s->p_obj), s->UID());
437// }
438// }
439
440// return nullptr;
441// }
442
443} // namespace conffwk
444} // namespace dunedaq
445
446#endif // CONFFWK_DAL_OBJECT_H_
dunedaq::conffwk::ObjectState m_state
bool is_deleted() const
Check object and return true if the object has been deleted.
Represents database objects.
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.
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.
bool is_superclass_of(const std::string &target, const std::string &source) noexcept
The base class for any generated DAL object.
Definition DalObject.hpp:51
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:54
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:62
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:44
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:50
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:72
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:56
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:7
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:81
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:64
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:22
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:90
void move(const std::string &at)
Configuration & configuration()
conffwk entry point
double offset
The DUNE-DAQ namespace.
msgpack::object obj
std::ostream & operator<<(std::ostream &s, const ConfigurationChange &c)
default char v[0]