File indexing completed on 2025-01-18 09:14:59
0001 #ifndef GAUDIPLUGINSERVICE_GAUDI_PLUGINSERVICEV2_H
0002 #define GAUDIPLUGINSERVICE_GAUDI_PLUGINSERVICEV2_H
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <Gaudi/Details/PluginServiceDetailsV2.h>
0018 #include <functional>
0019 #include <memory>
0020 #include <string>
0021 #include <type_traits>
0022 #include <typeinfo>
0023 #include <utility>
0024
0025 namespace Gaudi {
0026
0027 namespace PluginService {
0028 GAUDI_PLUGIN_SERVICE_V2_INLINE namespace v2 {
0029
0030 template <typename>
0031 struct Factory;
0032
0033
0034
0035 template <typename R, typename... Args>
0036 struct Factory<R( Args... )> {
0037 using Traits = Details::Traits<R( Args... )>;
0038 using ReturnType = typename Traits::ReturnType;
0039 using FactoryType = typename Traits::FactoryType;
0040 using ReturnValueType = R;
0041
0042
0043 template <typename T>
0044 static ReturnType create( const T& id, Args... args ) {
0045 try {
0046 return Details::Registry::instance().get<FactoryType>( Details::stringify_id( id ) )(
0047 std::forward<Args>( args )... );
0048 } catch ( std::bad_any_cast& ) {
0049 Details::reportBadAnyCast( typeid( FactoryType ), Details::stringify_id( id ) );
0050 return nullptr;
0051 }
0052 }
0053 };
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084 template <typename T, typename F = typename T::Factory>
0085 struct DeclareFactory {
0086 using DefaultFactory = Details::DefaultFactory<T, F>;
0087
0088 DeclareFactory( typename F::FactoryType f = DefaultFactory{}, Details::Registry::Properties props = {} )
0089 : DeclareFactory( Details::demangle<T>(), std::move( f ), std::move( props ) ) {}
0090
0091 DeclareFactory( const std::string& id, typename F::FactoryType f = DefaultFactory{},
0092 Details::Registry::Properties props = {} ) {
0093 using Details::Registry;
0094
0095 if ( props.find( "ClassName" ) == end( props ) ) props.emplace( "ClassName", Details::demangle<T>() );
0096
0097 Registry::instance().add( id, {libraryName(), std::move( f ), std::move( props )} );
0098 }
0099
0100 DeclareFactory( Details::Registry::Properties props )
0101 : DeclareFactory( DefaultFactory{}, std::move( props ) ) {}
0102
0103 private:
0104
0105 static std::string libraryName() { return Details::getDSONameFor( reinterpret_cast<void*>( libraryName ) ); }
0106 };
0107 }
0108 }
0109 }
0110
0111 #define _PS_V2_DECLARE_COMPONENT( type ) \
0112 namespace { \
0113 ::Gaudi::PluginService::v2::DeclareFactory<type> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{}; \
0114 }
0115
0116 #define _PS_V2_DECLARE_COMPONENT_WITH_ID( type, id ) \
0117 namespace { \
0118 ::Gaudi::PluginService::v2::DeclareFactory<type> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{ \
0119 ::Gaudi::PluginService::v2::Details::stringify_id( id )}; \
0120 }
0121
0122 #define _PS_V2_DECLARE_FACTORY( type, factory ) \
0123 namespace { \
0124 ::Gaudi::PluginService::v2::DeclareFactory<type, factory> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{}; \
0125 }
0126
0127 #define _PS_V2_DECLARE_FACTORY_WITH_ID( type, id, factory ) \
0128 namespace { \
0129 ::Gaudi::PluginService::v2::DeclareFactory<type, factory> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{ \
0130 ::Gaudi::PluginService::v2::Details::stringify_id( id )}; \
0131 }
0132
0133 #if GAUDI_PLUGIN_SERVICE_USE_V2
0134 # define DECLARE_COMPONENT( type ) _PS_V2_DECLARE_COMPONENT( type )
0135 # define DECLARE_COMPONENT_WITH_ID( type, id ) _PS_V2_DECLARE_COMPONENT_WITH_ID( type, id )
0136 # define DECLARE_FACTORY( type, factory ) _PS_V2_DECLARE_FACTORY( type, factory )
0137 # define DECLARE_FACTORY_WITH_ID( type, id, factory ) _PS_V2_DECLARE_FACTORY_WITH_ID( type, id, factory )
0138 #endif
0139
0140 #endif