The Gaudi Framework  v30r3 (a5ef0a68)
PluginServiceV2.h
Go to the documentation of this file.
1 #ifndef _GAUDI_PLUGIN_SERVICE_V2_H_
2 #define _GAUDI_PLUGIN_SERVICE_V2_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
26 {
28  namespace PluginService
29  {
31  {
33  template <typename>
34  struct Factory;
36 
38  template <typename R, typename... Args>
39  struct Factory<R( Args... )> {
40  using Traits = Details::Traits<R( Args... )>;
41  using ReturnType = typename Traits::ReturnType;
42  using FactoryType = typename Traits::FactoryType;
43  using ReturnValueType = R;
44 
46  template <typename T>
47  static ReturnType create( const T& id, Args... args )
48  {
49  try {
50  return Details::Registry::instance().get<FactoryType>( Details::stringify_id( id ) )(
51  std::forward<Args>( args )... );
52  } catch ( std::bad_any_cast& ) {
53  Details::reportBadAnyCast( typeid( FactoryType ), Details::stringify_id( id ) );
54  return nullptr;
55  }
56  }
57  };
58 
88  template <typename T, typename F = typename T::Factory>
89  struct DeclareFactory {
90  using DefaultFactory = Details::DefaultFactory<T, F>;
91 
92  DeclareFactory( typename F::FactoryType f = DefaultFactory{}, Details::Registry::Properties props = {} )
93  : DeclareFactory( Details::demangle<T>(), std::move( f ), std::move( props ) )
94  {
95  }
96 
97  DeclareFactory( const std::string& id, typename F::FactoryType f = DefaultFactory{},
99  {
100  using Details::Registry;
101 
102  if ( props.find( "ClassName" ) == end( props ) ) props.emplace( "ClassName", Details::demangle<T>() );
103 
104  Registry::instance().add( id, {libraryName(), std::move( f ), std::move( props )} );
105  }
106 
108  {
109  }
110 
111  private:
113  static std::string libraryName() { return Details::getDSONameFor( reinterpret_cast<void*>( libraryName ) ); }
114  };
115  }
116  }
117 }
118 
119 #define _PS_V2_DECLARE_COMPONENT( type ) \
120  namespace \
121  { \
122  ::Gaudi::PluginService::v2::DeclareFactory<type> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{}; \
123  }
124 
125 #define _PS_V2_DECLARE_COMPONENT_WITH_ID( type, id ) \
126  namespace \
127  { \
128  ::Gaudi::PluginService::v2::DeclareFactory<type> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{ \
129  ::Gaudi::PluginService::v2::Details::stringify_id( id )}; \
130  }
131 
132 #define _PS_V2_DECLARE_FACTORY( type, factory ) \
133  namespace \
134  { \
135  ::Gaudi::PluginService::v2::DeclareFactory<type, factory> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{}; \
136  }
137 
138 #define _PS_V2_DECLARE_FACTORY_WITH_ID( type, id, factory ) \
139  namespace \
140  { \
141  ::Gaudi::PluginService::v2::DeclareFactory<type, factory> _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME{ \
142  ::Gaudi::PluginService::v2::Details::stringify_id( id )}; \
143  }
144 
145 #if GAUDI_PLUGIN_SERVICE_USE_V2
146 #define DECLARE_COMPONENT( type ) _PS_V2_DECLARE_COMPONENT( type )
147 #define DECLARE_COMPONENT_WITH_ID( type, id ) _PS_V2_DECLARE_COMPONENT_WITH_ID( type, id )
148 #define DECLARE_FACTORY( type, factory ) _PS_V2_DECLARE_FACTORY( type, factory )
149 #define DECLARE_FACTORY_WITH_ID( type, id, factory ) _PS_V2_DECLARE_FACTORY_WITH_ID( type, id, factory )
150 #endif
151 
152 #endif //_GAUDI_PLUGIN_SERVICE_H_
#define GAUDI_PLUGIN_SERVICE_V2_INLINE
DeclareFactory(Details::Registry::Properties props)
std::vector< Gaudi::Details::PropertyBase * > Properties
Definition: PropertyMgr.h:133
Helper to declare the factory implementation for a user defined type T.
DeclareFactory(typename F::FactoryType f=DefaultFactory{}, Details::Registry::Properties props={})
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...
std::string getDSONameFor(void *fptr)
void reportBadAnyCast(const std::type_info &factory_type, const std::string &id)
STL class.
Details::DefaultFactory< T, F > DefaultFactory
T move(T...args)
DeclareFactory(const std::string &id, typename F::FactoryType f=DefaultFactory{}, Details::Registry::Properties props={})
static std::string libraryName()
Helper to record the name of the library that declare the factory.
Helper functions to set/get the application return code.
Definition: __init__.py:1