DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
DalFactory.cpp
Go to the documentation of this file.
2
3#include "ers/ers.hpp"
5
8#include "conffwk/Schema.hpp"
9#include "conffwk/Errors.hpp"
10
11#include <dlfcn.h>
12
13namespace dunedaq {
14namespace conffwk {
15
16const std::string&
17DalFactory::get_known_class_name_ref(const std::string& name)
18{
19 std::lock_guard<std::mutex> scoped_lock(m_known_class_mutex);
20 return *m_known_classes.emplace(name).first;
21}
22
23bool
24DalFactory::try_load_class_library(Configuration& db, const std::string& class_name) {
25
26 auto& c = db.get_class_info(class_name);
27 TLOG_DEBUG(1) << "Resolvung dal library for class " << class_name;
28
29 std::string file = c.p_schema_path;
30 std::string search {"/schema/"};
31 auto start = file.rfind(search);
32 if ( start == std::string::npos) {
33 throw (DalPackageNameNotFound(ERS_HERE, class_name, file));
34 }
35 start += search.size();
36 auto end = file.find("/", start);
37 if ( end == std::string::npos) {
38 throw (DalPackageNameNotFound(ERS_HERE, class_name, file));
39 }
40
41 std::string package = file.substr(start,end-start);
42 std::string library = "lib"+package+"_dal.so";
43 TLOG_DEBUG(1) << "Loading dal library " << library << " for class " << class_name;
44
45 auto handle = dlopen(library.c_str(), RTLD_LAZY|RTLD_GLOBAL);
46 if (handle == nullptr) {
47 throw (LoadDalFailed(ERS_HERE, library));
48 }
49
50 return handle != nullptr;
51}
52
56conffwk::DalObject*
57DalFactory::make(conffwk::DalRegistry& reg, conffwk::ConfigObject& o, bool upcast_unregistered) {
58
59
60 TLOG_DEBUG(50) << "Building object " << o.UID() << " of class " << o.class_name();
61
62 auto it = m_creators.find(o.class_name());
63
64 if (it == m_creators.end()) {
65 TLOG_DEBUG(50) << "Constructor for class " << o.class_name() << " not found";
66
67 if (!this->try_load_class_library(reg.configuration(), o.class_name())) {
68 throw NotFound(ERS_HERE, "class", o.class_name().c_str());
69 }
70 it = m_creators.find(o.class_name());
71 if (it == m_creators.end()) {
72 throw dunedaq::conffwk::NotFound(ERS_HERE, "class", o.class_name().c_str());
73 }
74 } else {
75 TLOG() << ">>> Constructor for class " << o.class_name() << " found";
76 }
77
78 auto dal_obj = it->second(reg,o);
79 TLOG_DEBUG(50) << "Object " << o.UID() << " of class " << o.class_name() << " created " << (void*)dal_obj;
80
81 return dal_obj;
82
83}
84
88conffwk::DalObject*
89DalFactory::make(conffwk::DalRegistry& reg, conffwk::ConfigObject& o, const std::string& fallback_unregistred) {
90
91
92 TLOG_DEBUG(50) << "Building object " << o.UID() << " of class " << o.class_name();
93
94 auto it = m_creators.find(o.class_name());
95
96 if (it == m_creators.end()) {
97 if (!this->try_load_class_library(reg.configuration(), o.class_name())) {
98 throw NotFound(ERS_HERE, "class", o.class_name().c_str());
99 }
100 it = m_creators.find(o.class_name());
101 if (it == m_creators.end()) {
102 throw dunedaq::conffwk::NotFound(ERS_HERE, "class", o.class_name().c_str());
103 }
104
105 }
106
107 auto dal_obj = it->second(reg,o);
108 TLOG_DEBUG(50) << "Object " << o.UID() << " of class " << o.class_name() << " created " << (void*)dal_obj;
109
110 return dal_obj;
111
112}
113
114DalFactory &
115DalFactory::instance()
116{
117 static DalFactory * instance = ers::SingletonCreator<DalFactory>::create();
118 return *instance;
119}
120
121
122DalObject *
123DalFactory::get(Configuration& db, ConfigObject& obj, const std::string& uid, bool upcast_unregistered) const
124{
125 return (*instance().functions(db, obj.class_name(), upcast_unregistered).m_creator_fn)(db, obj, uid);
126}
127
128DalObject *
129DalFactory::get(Configuration& db, ConfigObject& obj, const std::string& uid, const std::string& class_name) const
130{
131 return (*instance().functions(db, class_name, false).m_creator_fn)(db, obj, uid);
132}
133
134
135const DalFactoryFunctions&
136DalFactory::functions(Configuration& db, const std::string& name, bool upcast_unregistered)
137{
138 auto it = m_classes.find(name);
139
140 if (it == m_classes.end())
141 {
142 if (!this->try_load_class_library(db, name)) {
143 throw NotFound(ERS_HERE, "class", name.c_str());
144 }
145 it = m_classes.find(name);
146 if (it == m_classes.end()) {
147 throw dunedaq::conffwk::NotFound(ERS_HERE, "class", name.c_str());
148 }
149
150 }
151
152 return it->second;
153}
154
155const std::string&
156DalFactory::class4algo(Configuration& db, const std::string& name, const std::string& algorithm) const
157{
158 for (const auto& x : m_classes)
159 if (x.second.m_algorithms.find(algorithm) != x.second.m_algorithms.end() && db.try_cast(x.first, name))
160 return x.first;
161
162 static const std::string empty;
163 return empty;
164}
165
166
167const DalFactoryFunctions&
168DalFactory::functions(const std::string& name) const
169{
170 auto it = m_classes.find(name);
171
172 ERS_ASSERT_MSG( (it != m_classes.end()), "writer lock was not initialized" );
173
174 return it->second;
175}
176
177
178} // namespace conffwk
179} // namespace dunedaq
#define ERS_ASSERT_MSG(expression, message)
#define ERS_HERE
Try to access non-existent object or class.
Definition Errors.hpp:47
conffwk entry point
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112
#define TLOG(...)
Definition macro.hpp:22
Including Qt Headers.