 |
DD4hep
1.35.0
Detector Description Toolkit for High Energy Physics
|
Go to the documentation of this file.
13 #ifndef DD4HEP_PLUGINS_H
14 #define DD4HEP_PLUGINS_H
27 #ifndef DD4HEP_PARSERS_NO_ROOT
36 template <
typename T>
class Handle;
42 template <
typename T>
static T*
ptr(
const T*
_p) {
return (T*)
_p; }
43 template <
typename T>
static T&
ref(
const T*
_p) {
return *(T*)
_p; }
44 template <
typename T>
static T
val(
const T*
_p) {
return T(*
_p); }
45 template <
typename T>
static T
value(
const void*
_p) {
return (T)
_p; }
46 static const char*
value(
const void*
_p) {
return (
const char*)(
_p); }
48 template <>
inline int PluginFactoryBase::value<int>(
const void*
_p) {
return *(
int*)(
_p); }
49 template <>
inline long PluginFactoryBase::value<long>(
const void*
_p) {
return *(
long*)(
_p); }
50 template <>
inline std::string PluginFactoryBase::value<std::string>(
const void*
_p) {
return *(std::string*)(
_p); }
51 template <>
inline const std::string& PluginFactoryBase::value<const std::string&>(
const void*
_p) {
return *(std::string*)(
_p); }
78 template <
typename R,
typename... Args>
static R
Create(
const std::string&
id, Args... args) {
79 typedef R(*func)(Args...);
82 #if DD4HEP_PLUGINSVC_VERSION==1
83 union {
void* ptr; func fcn; } fptr;
85 fptr.ptr = std::any_cast<void*>(f);
87 return (*fptr.fcn)(std::forward<Args>(args)...);
88 #elif DD4HEP_PLUGINSVC_VERSION==2
90 if ( std::any_cast<func>(f) )
91 return std::any_cast<func>(f)(std::forward<Args>(args)...);
94 catch(
const std::bad_any_cast& e) {
95 print_bad_cast(
id, f,
typeid(R(Args...)), e.what());
99 template <
typename FUNCTION>
static std::any
function(FUNCTION func) {
100 #if DD4HEP_PLUGINSVC_VERSION==1
101 union {
void* ptr; FUNCTION fcn; } fptr;
103 return std::any(fptr.ptr);
104 #elif DD4HEP_PLUGINSVC_VERSION==2
105 return std::any(func);
109 static bool setDebug(
bool new_value);
110 static stub_t
getCreator(
const std::string&
id,
const std::type_info&
info);
111 static void addFactory(
const std::string&
id,
113 const std::type_info& signature_type,
114 const std::type_info& return_type);
115 static void print_bad_cast(
const std::string&
id,
const stub_t& stub,
const std::type_info& signature,
const char* msg);
123 template <
typename R,
typename... Args>
static void add(
const std::string&
id, R(*func)(Args...)) {
124 svc_t::addFactory(
id,svc_t::function(func),
typeid(R(Args...)),
typeid(R));
131 template <
typename P,
typename S>
class Factory {};
134 #define DD4HEP_FACTORY_CALL(type,name,signature) dd4hep::PluginRegistry<signature>::add(name,Factory<type,signature>::call)
135 #define DD4HEP_IMPLEMENT_PLUGIN_REGISTRY(X,Y)
137 #define DD4HEP_OPEN_PLUGIN(ns,name) namespace ns { namespace { struct name {}; } } namespace dd4hep
138 #define DD4HEP_PLUGINSVC_CNAME(name, serial) name##_dict_##serial
139 #define DD4HEP_PLUGINSVC_FACTORY(type,name,signature,serial) \
141 struct DD4HEP_PLUGINSVC_CNAME(__typeFactory__,serial) { \
142 DD4HEP_PLUGINSVC_CNAME(__typeFactory__,serial)() { \
143 DD4HEP_FACTORY_CALL(type,#name,signature); }}; \
144 static const DD4HEP_PLUGINSVC_CNAME(__typeFactory__,serial) \
145 DD4HEP_PLUGINSVC_CNAME(s____typeFactory__,serial); \
149 #define DD4HEP_PLUGIN_FACTORY_ARGS_0(R) \
150 template <typename P> class Factory<P, R()> \
151 : public dd4hep::PluginFactoryBase { \
155 template <typename P> inline R Factory<P,R()>::call()
157 #define DD4HEP_PLUGIN_FACTORY_ARGS_1(R,A0) \
158 template <typename P> class Factory<P, R(A0)> \
159 : public dd4hep::PluginFactoryBase { \
161 static R call(A0 a0); \
163 template <typename P> inline R Factory<P,R(A0)>::call(A0 a0)
165 #define DD4HEP_PLUGIN_FACTORY_ARGS_2(R,A0,A1) \
166 template <typename P> class Factory<P, R(A0,A1)> \
167 : public dd4hep::PluginFactoryBase { \
169 static R call(A0 a0,A1 a1); \
171 template <typename P> inline R Factory<P,R(A0,A1)>::call(A0 a0, A1 a1)
173 #define DD4HEP_PLUGIN_FACTORY_ARGS_3(R,A0,A1,A2) \
174 template <typename P> class Factory<P, R(A0,A1,A2)> \
175 : public dd4hep::PluginFactoryBase { \
177 static R call(A0 a0,A1 a1,A2 a2); \
179 template <typename P> inline R Factory<P,R(A0,A1,A2)>::call(A0 a0, A1 a1, A2 a2)
181 #define DD4HEP_PLUGIN_FACTORY_ARGS_4(R,A0,A1,A2,A3) \
182 template <typename P> class Factory<P, R(A0,A1,A2,A3)> \
183 : public dd4hep::PluginFactoryBase { \
185 static R call(A0 a0,A1 a1,A2 a2, A3 a3); \
187 template <typename P> inline R Factory<P,R(A0,A1,A2,A3)>::call(A0 a0, A1 a1, A2 a2, A3 a3)
189 #define DD4HEP_PLUGIN_FACTORY_ARGS_5(R,A0,A1,A2,A3,A4) \
190 template <typename P> class Factory<P, R(A0,A1,A2,A3,A4)> \
191 : public dd4hep::PluginFactoryBase { \
193 static R call(A0 a0,A1 a1,A2 a2, A3 a3, A4 a4); \
195 template <typename P> inline R Factory<P,R(A0,A1,A2,A3,A4)>::call(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4)
198 #endif // DD4HEP_PLUGINS_H
bool any_has_value(std::any a)
DD4HEP_SIGNATURE signature_t
Factory template for the plugin mechanism.
std::size_t info(const std::string &src, const std::string &msg)
PluginDebug(int dbg=2) noexcept(false)
Default constructor.
std::size_t debug(const std::string &src, const std::string &msg)
Factory template for the plugin mechanism.
static T * ptr(const T *_p)
Factory base class implementing some utilities.
Helper to debug plugin manager calls.
GAUDIPS_API void * getCreator(const std::string &id, const std::string &type)
static void add(const std::string &id, R(*func)(Args...))
static const char * value(const void *_p)
std::string missingFactory(const std::string &name) const
Helper to check factory existence.
static R Create(const std::string &id, Args... args)
Namespace for the AIDA detector description toolkit.
static T val(const T *_p)
static T value(const void *_p)
static T & ref(const T *_p)