| The Gaudi Framework v30r0 (c919700c)

- 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 #include <mutex>
24
25 #if __GNUC__ >= 4
26 #define GAUDIPS_HASCLASSVISIBILITY
27 #endif
28
29 #if defined( GAUDIPS_HASCLASSVISIBILITY )
30 #define GAUDIPS_IMPORT __attribute__( ( visibility( "default" ) ) )
31 #define GAUDIPS_EXPORT __attribute__( ( visibility( "default" ) ) )
32 #define GAUDIPS_LOCAL __attribute__( ( visibility( "hidden" ) ) )
33 #else
34 #define GAUDIPS_IMPORT
35 #define GAUDIPS_EXPORT
36 #define GAUDIPS_LOCAL
37 #endif
38
39 #ifdef GaudiPluginService_EXPORTS
40 #define GAUDIPS_API GAUDIPS_EXPORT
41 #else
42 #define GAUDIPS_API GAUDIPS_IMPORT
43 #endif
44
45 namespace Gaudi
46 {
47 namespace PluginService
48 {
49
50 namespace Details
51 {
56 template <class T>
57 class Factory
58 {
59 public:
60 #if !defined( __REFLEX__ ) || defined( ATLAS )
61 template <typename S, typename... Args>
62 static typename S::ReturnType create( Args&&... args )
63 {
64 return new T( std::forward<Args>( args )... );
65 }
66 #endif
67 };
68
71 GAUDIPS_API
72 void* getCreator( const std::string& id, const std::string& type );
73
86 template <typename F>
87 inline F getCreator( const std::string& id )
88 {
89 union {
90 void* src;
91 F dst;
92 } p2p;
93 p2p.src = getCreator( id, typeid( F ).name() );
94 return p2p.dst;
95 }
96
99 GAUDIPS_API
100 std::string demangle( const std::type_info& id );
101
103 template <typename T>
104 inline std::string demangle()
105 {
106 return demangle( typeid( T ) );
107 }
108
110 class GAUDIPS_API Registry
111 {
112 public:
113 typedef std::string KeyType;
114
116 typedef std::map<KeyType, std::string> Properties;
117
118 struct FactoryInfo {
119 FactoryInfo( std::string lib, void* p = nullptr, std::string t = "", std::string rt = "", std::string cn = "",
120 Properties props = Properties() )
121 : library( std::move( lib ) )
122 , ptr( p )
123 , type( std::move( t ) )
124 , rtype( std::move( rt ) )
125 , className( std::move( cn ) )
126 , properties( std::move( props ) )
127 {
128 }
129
130 std::string library;
131 void* ptr;
132 std::string type;
133 std::string rtype;
134 std::string className;
135 Properties properties;
136
137 FactoryInfo& addProperty( const KeyType& k, std::string v )
138 {
139 properties[k] = std::move( v );
140 return *this;
141 }
142 };
143
145 typedef std::map<KeyType, FactoryInfo> FactoryMap;
146
148 static Registry& instance();
149
151 template <typename F, typename T, typename I>
152 inline FactoryInfo& add( const I& id, typename F::FuncType ptr )
153 {
154 union {
155 typename F::FuncType src;
156 void* dst;
157 } p2p;
158 p2p.src = ptr;
159 std::ostringstream o;
160 o << id;
161 return add( o.str(), p2p.dst, typeid( typename F::FuncType ).name(), typeid( typename F::ReturnType ).name(),
162 demangle<T>() );
163 }
164
166 void* get( const std::string& id, const std::string& type ) const;
167
169 const FactoryInfo& getInfo( const std::string& id ) const;
170
172 Registry& addProperty( const std::string& id, const std::string& k, const std::string& v );
173
175 std::set<KeyType> loadedFactoryNames() const;
176
178 inline const FactoryMap& factories() const
179 {
180 if ( !m_initialized ) const_cast<Registry*>( this )->initialize();
181 return m_factories;
182 }
183
184 private:
189 Registry();
190
192 Registry( const Registry& ) : m_initialized( false ) {}
193
195 FactoryInfo& add( const std::string& id, void* factory, const std::string& type, const std::string& rtype,
196 const std::string& className, const Properties& props = Properties() );
197
199 inline FactoryMap& factories()
200 {
201 if ( !m_initialized ) initialize();
202 return m_factories;
203 }
204
207 void initialize();
208
210 bool m_initialized;
211
213 FactoryMap m_factories;
214
216 mutable std::recursive_mutex m_mutex;
217 };
218
220 class GAUDIPS_API Logger
221 {
222 public:
223 enum Level { Debug = 0, Info = 1, Warning = 2, Error = 3 };
224 Logger( Level level = Warning ) : m_level( level ) {}
225 virtual ~Logger() {}
226 inline Level level() const { return m_level; }
227 inline void setLevel( Level level ) { m_level = level; }
228 inline void info( const std::string& msg ) { report( Info, msg ); }
229 inline void debug( const std::string& msg ) { report( Debug, msg ); }
230 inline void warning( const std::string& msg ) { report( Warning, msg ); }
231 inline void error( const std::string& msg ) { report( Error, msg ); }
232
233 private:
234 virtual void report( Level lvl, const std::string& msg );
235 Level m_level;
236 };
237
239 GAUDIPS_API Logger& logger();
242 GAUDIPS_API void setLogger( Logger* logger );
243 }
244
246 GAUDIPS_API void SetDebug( int debugLevel );
248 GAUDIPS_API int Debug();
249 }
250 }
251
252 #define _INTERNAL_FACTORY_REGISTER_CNAME( name, serial ) _register_##_##serial
253
254 #define _INTERNAL_DECLARE_FACTORY_WITH_CREATOR( type, typecreator, id, factory, serial ) \
255 namespace \
256 { \
257 class _INTERNAL_FACTORY_REGISTER_CNAME( type, serial ) \
258 { \
259 public: \
260 typedef factory s_t; \
261 typedef typecreator f_t; \
262 static s_t::FuncType creator() { return &f_t::create<s_t>; } \
263 _INTERNAL_FACTORY_REGISTER_CNAME( type, serial )() \
264 { \
265 using ::Gaudi::PluginService::Details::Registry; \
266 Registry::instance().add<s_t, type>( id, creator() ); \
267 } \
268 } _INTERNAL_FACTORY_REGISTER_CNAME( s_##type, serial ); \
269 }
270
271 #define _INTERNAL_DECLARE_FACTORY( type, id, factory, serial ) \
272 _INTERNAL_DECLARE_FACTORY_WITH_CREATOR( type, ::Gaudi::PluginService::Details::Factory<type>, id, factory, serial )
273
274 #endif //_GAUDI_PLUGIN_SERVICE_DETAILS_H_
GAUDIPS_API
#define GAUDIPS_API
Definition: PluginServiceDetails.h:42
Gaudi::PluginService::Details::Logger::error
void error(const std::string &msg)
Definition: PluginServiceDetails.h:231
Gaudi::PluginService::Details::Registry::Registry
Registry(const Registry &)
Private copy constructor for the singleton pattern.
Definition: PluginServiceDetails.h:192
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:57
Gaudi::PluginService::Details::Logger::level
Level level() const
Definition: PluginServiceDetails.h:226
std
STL namespace.
Gaudi::PluginService::Details::Registry::factories
FactoryMap & factories()
Return the known factories (loading the list if not yet done).
Definition: PluginServiceDetails.h:199
Properties
std::vector< Gaudi::Details::PropertyBase * > Properties
Definition: PropertyMgr.h:134
Gaudi::PluginService::Details::Registry::FactoryInfo
Definition: PluginServiceDetails.h:118
std::map< KeyType, std::string >
Gaudi::PluginService::Details::Registry::FactoryInfo::properties
Properties properties
Definition: PluginServiceDetails.h:135
Gaudi::PluginService::Details::logger
GAUDIPS_API Logger & logger()
Return the current logger instance.
Definition: PluginService.cpp:339
Gaudi::PluginService::Details::Registry::FactoryInfo::addProperty
FactoryInfo & addProperty(const KeyType &k, std::string v)
Definition: PluginServiceDetails.h:137
Gaudi::PluginService::Details::Registry::Properties
std::map< KeyType, std::string > Properties
Type used for the properties implementation.
Definition: PluginServiceDetails.h:116
Gaudi::PluginService::Details::Logger
Simple logging class, just to provide a default implementation.
Definition: PluginServiceDetails.h:220
std::type_info
Gaudi::PluginService::Details::Factory::create
static S::ReturnType create(Args &&...args)
Definition: PluginServiceDetails.h:62
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:344
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:146
Gaudi::PluginService::Details::Logger::Level
Level
Definition: PluginServiceDetails.h:223
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:178
Gaudi::PluginService::Details::Registry::FactoryInfo::ptr
void * ptr
Definition: PluginServiceDetails.h:131
Gaudi::PluginService::Details::Registry::KeyType
std::string KeyType
Definition: PluginServiceDetails.h:113
std::move
T move(T...args)
Gaudi::PluginService::Details::Logger::warning
void warning(const std::string &msg)
Definition: PluginServiceDetails.h:230
Gaudi::PluginService::Details::Logger::info
void info(const std::string &msg)
Definition: PluginServiceDetails.h:228
Gaudi::PluginService::Details::Logger::Logger
Logger(Level level=Warning)
Definition: PluginServiceDetails.h:224
ReadAndWriteWhiteBoard.dst
dst
Definition: ReadAndWriteWhiteBoard.py:17
Gaudi::PluginService::Details::Registry::FactoryInfo::library
std::string library
Definition: PluginServiceDetails.h:130
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:126
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:133
Gaudi::PluginService::Details::Registry
In-memory database of the loaded factories.
Definition: PluginServiceDetails.h:110
Gaudi::PluginService::Details::Registry::add
FactoryInfo & add(const I &id, typename F::FuncType ptr)
Add a factory to the database.
Definition: PluginServiceDetails.h:152
Gaudi::PluginService::Details::Registry::m_mutex
std::recursive_mutex m_mutex
Mutex used to control concurrent access to the internal data.
Definition: PluginServiceDetails.h:216
gaudirun.level
level
Definition: gaudirun.py:271
Gaudi::PluginService::Debug
GAUDIPS_API int Debug()
Backward compatibility with Reflex.
Definition: PluginService.cpp:356
Gaudi::PluginService::Details::Registry::FactoryInfo::FactoryInfo
FactoryInfo(std::string lib, void *p=nullptr, std::string t="", std::string rt="", std::string cn="", Properties props=Properties())
Definition: PluginServiceDetails.h:119
Gaudi::PluginService::Details::Logger::m_level
Level m_level
Definition: PluginServiceDetails.h:235
Gaudi::PluginService::Details::setLogger
GAUDIPS_API void setLogger(Logger *logger)
Set the logger instance to use.
Definition: PluginService.cpp:340
Gaudi::PluginService::Details::Logger::setLevel
void setLevel(Level level)
Definition: PluginServiceDetails.h:227
Gaudi::PluginService::Details::Registry::FactoryMap
std::map< KeyType, FactoryInfo > FactoryMap
Type used for the database implementation.
Definition: PluginServiceDetails.h:145
Gaudi::PluginService::Details::Registry::m_factories
FactoryMap m_factories
Internal storage for factories.
Definition: PluginServiceDetails.h:213
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:134
Gaudi::PluginService::Details::Registry::m_initialized
bool m_initialized
Flag recording if the registry has been initialized or not.
Definition: PluginServiceDetails.h:210
Gaudi::PluginService::Details::Registry::FactoryInfo::type
std::string type
Definition: PluginServiceDetails.h:132
Gaudi::PluginService::Details::Logger::debug
void debug(const std::string &msg)
Definition: PluginServiceDetails.h:229
Gaudi::PluginService::Details::Logger::~Logger
virtual ~Logger()
Definition: PluginServiceDetails.h:225