DD4hep  1.36.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 #include <any>
19 
20 #include <functional>
21 #include <map>
22 #include <memory>
23 #include <mutex>
24 #include <set>
25 #include <sstream>
26 #include <string>
27 #include <typeinfo>
28 #include <utility>
29 
30 namespace Gaudi {
31  namespace PluginService {
32  GAUDI_PLUGIN_SERVICE_V2_INLINE namespace v2 {
34  template <typename>
35  struct Factory;
37 
39  namespace Details {
40  template <typename>
41  struct Traits;
42 
43  template <typename R, typename... Args>
44  struct Traits<R( Args... )> {
45  using ReturnType = std::unique_ptr<std::remove_pointer_t<R>>;
46  using FactoryType = std::function<ReturnType( Args... )>;
47  };
48 
52  std::string demangle( const std::type_info& id );
53 
55  template <typename T>
56  inline std::string demangle() {
57  return demangle( typeid( T ) );
58  }
59 
61  template <typename ID>
62  inline std::string stringify_id( const ID& id ) {
63  std::ostringstream o;
64  o << id;
65  return o.str();
66  }
68  template <>
69  inline std::string stringify_id<std::string>( const std::string& id ) {
70  return id;
71  }
72 
74  void reportBadAnyCast( const std::type_info& factory_type, const std::string& id );
75 
77  class GAUDIPS_API Logger {
78  public:
79  enum Level { Debug = 0, Info = 1, Warning = 2, Error = 3 };
80  Logger( Level level = Warning ) : m_level( level ) {}
81  virtual ~Logger() {}
82  inline Level level() const { return m_level; }
83  inline void setLevel( Level level ) { m_level = level; }
84  inline void info( const std::string& msg ) { report( Info, msg ); }
85  inline void debug( const std::string& msg ) { report( Debug, msg ); }
86  inline void warning( const std::string& msg ) { report( Warning, msg ); }
87  inline void error( const std::string& msg ) { report( Error, msg ); }
88 
89  private:
90  virtual void report( Level lvl, const std::string& msg );
91  Level m_level;
92  };
93 
95  GAUDIPS_API Logger& logger();
98  GAUDIPS_API void setLogger( Logger* logger );
99 
101  class GAUDIPS_API Registry {
102  public:
103  using KeyType = std::string;
104 
106  using Properties = std::map<KeyType, std::string>;
107 
108  struct FactoryInfo {
109  std::string library;
110  std::any factory{};
111  Properties properties{};
112 
113  inline bool is_set() const { return factory.has_value(); }
114  Properties::mapped_type getprop( const Properties::key_type& name ) const;
115  };
116 
118  using FactoryMap = std::map<KeyType, FactoryInfo>;
119 
121  template <typename F>
122  F get( const KeyType& id ) {
123  const FactoryInfo& info = Registry::instance().getInfo( id, true );
124 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
125  if ( !info.getprop( "ReflexName" ).empty() ) {
126  const std::string real_name = info.getprop( "ClassName" );
127  logger().warning( "requesting factory via old name '" + id + "' use '" +
128  ( real_name.empty() ? "<undefined>" : real_name ) + "' instead" );
129  }
130 #endif
131  return std::any_cast<F>( info.factory );
132  }
133 
135  static Registry& instance();
136 
138  FactoryInfo& add( const KeyType& id, FactoryInfo info );
139 
141  const FactoryInfo& getInfo( const KeyType& id, const bool load = false ) const;
142 
144  Registry& addProperty( const KeyType& id, const KeyType& k, const std::string& v );
145 
147  std::set<KeyType> loadedFactoryNames() const;
148 
154  const FactoryMap& factories() const;
155 
156  private:
158  Registry();
159 
161  Registry( const Registry& ) = delete;
162 
164  FactoryMap& factories();
165 
168  void initialize();
169 
171  mutable std::once_flag m_initialized;
172 
174  FactoryMap m_factories;
175 
177  mutable std::recursive_mutex m_mutex;
178  };
179 
184  template <typename, typename>
185  struct DefaultFactory;
186  template <typename T, typename R, typename... Args>
187  struct DefaultFactory<T, Factory<R( Args... )>> {
188  inline typename Factory<R( Args... )>::ReturnType operator()( Args... args ) {
189  return std::make_unique<T>( std::move( args )... );
190  }
191  };
192 
196  std::string getDSONameFor( void* fptr );
197  } // namespace Details
198 
200  GAUDIPS_API void SetDebug( int debugLevel );
202  GAUDIPS_API int Debug();
203  }
204  } // namespace PluginService
205 } // namespace Gaudi
206 
207 #define _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME_TOKEN( serial ) _register_##serial
208 #define _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME( serial ) \
209  _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME_TOKEN( serial )
210 #define _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME( __LINE__ )
211 
212 #endif // GAUDIPLUGINSERVICE_GAUDI_DETAILS_PLUGINSERVICEDETAILSV2_H
Gaudi::PluginService::v2::Details::logger
Logger & logger()
Definition: PluginServiceV2.cpp:294
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:94
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:295
Gaudi::PluginService::v2::Details::reportBadAnyCast
void reportBadAnyCast(const std::type_info &factory_type, const std::string &id)
Definition: PluginServiceV2.cpp:116
Gaudi::PluginService::v2::Debug
int Debug()
Definition: PluginServiceV2.cpp:326
PluginServiceCommon.h
dd4hep::warning
std::size_t warning(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:66
Gaudi
Definition: PluginServiceDetailsV1.h:27
Gaudi::PluginService::v2::Details::getDSONameFor
std::string getDSONameFor(void *fptr)
Definition: PluginServiceV2.cpp:298
Gaudi::PluginService::v2::SetDebug
void SetDebug(int debugLevel)
Definition: PluginServiceV2.cpp:315
GAUDIPS_API
#define GAUDIPS_API
Definition: PluginServiceCommon.h:47