The Gaudi Framework  master (37c0b60a)
PluginServiceDetailsV2.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_V2_H_
12 #define _GAUDI_PLUGIN_SERVICE_DETAILS_V2_H_
13 
15 
17 
18 #include <any>
19 #include <functional>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <set>
24 #include <sstream>
25 #include <string>
26 #include <typeinfo>
27 #include <utility>
28 
29 namespace Gaudi {
30  namespace PluginService {
33  template <typename>
34  struct Factory;
36 
38  namespace Details {
39  template <typename>
40  struct Traits;
41 
42  template <typename R, typename... Args>
43  struct Traits<R( Args... )> {
44  using ReturnType = std::unique_ptr<std::remove_pointer_t<R>>;
45  using FactoryType = std::function<ReturnType( Args... )>;
46  };
47 
51  std::string demangle( const std::type_info& id );
52 
54  template <typename T>
55  inline std::string demangle() {
56  return demangle( typeid( T ) );
57  }
58 
60  template <typename ID>
61  inline std::string stringify_id( const ID& id ) {
63  o << id;
64  return o.str();
65  }
67  template <>
68  inline std::string stringify_id<std::string>( const std::string& id ) {
69  return id;
70  }
71 
73  void reportBadAnyCast( const std::type_info& factory_type, const std::string& id );
74 
76  class GAUDIPS_API Logger {
77  public:
78  enum Level { Debug = 0, Info = 1, Warning = 2, Error = 3 };
79  Logger( Level level = Warning ) : m_level( level ) {}
80  virtual ~Logger() {}
81  inline Level level() const { return m_level; }
82  inline void setLevel( Level level ) { m_level = level; }
83  inline void info( const std::string& msg ) { report( Info, msg ); }
84  inline void debug( const std::string& msg ) { report( Debug, msg ); }
85  inline void warning( const std::string& msg ) { report( Warning, msg ); }
86  inline void error( const std::string& msg ) { report( Error, msg ); }
87 
88  private:
89  virtual void report( Level lvl, const std::string& msg );
90  Level m_level;
91  };
92 
94  GAUDIPS_API Logger& logger();
97  GAUDIPS_API void setLogger( Logger* logger );
98 
100  class GAUDIPS_API Registry {
101  public:
102  using KeyType = std::string;
103 
106 
107  struct FactoryInfo {
108  std::string library;
109  std::any factory{};
111 
112  inline bool is_set() const { return factory.has_value(); }
113  Properties::mapped_type getprop( const Properties::key_type& name ) const;
114  };
115 
117  using FactoryMap = std::map<KeyType, FactoryInfo>;
118 
120  template <typename F>
121  F get( const KeyType& id ) {
122  const FactoryInfo& info = Registry::instance().getInfo( id, true );
123 
124  // Check if factory has been marked as deprecated.
125  const auto& prop = info.properties.find( "deprecated" );
126  if ( prop != info.properties.end() ) {
127  std::string msg = "factory '" + info.getprop( "ClassName" ) + "' is deprecated" +
128  ( prop->second.empty() ? "" : ( ": " + prop->second ) );
129  if ( m_werror.find( "deprecated" ) != m_werror.end() ) {
130  logger().error( msg );
131  return std::any_cast<F>( std::any() );
132  } else {
133  logger().warning( msg );
134  }
135  }
136 
137 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
138  if ( !info.getprop( "ReflexName" ).empty() ) {
139  const std::string real_name = info.getprop( "ClassName" );
140  logger().warning( "requesting factory via old name '" + id + "' use '" +
141  ( real_name.empty() ? "<undefined>" : real_name ) + "' instead" );
142  }
143 #endif
144  return std::any_cast<F>( info.factory );
145  }
146 
148  static Registry& instance();
149 
151  FactoryInfo& add( const KeyType& id, FactoryInfo info );
152 
154  FactoryMap::size_type erase( const KeyType& id );
155 
157  const FactoryInfo& getInfo( const KeyType& id, const bool load = false ) const;
158 
160  Registry& addProperty( const KeyType& id, const KeyType& k, const std::string& v );
161 
163  void setError( const KeyType& warning );
164 
166  void unsetError( const KeyType& warning );
167 
169  std::set<KeyType> loadedFactoryNames() const;
170 
176  const FactoryMap& factories() const;
177 
178  private:
180  Registry();
181 
183  Registry( const Registry& ) = delete;
184 
186  FactoryMap& factories();
187 
190  void initialize();
191 
193  mutable std::once_flag m_initialized;
194 
196  FactoryMap m_factories;
197 
199  std::set<KeyType> m_werror;
200 
202  mutable std::recursive_mutex m_mutex;
203  };
204 
209  template <typename, typename>
210  struct DefaultFactory;
211  template <typename T, typename R, typename... Args>
212  struct DefaultFactory<T, Factory<R( Args... )>> {
213  inline typename Factory<R( Args... )>::ReturnType operator()( Args... args ) {
214  return std::make_unique<T>( std::move( args )... );
215  }
216  };
217 
221  std::string getDSONameFor( void* fptr );
222  } // namespace Details
223 
225  GAUDIPS_API void SetDebug( int debugLevel );
227  GAUDIPS_API int Debug();
228  }
229  } // namespace PluginService
230 } // namespace Gaudi
231 
232 #define _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME_TOKEN( serial ) _register_##serial
233 #define _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME( serial ) \
234  _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME_TOKEN( serial )
235 #define _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME( __LINE__ )
236 
237 #endif //_GAUDI_PLUGIN_SERVICE_DETAILS_H_
std::string
STL class.
Gaudi::PluginService::v2::Details::reportBadAnyCast
void reportBadAnyCast(const std::type_info &factory_type, const std::string &id)
Definition: PluginServiceV2.cpp:117
std::move
T move(T... args)
Gaudi::PluginService::v2::SetDebug
void SetDebug(int debugLevel)
Definition: PluginServiceV2.cpp:365
std::type_info
GAUDI_PLUGIN_SERVICE_V2_INLINE
#define GAUDI_PLUGIN_SERVICE_V2_INLINE
Definition: PluginServiceCommon.h:17
GaudiPartProp.decorators.get
get
decorate the vector of properties
Definition: decorators.py:283
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
std::recursive_mutex
STL class.
std::function
GaudiPartProp.tests.id
id
Definition: tests.py:111
Properties
Definition: Properties.py:1
std::once_flag
Gaudi::PluginService::v2::Details::setLogger
void setLogger(Logger *logger)
Definition: PluginServiceV2.cpp:345
GaudiPartProp.tests.v2
v2
Definition: tests.py:59
Gaudi::PluginService::v2::Details::getDSONameFor
std::string getDSONameFor(void *fptr)
Definition: PluginServiceV2.cpp:348
Gaudi::Details::Property::ParsingErrorPolicy::Warning
@ Warning
cpluginsvc.factories
def factories()
Definition: cpluginsvc.py:93
std::map< KeyType, std::string >
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
gaudiComponentHelp.properties
properties
Definition: gaudiComponentHelp.py:68
Gaudi::PluginService::v2::Details::logger
Logger & logger()
Definition: PluginServiceV2.cpp:344
Gaudi::PluginService::v2::Debug
int Debug()
Definition: PluginServiceV2.cpp:376
std::ostringstream
STL class.
MSG::Level
Level
Definition: IMessageSvc.h:25
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
gaudirun.args
args
Definition: gaudirun.py:336
GAUDIPS_API
#define GAUDIPS_API
Definition: PluginServiceCommon.h:42
std::string::empty
T empty(T... args)
Properties.v
v
Definition: Properties.py:122
std::ostringstream::str
T str(T... args)
std::unique_ptr
STL class.
std::set
STL class.
GaudiPython.Persistency.add
def add(instance)
Definition: Persistency.py:50
Gaudi::PluginService::v2::Details::demangle
std::string demangle(const std::string &id)
Definition: PluginServiceV2.cpp:95
PluginServiceCommon.h