The Gaudi Framework  master (181af51f)
Loading...
Searching...
No Matches
PluginServiceDetailsV1.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 2013-2025 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#pragma once
12
14
16
17#include <map>
18#include <set>
19#include <sstream>
20#include <string>
21#include <typeinfo>
22#include <utility>
23
24#include <mutex>
25
26namespace Gaudi {
27 namespace PluginService {
28 // cppcheck-suppress unknownMacro
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:
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;
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
167 if ( !m_initialized ) initialize();
168 return m_factories;
169 }
170
173 void initialize();
174
177
180
182 mutable std::recursive_mutex m_mutex;
183 };
184
187 public:
188 enum Level { Debug = 0, Info = 1, Warning = 2, Error = 3 };
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 )
#define GAUDIPS_API
#define GAUDI_PLUGIN_SERVICE_V1_INLINE
Class providing default factory functions.
static S::ReturnType create(Args &&... args)
Simple logging class, just to provide a default implementation.
virtual void report(Level lvl, const std::string &msg)
In-memory database of the loaded factories.
bool m_initialized
Flag recording if the registry has been initialized or not.
static Registry & instance()
Retrieve the singleton instance of Registry.
std::map< KeyType, FactoryInfo > FactoryMap
Type used for the database implementation.
std::recursive_mutex m_mutex
Mutex used to control concurrent access to the internal data.
FactoryInfo & add(const I &id, typename F::FuncType ptr)
Add a factory to the database.
Registry(const Registry &)
Private copy constructor for the singleton pattern.
void initialize()
Initialize the registry loading the list of factories from the .component files in the library search...
Registry()
Private constructor for the singleton pattern.
const FactoryMap & factories() const
Return the known factories (loading the list if not yet done).
FactoryMap & factories()
Return the known factories (loading the list if not yet done).
FactoryMap m_factories
Internal storage for factories.
GAUDIPS_API void setLogger(Logger *logger)
Set the logger instance to use.
GAUDIPS_API Logger & logger()
Return the current logger instance.
std::string demangle()
Return a canonical name for the template argument.
GAUDIPS_API std::string demangle(const std::type_info &id)
Return a canonical name for type_info object (implementation borrowed from GaudiKernel/System).
GAUDIPS_API void * getCreator(const std::string &id, const std::string &type)
Function used to load a specific factory function.
GAUDIPS_API void SetDebug(int debugLevel)
Backward compatibility with Reflex.
GAUDIPS_API int Debug()
Backward compatibility with Reflex.
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1
STL namespace.
FactoryInfo(std::string lib, void *p=nullptr, std::string t="", std::string rt="", std::string cn="", Properties props=Properties())
FactoryInfo & addProperty(const KeyType &k, std::string v)