| The Gaudi Framework v29r1 (5cd95fa2)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

PluginServiceDetails.h
Go to the documentation of this file.
1 #ifndef _GAUDI_PLUGIN_SERVICE_DETAILS_H_
2 #define _GAUDI_PLUGIN_SERVICE_DETAILS_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 <map>
17 #include <set>
18 #include <sstream>
19 #include <string>
20 #include <typeinfo>
21 #include <utility>
22
23 #if defined( __GXX_EXPERIMENTAL_CXX0X__ ) || __cplusplus >= 201103L
24 #include <mutex>
25 #endif
26
27 #if __GNUC__ >= 4
28 #define GAUDIPS_HASCLASSVISIBILITY
29 #endif
30
31 #if defined( GAUDIPS_HASCLASSVISIBILITY )
32 #define GAUDIPS_IMPORT __attribute__( ( visibility( "default" ) ) )
33 #define GAUDIPS_EXPORT __attribute__( ( visibility( "default" ) ) )
34 #define GAUDIPS_LOCAL __attribute__( ( visibility( "hidden" ) ) )
35 #else
36 #define GAUDIPS_IMPORT
37 #define GAUDIPS_EXPORT
38 #define GAUDIPS_LOCAL
39 #endif
40
41 #ifdef GaudiPluginService_EXPORTS
42 #define GAUDIPS_API GAUDIPS_EXPORT
43 #else
44 #define GAUDIPS_API GAUDIPS_IMPORT
45 #endif
46
47 namespace Gaudi
48 {
49 namespace PluginService
50 {
51
52 namespace Details
53 {
58 template <class T>
59 class Factory
60 {
61 public:
62 #if !defined( __REFLEX__ ) || defined( ATLAS )
63 template <typename S, typename... Args>
64 static typename S::ReturnType create( Args&&... args )
65 {
66 return new T( std::forward<Args>( args )... );
67 }
68 #endif
69 };
70
73 GAUDIPS_API
74 void* getCreator( const std::string& id, const std::string& type );
75
88 template <typename F>
89 inline F getCreator( const std::string& id )
90 {
91 union {
92 void* src;
93 F dst;
94 } p2p;
95 p2p.src = getCreator( id, typeid( F ).name() );
96 return p2p.dst;
97 }
98
101 GAUDIPS_API
102 std::string demangle( const std::type_info& id );
103
105 template <typename T>
106 inline std::string demangle()
107 {
108 return demangle( typeid( T ) );
109 }
110
112 class GAUDIPS_API Registry
113 {
114 public:
115 typedef std::string KeyType;
116
118 typedef std::map<KeyType, std::string> Properties;
119
120 struct FactoryInfo {
121 FactoryInfo( std::string lib, void* p = 0, std::string t = "", std::string rt = "", std::string cn = "",
122 Properties props = Properties() )
123 : library( std::move( lib ) )
124 , ptr( p )
125 , type( std::move( t ) )
126 , rtype( std::move( rt ) )
127 , className( std::move( cn ) )
128 , properties( std::move( props ) )
129 {
130 }
131
132 std::string library;
133 void* ptr;
134 std::string type;
135 std::string rtype;
136 std::string className;
137 Properties properties;
138
139 FactoryInfo& addProperty( const KeyType& k, std::string v )
140 {
141 properties[k] = std::move( v );
142 return *this;
143 }
144 };
145
147 typedef std::map<KeyType, FactoryInfo> FactoryMap;
148
150 static Registry& instance();
151
153 template <typename F, typename T, typename I>
154 inline FactoryInfo& add( const I& id, typename F::FuncType ptr )
155 {
156 union {
157 typename F::FuncType src;
158 void* dst;
159 } p2p;
160 p2p.src = ptr;
161 std::ostringstream o;
162 o << id;
163 return add( o.str(), p2p.dst, typeid( typename F::FuncType ).name(), typeid( typename F::ReturnType ).name(),
164 demangle<T>() );
165 }
166
168 void* get( const std::string& id, const std::string& type ) const;
169
171 const FactoryInfo& getInfo( const std::string& id ) const;
172
174 Registry& addProperty( const std::string& id, const std::string& k, const std::string& v );
175
177 std::set<KeyType> loadedFactories() const;
178
180 inline const FactoryMap& factories() const
181 {
182 if ( !m_initialized ) const_cast<Registry*>( this )->initialize();
183 return m_factories;
184 }
185
186 private:
191 Registry();
192
194 Registry( const Registry& ) : m_initialized( false ) {}
195
197 FactoryInfo& add( const std::string& id, void* factory, const std::string& type, const std::string& rtype,
198 const std::string& className, const Properties& props = Properties() );
199
201 inline FactoryMap& factories()
202 {
203 if ( !m_initialized ) initialize();
204 return m_factories;
205 }
206
209 void initialize();
210
212 bool m_initialized;
213
215 FactoryMap m_factories;
216
217 #if defined( __GXX_EXPERIMENTAL_CXX0X__ ) || __cplusplus >= 201103L
218 mutable std::recursive_mutex m_mutex;
220 #endif
221 };
222
224 class GAUDIPS_API Logger
225 {
226 public:
227 enum Level { Debug = 0, Info = 1, Warning = 2, Error = 3 };
228 Logger( Level level = Warning ) : m_level( level ) {}
229 virtual ~Logger() {}
230 inline Level level() const { return m_level; }
231 inline void setLevel( Level level ) { m_level = level; }
232 inline void info( const std::string& msg ) { report( Info, msg ); }
233 inline void debug( const std::string& msg ) { report( Debug, msg ); }
234 inline void warning( const std::string& msg ) { report( Warning, msg ); }
235 inline void error( const std::string& msg ) { report( Error, msg ); }
236
237 private:
238 virtual void report( Level lvl, const std::string& msg );
239 Level m_level;
240 };
241
243 GAUDIPS_API Logger& logger();
246 GAUDIPS_API void setLogger( Logger* logger );
247 }
248
250 GAUDIPS_API void SetDebug( int debugLevel );
252 GAUDIPS_API int Debug();
253 }
254 }
255
256 #define _INTERNAL_FACTORY_REGISTER_CNAME( name, serial ) _register_##_##serial
257
258 #define _INTERNAL_DECLARE_FACTORY_WITH_CREATOR( type, typecreator, id, factory, serial ) \
259 namespace \
260 { \
261 class _INTERNAL_FACTORY_REGISTER_CNAME( type, serial ) \
262 { \
263 public: \
264 typedef factory s_t; \
265 typedef typecreator f_t; \
266 static s_t::FuncType creator() { return &f_t::create<s_t>; } \
267 _INTERNAL_FACTORY_REGISTER_CNAME( type, serial )() \
268 { \
269 using ::Gaudi::PluginService::Details::Registry; \
270 Registry::instance().add<s_t, type>( id, creator() ); \
271 } \
272 } _INTERNAL_FACTORY_REGISTER_CNAME( s_##type, serial ); \
273 }
274
275 #define _INTERNAL_DECLARE_FACTORY( type, id, factory, serial ) \
276 _INTERNAL_DECLARE_FACTORY_WITH_CREATOR( type, ::Gaudi::PluginService::Details::Factory<type>, id, factory, serial )
277
278 #endif //_GAUDI_PLUGIN_SERVICE_DETAILS_H_
GAUDIPS_API
#define GAUDIPS_API
Definition: PluginServiceDetails.h:44
Gaudi::PluginService::Details::Logger::error
void error(const std::string &msg)
Definition: PluginServiceDetails.h:235
Gaudi::PluginService::Details::Registry::Registry
Registry(const Registry &)
Private copy constructor for the singleton pattern.
Definition: PluginServiceDetails.h:194
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:32
gaudirun.args
args
Definition: gaudirun.py:244
Gaudi::PluginService::Details::Factory
Class providing default factory functions.
Definition: PluginServiceDetails.h:59
Gaudi::PluginService::Details::Logger::level
Level level() const
Definition: PluginServiceDetails.h:230
std
STL namespace.
Gaudi::PluginService::Details::Registry::factories
FactoryMap & factories()
Return the known factories (loading the list if not yet done).
Definition: PluginServiceDetails.h:201
Properties
std::vector< Gaudi::Details::PropertyBase * > Properties
Definition: PropertyMgr.h:134
Gaudi::PluginService::Details::Registry::FactoryInfo
Definition: PluginServiceDetails.h:120
std::map< KeyType, std::string >
Gaudi::PluginService::Details::Registry::FactoryInfo::properties
Properties properties
Definition: PluginServiceDetails.h:137
Gaudi::PluginService::Details::logger
GAUDIPS_API Logger & logger()
Return the current logger instance.
Definition: PluginService.cpp:345
Gaudi::PluginService::Details::Registry::FactoryInfo::addProperty
FactoryInfo & addProperty(const KeyType &k, std::string v)
Definition: PluginServiceDetails.h:139
Gaudi::PluginService::Details::Registry::Properties
std::map< KeyType, std::string > Properties
Type used for the properties implementation.
Definition: PluginServiceDetails.h:118
Gaudi::PluginService::Details::Logger
Simple logging class, just to provide a default implementation.
Definition: PluginServiceDetails.h:224
std::type_info
Gaudi::PluginService::Details::Factory::create
static S::ReturnType create(Args &&...args)
Definition: PluginServiceDetails.h:64
std::string
STL class.
GaudiPython.Persistency.add
def add(instance)
Definition: Persistency.py:39
Gaudi::PluginService::SetDebug
GAUDIPS_API void SetDebug(int debugLevel)
Backward compatibility with Reflex.
Definition: PluginService.cpp:350
Gaudi::PluginService::Details::Registry::FactoryInfo::FactoryInfo
FactoryInfo(std::string lib, void *p=0, std::string t="", std::string rt="", std::string cn="", Properties props=Properties())
Definition: PluginServiceDetails.h:121
std::ostringstream
STL class.
gaudirun.type
type
Definition: gaudirun.py:133
Gaudi::PluginService::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: PluginService.cpp:151
Gaudi::PluginService::Details::Logger::Level
Level
Definition: PluginServiceDetails.h:227
std::recursive_mutex
std::ostringstream::str
T str(T...args)
Gaudi::PluginService::Details::Registry::factories
const FactoryMap & factories() const
Return the known factories (loading the list if not yet done).
Definition: PluginServiceDetails.h:180
Gaudi::PluginService::Details::Registry::FactoryInfo::ptr
void * ptr
Definition: PluginServiceDetails.h:133
Gaudi::PluginService::Details::Registry::KeyType
std::string KeyType
Definition: PluginServiceDetails.h:115
std::move
T move(T...args)
Gaudi::PluginService::Details::Logger::warning
void warning(const std::string &msg)
Definition: PluginServiceDetails.h:234
Gaudi::PluginService::Details::Logger::info
void info(const std::string &msg)
Definition: PluginServiceDetails.h:232
Gaudi::PluginService::Details::Logger::Logger
Logger(Level level=Warning)
Definition: PluginServiceDetails.h:228
ReadAndWriteWhiteBoard.dst
dst
Definition: ReadAndWriteWhiteBoard.py:17
Gaudi::PluginService::Details::Registry::FactoryInfo::library
std::string library
Definition: PluginServiceDetails.h:132
std::set
STL class.
Gaudi::PluginService::Details::getCreator
GAUDIPS_API void * getCreator(const std::string &id, const std::string &type)
Function used to load a specific factory function.
Definition: PluginService.cpp:131
gaudiComponentHelp.properties
properties
Definition: gaudiComponentHelp.py:41
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:8
Gaudi::PluginService::Details::Registry::FactoryInfo::rtype
std::string rtype
Definition: PluginServiceDetails.h:135
Gaudi::PluginService::Details::Registry
In-memory database of the loaded factories.
Definition: PluginServiceDetails.h:112
Gaudi::PluginService::Details::Registry::add
FactoryInfo & add(const I &id, typename F::FuncType ptr)
Add a factory to the database.
Definition: PluginServiceDetails.h:154
gaudirun.level
level
Definition: gaudirun.py:271
Gaudi::PluginService::Debug
GAUDIPS_API int Debug()
Backward compatibility with Reflex.
Definition: PluginService.cpp:362
Gaudi::PluginService::Details::Logger::m_level
Level m_level
Definition: PluginServiceDetails.h:239
Gaudi::PluginService::Details::setLogger
GAUDIPS_API void setLogger(Logger *logger)
Set the logger instance to use.
Definition: PluginService.cpp:346
Gaudi::PluginService::Details::Logger::setLevel
void setLevel(Level level)
Definition: PluginServiceDetails.h:231
Gaudi::PluginService::Details::Registry::FactoryMap
std::map< KeyType, FactoryInfo > FactoryMap
Type used for the database implementation.
Definition: PluginServiceDetails.h:147
Gaudi::PluginService::Details::Registry::m_factories
FactoryMap m_factories
Internal storage for factories.
Definition: PluginServiceDetails.h:215
Gaudi
Helper functions to set/get the application return code.
Definition: __init__.py:1
Gaudi::PluginService::Details::Registry::FactoryInfo::className
std::string className
Definition: PluginServiceDetails.h:136
Gaudi::PluginService::Details::Registry::m_initialized
bool m_initialized
Flag recording if the registry has been initialized or not.
Definition: PluginServiceDetails.h:212
Gaudi::PluginService::Details::Registry::FactoryInfo::type
std::string type
Definition: PluginServiceDetails.h:134
Gaudi::PluginService::Details::Logger::debug
void debug(const std::string &msg)
Definition: PluginServiceDetails.h:233
Gaudi::PluginService::Details::Logger::~Logger
virtual ~Logger()
Definition: PluginServiceDetails.h:229