The Gaudi Framework  v36r1 (3e2fb5a8)
PluginServiceV2.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 2013-2019 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 <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 )
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 //_GAUDI_PLUGIN_SERVICE_H_
Gaudi::PluginService::v2::DeclareFactory::DeclareFactory
DeclareFactory(const std::string &id, typename F::FactoryType f=DefaultFactory{}, Details::Registry::Properties props={})
Definition: PluginServiceV2.h:91
Gaudi::PluginService::v2::Factory< R(Args...)>::ReturnValueType
R ReturnValueType
Definition: PluginServiceV2.h:40
HistoDumpEx.v2
v2
Definition: HistoDumpEx.py:28
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:117
std::move
T move(T... args)
Gaudi::PluginService::v2::Factory< R(Args...)>::ReturnType
typename Traits::ReturnType ReturnType
Definition: PluginServiceV2.h:38
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_PLUGIN_SERVICE_V2_INLINE
#define GAUDI_PLUGIN_SERVICE_V2_INLINE
Definition: PluginServiceCommon.h:17
Gaudi::PluginService::v2::Factory< R(Args...)>::Traits
Details::Traits< R(Args...)> Traits
Definition: PluginServiceV2.h:37
Gaudi::PluginService::v2::Factory< R(Args...)>::FactoryType
typename Traits::FactoryType FactoryType
Definition: PluginServiceV2.h:39
Gaudi::PluginService::v2::DeclareFactory
Helper to declare the factory implementation for a user defined type T.
Definition: PluginServiceV2.h:85
Gaudi::PluginService::v2::Details::getDSONameFor
std::string getDSONameFor(void *fptr)
Definition: PluginServiceV2.cpp:300
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
Gaudi::PluginService::v2::DeclareFactory::DeclareFactory
DeclareFactory(typename F::FactoryType f=DefaultFactory{}, Details::Registry::Properties props={})
Definition: PluginServiceV2.h:88
gaudirun.args
args
Definition: gaudirun.py:319
ProduceConsume.props
props
Definition: ProduceConsume.py:54
Gaudi::PluginService::v2::DeclareFactory::DefaultFactory
Details::DefaultFactory< T, F > DefaultFactory
Definition: PluginServiceV2.h:86
Gaudi::PluginService::v2::DeclareFactory::DeclareFactory
DeclareFactory(Details::Registry::Properties props)
Definition: PluginServiceV2.h:100
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
IOTest.end
end
Definition: IOTest.py:123