DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
PluginServiceDetailsV1.h
Go to the documentation of this file.
1 #ifndef GAUDIPLUGINSERVICE_GAUDI_DETAILS_PLUGINSERVICEDETAILSV1_H
2 #define GAUDIPLUGINSERVICE_GAUDI_DETAILS_PLUGINSERVICEDETAILSV1_H
3 /*****************************************************************************\
4 * (c) Copyright 2013 CERN *
5 * *
6 * This software is distributed under the terms of the GNU General Public *
7 * Licence version 3 (GPL Version 3), copied verbatim in the file "LICENCE". *
8 * *
9 * In applying this licence, CERN does not waive the privileges and immunities *
10 * granted to it by virtue of its status as an Intergovernmental Organization *
11 * or submit itself to any jurisdiction. *
12 \*****************************************************************************/
13 
15 
17 
18 #include <map>
19 #include <set>
20 #include <sstream>
21 #include <string>
22 #include <typeinfo>
23 #include <utility>
24 
25 #include <mutex>
26 
27 namespace Gaudi {
28  namespace PluginService {
30  namespace Details {
35  template <class T>
36  class Factory {
37  public:
38  template <typename S, typename... Args>
39  static typename S::ReturnType create( Args&&... args ) {
40  return new T( std::forward<Args>( args )... );
41  }
42  };
43 
47  void* getCreator( const std::string& id, const std::string& type );
48 
61  template <typename F>
62  inline F getCreator( const std::string& id ) {
63  union {
64  void* src;
65  F dst;
66  } p2p;
67  p2p.src = getCreator( id, typeid( F ).name() );
68  return p2p.dst;
69  }
70 
74  std::string demangle( const std::type_info& id );
75 
77  template <typename T>
78  inline std::string demangle() {
79  return demangle( typeid( T ) );
80  }
81 
84  public:
85  typedef std::string KeyType;
86 
88  typedef std::map<KeyType, std::string> Properties;
89 
90  struct FactoryInfo {
91  FactoryInfo( std::string lib, void* p = nullptr, std::string t = "", std::string rt = "",
92  std::string cn = "", Properties props = Properties() )
93  : library( std::move( lib ) )
94  , ptr( p )
95  , type( std::move( t ) )
96  , rtype( std::move( rt ) )
97  , className( std::move( cn ) )
98  , properties( std::move( props ) ) {}
99 
100  std::string library;
101  void* ptr;
102  std::string type;
103  std::string rtype;
104  std::string className;
106 
107  FactoryInfo& addProperty( const KeyType& k, std::string v ) {
108  properties[k] = std::move( v );
109  return *this;
110  }
111  };
112 
114  typedef std::map<KeyType, FactoryInfo> FactoryMap;
115 
117  static Registry& instance();
118 
120  template <typename F, typename T, typename I>
121  inline FactoryInfo& add( const I& id, typename F::FuncType ptr ) {
122  union {
123  typename F::FuncType src;
124  void* dst;
125  } p2p;
126  p2p.src = ptr;
127  std::ostringstream o;
128  o << id;
129  return add( o.str(), p2p.dst, typeid( typename F::FuncType ).name(),
130  typeid( typename F::ReturnType ).name(), demangle<T>() );
131  }
132 
134  void* get( const std::string& id, const std::string& type ) const;
135 
137  const FactoryInfo& getInfo( const std::string& id ) const;
138 
140  Registry& addProperty( const std::string& id, const std::string& k, const std::string& v );
141 
143  std::set<KeyType> loadedFactoryNames() const;
144 
146  inline const FactoryMap& factories() const {
147  if ( !m_initialized ) const_cast<Registry*>( this )->initialize();
148  return m_factories;
149  }
150 
151  private:
156  Registry();
157 
159  Registry( const Registry& ) : m_initialized( false ) {}
160 
162  FactoryInfo& add( const std::string& id, void* factory, const std::string& type, const std::string& rtype,
163  const std::string& className, const Properties& props = Properties() );
164 
166  inline FactoryMap& factories() {
167  if ( !m_initialized ) initialize();
168  return m_factories;
169  }
170 
173  void initialize();
174 
177 
180 
182  mutable std::recursive_mutex m_mutex;
183  };
184 
187  public:
188  enum Level { Debug = 0, Info = 1, Warning = 2, Error = 3 };
189  Logger( Level level = Warning ) : m_level( level ) {}
190  virtual ~Logger() {}
191  inline Level level() const { return m_level; }
192  inline void setLevel( Level level ) { m_level = level; }
193  inline void info( const std::string& msg ) { report( Info, msg ); }
194  inline void debug( const std::string& msg ) { report( Debug, msg ); }
195  inline void warning( const std::string& msg ) { report( Warning, msg ); }
196  inline void error( const std::string& msg ) { report( Error, msg ); }
197 
198  private:
199  virtual void report( Level lvl, const std::string& msg );
201  };
202 
208  } // namespace Details
209 
211  GAUDIPS_API void SetDebug( int debugLevel );
213  GAUDIPS_API int Debug();
214  }
215  } // namespace PluginService
216 } // namespace Gaudi
217 
218 #define _PS_V1_INTERNAL_FACTORY_REGISTER_CNAME( name, serial ) _register_##_##serial
219 
220 #define _PS_V1_INTERNAL_DECLARE_FACTORY_WITH_CREATOR( type, typecreator, id, factory, serial ) \
221  namespace { \
222  class _PS_V1_INTERNAL_FACTORY_REGISTER_CNAME( type, serial ) { \
223  public: \
224  typedef factory s_t; \
225  typedef typecreator f_t; \
226  static s_t::FuncType creator() { return &f_t::create<s_t>; } \
227  _PS_V1_INTERNAL_FACTORY_REGISTER_CNAME( type, serial )() { \
228  using ::Gaudi::PluginService::v1::Details::Registry; \
229  Registry::instance().add<s_t, type>( id, creator() ); \
230  } \
231  } _PS_V1_INTERNAL_FACTORY_REGISTER_CNAME( s_##type, serial ); \
232  }
233 
234 #define _PS_V1_INTERNAL_DECLARE_FACTORY( type, id, factory, serial ) \
235  _PS_V1_INTERNAL_DECLARE_FACTORY_WITH_CREATOR( type, ::Gaudi::PluginService::v1::Details::Factory<type>, id, factory, \
236  serial )
237 
238 #endif // GAUDIPLUGINSERVICE_GAUDI_DETAILS_PLUGINSERVICEDETAILSV1_H
Gaudi::PluginService::v1::Details::Registry::add
FactoryInfo & add(const I &id, typename F::FuncType ptr)
Add a factory to the database.
Definition: PluginServiceDetailsV1.h:121
Gaudi::PluginService::v1::Details::Registry
In-memory database of the loaded factories.
Definition: PluginServiceDetailsV1.h:83
Gaudi::PluginService::v1::Details::Registry::KeyType
std::string KeyType
Definition: PluginServiceDetailsV1.h:85
v
View * v
Definition: MultiView.cpp:28
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::properties
Properties properties
Definition: PluginServiceDetailsV1.h:105
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::className
std::string className
Definition: PluginServiceDetailsV1.h:104
Gaudi::PluginService::v1::Details::Logger::info
void info(const std::string &msg)
Definition: PluginServiceDetailsV1.h:193
Gaudi::PluginService::v1::Details::Logger::level
Level level() const
Definition: PluginServiceDetailsV1.h:191
Gaudi::PluginService::v1::Details::Registry::m_initialized
bool m_initialized
Flag recording if the registry has been initialized or not.
Definition: PluginServiceDetailsV1.h:176
Gaudi::PluginService::v1::Details::logger
GAUDIPS_API Logger & logger()
Return the current logger instance.
Definition: PluginServiceV1.cpp:326
Gaudi::PluginService::v1::Details::Logger::m_level
Level m_level
Definition: PluginServiceDetailsV1.h:200
Gaudi::PluginService::v1::Details::Registry::Registry
Registry(const Registry &)
Private copy constructor for the singleton pattern.
Definition: PluginServiceDetailsV1.h:159
Gaudi::PluginService::v1::Details::Factory
Definition: PluginServiceDetailsV1.h:36
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::library
std::string library
Definition: PluginServiceDetailsV1.h:100
Gaudi::PluginService::v1::Details::Logger::debug
void debug(const std::string &msg)
Definition: PluginServiceDetailsV1.h:194
Gaudi::PluginService::v1::Details::Registry::Properties
std::map< KeyType, std::string > Properties
Type used for the properties implementation.
Definition: PluginServiceDetailsV1.h:88
Gaudi::PluginService::v1::Details::Logger::Level
Level
Definition: PluginServiceDetailsV1.h:188
Gaudi::PluginService::v1::Details::Logger::error
void error(const std::string &msg)
Definition: PluginServiceDetailsV1.h:196
Gaudi::PluginService::v1::Details::Logger::setLevel
void setLevel(Level level)
Definition: PluginServiceDetailsV1.h:192
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::rtype
std::string rtype
Definition: PluginServiceDetailsV1.h:103
Gaudi::PluginService::v1::Details::Registry::factories
const FactoryMap & factories() const
Return the known factories (loading the list if not yet done).
Definition: PluginServiceDetailsV1.h:146
Gaudi::PluginService::v1::Details::Registry::FactoryInfo
Definition: PluginServiceDetailsV1.h:90
Gaudi::PluginService::v1::SetDebug
GAUDIPS_API void SetDebug(int debugLevel)
Backward compatibility with Reflex.
Definition: PluginServiceV1.cpp:331
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::FactoryInfo
FactoryInfo(std::string lib, void *p=nullptr, std::string t="", std::string rt="", std::string cn="", Properties props=Properties())
Definition: PluginServiceDetailsV1.h:91
Gaudi::PluginService::v1::Details::getCreator
GAUDIPS_API void * getCreator(const std::string &id, const std::string &type)
Definition: PluginServiceV1.cpp:116
Gaudi::PluginService::v1::Details::Logger::warning
void warning(const std::string &msg)
Definition: PluginServiceDetailsV1.h:195
PluginServiceCommon.h
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::addProperty
FactoryInfo & addProperty(const KeyType &k, std::string v)
Definition: PluginServiceDetailsV1.h:107
GAUDI_PLUGIN_SERVICE_V1_INLINE
#define GAUDI_PLUGIN_SERVICE_V1_INLINE
Definition: PluginServiceCommon.h:21
Gaudi::PluginService::v1::Details::Logger::Logger
Logger(Level level=Warning)
Definition: PluginServiceDetailsV1.h:189
Gaudi::PluginService::v1::Details::Logger::~Logger
virtual ~Logger()
Definition: PluginServiceDetailsV1.h:190
Gaudi::PluginService::v1::Details::Registry::m_factories
FactoryMap m_factories
Internal storage for factories.
Definition: PluginServiceDetailsV1.h:179
std
Definition: Plugins.h:30
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::ptr
void * ptr
Definition: PluginServiceDetailsV1.h:101
Gaudi::PluginService::v1::Details::setLogger
GAUDIPS_API void setLogger(Logger *logger)
Definition: PluginServiceV1.cpp:327
Gaudi::PluginService::v1::Details::Registry::FactoryMap
std::map< KeyType, FactoryInfo > FactoryMap
Type used for the database implementation.
Definition: PluginServiceDetailsV1.h:114
Gaudi::PluginService::v1::Details::Factory::create
static S::ReturnType create(Args &&... args)
Definition: PluginServiceDetailsV1.h:39
Gaudi
Definition: PluginServiceDetailsV1.h:27
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::type
std::string type
Definition: PluginServiceDetailsV1.h:102
Gaudi::PluginService::v1::Details::demangle
GAUDIPS_API std::string demangle(const std::type_info &id)
Definition: PluginServiceV1.cpp:134
Gaudi::PluginService::v1::Details::Registry::m_mutex
std::recursive_mutex m_mutex
Mutex used to control concurrent access to the internal data.
Definition: PluginServiceDetailsV1.h:182
GAUDIPS_API
#define GAUDIPS_API
Definition: PluginServiceCommon.h:47
Gaudi::PluginService::v1::Debug
GAUDIPS_API int Debug()
Backward compatibility with Reflex.
Definition: PluginServiceV1.cpp:342
Gaudi::PluginService::v1::Details::Logger
Simple logging class, just to provide a default implementation.
Definition: PluginServiceDetailsV1.h:186
Gaudi::PluginService::v1::Details::Registry::factories
FactoryMap & factories()
Return the known factories (loading the list if not yet done).
Definition: PluginServiceDetailsV1.h:166