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