DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
PluginServiceV2.h
Go to the documentation of this file.
1 #ifndef GAUDIPLUGINSERVICE_GAUDI_PLUGINSERVICEV2_H
2 #define GAUDIPLUGINSERVICE_GAUDI_PLUGINSERVICEV2_H
3 /*****************************************************************************\
4 * (c) Copyright 2013 CERN *
5 * *
6 * This software is distributed under the terms of the GNU General Public *
7 * Licence version 3 (GPL Version 3), copied verbatim in the file "LICENCE". *
8 * *
9 * In applying this licence, CERN does not waive the privileges and immunities *
10 * granted to it by virtue of its status as an Intergovernmental Organization *
11 * or submit itself to any jurisdiction. *
12 \*****************************************************************************/
13 
16 
18 #include <functional>
19 #include <memory>
20 #include <string>
21 #include <type_traits>
22 #include <typeinfo>
23 #include <utility>
24 
25 namespace Gaudi {
27  namespace PluginService {
30  template <typename>
31  struct Factory;
33 
35  template <typename R, typename... Args>
36  struct Factory<R( Args... )> {
37  using Traits = Details::Traits<R( Args... )>;
38  using ReturnType = typename Traits::ReturnType;
39  using FactoryType = typename Traits::FactoryType;
40  using ReturnValueType = R;
41 
43  template <typename T>
44  static ReturnType create( const T& id, Args... args ) {
45  try {
46  return Details::Registry::instance().get<FactoryType>( Details::stringify_id( id ) )(
47  std::forward<Args>( args )... );
48  } catch ( std::bad_any_cast& ) {
49  Details::reportBadAnyCast( typeid( FactoryType ), Details::stringify_id( id ) );
50  return nullptr;
51  }
52  }
53  };
54 
84  template <typename T, typename F = typename T::Factory>
85  struct DeclareFactory {
86  using DefaultFactory = Details::DefaultFactory<T, F>;
87 
88  DeclareFactory( typename F::FactoryType f = DefaultFactory{}, Details::Registry::Properties props = {} )
89  : DeclareFactory( Details::demangle<T>(), std::move( f ), std::move( props ) ) {}
90 
91  DeclareFactory( const std::string& id, typename F::FactoryType f = DefaultFactory{},
92  Details::Registry::Properties props = {} ) {
93  using Details::Registry;
94 
95  if ( props.find( "ClassName" ) == end( props ) ) props.emplace( "ClassName", Details::demangle<T>() );
96 
97  Registry::instance().add( id, {libraryName(), std::move( f ), std::move( props )} );
98  }
99 
100  DeclareFactory( Details::Registry::Properties props )
101  : DeclareFactory( DefaultFactory{}, std::move( props ) ) {}
102 
103  private:
105  static std::string libraryName() { return Details::getDSONameFor( reinterpret_cast<void*>( libraryName ) ); }
106  };
107  }
108  } // namespace PluginService
109 } // namespace Gaudi
110 
111 #define _PS_V2_DECLARE_COMPONENT( type ) \
112  namespace { \
113  ::Gaudi::PluginService::v2::DeclareFactory<type> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{}; \
114  }
115 
116 #define _PS_V2_DECLARE_COMPONENT_WITH_ID( type, id ) \
117  namespace { \
118  ::Gaudi::PluginService::v2::DeclareFactory<type> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{ \
119  ::Gaudi::PluginService::v2::Details::stringify_id( id )}; \
120  }
121 
122 #define _PS_V2_DECLARE_FACTORY( type, factory ) \
123  namespace { \
124  ::Gaudi::PluginService::v2::DeclareFactory<type, factory> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{}; \
125  }
126 
127 #define _PS_V2_DECLARE_FACTORY_WITH_ID( type, id, factory ) \
128  namespace { \
129  ::Gaudi::PluginService::v2::DeclareFactory<type, factory> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{ \
130  ::Gaudi::PluginService::v2::Details::stringify_id( id )}; \
131  }
132 
133 #if GAUDI_PLUGIN_SERVICE_USE_V2
134 # define DECLARE_COMPONENT( type ) _PS_V2_DECLARE_COMPONENT( type )
135 # define DECLARE_COMPONENT_WITH_ID( type, id ) _PS_V2_DECLARE_COMPONENT_WITH_ID( type, id )
136 # define DECLARE_FACTORY( type, factory ) _PS_V2_DECLARE_FACTORY( type, factory )
137 # define DECLARE_FACTORY_WITH_ID( type, id, factory ) _PS_V2_DECLARE_FACTORY_WITH_ID( type, id, factory )
138 #endif
139 
140 #endif // GAUDIPLUGINSERVICE_GAUDI_PLUGINSERVICEV2_H
Gaudi::PluginService::v2::Factory< R(Args...)>::ReturnValueType
R ReturnValueType
Definition: PluginServiceV2.h:40
Gaudi::PluginService::v2::Factory< R(Args...)>::ReturnType
typename Traits::ReturnType ReturnType
Definition: PluginServiceV2.h:38
GAUDI_PLUGIN_SERVICE_V2_INLINE
#define GAUDI_PLUGIN_SERVICE_V2_INLINE
Definition: PluginServiceCommon.h:20
Gaudi::PluginService::v2::DeclareFactory::DeclareFactory
DeclareFactory(Details::Registry::Properties props)
Definition: PluginServiceV2.h:100
PluginServiceDetailsV2.h
Gaudi::PluginService::v2::DeclareFactory
Definition: PluginServiceV2.h:85
Gaudi::PluginService::v2::DeclareFactory::DefaultFactory
Details::DefaultFactory< T, F > DefaultFactory
Definition: PluginServiceV2.h:86
Gaudi::PluginService::v2::Details::reportBadAnyCast
void reportBadAnyCast(const std::type_info &factory_type, const std::string &id)
Definition: PluginServiceV2.cpp:123
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:44
Gaudi::PluginService::v2::DeclareFactory::libraryName
static std::string libraryName()
Helper to record the name of the library that declare the factory.
Definition: PluginServiceV2.h:105
Gaudi::PluginService::v2::DeclareFactory::DeclareFactory
DeclareFactory(const std::string &id, typename F::FactoryType f=DefaultFactory{}, Details::Registry::Properties props={})
Definition: PluginServiceV2.h:91
Gaudi
Definition: PluginServiceDetailsV1.h:27
Gaudi::PluginService::v2::Factory< R(Args...)>::FactoryType
typename Traits::FactoryType FactoryType
Definition: PluginServiceV2.h:39
Gaudi::PluginService::v2::Details::getDSONameFor
std::string getDSONameFor(void *fptr)
Definition: PluginServiceV2.cpp:305
Gaudi::PluginService::v2::Factory< R(Args...)>::Traits
Details::Traits< R(Args...)> Traits
Definition: PluginServiceV2.h:37
Gaudi::PluginService::v2::DeclareFactory::DeclareFactory
DeclareFactory(typename F::FactoryType f=DefaultFactory{}, Details::Registry::Properties props={})
Definition: PluginServiceV2.h:88