The Gaudi Framework  v36r7 (7f57a304)
PluginServiceDetailsV2.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 2013-2019 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 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
124  if ( !info.getprop( "ReflexName" ).empty() ) {
125  const std::string real_name = info.getprop( "ClassName" );
126  logger().warning( "requesting factory via old name '" + id + "' use '" +
127  ( real_name.empty() ? "<undefined>" : real_name ) + "' instead" );
128  }
129 #endif
130  return std::any_cast<F>( info.factory );
131  }
132 
134  static Registry& instance();
135 
137  FactoryInfo& add( const KeyType& id, FactoryInfo info );
138 
140  FactoryMap::size_type erase( const KeyType& id );
141 
143  const FactoryInfo& getInfo( const KeyType& id, const bool load = false ) const;
144 
146  Registry& addProperty( const KeyType& id, const KeyType& k, const std::string& v );
147 
149  std::set<KeyType> loadedFactoryNames() const;
150 
156  const FactoryMap& factories() const;
157 
158  private:
160  Registry();
161 
163  Registry( const Registry& ) = delete;
164 
166  FactoryMap& factories();
167 
170  void initialize();
171 
173  mutable std::once_flag m_initialized;
174 
176  FactoryMap m_factories;
177 
179  mutable std::recursive_mutex m_mutex;
180  };
181 
186  template <typename, typename>
187  struct DefaultFactory;
188  template <typename T, typename R, typename... Args>
189  struct DefaultFactory<T, Factory<R( Args... )>> {
190  inline typename Factory<R( Args... )>::ReturnType operator()( Args... args ) {
191  return std::make_unique<T>( std::move( args )... );
192  }
193  };
194 
198  std::string getDSONameFor( void* fptr );
199  } // namespace Details
200 
202  GAUDIPS_API void SetDebug( int debugLevel );
204  GAUDIPS_API int Debug();
205  }
206  } // namespace PluginService
207 } // namespace Gaudi
208 
209 #define _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME_TOKEN( serial ) _register_##serial
210 #define _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME( serial ) \
211  _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME_TOKEN( serial )
212 #define _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME( __LINE__ )
213 
214 #endif //_GAUDI_PLUGIN_SERVICE_DETAILS_H_
HistoDumpEx.v2
v2
Definition: HistoDumpEx.py:28
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:324
std::type_info
GAUDI_PLUGIN_SERVICE_V2_INLINE
#define GAUDI_PLUGIN_SERVICE_V2_INLINE
Definition: PluginServiceCommon.h:17
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
std::recursive_mutex
STL class.
std::function
Properties
Definition: Properties.py:1
std::once_flag
Gaudi::Functional::details::get
auto get(const Handle &handle, const Algo &, const EventContext &) -> decltype(details::deref(handle.get()))
Definition: FunctionalDetails.h:444
TimingHistograms.name
name
Definition: TimingHistograms.py:25
Gaudi::PluginService::v2::Details::setLogger
void setLogger(Logger *logger)
Definition: PluginServiceV2.cpp:304
Gaudi::PluginService::v2::Details::getDSONameFor
std::string getDSONameFor(void *fptr)
Definition: PluginServiceV2.cpp:307
Gaudi::Details::Property::ParsingErrorPolicy::Warning
@ Warning
std::map< KeyType, std::string >
gaudirun.level
level
Definition: gaudirun.py:364
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
gaudiComponentHelp.properties
properties
Definition: gaudiComponentHelp.py:69
Gaudi::PluginService::v2::Details::logger
Logger & logger()
Definition: PluginServiceV2.cpp:303
HistoDumpEx.v
v
Definition: HistoDumpEx.py:27
Gaudi::PluginService::v2::Debug
int Debug()
Definition: PluginServiceV2.cpp:335
std::ostringstream
STL class.
MSG::Level
Level
Definition: IMessageSvc.h:25
gaudirun.args
args
Definition: gaudirun.py:336
GAUDIPS_API
#define GAUDIPS_API
Definition: PluginServiceCommon.h:44
std::string::empty
T empty(T... args)
std::ostringstream::str
T str(T... args)
GaudiPluginService.cpluginsvc.factories
def factories()
Definition: cpluginsvc.py:94
std::unique_ptr
STL class.
std::set
STL class.
GaudiPython.Persistency.add
def add(instance)
Definition: Persistency.py:49
Gaudi::PluginService::v2::Details::demangle
std::string demangle(const std::string &id)
Definition: PluginServiceV2.cpp:95
PluginServiceCommon.h