The Gaudi Framework  master (b9786168)
Loading...
Searching...
No Matches
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>
24namespace Gaudi::PluginService::Details {
25 using std::source_location;
26}
27
28#elif !defined( __clang__ ) && __GNUC__ >= 8
29# include <experimental/source_location>
30namespace Gaudi::PluginService::Details {
31 using std::experimental::source_location;
32}
33#else
34namespace Gaudi::PluginService::Details {
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
68namespace Gaudi {
70 namespace PluginService {
72 using Gaudi::PluginService::Details::source_location;
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
#define GAUDI_PLUGIN_SERVICE_V2_INLINE
GAUDIPS_API std::string demangle(const std::type_info &id)
Return a canonical name for type_info object (implementation borrowed from GaudiKernel/System).
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1