Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PluginServiceDetailsV1.h
Go to the documentation of this file.
1 #ifndef _GAUDI_PLUGIN_SERVICE_DETAILS_V1_H_
2 #define _GAUDI_PLUGIN_SERVICE_DETAILS_V1_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:
86 
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 
101  void* ptr;
105  Properties properties;
106 
107  FactoryInfo& addProperty( const KeyType& k, std::string v ) {
108  properties[k] = std::move( v );
109  return *this;
110  }
111  };
112 
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;
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 
179  FactoryMap m_factories;
180 
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 //_GAUDI_PLUGIN_SERVICE_DETAILS_H_
std::map< KeyType, std::string > Properties
Type used for the properties implementation.
FactoryInfo & add(const I &id, typename F::FuncType ptr)
Add a factory to the database.
GAUDIPS_API void * getCreator(const std::string &id, const std::string &type)
Function used to load a specific factory function.
Simple logging class, just to provide a default implementation.
FactoryMap m_factories
Internal storage for factories.
GAUDIPS_API void setLogger(Logger *logger)
Set the logger instance to use.
STL namespace.
std::vector< Gaudi::Details::PropertyBase * > Properties
Definition: PropertyMgr.h:132
Registry(const Registry &)
Private copy constructor for the singleton pattern.
#define GAUDI_PLUGIN_SERVICE_V1_INLINE
FactoryMap & factories()
Return the known factories (loading the list if not yet done).
const FactoryMap & factories() const
Return the known factories (loading the list if not yet done).
STL class.
GAUDIPS_API Logger & logger()
Return the current logger instance.
In-memory database of the loaded factories.
std::map< KeyType, FactoryInfo > FactoryMap
Type used for the database implementation.
#define GAUDIPS_API
T move(T...args)
FactoryInfo & addProperty(const KeyType &k, std::string v)
STL class.
GAUDIPS_API int Debug()
Backward compatibility with Reflex.
std::recursive_mutex m_mutex
Mutex used to control concurrent access to the internal data.
static S::ReturnType create(Args &&...args)
GAUDIPS_API std::string demangle(const std::type_info &id)
Return a canonical name for type_info object (implementation borrowed from GaudiKernel/System).
Class providing default factory functions.
FactoryInfo(std::string lib, void *p=nullptr, std::string t="", std::string rt="", std::string cn="", Properties props=Properties())
GAUDIPS_API void SetDebug(int debugLevel)
Backward compatibility with Reflex.
Helper functions to set/get the application return code.
Definition: __init__.py:1
bool m_initialized
Flag recording if the registry has been initialized or not.