DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
PluginServiceDetailsV2.h
Go to the documentation of this file.
1 #ifndef GAUDIPLUGINSERVICE_GAUDI_DETAILS_PLUGINSERVICEDETAILSV2_H
2 #define GAUDIPLUGINSERVICE_GAUDI_DETAILS_PLUGINSERVICEDETAILSV2_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 
17 
18 #if __cplusplus >= 201703
19 # include <any>
20 #else
21 # include <boost/any.hpp>
22 namespace std {
23  using boost::any;
24  using boost::any_cast;
25  using boost::bad_any_cast;
26 } // namespace std
27 #endif
28 
29 #include <functional>
30 #include <map>
31 #include <memory>
32 #include <mutex>
33 #include <set>
34 #include <sstream>
35 #include <string>
36 #include <typeinfo>
37 #include <utility>
38 
39 namespace Gaudi {
40  namespace PluginService {
41  GAUDI_PLUGIN_SERVICE_V2_INLINE namespace v2 {
43  template <typename>
44  struct Factory;
46 
48  namespace Details {
49  template <typename>
50  struct Traits;
51 
52  template <typename R, typename... Args>
53  struct Traits<R( Args... )> {
54  using ReturnType = std::unique_ptr<std::remove_pointer_t<R>>;
55  using FactoryType = std::function<ReturnType( Args... )>;
56  };
57 
61  std::string demangle( const std::type_info& id );
62 
64  template <typename T>
65  inline std::string demangle() {
66  return demangle( typeid( T ) );
67  }
68 
70  template <typename ID>
71  inline std::string stringify_id( const ID& id ) {
72  std::ostringstream o;
73  o << id;
74  return o.str();
75  }
77  template <>
78  inline std::string stringify_id<std::string>( const std::string& id ) {
79  return id;
80  }
81 
83  void reportBadAnyCast( const std::type_info& factory_type, const std::string& id );
84 
86  class GAUDIPS_API Logger {
87  public:
88  enum Level { Debug = 0, Info = 1, Warning = 2, Error = 3 };
89  Logger( Level level = Warning ) : m_level( level ) {}
90  virtual ~Logger() {}
91  inline Level level() const { return m_level; }
92  inline void setLevel( Level level ) { m_level = level; }
93  inline void info( const std::string& msg ) { report( Info, msg ); }
94  inline void debug( const std::string& msg ) { report( Debug, msg ); }
95  inline void warning( const std::string& msg ) { report( Warning, msg ); }
96  inline void error( const std::string& msg ) { report( Error, msg ); }
97 
98  private:
99  virtual void report( Level lvl, const std::string& msg );
100  Level m_level;
101  };
102 
104  GAUDIPS_API Logger& logger();
107  GAUDIPS_API void setLogger( Logger* logger );
108 
110  class GAUDIPS_API Registry {
111  public:
112  using KeyType = std::string;
113 
115  using Properties = std::map<KeyType, std::string>;
116 
117  struct FactoryInfo {
118  std::string library;
119  std::any factory{};
120  Properties properties{};
121 
122  inline bool is_set() const {
123 #if __cplusplus >= 201703
124  return factory.has_value();
125 #else
126  return !factory.empty();
127 #endif
128  }
129  Properties::mapped_type getprop( const Properties::key_type& name ) const;
130  };
131 
133  using FactoryMap = std::map<KeyType, FactoryInfo>;
134 
136  template <typename F>
137  F get( const KeyType& id ) {
138  const FactoryInfo& info = Registry::instance().getInfo( id, true );
139 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
140  if ( !info.getprop( "ReflexName" ).empty() ) {
141  const std::string real_name = info.getprop( "ClassName" );
142  logger().warning( "requesting factory via old name '" + id + "' use '" +
143  ( real_name.empty() ? "<undefined>" : real_name ) + "' instead" );
144  }
145 #endif
146  return std::any_cast<F>( info.factory );
147  }
148 
150  static Registry& instance();
151 
153  FactoryInfo& add( const KeyType& id, FactoryInfo info );
154 
156  const FactoryInfo& getInfo( const KeyType& id, const bool load = false ) const;
157 
159  Registry& addProperty( const KeyType& id, const KeyType& k, const std::string& v );
160 
162  std::set<KeyType> loadedFactoryNames() const;
163 
169  const FactoryMap& factories() const;
170 
171  private:
173  Registry();
174 
176  Registry( const Registry& ) = delete;
177 
179  FactoryMap& factories();
180 
183  void initialize();
184 
186  mutable std::once_flag m_initialized;
187 
189  FactoryMap m_factories;
190 
192  mutable std::recursive_mutex m_mutex;
193  };
194 
199  template <typename, typename>
200  struct DefaultFactory;
201  template <typename T, typename R, typename... Args>
202  struct DefaultFactory<T, Factory<R( Args... )>> {
203  inline typename Factory<R( Args... )>::ReturnType operator()( Args... args ) {
204  return std::make_unique<T>( std::move( args )... );
205  }
206  };
207 
211  std::string getDSONameFor( void* fptr );
212  } // namespace Details
213 
215  GAUDIPS_API void SetDebug( int debugLevel );
217  GAUDIPS_API int Debug();
218  }
219  } // namespace PluginService
220 } // namespace Gaudi
221 
222 #define _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME_TOKEN( serial ) _register_##serial
223 #define _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME( serial ) \
224  _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME_TOKEN( serial )
225 #define _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME( __LINE__ )
226 
227 #endif // GAUDIPLUGINSERVICE_GAUDI_DETAILS_PLUGINSERVICEDETAILSV2_H
Gaudi::PluginService::v2::Details::logger
Logger & logger()
Definition: PluginServiceV2.cpp:301
GAUDI_PLUGIN_SERVICE_V2_INLINE
#define GAUDI_PLUGIN_SERVICE_V2_INLINE
Definition: PluginServiceCommon.h:20
dd4hep::error
std::size_t error(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:67
v
View * v
Definition: MultiView.cpp:28
dd4hep::info
std::size_t info(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:65
Gaudi::PluginService::v2::Details::demangle
std::string demangle(const std::string &id)
Definition: PluginServiceV2.cpp:101
dd4hep::debug
std::size_t debug(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:64
Gaudi::PluginService::v2::Details::setLogger
void setLogger(Logger *logger)
Definition: PluginServiceV2.cpp:302
Gaudi::PluginService::v2::Details::reportBadAnyCast
void reportBadAnyCast(const std::type_info &factory_type, const std::string &id)
Definition: PluginServiceV2.cpp:123
Gaudi::PluginService::v2::Debug
int Debug()
Definition: PluginServiceV2.cpp:333
PluginServiceCommon.h
dd4hep::warning
std::size_t warning(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:66
std
Definition: Plugins.h:30
Gaudi
Definition: PluginServiceDetailsV1.h:27
Gaudi::PluginService::v2::Details::getDSONameFor
std::string getDSONameFor(void *fptr)
Definition: PluginServiceV2.cpp:305
Gaudi::PluginService::v2::SetDebug
void SetDebug(int debugLevel)
Definition: PluginServiceV2.cpp:322
GAUDIPS_API
#define GAUDIPS_API
Definition: PluginServiceCommon.h:47