The Gaudi Framework  master (da3d77e1)
PluginServiceDetailsV1.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 2013-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef _GAUDI_PLUGIN_SERVICE_DETAILS_V1_H_
12 #define _GAUDI_PLUGIN_SERVICE_DETAILS_V1_H_
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;
106 
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 
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_
Gaudi::PluginService::v1::Details::Registry::KeyType
std::string KeyType
Definition: PluginServiceDetailsV1.h:85
Gaudi::PluginService::v1::Details::Factory::create
static S::ReturnType create(Args &&... args)
Definition: PluginServiceDetailsV1.h:39
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::library
std::string library
Definition: PluginServiceDetailsV1.h:100
Gaudi::PluginService::v1::Details::Registry
In-memory database of the loaded factories.
Definition: PluginServiceDetailsV1.h:83
std::string
STL class.
Gaudi::PluginService::v1::Details::Logger::~Logger
virtual ~Logger()
Definition: PluginServiceDetailsV1.h:190
std::move
T move(T... args)
GaudiPartProp.tests.v1
v1
Definition: tests.py:39
Gaudi::PluginService::v1::Details::Logger::m_level
Level m_level
Definition: PluginServiceDetailsV1.h:200
std::type_info
check_ParticleID.props
props
Definition: check_ParticleID.py:21
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
Gaudi::PluginService::v1::Details::Logger::error
void error(const std::string &msg)
Definition: PluginServiceDetailsV1.h:196
Gaudi::PluginService::v1::Details::Logger::debug
void debug(const std::string &msg)
Definition: PluginServiceDetailsV1.h:194
GaudiPartProp.decorators.get
get
decorate the vector of properties
Definition: decorators.py:283
Gaudi::PluginService::v1::Details::getCreator
GAUDIPS_API void * getCreator(const std::string &id, const std::string &type)
Function used to load a specific factory function.
Definition: PluginServiceV1.cpp:113
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
Gaudi::PluginService::v1::Details::Factory
Class providing default factory functions.
Definition: PluginServiceDetailsV1.h:36
std::recursive_mutex
STL class.
GaudiPartProp.tests.id
id
Definition: tests.py:111
Gaudi::PluginService::v1::Details::Registry::m_factories
FactoryMap m_factories
Internal storage for factories.
Definition: PluginServiceDetailsV1.h:179
Gaudi::PluginService::v1::Details::Registry::m_initialized
bool m_initialized
Flag recording if the registry has been initialized or not.
Definition: PluginServiceDetailsV1.h:176
Properties
Definition: Properties.py:1
bug_34121.t
t
Definition: bug_34121.py:31
Gaudi::PluginService::v1::Details::setLogger
GAUDIPS_API void setLogger(Logger *logger)
Set the logger instance to use.
Definition: PluginServiceV1.cpp:319
Gaudi::PluginService::v1::Details::Logger::info
void info(const std::string &msg)
Definition: PluginServiceDetailsV1.h:193
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::properties
Properties properties
Definition: PluginServiceDetailsV1.h:105
Gaudi::PluginService::v1::Details::Logger::setLevel
void setLevel(Level level)
Definition: PluginServiceDetailsV1.h:192
Gaudi::PluginService::v1::Details::Logger::Logger
Logger(Level level=Warning)
Definition: PluginServiceDetailsV1.h:189
Gaudi::PluginService::v1::Details::Registry::factories
FactoryMap & factories()
Return the known factories (loading the list if not yet done).
Definition: PluginServiceDetailsV1.h:166
Gaudi::PluginService::v1::Details::Registry::Properties
std::map< KeyType, std::string > Properties
Type used for the properties implementation.
Definition: PluginServiceDetailsV1.h:88
std::map< KeyType, std::string >
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
gaudirun.level
level
Definition: gaudirun.py:364
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
Gaudi::PluginService::v1::Details::Logger::level
Level level() const
Definition: PluginServiceDetailsV1.h:191
Gaudi::PluginService::v1::SetDebug
GAUDIPS_API void SetDebug(int debugLevel)
Backward compatibility with Reflex.
Definition: PluginServiceV1.cpp:323
Gaudi::PluginService::v1::Details::Registry::FactoryMap
std::map< KeyType, FactoryInfo > FactoryMap
Type used for the database implementation.
Definition: PluginServiceDetailsV1.h:114
gaudiComponentHelp.properties
properties
Definition: gaudiComponentHelp.py:68
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::rtype
std::string rtype
Definition: PluginServiceDetailsV1.h:103
GAUDI_PLUGIN_SERVICE_V1_INLINE
#define GAUDI_PLUGIN_SERVICE_V1_INLINE
Definition: PluginServiceCommon.h:18
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
std::ostringstream
STL class.
MSG::Level
Level
Definition: IMessageSvc.h:25
gaudirun.type
type
Definition: gaudirun.py:160
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::addProperty
FactoryInfo & addProperty(const KeyType &k, std::string v)
Definition: PluginServiceDetailsV1.h:107
Gaudi::PluginService::v1::Details::logger
GAUDIPS_API Logger & logger()
Return the current logger instance.
Definition: PluginServiceV1.cpp:318
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::className
std::string className
Definition: PluginServiceDetailsV1.h:104
gaudirun.args
args
Definition: gaudirun.py:336
std
STL namespace.
GAUDIPS_API
#define GAUDIPS_API
Definition: PluginServiceCommon.h:42
ReadAndWriteWhiteBoard.dst
dst
Definition: ReadAndWriteWhiteBoard.py:33
Gaudi::PluginService::v1::Details::Logger::Level
Level
Definition: PluginServiceDetailsV1.h:188
Properties.v
v
Definition: Properties.py:122
Gaudi::PluginService::v1::Debug
GAUDIPS_API int Debug()
Backward compatibility with Reflex.
Definition: PluginServiceV1.cpp:334
std::ostringstream::str
T str(T... args)
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::Registry::FactoryInfo::type
std::string type
Definition: PluginServiceDetailsV1.h:102
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::ptr
void * ptr
Definition: PluginServiceDetailsV1.h:101
Gaudi::PluginService::v1::Details::Registry::FactoryInfo
Definition: PluginServiceDetailsV1.h:90
Gaudi::PluginService::v1::Details::demangle
GAUDIPS_API std::string demangle(const std::type_info &id)
Return a canonical name for type_info object (implementation borrowed from GaudiKernel/System).
Definition: PluginServiceV1.cpp:131
Gaudi::PluginService::v1::Details::Registry::Registry
Registry(const Registry &)
Private copy constructor for the singleton pattern.
Definition: PluginServiceDetailsV1.h:159
Gaudi::PluginService::v1::Details::Logger
Simple logging class, just to provide a default implementation.
Definition: PluginServiceDetailsV1.h:186
Gaudi::PluginService::v1::Details::Logger::warning
void warning(const std::string &msg)
Definition: PluginServiceDetailsV1.h:195
std::set
STL class.
GaudiPython.Persistency.add
def add(instance)
Definition: Persistency.py:50
PluginServiceCommon.h