Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (f31105fd)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PluginServiceV2.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 2013-2025 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 <sstream>
19 #include <string>
20 #include <string_view>
21 #include <utility>
22 
23 #if __has_include( <source_location> ) && !defined (__CLING__)
24 # include <source_location>
26  using std::source_location;
27 }
28 
29 #elif !defined( __clang__ ) && __GNUC__ >= 8
30 # include <experimental/source_location>
32  using std::experimental::source_location;
33 }
34 #else
36 
37  struct source_location {
38 
39  // 14.1.2, source_location creation
40  static constexpr source_location current( const char* __file = __builtin_FILE(),
41  const char* __func = __builtin_FUNCTION(), int __line = __builtin_LINE(),
42  int __col = 0 ) noexcept {
43  source_location __loc;
44  __loc._M_file = __file;
45  __loc._M_func = __func;
46  __loc._M_line = __line;
47  __loc._M_col = __col;
48  return __loc;
49  }
50 
51  constexpr source_location() noexcept : _M_file( "unknown" ), _M_func( _M_file ), _M_line( 0 ), _M_col( 0 ) {}
52 
53  // 14.1.3, source_location field access
54  constexpr uint_least32_t line() const noexcept { return _M_line; }
55  constexpr uint_least32_t column() const noexcept { return _M_col; }
56  constexpr const char* file_name() const noexcept { return _M_file; }
57  constexpr const char* function_name() const noexcept { return _M_func; }
58 
59  private:
60  const char* _M_file;
61  const char* _M_func;
62  uint_least32_t _M_line;
63  uint_least32_t _M_col;
64  };
65 
66 } // namespace Gaudi::PluginService::Details
67 #endif
68 
69 namespace Gaudi {
71  namespace PluginService {
74 
76  template <typename>
77  struct Factory;
79 
81  template <typename R, typename... Args>
82  struct Factory<R( Args... )> {
83  using Traits = Details::Traits<R( Args... )>;
84  using ReturnType = typename Traits::ReturnType;
85  using FactoryType = typename Traits::FactoryType;
86  using ReturnValueType = R;
87 
89  template <typename T>
90  static ReturnType create( const T& id, Args... args ) {
91  try {
92  return Details::Registry::instance().get<FactoryType>( Details::stringify_id( id ) )(
93  std::forward<Args>( args )... );
94  } catch ( std::bad_any_cast& ) {
95  Details::reportBadAnyCast( typeid( FactoryType ), Details::stringify_id( id ) );
96  return nullptr;
97  }
98  }
99  };
100 
130  template <typename T, typename F = typename T::Factory>
131  struct DeclareFactory {
132  using DefaultFactory = Details::DefaultFactory<T, F>;
133 
134  DeclareFactory( typename F::FactoryType f = DefaultFactory{}, Details::Registry::Properties props = {},
135  source_location src_loc = source_location::current() )
136  : DeclareFactory( Details::demangle<T>(), std::move( f ), std::move( props ), src_loc ) {}
137 
138  DeclareFactory( const std::string& id, typename F::FactoryType f = DefaultFactory{},
139  Details::Registry::Properties props = {},
140  [[maybe_unused]] source_location src_loc = source_location::current() ) {
141  using Details::Registry;
142 
143  if ( props.find( "ClassName" ) == end( props ) ) props.emplace( "ClassName", Details::demangle<T>() );
144  // keep only the file name
145  std::string_view fn = src_loc.file_name();
146  auto pos = fn.rfind( '/' );
147  if ( pos != std::string_view::npos ) { fn.remove_prefix( pos + 1 ); }
149  s << fn << ':' << src_loc.line();
150  props["declaration_location"] = s.str();
151  Registry::instance().add( id, { libraryName(), std::move( f ), std::move( props ) } );
152  }
153 
154  DeclareFactory( Details::Registry::Properties props, source_location src_loc = source_location::current() )
155  : DeclareFactory( DefaultFactory{}, std::move( props ), src_loc ) {}
156 
157  private:
159  static std::string libraryName() { return Details::getDSONameFor( reinterpret_cast<void*>( libraryName ) ); }
160  };
161  }
162  } // namespace PluginService
163 } // namespace Gaudi
164 
165 #define _PS_V2_DECLARE_COMPONENT( type ) \
166  namespace { \
167  ::Gaudi::PluginService::v2::DeclareFactory<type> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{}; \
168  }
169 
170 #define _PS_V2_DECLARE_COMPONENT_WITH_ID( type, id ) \
171  namespace { \
172  ::Gaudi::PluginService::v2::DeclareFactory<type> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{ \
173  ::Gaudi::PluginService::v2::Details::stringify_id( id ) }; \
174  }
175 
176 #define _PS_V2_DECLARE_FACTORY( type, factory ) \
177  namespace { \
178  ::Gaudi::PluginService::v2::DeclareFactory<type, factory> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{}; \
179  }
180 
181 #define _PS_V2_DECLARE_FACTORY_WITH_ID( type, id, factory ) \
182  namespace { \
183  ::Gaudi::PluginService::v2::DeclareFactory<type, factory> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{ \
184  ::Gaudi::PluginService::v2::Details::stringify_id( id ) }; \
185  }
186 
187 #define _PS_V2_DECLARE_COMPONENT_PROPERTY( id, name, value ) \
188  namespace { \
189  struct _PS_V2_INTERNAL_SET_PROPERTY { \
190  _PS_V2_INTERNAL_SET_PROPERTY() { \
191  ::Gaudi::PluginService::v2::Details::Registry::instance().addProperty( #id, name, value ); \
192  } \
193  } _ps_v2_internal_set_property; \
194  }
195 
196 #if GAUDI_PLUGIN_SERVICE_USE_V2
197 # define DECLARE_COMPONENT( type ) _PS_V2_DECLARE_COMPONENT( type )
198 # define DECLARE_COMPONENT_WITH_ID( type, id ) _PS_V2_DECLARE_COMPONENT_WITH_ID( type, id )
199 # define DECLARE_FACTORY( type, factory ) _PS_V2_DECLARE_FACTORY( type, factory )
200 # define DECLARE_FACTORY_WITH_ID( type, id, factory ) _PS_V2_DECLARE_FACTORY_WITH_ID( type, id, factory )
201 # define DECLARE_COMPONENT_PROPERTY( id, name, value ) _PS_V2_DECLARE_COMPONENT_PROPERTY( id, name, value )
202 #endif
203 
204 #endif //_GAUDI_PLUGIN_SERVICE_H_
Gaudi::PluginService::Details::source_location::_M_func
const char * _M_func
Definition: PluginServiceV2.h:61
Gaudi::PluginService::v2::Factory< R(Args...)>::ReturnValueType
R ReturnValueType
Definition: PluginServiceV2.h:86
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:105
std::move
T move(T... args)
Gaudi::PluginService::v2::Factory< R(Args...)>::ReturnType
typename Traits::ReturnType ReturnType
Definition: PluginServiceV2.h:84
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:159
Gaudi::PluginService::v2::DeclareFactory::DeclareFactory
DeclareFactory(Details::Registry::Properties props, source_location src_loc=source_location::current())
Definition: PluginServiceV2.h:154
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:62
Gaudi::PluginService::v2::Factory< R(Args...)>::Traits
Details::Traits< R(Args...)> Traits
Definition: PluginServiceV2.h:83
Gaudi::PluginService::Details::source_location::function_name
constexpr const char * function_name() const noexcept
Definition: PluginServiceV2.h:57
Gaudi::PluginService::v2::Factory< R(Args...)>::FactoryType
typename Traits::FactoryType FactoryType
Definition: PluginServiceV2.h:85
Gaudi::PluginService::Details::source_location::source_location
constexpr source_location() noexcept
Definition: PluginServiceV2.h:51
Gaudi::PluginService::v2::DeclareFactory
Helper to declare the factory implementation for a user defined type T.
Definition: PluginServiceV2.h:131
GaudiPartProp.tests.v2
v2
Definition: tests.py:59
Gaudi::PluginService::v2::Details::getDSONameFor
std::string getDSONameFor(void *fptr)
Definition: PluginServiceV2.cpp:336
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:54
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:134
Gaudi::PluginService::Details::source_location
Definition: PluginServiceV2.h:37
Gaudi::PluginService::Details::source_location::file_name
constexpr const char * file_name() const noexcept
Definition: PluginServiceV2.h:56
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:40
Gaudi::PluginService::Details::source_location::_M_file
const char * _M_file
Definition: PluginServiceV2.h:60
Gaudi::PluginService::v2::DeclareFactory::DefaultFactory
Details::DefaultFactory< T, F > DefaultFactory
Definition: PluginServiceV2.h:132
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:90
Gaudi::PluginService::Details::source_location::_M_col
uint_least32_t _M_col
Definition: PluginServiceV2.h:63
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:138
Gaudi::PluginService::Details::source_location::column
constexpr uint_least32_t column() const noexcept
Definition: PluginServiceV2.h:55
Gaudi::PluginService::Details
Definition: PluginServiceV2.h:35