|
DD4hep
1.30.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
25 #if __cplusplus >= 201703
27 inline bool any_has_value(std::any a){
return a.has_value(); }
29 # include <boost/any.hpp>
32 using boost::any_cast;
33 using boost::bad_any_cast;
38 #ifndef DD4HEP_PARSERS_NO_ROOT
47 template <
typename T>
class Handle;
53 template <
typename T>
static T*
ptr(
const T*
_p) {
return (T*)
_p; }
54 template <
typename T>
static T&
ref(
const T*
_p) {
return *(T*)
_p; }
55 template <
typename T>
static T
val(
const T*
_p) {
return T(*
_p); }
56 template <
typename T>
static T
value(
const void*
_p) {
return (T)
_p; }
57 static const char*
value(
const void*
_p) {
return (
const char*)(
_p); }
59 template <>
inline int PluginFactoryBase::value<int>(
const void*
_p) {
return *(
int*)(
_p); }
60 template <>
inline long PluginFactoryBase::value<long>(
const void*
_p) {
return *(
long*)(
_p); }
61 template <>
inline std::string PluginFactoryBase::value<std::string>(
const void*
_p) {
return *(std::string*)(
_p); }
62 template <>
inline const std::string& PluginFactoryBase::value<const std::string&>(
const void*
_p) {
return *(std::string*)(
_p); }
89 template <
typename R,
typename... Args>
static R
Create(
const std::string&
id, Args... args) {
90 typedef R(*func)(Args...);
93 #if DD4HEP_PLUGINSVC_VERSION==1
94 union {
void* ptr; func fcn; } fptr;
96 fptr.ptr = std::any_cast<void*>(f);
98 return (*fptr.fcn)(std::forward<Args>(args)...);
99 #elif DD4HEP_PLUGINSVC_VERSION==2
101 if ( std::any_cast<func>(f) )
102 return std::any_cast<func>(f)(std::forward<Args>(args)...);
105 catch(
const std::bad_any_cast& e) {
106 print_bad_cast(
id, f,
typeid(R(Args...)), e.what());
110 template <
typename FUNCTION>
static std::any
function(FUNCTION func) {
111 #if DD4HEP_PLUGINSVC_VERSION==1
112 union {
void* ptr; FUNCTION fcn; } fptr;
114 return std::any(fptr.ptr);
115 #elif DD4HEP_PLUGINSVC_VERSION==2
116 return std::any(func);
120 static bool setDebug(
bool new_value);
121 static stub_t
getCreator(
const std::string&
id,
const std::type_info&
info);
122 static void addFactory(
const std::string&
id,
124 const std::type_info& signature_type,
125 const std::type_info& return_type);
126 static void print_bad_cast(
const std::string&
id,
const stub_t& stub,
const std::type_info& signature,
const char* msg);
134 template <
typename R,
typename... Args>
static void add(
const std::string&
id, R(*func)(Args...)) {
135 svc_t::addFactory(
id,svc_t::function(func),
typeid(R(Args...)),
typeid(R));
142 template <
typename P,
typename S>
class Factory {};
145 #define DD4HEP_FACTORY_CALL(type,name,signature) dd4hep::PluginRegistry<signature>::add(name,Factory<type,signature>::call)
146 #define DD4HEP_IMPLEMENT_PLUGIN_REGISTRY(X,Y)
148 #define DD4HEP_OPEN_PLUGIN(ns,name) namespace ns { namespace { struct name {}; } } namespace dd4hep
149 #define DD4HEP_PLUGINSVC_CNAME(name, serial) name##_dict_##serial
150 #define DD4HEP_PLUGINSVC_FACTORY(type,name,signature,serial) \
152 struct DD4HEP_PLUGINSVC_CNAME(__typeFactory__,serial) { \
153 DD4HEP_PLUGINSVC_CNAME(__typeFactory__,serial)() { \
154 DD4HEP_FACTORY_CALL(type,#name,signature); }}; \
155 static const DD4HEP_PLUGINSVC_CNAME(__typeFactory__,serial) \
156 DD4HEP_PLUGINSVC_CNAME(s____typeFactory__,serial); \
160 #define DD4HEP_PLUGIN_FACTORY_ARGS_0(R) \
161 template <typename P> class Factory<P, R()> \
162 : public dd4hep::PluginFactoryBase { \
166 template <typename P> inline R Factory<P,R()>::call()
168 #define DD4HEP_PLUGIN_FACTORY_ARGS_1(R,A0) \
169 template <typename P> class Factory<P, R(A0)> \
170 : public dd4hep::PluginFactoryBase { \
172 static R call(A0 a0); \
174 template <typename P> inline R Factory<P,R(A0)>::call(A0 a0)
176 #define DD4HEP_PLUGIN_FACTORY_ARGS_2(R,A0,A1) \
177 template <typename P> class Factory<P, R(A0,A1)> \
178 : public dd4hep::PluginFactoryBase { \
180 static R call(A0 a0,A1 a1); \
182 template <typename P> inline R Factory<P,R(A0,A1)>::call(A0 a0, A1 a1)
184 #define DD4HEP_PLUGIN_FACTORY_ARGS_3(R,A0,A1,A2) \
185 template <typename P> class Factory<P, R(A0,A1,A2)> \
186 : public dd4hep::PluginFactoryBase { \
188 static R call(A0 a0,A1 a1,A2 a2); \
190 template <typename P> inline R Factory<P,R(A0,A1,A2)>::call(A0 a0, A1 a1, A2 a2)
192 #define DD4HEP_PLUGIN_FACTORY_ARGS_4(R,A0,A1,A2,A3) \
193 template <typename P> class Factory<P, R(A0,A1,A2,A3)> \
194 : public dd4hep::PluginFactoryBase { \
196 static R call(A0 a0,A1 a1,A2 a2, A3 a3); \
198 template <typename P> inline R Factory<P,R(A0,A1,A2,A3)>::call(A0 a0, A1 a1, A2 a2, A3 a3)
200 #define DD4HEP_PLUGIN_FACTORY_ARGS_5(R,A0,A1,A2,A3,A4) \
201 template <typename P> class Factory<P, R(A0,A1,A2,A3,A4)> \
202 : public dd4hep::PluginFactoryBase { \
204 static R call(A0 a0,A1 a1,A2 a2, A3 a3, A4 a4); \
206 template <typename P> inline R Factory<P,R(A0,A1,A2,A3,A4)>::call(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4)
209 #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)