| The Gaudi Framework v32r1 (f65d50dc)

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
16 #include "Gaudi/Details/PluginServiceCommon.h"
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 {
29 GAUDI_PLUGIN_SERVICE_V1_INLINE namespace v1 {
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
46 GAUDIPS_API
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
73 GAUDIPS_API
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
83 class GAUDIPS_API Registry {
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;
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
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
176 bool m_initialized;
177
179 FactoryMap m_factories;
180
182 mutable std::recursive_mutex m_mutex;
183 };
184
186 class GAUDIPS_API Logger {
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 );
200 Level m_level;
201 };
202
204 GAUDIPS_API Logger& logger();
207 GAUDIPS_API void setLogger( Logger* logger );
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::Properties
std::map< KeyType, std::string > Properties
Type used for the properties implementation.
Definition: PluginServiceDetailsV1.h:88
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::Logger::Logger
Logger(Level level=Warning)
Definition: PluginServiceDetailsV1.h:189
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::FactoryInfo::type
std::string type
Definition: PluginServiceDetailsV1.h:102
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:116
Gaudi::PluginService::v1::Details::Logger
Simple logging class, just to provide a default implementation.
Definition: PluginServiceDetailsV1.h:186
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
Gaudi::PluginService::v1::Details::Registry::m_factories
FactoryMap m_factories
Internal storage for factories.
Definition: PluginServiceDetailsV1.h:179
Gaudi::PluginService::v1::Details::setLogger
GAUDIPS_API void setLogger(Logger *logger)
Set the logger instance to use.
Definition: PluginServiceV1.cpp:325
std
STL namespace.
Gaudi::PluginService::v1::Details::Logger::debug
void debug(const std::string &msg)
Definition: PluginServiceDetailsV1.h:194
PluginServiceCommon.h
Gaudi::PluginService::v1::Details::Logger::error
void error(const std::string &msg)
Definition: PluginServiceDetailsV1.h:196
std::map< KeyType, std::string >
Gaudi::PluginService::v1::Details::Registry::Registry
Registry(const Registry &)
Private copy constructor for the singleton pattern.
Definition: PluginServiceDetailsV1.h:159
GAUDI_PLUGIN_SERVICE_V1_INLINE
#define GAUDI_PLUGIN_SERVICE_V1_INLINE
Definition: PluginServiceCommon.h:18
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::Logger::warning
void warning(const std::string &msg)
Definition: PluginServiceDetailsV1.h:195
std::type_info
Gaudi::PluginService::v1::Details::Registry::KeyType
std::string KeyType
Definition: PluginServiceDetailsV1.h:85
std::string
STL class.
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::properties
Properties properties
Definition: PluginServiceDetailsV1.h:105
GaudiPython.Persistency.add
def add(instance)
Definition: Persistency.py:39
MSG::Level
Level
Definition: IMessageSvc.h:15
gaudirun.args
args
Definition: gaudirun.py:307
Gaudi::PluginService::v1::Details::logger
GAUDIPS_API Logger & logger()
Return the current logger instance.
Definition: PluginServiceV1.cpp:324
std::ostringstream
STL class.
Gaudi::PluginService::v1::Details::Registry
In-memory database of the loaded factories.
Definition: PluginServiceDetailsV1.h:83
gaudirun.type
type
Definition: gaudirun.py:142
Gaudi::PluginService::v1::Details::Registry::FactoryInfo
Definition: PluginServiceDetailsV1.h:90
std::recursive_mutex
Gaudi::PluginService::v1::Details::Registry::FactoryMap
std::map< KeyType, FactoryInfo > FactoryMap
Type used for the database implementation.
Definition: PluginServiceDetailsV1.h:114
std::ostringstream::str
T str(T... args)
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::rtype
std::string rtype
Definition: PluginServiceDetailsV1.h:103
GAUDIPS_API
#define GAUDIPS_API
Definition: PluginServiceCommon.h:44
Gaudi::PluginService::v1::Details::Logger::~Logger
virtual ~Logger()
Definition: PluginServiceDetailsV1.h:190
std::move
T move(T... args)
Gaudi::PluginService::v1::Details::Logger::info
void info(const std::string &msg)
Definition: PluginServiceDetailsV1.h:193
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::addProperty
FactoryInfo & addProperty(const KeyType &k, std::string v)
Definition: PluginServiceDetailsV1.h:107
Gaudi::PluginService::v1::Details::Logger::m_level
Level m_level
Definition: PluginServiceDetailsV1.h:200
ReadAndWriteWhiteBoard.dst
dst
Definition: ReadAndWriteWhiteBoard.py:17
std::set
STL class.
Gaudi::PluginService::v1::Debug
GAUDIPS_API int Debug()
Backward compatibility with Reflex.
Definition: PluginServiceV1.cpp:340
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::Registry::FactoryInfo::className
std::string className
Definition: PluginServiceDetailsV1.h:104
Gaudi::Functional::details::get
auto get(const Handle &handle, const Algo &, const EventContext &) -> decltype(details::deref(handle.get()))
Definition: FunctionalDetails.h:332
Gaudi::PluginService::v1::Details::Logger::setLevel
void setLevel(Level level)
Definition: PluginServiceDetailsV1.h:192
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:134
gaudiComponentHelp.properties
properties
Definition: gaudiComponentHelp.py:51
Gaudi::PluginService::v1::Details::Factory
Class providing default factory functions.
Definition: PluginServiceDetailsV1.h:36
Gaudi::PluginService::v1::Details::Logger::Level
Level
Definition: PluginServiceDetailsV1.h:188
Gaudi::PluginService::v1::Details::Logger::level
Level level() const
Definition: PluginServiceDetailsV1.h:191
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:8
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::ptr
void * ptr
Definition: PluginServiceDetailsV1.h:101
gaudirun.level
level
Definition: gaudirun.py:334
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::library
std::string library
Definition: PluginServiceDetailsV1.h:100
Gaudi::PluginService::v1::SetDebug
GAUDIPS_API void SetDebug(int debugLevel)
Backward compatibility with Reflex.
Definition: PluginServiceV1.cpp:329
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
Gaudi::PluginService::v1::Details::Factory::create
static S::ReturnType create(Args &&... args)
Definition: PluginServiceDetailsV1.h:39
Gaudi::PluginService::v1::Details::Registry::m_initialized
bool m_initialized
Flag recording if the registry has been initialized or not.
Definition: PluginServiceDetailsV1.h:176