The Gaudi Framework  v36r7 (7f57a304)
PluginServiceV2.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 2013-2022 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_V2_H_
12 #define _GAUDI_PLUGIN_SERVICE_V2_H_
13 
16 
18 #include <functional>
19 #include <memory>
20 #include <sstream>
21 #include <string>
22 #include <string_view>
23 #include <type_traits>
24 #include <typeinfo>
25 #include <utility>
26 
27 #if __cplusplus > 201703L && __has_include( <source_location> )
28 # include <source_location>
29 namespace Gaudi::PluginService::Details {
30  using std::source_location;
31 }
32 #elif __cplusplus >= 201402L
33 # include <experimental/source_location>
34 namespace Gaudi::PluginService::Details {
35  using std::experimental::source_location;
36 }
37 #endif
38 
39 namespace Gaudi {
41  namespace PluginService {
43  using Gaudi::PluginService::Details::source_location;
44 
46  template <typename>
47  struct Factory;
49 
51  template <typename R, typename... Args>
52  struct Factory<R( Args... )> {
53  using Traits = Details::Traits<R( Args... )>;
54  using ReturnType = typename Traits::ReturnType;
55  using FactoryType = typename Traits::FactoryType;
56  using ReturnValueType = R;
57 
59  template <typename T>
60  static ReturnType create( const T& id, Args... args ) {
61  try {
62  return Details::Registry::instance().get<FactoryType>( Details::stringify_id( id ) )(
63  std::forward<Args>( args )... );
64  } catch ( std::bad_any_cast& ) {
65  Details::reportBadAnyCast( typeid( FactoryType ), Details::stringify_id( id ) );
66  return nullptr;
67  }
68  }
69  };
70 
100  template <typename T, typename F = typename T::Factory>
101  struct DeclareFactory {
102  using DefaultFactory = Details::DefaultFactory<T, F>;
103 
104  DeclareFactory( typename F::FactoryType f = DefaultFactory{}, Details::Registry::Properties props = {},
105  source_location src_loc = source_location::current() )
106  : DeclareFactory( Details::demangle<T>(), std::move( f ), std::move( props ), src_loc ) {}
107 
108  DeclareFactory( const std::string& id, typename F::FactoryType f = DefaultFactory{},
109  Details::Registry::Properties props = {},
110  [[maybe_unused]] source_location src_loc = source_location::current() ) {
111  using Details::Registry;
112 
113  if ( props.find( "ClassName" ) == end( props ) ) props.emplace( "ClassName", Details::demangle<T>() );
114  // keep only the file name
115  std::string_view fn = src_loc.file_name();
116  auto pos = fn.rfind( '/' );
117  if ( pos != std::string_view::npos ) { fn.remove_prefix( pos + 1 ); }
119  s << fn << ':' << src_loc.line();
120  props["declaration_location"] = s.str();
121  Registry::instance().add( id, { libraryName(), std::move( f ), std::move( props ) } );
122  }
123 
124  DeclareFactory( Details::Registry::Properties props, source_location src_loc = source_location::current() )
125  : DeclareFactory( DefaultFactory{}, std::move( props ), src_loc ) {}
126 
127  private:
129  static std::string libraryName() { return Details::getDSONameFor( reinterpret_cast<void*>( libraryName ) ); }
130  };
131  }
132  } // namespace PluginService
133 } // namespace Gaudi
134 
135 #define _PS_V2_DECLARE_COMPONENT( type ) \
136  namespace { \
137  ::Gaudi::PluginService::v2::DeclareFactory<type> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{}; \
138  }
139 
140 #define _PS_V2_DECLARE_COMPONENT_WITH_ID( type, id ) \
141  namespace { \
142  ::Gaudi::PluginService::v2::DeclareFactory<type> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{ \
143  ::Gaudi::PluginService::v2::Details::stringify_id( id ) }; \
144  }
145 
146 #define _PS_V2_DECLARE_FACTORY( type, factory ) \
147  namespace { \
148  ::Gaudi::PluginService::v2::DeclareFactory<type, factory> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{}; \
149  }
150 
151 #define _PS_V2_DECLARE_FACTORY_WITH_ID( type, id, factory ) \
152  namespace { \
153  ::Gaudi::PluginService::v2::DeclareFactory<type, factory> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{ \
154  ::Gaudi::PluginService::v2::Details::stringify_id( id ) }; \
155  }
156 
157 #if GAUDI_PLUGIN_SERVICE_USE_V2
158 # define DECLARE_COMPONENT( type ) _PS_V2_DECLARE_COMPONENT( type )
159 # define DECLARE_COMPONENT_WITH_ID( type, id ) _PS_V2_DECLARE_COMPONENT_WITH_ID( type, id )
160 # define DECLARE_FACTORY( type, factory ) _PS_V2_DECLARE_FACTORY( type, factory )
161 # define DECLARE_FACTORY_WITH_ID( type, id, factory ) _PS_V2_DECLARE_FACTORY_WITH_ID( type, id, factory )
162 #endif
163 
164 #endif //_GAUDI_PLUGIN_SERVICE_H_
Gaudi::PluginService::v2::Factory< R(Args...)>::ReturnValueType
R ReturnValueType
Definition: PluginServiceV2.h:56
HistoDumpEx.v2
v2
Definition: HistoDumpEx.py:28
std::string
STL class.
PluginServiceDetailsV2.h
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::Factory< R(Args...)>::ReturnType
typename Traits::ReturnType ReturnType
Definition: PluginServiceV2.h:54
gaudirun.s
string s
Definition: gaudirun.py:346
Gaudi::PluginService::v2::DeclareFactory::libraryName
static std::string libraryName()
Helper to record the name of the library that declare the factory.
Definition: PluginServiceV2.h:129
Gaudi::PluginService::v2::DeclareFactory::DeclareFactory
DeclareFactory(Details::Registry::Properties props, source_location src_loc=source_location::current())
Definition: PluginServiceV2.h:124
GAUDI_PLUGIN_SERVICE_V2_INLINE
#define GAUDI_PLUGIN_SERVICE_V2_INLINE
Definition: PluginServiceCommon.h:17
std::stringstream
STL class.
Gaudi::PluginService::v2::Factory< R(Args...)>::Traits
Details::Traits< R(Args...)> Traits
Definition: PluginServiceV2.h:53
Gaudi::PluginService::v2::Factory< R(Args...)>::FactoryType
typename Traits::FactoryType FactoryType
Definition: PluginServiceV2.h:55
Gaudi::PluginService::v2::DeclareFactory
Helper to declare the factory implementation for a user defined type T.
Definition: PluginServiceV2.h:101
Gaudi::PluginService::v2::Details::getDSONameFor
std::string getDSONameFor(void *fptr)
Definition: PluginServiceV2.cpp:307
IOTest.end
def end
Definition: IOTest.py:128
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
Gaudi::PluginService::v2::DeclareFactory::DeclareFactory
DeclareFactory(typename F::FactoryType f=DefaultFactory{}, Details::Registry::Properties props={}, source_location src_loc=source_location::current())
Definition: PluginServiceV2.h:104
gaudirun.args
args
Definition: gaudirun.py:336
ProduceConsume.props
props
Definition: ProduceConsume.py:84
Gaudi::PluginService::v2::DeclareFactory::DefaultFactory
Details::DefaultFactory< T, F > DefaultFactory
Definition: PluginServiceV2.h:102
Gaudi::PluginService::v2::Factory< R(Args...)>::create
static ReturnType create(const T &id, Args... args)
Function to call to create an instance of type identified by id and that uses this factory signature.
Definition: PluginServiceV2.h:60
Gaudi::PluginService::v2::DeclareFactory::DeclareFactory
DeclareFactory(const std::string &id, typename F::FactoryType f=DefaultFactory{}, Details::Registry::Properties props={}, [[maybe_unused]] source_location src_loc=source_location::current())
Definition: PluginServiceV2.h:108