The Gaudi Framework  master (37c0b60a)
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 <cstdint>
19 #include <functional>
20 #include <memory>
21 #include <sstream>
22 #include <string>
23 #include <string_view>
24 #include <type_traits>
25 #include <typeinfo>
26 #include <utility>
27 
28 #if __cplusplus > 201703L && __has_include( <source_location> ) && !defined (__CLING__)
29 # include <source_location>
31  using std::source_location;
32 }
33 
34 #elif __cplusplus >= 201402L && !defined( __clang__ ) && __GNUC__ >= 8
35 # include <experimental/source_location>
37  using std::experimental::source_location;
38 }
39 #else
41 
42  struct source_location {
43 
44  // 14.1.2, source_location creation
45  static constexpr source_location current( const char* __file = __builtin_FILE(),
46  const char* __func = __builtin_FUNCTION(), int __line = __builtin_LINE(),
47  int __col = 0 ) noexcept {
48  source_location __loc;
49  __loc._M_file = __file;
50  __loc._M_func = __func;
51  __loc._M_line = __line;
52  __loc._M_col = __col;
53  return __loc;
54  }
55 
56  constexpr source_location() noexcept : _M_file( "unknown" ), _M_func( _M_file ), _M_line( 0 ), _M_col( 0 ) {}
57 
58  // 14.1.3, source_location field access
59  constexpr uint_least32_t line() const noexcept { return _M_line; }
60  constexpr uint_least32_t column() const noexcept { return _M_col; }
61  constexpr const char* file_name() const noexcept { return _M_file; }
62  constexpr const char* function_name() const noexcept { return _M_func; }
63 
64  private:
65  const char* _M_file;
66  const char* _M_func;
67  uint_least32_t _M_line;
68  uint_least32_t _M_col;
69  };
70 
71 } // namespace Gaudi::PluginService::Details
72 #endif
73 
74 namespace Gaudi {
76  namespace PluginService {
79 
81  template <typename>
82  struct Factory;
84 
86  template <typename R, typename... Args>
87  struct Factory<R( Args... )> {
88  using Traits = Details::Traits<R( Args... )>;
89  using ReturnType = typename Traits::ReturnType;
90  using FactoryType = typename Traits::FactoryType;
91  using ReturnValueType = R;
92 
94  template <typename T>
95  static ReturnType create( const T& id, Args... args ) {
96  try {
97  return Details::Registry::instance().get<FactoryType>( Details::stringify_id( id ) )(
98  std::forward<Args>( args )... );
99  } catch ( std::bad_any_cast& ) {
100  Details::reportBadAnyCast( typeid( FactoryType ), Details::stringify_id( id ) );
101  return nullptr;
102  }
103  }
104  };
105 
135  template <typename T, typename F = typename T::Factory>
136  struct DeclareFactory {
137  using DefaultFactory = Details::DefaultFactory<T, F>;
138 
139  DeclareFactory( typename F::FactoryType f = DefaultFactory{}, Details::Registry::Properties props = {},
140  source_location src_loc = source_location::current() )
141  : DeclareFactory( Details::demangle<T>(), std::move( f ), std::move( props ), src_loc ) {}
142 
143  DeclareFactory( const std::string& id, typename F::FactoryType f = DefaultFactory{},
144  Details::Registry::Properties props = {},
145  [[maybe_unused]] source_location src_loc = source_location::current() ) {
146  using Details::Registry;
147 
148  if ( props.find( "ClassName" ) == end( props ) ) props.emplace( "ClassName", Details::demangle<T>() );
149  // keep only the file name
150  std::string_view fn = src_loc.file_name();
151  auto pos = fn.rfind( '/' );
152  if ( pos != std::string_view::npos ) { fn.remove_prefix( pos + 1 ); }
154  s << fn << ':' << src_loc.line();
155  props["declaration_location"] = s.str();
156  Registry::instance().add( id, { libraryName(), std::move( f ), std::move( props ) } );
157  }
158 
159  DeclareFactory( Details::Registry::Properties props, source_location src_loc = source_location::current() )
160  : DeclareFactory( DefaultFactory{}, std::move( props ), src_loc ) {}
161 
162  private:
164  static std::string libraryName() { return Details::getDSONameFor( reinterpret_cast<void*>( libraryName ) ); }
165  };
166  }
167  } // namespace PluginService
168 } // namespace Gaudi
169 
170 #define _PS_V2_DECLARE_COMPONENT( type ) \
171  namespace { \
172  ::Gaudi::PluginService::v2::DeclareFactory<type> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{}; \
173  }
174 
175 #define _PS_V2_DECLARE_COMPONENT_WITH_ID( type, id ) \
176  namespace { \
177  ::Gaudi::PluginService::v2::DeclareFactory<type> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{ \
178  ::Gaudi::PluginService::v2::Details::stringify_id( id ) }; \
179  }
180 
181 #define _PS_V2_DECLARE_FACTORY( type, factory ) \
182  namespace { \
183  ::Gaudi::PluginService::v2::DeclareFactory<type, factory> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{}; \
184  }
185 
186 #define _PS_V2_DECLARE_FACTORY_WITH_ID( type, id, factory ) \
187  namespace { \
188  ::Gaudi::PluginService::v2::DeclareFactory<type, factory> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{ \
189  ::Gaudi::PluginService::v2::Details::stringify_id( id ) }; \
190  }
191 
192 #define _PS_V2_DECLARE_COMPONENT_PROPERTY( id, name, value ) \
193  namespace { \
194  struct _PS_V2_INTERNAL_SET_PROPERTY { \
195  _PS_V2_INTERNAL_SET_PROPERTY() { \
196  ::Gaudi::PluginService::v2::Details::Registry::instance().addProperty( #id, name, value ); \
197  } \
198  } _ps_v2_internal_set_property; \
199  }
200 
201 #if GAUDI_PLUGIN_SERVICE_USE_V2
202 # define DECLARE_COMPONENT( type ) _PS_V2_DECLARE_COMPONENT( type )
203 # define DECLARE_COMPONENT_WITH_ID( type, id ) _PS_V2_DECLARE_COMPONENT_WITH_ID( type, id )
204 # define DECLARE_FACTORY( type, factory ) _PS_V2_DECLARE_FACTORY( type, factory )
205 # define DECLARE_FACTORY_WITH_ID( type, id, factory ) _PS_V2_DECLARE_FACTORY_WITH_ID( type, id, factory )
206 # define DECLARE_COMPONENT_PROPERTY( id, name, value ) _PS_V2_DECLARE_COMPONENT_PROPERTY( id, name, value )
207 #endif
208 
209 #endif //_GAUDI_PLUGIN_SERVICE_H_
Gaudi::PluginService::Details::source_location::_M_func
const char * _M_func
Definition: PluginServiceV2.h:66
Gaudi::PluginService::v2::Factory< R(Args...)>::ReturnValueType
R ReturnValueType
Definition: PluginServiceV2.h:91
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:89
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:164
Gaudi::PluginService::v2::DeclareFactory::DeclareFactory
DeclareFactory(Details::Registry::Properties props, source_location src_loc=source_location::current())
Definition: PluginServiceV2.h:159
check_ParticleID.props
props
Definition: check_ParticleID.py:21
GAUDI_PLUGIN_SERVICE_V2_INLINE
#define GAUDI_PLUGIN_SERVICE_V2_INLINE
Definition: PluginServiceCommon.h:17
std::stringstream
STL class.
Gaudi::PluginService::Details::source_location::_M_line
uint_least32_t _M_line
Definition: PluginServiceV2.h:67
Gaudi::PluginService::v2::Factory< R(Args...)>::Traits
Details::Traits< R(Args...)> Traits
Definition: PluginServiceV2.h:88
Gaudi::PluginService::Details::source_location::function_name
constexpr const char * function_name() const noexcept
Definition: PluginServiceV2.h:62
Gaudi::PluginService::v2::Factory< R(Args...)>::FactoryType
typename Traits::FactoryType FactoryType
Definition: PluginServiceV2.h:90
Gaudi::PluginService::Details::source_location::source_location
constexpr source_location() noexcept
Definition: PluginServiceV2.h:56
Gaudi::PluginService::v2::DeclareFactory
Helper to declare the factory implementation for a user defined type T.
Definition: PluginServiceV2.h:136
GaudiPartProp.tests.v2
v2
Definition: tests.py:59
Gaudi::PluginService::v2::Details::getDSONameFor
std::string getDSONameFor(void *fptr)
Definition: PluginServiceV2.cpp:348
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
Gaudi::PluginService::Details::source_location::line
constexpr uint_least32_t line() const noexcept
Definition: PluginServiceV2.h:59
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:139
Gaudi::PluginService::Details::source_location
Definition: PluginServiceV2.h:42
Gaudi::PluginService::Details::source_location::file_name
constexpr const char * file_name() const noexcept
Definition: PluginServiceV2.h:61
gaudirun.args
args
Definition: gaudirun.py:336
Gaudi::PluginService::Details::source_location::current
static constexpr source_location current(const char *__file=__builtin_FILE(), const char *__func=__builtin_FUNCTION(), int __line=__builtin_LINE(), int __col=0) noexcept
Definition: PluginServiceV2.h:45
Gaudi::PluginService::Details::source_location::_M_file
const char * _M_file
Definition: PluginServiceV2.h:65
Gaudi::PluginService::v2::DeclareFactory::DefaultFactory
Details::DefaultFactory< T, F > DefaultFactory
Definition: PluginServiceV2.h:137
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:95
Gaudi::PluginService::Details::source_location::_M_col
uint_least32_t _M_col
Definition: PluginServiceV2.h:68
IOTest.end
end
Definition: IOTest.py:125
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:143
Gaudi::PluginService::Details::source_location::column
constexpr uint_least32_t column() const noexcept
Definition: PluginServiceV2.h:60
Gaudi::PluginService::Details
Definition: PluginServiceV2.h:40