DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
DalObject.hpp
Go to the documentation of this file.
1
8#ifndef CONFFWK_DAL_OBJECT_H_
9#define CONFFWK_DAL_OBJECT_H_
10
11#include <iostream>
12#include <memory>
13#include <mutex>
14#include <string>
15
18#include "conffwk/Change.hpp"
20#include "conffwk/Errors.hpp"
22
23namespace dunedaq {
24namespace conffwk {
25
26
45{
46
47 friend class Configuration;
48 friend class DalFactoryFunctions;
49 friend class DalRegistry;
50
51 friend std::ostream&
52 operator<<(std::ostream& s, const DalObject * obj);
53
54protected:
55
60 DalObject(DalRegistry& db, const ConfigObject& o) noexcept :
61 p_was_read(false), p_registry(db), p_obj(o), p_UID(p_obj.UID())
62 {
64 }
65
66 virtual ~DalObject()
67 {
68 ;
69 }
70
76 void clear() noexcept
77 {
78 p_obj._clear();
79 }
80
87 void check() const
88 {
90 }
91
92
99 bool is_deleted() const
100 {
101 return (p_obj.m_impl->is_deleted());
102 }
103
104
105protected:
106
108 mutable std::mutex m_mutex;
109
112
115
118
120 std::string p_UID;
121
122public:
123
128 const std::string& UID() const noexcept
129 {
130 return p_UID;
131 }
132
137 const std::string& class_name() const noexcept
138 {
139 return p_obj.class_name();
140 }
141
148 bool castable(const std::string& target) const noexcept
149 {
151 }
152
157 bool castable(const std::string * target) const noexcept
158 {
160 }
161
171 template<class TARGET> const TARGET *
172 cast() const noexcept
173 {
174 // std::lock_guard<std::mutex> scoped_lock(m_mutex);
175 // return const_cast<Configuration&>(p_registry).cast<TARGET>(this);
176 return dynamic_cast<const TARGET*>(this);
177 }
178
179
184 std::string
185 full_name() const noexcept
186 {
187 std::lock_guard<std::mutex> scoped_lock(m_mutex);
188 return (p_UID + '@' + class_name());
189 }
190
191
198 const ConfigObject&
200 {
201 std::lock_guard<std::mutex> scoped_lock(m_mutex);
202 check();
203 return p_obj;
204 }
205
210 DalRegistry& registry() const noexcept
211 {
212 return p_registry;
213 }
214
219 Configuration& configuration() const noexcept
220 {
221 return p_registry.configuration();
222 }
223
224
232 void unread()
233 {
234 std::lock_guard<std::mutex> scoped_lock(m_mutex);
235 check();
236 p_was_read = false;
237 }
238
239
245 void set(const ConfigObject& o) noexcept
246 {
247 p_obj = o;
248 }
249
250
256 void move(const std::string& at)
257 {
258 p_obj.move(at);
259 }
260
266 void rename(const std::string& new_id)
267 {
268 p_obj.rename(new_id);
269 }
270
271 virtual std::vector<const DalObject *> get(const std::string& name, bool upcast_unregistered = true) const = 0;
272
273
274 // helper methods used by generated DALs
275
276public:
277
278
287 virtual void print(unsigned int offset, bool print_header, std::ostream& s) const = 0;
288
290 static void p_null(std::ostream& s);
291
293 static void p_rm(std::ostream& s);
294
296 static void p_error(std::ostream& s, dunedaq::conffwk::Exception& ex);
297
299 void p_hdr(std::ostream& s, unsigned int indent, const std::string& cl, const char * nm = nullptr) const;
300
302 std::ostream& print_object(std::ostream& s) const
303 {
304 if(DalObject::is_null(this))
305 {
307 }
309 {
311 }
312 else
313 {
314 print(0, true, s);
315 }
316
317 return s;
318 }
319
321 void throw_init_ex(dunedaq::conffwk::Exception& ex);
322
324 static void throw_get_ex(const std::string& what, const std::string& class_name, const DalObject * obj);
325
327 static bool
328 is_null(const DalObject * ref) noexcept
329 {
330 return (ref == nullptr);
331 }
332
333
334
335// methods for configuration profiler
336
337protected:
338
343 void increment_created() noexcept
344 {
345 // ++(p_registry.p_number_of_template_object_created);
346 }
347
352 void increment_read() noexcept
353 {
354 // ++(p_registry.p_number_of_template_object_read);
355 }
356
357
358private:
359
360 // prevent copy constructor and operator=
361 DalObject(const DalObject&) = delete;
362 DalObject& operator=(const DalObject&) = delete;
363
364 // template<typename T>
365 // static void update(Configuration& db, const ConfigurationChange * change) noexcept
366 // {
367 // db.update<T>(change->get_modified_objs(), change->get_removed_objs(), change->get_created_objs());
368 // }
369
370 // template<typename T>
371 // static void change_id(CacheBase* x, const std::string& old_id, const std::string& new_id) noexcept
372 // {
373 // Configuration::_rename_object<T>(x, old_id, new_id);
374 // }
375
376 // template<typename T>
377 // static void unread(CacheBase* x) noexcept
378 // {
379 // Configuration::_unread_objects<T>(x);
380 // }
381
382 // template<typename T>
383 // static DalObject *
384 // create_instance(Configuration& db, ConfigObject& obj, const std::string& uid)
385 // {
386 // return db._make_instance<T>(obj, uid);
387 // }
388
389 // template<typename T>
390 // static DalObject *
391 // new_instance(Configuration& db, ConfigObject& obj)
392 // {
393 // return new T(db, obj);
394 // }
395
396protected:
397
402 virtual void init(bool init_children) = 0;
403
405 void check_init() const
406 {
407 if(!p_was_read)
408 {
409 std::lock_guard<std::mutex> scoped_lock(this->p_registry.m_mutex);
410 const_cast<DalObject*>(this)->init(false);
411 }
412 }
413
415 template<typename T>
416 void
417 _set_object(const std::string &name, const T *value)
418 {
419 std::lock_guard<std::mutex> scoped_lock(m_mutex);
420 check();
421 clear();
422 p_obj.set_obj(name, (value ? &value->config_object() : (ConfigObject*) nullptr));
423 }
424
426 template<typename T>
427 void
428 _set_objects(const std::string &name, const std::vector<const T*> &value)
429 {
430 std::lock_guard<std::mutex> scoped_lock(m_mutex);
431 check();
432 clear();
433 std::vector<const ConfigObject*> v;
434 for (auto &i : value)
435 v.push_back(&(i->config_object()));
436 p_obj.set_objs(name, v);
437 }
438
439
441 bool
442 get_rel_objects(const std::string& name, bool upcast_unregistered, std::vector<const DalObject*>& objs) const;
443
445 bool
446 get_algo_objects(const std::string& name, std::vector<const DalObject*>& objs) const;
447
448};
449
452std::ostream&
453operator<<(std::ostream&, const DalObject *);
454
456
457
458// template<class T>
459// DalFactoryFunctions::DalFactoryFunctions(boost::compute::identity<T>, const std::set<std::string> algorithms) :
460// m_update_fn(DalObject::update<T>),
461// m_creator_fn(DalObject::create_instance<T>),
462// m_instantiator_fn(DalObject::new_instance<T>),
463// {
464// ;
465// }
466
467
468// template<class T>
469// const T *
470// Configuration::create(const DalObject& at, const std::string& id, bool init_object)
471// {
472// return create<T>(at.config_object().contained_in(), id, init_object);
473// }
474
475// template<class T>
476// Configuration::Cache<T> *
477// Configuration::get_cache() noexcept
478// {
479// CacheBase*& c(m_cache_map[&T::s_class_name]);
480
481// if (c == nullptr)
482// c = new CacheBase(T::s_class_name, DalFactory::instance().functions(*this, T::s_class_name, true));
483
484// return static_cast<Cache<T>*>(c);
485// }
486
487// template<class TARGET, class SOURCE>
488// const TARGET *
489// Configuration::cast(const SOURCE *s) noexcept
490// {
491// if (s)
492// {
493// std::lock_guard<std::mutex> scoped_lock(m_tmpl_mutex);
494// ConfigObjectImpl * obj = s->p_obj.m_impl;
495
496// if (try_cast(&TARGET::s_class_name, obj->m_class_name) == true)
497// {
498// std::lock_guard<std::mutex> scoped_lock(obj->m_mutex);
499// if (obj->m_state == dunedaq::conffwk::Valid)
500// return _get<TARGET>(*const_cast<ConfigObject *>(&s->p_obj), s->UID());
501// }
502// }
503
504// return nullptr;
505// }
506
507} // namespace conffwk
508} // namespace dunedaq
509
510#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.
const std::string & UID() const noexcept
Return object identity.
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.
Defines base class for cache of template objects.
bool is_superclass_of(const std::string &target, const std::string &source) noexcept
The base class for any generated DAL object.
Definition DalObject.hpp:45
virtual void print(unsigned int offset, bool print_header, std::ostream &s) const =0
std::string full_name() const noexcept
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:60
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)
DalRegistry: A registry of DalObjects It provides a single interface to create, cache and manage DalO...
Configuration & configuration()
conffwk entry point
double offset
std::ostream & operator<<(std::ostream &, const ConfigurationChange &)
Including Qt Headers.