The Gaudi Framework  v36r1 (3e2fb5a8)
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  const FactoryInfo& getInfo( const KeyType& id, const bool load = false ) const;
141 
143  Registry& addProperty( const KeyType& id, const KeyType& k, const std::string& v );
144 
146  std::set<KeyType> loadedFactoryNames() const;
147 
153  const FactoryMap& factories() const;
154 
155  private:
157  Registry();
158 
160  Registry( const Registry& ) = delete;
161 
163  FactoryMap& factories();
164 
167  void initialize();
168 
170  mutable std::once_flag m_initialized;
171 
173  FactoryMap m_factories;
174 
176  mutable std::recursive_mutex m_mutex;
177  };
178 
183  template <typename, typename>
184  struct DefaultFactory;
185  template <typename T, typename R, typename... Args>
186  struct DefaultFactory<T, Factory<R( Args... )>> {
187  inline typename Factory<R( Args... )>::ReturnType operator()( Args... args ) {
188  return std::make_unique<T>( std::move( args )... );
189  }
190  };
191 
195  std::string getDSONameFor( void* fptr );
196  } // namespace Details
197 
199  GAUDIPS_API void SetDebug( int debugLevel );
201  GAUDIPS_API int Debug();
202  }
203  } // namespace PluginService
204 } // namespace Gaudi
205 
206 #define _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME_TOKEN( serial ) _register_##serial
207 #define _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME( serial ) \
208  _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME_TOKEN( serial )
209 #define _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME( __LINE__ )
210 
211 #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:317
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:18
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:391
TimingHistograms.name
name
Definition: TimingHistograms.py:23
Gaudi::PluginService::v2::Details::setLogger
void setLogger(Logger *logger)
Definition: PluginServiceV2.cpp:297
Gaudi::PluginService::v2::Details::getDSONameFor
std::string getDSONameFor(void *fptr)
Definition: PluginServiceV2.cpp:300
Gaudi::Details::Property::ParsingErrorPolicy::Warning
@ Warning
std::map< KeyType, std::string >
gaudirun.level
level
Definition: gaudirun.py:346
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
gaudiComponentHelp.properties
properties
Definition: gaudiComponentHelp.py:62
Gaudi::PluginService::v2::Details::logger
Logger & logger()
Definition: PluginServiceV2.cpp:296
HistoDumpEx.v
v
Definition: HistoDumpEx.py:27
Gaudi::PluginService::v2::Debug
int Debug()
Definition: PluginServiceV2.cpp:328
std::ostringstream
STL class.
MSG::Level
Level
Definition: IMessageSvc.h:25
gaudirun.args
args
Definition: gaudirun.py:319
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:92
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