| The Gaudi Framework v29r1 (5cd95fa2)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

IInterface.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_IINTERFACE_H
2 #define GAUDIKERNEL_IINTERFACE_H
3
4 // Include files
5 #include "GaudiKernel/Kernel.h"
6 #include "GaudiKernel/StatusCode.h"
7 #include "GaudiKernel/System.h"
8 #include <ostream>
9 #include <type_traits>
10 #include <typeinfo>
11
13 #define DeclareInterfaceID( iface, major, minor ) \
14 static const InterfaceID& interfaceID() { return iid::interfaceID(); } \
15 using iid = Gaudi::InterfaceId<iface, major, minor>; \
16 using ext_iids = iid::iids
17
29 class GAUDI_API InterfaceID final
30 {
31 public:
32 #if defined( GAUDI_V20_COMPAT ) && !defined( G21_NEW_INTERFACES )
33 constexpr InterfaceID( unsigned long lid )
35 : m_id( lid & 0xFFFF ), m_major_ver( ( lid & 0xFF000000 ) >> 24 ), m_minor_ver( ( lid & 0xFF0000 ) >> 16 )
36 {
37 }
38 #endif
39 constexpr InterfaceID( unsigned long id, unsigned long major, unsigned long minor = 0 )
41 : m_id( id ), m_major_ver( major ), m_minor_ver( minor )
42 {
43 }
45 InterfaceID( const char* name, unsigned long major, unsigned long minor = 0 )
46 : m_id( hash32( name ) ), m_major_ver( major ), m_minor_ver( minor )
47 {
48 }
49 #if defined( GAUDI_V20_COMPAT ) && !defined( G21_NEW_INTERFACES )
50 constexpr operator unsigned long() const { return ( m_major_ver << 24 ) + ( m_minor_ver << 16 ) + m_id; }
52 #endif
53 constexpr unsigned long id() const { return m_id; }
56 constexpr unsigned long majorVersion() const { return m_major_ver; }
58 constexpr unsigned long minorVersion() const { return m_minor_ver; }
62 constexpr bool versionMatch( const InterfaceID& iid ) const
63 {
64 return ( id() == iid.id() && majorVersion() == iid.majorVersion() && minorVersion() >= iid.minorVersion() );
65 }
67 constexpr bool fullMatch( const InterfaceID& iid ) const
68 {
69 return ( id() == iid.id() && majorVersion() == iid.majorVersion() && minorVersion() == iid.minorVersion() );
70 }
72 constexpr bool operator==( const InterfaceID& iid ) const { return fullMatch( iid ); }
74 static unsigned int hash32( const char* key )
75 {
76 unsigned int hash = 0;
77 for ( const char* k = key; *k; ++k ) {
78 hash += *k;
79 hash += ( hash << 10 );
80 hash ^= ( hash >> 6 );
81 }
82 hash += ( hash << 3 );
83 hash ^= ( hash >> 11 );
84 hash += ( hash << 15 );
85 return hash;
86 }
87
88 private:
89 unsigned long m_id;
90 unsigned long m_major_ver;
91 unsigned long m_minor_ver;
92 };
93
94 namespace Gaudi
95 {
96
97 template <typename... I>
98 struct interface_list {
99 };
100
101 namespace meta
102 {
103 // identity T -> type = T; note that id_<T> is a complete type, even if T is not
104 template <typename T>
105 struct id_ {
106 using type = T;
107 };
108
109 namespace detail
110 {
111 template <typename... Is>
112 struct inherit_from : Is... {
113 };
114
115 template <typename List, typename I>
116 struct append1 {
117 };
118
119 // interpose an id_<I> as id_<I> is a complete type, even if I is not... and we need complete
120 // types to inherit from
121 template <typename... Is, typename I>
122 struct append1<interface_list<Is...>, I>
123 : id_<typename std::conditional<std::is_base_of<id_<I>, inherit_from<id_<Is>...>>::value,
124 interface_list<Is...>, interface_list<Is..., I>>::type> {
125 };
126
127 template <typename, typename>
128 struct appendN {
129 };
130
131 template <typename State>
132 struct appendN<interface_list<>, State> : id_<State> {
133 };
134
135 template <typename... Is, typename I, typename List>
136 struct appendN<interface_list<I, Is...>, List> : appendN<interface_list<Is...>, typename append1<List, I>::type> {
137 };
138 }
139
140 template <typename... Is>
141 using unique_interface_list = detail::appendN<interface_list<Is...>, interface_list<>>;
142 }
143
144 // interface_list concatenation
145 template <typename... I>
146 struct interface_list_cat;
147
148 // identity
149 template <typename... I>
150 struct interface_list_cat<interface_list<I...>> : meta::unique_interface_list<I...> {
151 };
152
153 // binary op
154 template <typename... I1, typename... I2>
155 struct interface_list_cat<interface_list<I1...>, interface_list<I2...>> : meta::unique_interface_list<I1..., I2...> {
156 };
157
158 // induction of binary op
159 template <typename... I1, typename... I2, typename... Others>
160 struct interface_list_cat<interface_list<I1...>, interface_list<I2...>, Others...>
161 : interface_list_cat<interface_list<I1..., I2...>, Others...> {
162 };
163
164 // append is a special case of concatenation...
165 template <typename... I>
166 struct interface_list_append;
167
168 template <typename... Is, typename I>
169 struct interface_list_append<interface_list<Is...>, I>
170 : interface_list_cat<interface_list<Is...>, interface_list<I>> {
171 };
172
173 // helpers for implementation of interface cast
174 namespace iid_cast_details
175 {
176 template <typename I>
177 constexpr void* void_cast( const I* i )
178 {
179 return const_cast<I*>( i );
180 }
181
182 template <typename... Is>
183 struct iid_cast_t;
184
185 template <>
186 struct iid_cast_t<> {
187 template <typename P>
188 constexpr void* operator()( const InterfaceID&, P* ) const
189 {
190 return nullptr;
191 }
192 };
193
194 template <typename I, typename... Is>
195 struct iid_cast_t<I, Is...> {
196 template <typename P>
197 inline void* operator()( const InterfaceID& tid, P* ptr ) const
198 {
199 return tid.versionMatch( I::interfaceID() ) ? void_cast<typename I::interface_type>( ptr )
200 : iid_cast_t<Is...>{}( tid, ptr );
201 }
202 };
203 }
204
205 template <typename... Is>
206 std::vector<std::string> getInterfaceNames( Gaudi::interface_list<Is...> )
207 {
208 return {Is::name()...};
209 }
210
211 template <typename... Is, typename P>
212 inline void* iid_cast( const InterfaceID& tid, Gaudi::interface_list<Is...>, P* ptr )
213 {
214 constexpr auto iid_cast_ = iid_cast_details::iid_cast_t<Is...>{};
215 return iid_cast_( tid, ptr );
216 }
217
221 template <typename INTERFACE, unsigned long majVers, unsigned long minVers>
222 struct InterfaceId final {
224 using interface_type = INTERFACE;
226 using iids = typename Gaudi::interface_list_append<typename interface_type::ext_iids, InterfaceId>::type;
227
228 static inline std::string name() { return System::typeinfoName( typeid( INTERFACE ) ); }
229 static constexpr unsigned long majorVersion() { return majVers; }
230 static constexpr unsigned long minorVersion() { return minVers; }
231
232 static inline const std::type_info& TypeInfo() { return typeid( typename iids::type ); }
233
234 static inline const InterfaceID& interfaceID()
235 {
236 static const InterfaceID s_iid( name().c_str(), majVers, minVers );
237 return s_iid;
238 }
239 };
240
241 constexpr struct fullMatch_t {
242 template <typename IFACE1, typename IFACE2, unsigned long major1, unsigned long major2, unsigned long minor1,
243 unsigned long minor2>
244 constexpr bool operator()( InterfaceId<IFACE1, major1, minor1>, InterfaceId<IFACE2, major2, minor2> ) const
245 {
246 return false;
247 }
248 template <typename IFACE, unsigned long major, unsigned long minor>
249 constexpr bool operator()( InterfaceId<IFACE, major, minor>, InterfaceId<IFACE, major, minor> ) const
250 {
251 return true;
252 }
253 } fullMatch{};
254
255 constexpr struct majorMatch_t {
256 template <typename IFACE1, typename IFACE2, unsigned long major1, unsigned long major2, unsigned long minor1,
257 unsigned long minor2>
258 constexpr bool operator()( InterfaceId<IFACE1, major1, minor1>, InterfaceId<IFACE2, major2, minor2> ) const
259 {
260 return false;
261 }
262 template <typename IFACE, unsigned long major, unsigned long minor1, unsigned long minor2>
263 constexpr bool operator()( InterfaceId<IFACE, major, minor1>, InterfaceId<IFACE, major, minor2> ) const
264 {
265 return true;
266 }
267 } majorMatch{};
268 }
269
277 class GAUDI_API IInterface
278 {
279 public:
281 using iid = Gaudi::InterfaceId<IInterface, 0, 0>;
282
284 using ext_iids = Gaudi::interface_list<iid>;
285
287 static inline const InterfaceID& interfaceID() { return iid::interfaceID(); }
288
290 virtual void* i_cast( const InterfaceID& ) const
291 #if defined( GAUDI_V20_COMPAT ) && !defined( G21_NEW_INTERFACES )
292 {
293 return nullptr;
294 }
295 #else
296 = 0;
297 #endif
298
300 virtual std::vector<std::string> getInterfaceNames() const
301 #if defined( GAUDI_V20_COMPAT ) && !defined( G21_NEW_INTERFACES )
302 {
303 return {};
304 }
305 #else
306 = 0;
307 #endif
308
310 virtual unsigned long addRef() = 0;
311
313 virtual unsigned long release() = 0;
314
316 virtual unsigned long refCount() const
317 #if defined( GAUDI_V20_COMPAT ) && !defined( G21_NEW_INTERFACES )
318 {
319 IInterface* ths = const_cast<IInterface*>( this );
320 ths->addRef();
321 return ths->release();
322 } // new method, so we need a default implementation for v20 compatibility
323 #else
324 = 0;
325 #endif
326
328 virtual StatusCode queryInterface( const InterfaceID& ti, void** pp ) = 0;
329
331 enum Status {
333 SUCCESS = 1,
335 NO_INTERFACE,
337 VERSMISMATCH,
339 LAST_ERROR
340 };
341
343 virtual ~IInterface() = default;
344 };
345
346 namespace Gaudi
347 {
349 template <typename TARGET>
350 TARGET* Cast( IInterface* i )
351 {
352 return reinterpret_cast<TARGET*>( i->i_cast( TARGET::interfaceID() ) );
353 }
356 template <typename TARGET>
357 const TARGET* Cast( const IInterface* i )
358 {
359 return reinterpret_cast<const TARGET*>( i->i_cast( TARGET::interfaceID() ) );
360 }
361 }
362
373 template <class I>
374 bool isValidInterface( I* i )
375 {
376 void* ii = nullptr;
377 ;
378 StatusCode sc = i->queryInterface( I::interfaceID(), &ii );
379 return sc.isSuccess();
380 }
381
382 //#ifdef GAUDI_V20_COMPAT
384 inline std::ostream& operator<<( std::ostream& s, const InterfaceID& id )
385 {
386 s << "IID_" << id.id();
387 return s;
388 }
389 //#endif
390
401 template <class DEST, class SRC>
402 inline DEST** pp_cast( SRC** ptr )
403 {
404 return reinterpret_cast<DEST**>( ptr );
405 }
406
407 #include "GaudiKernel/extend_interfaces.h"
408 #include "GaudiKernel/extends.h"
409 #include "GaudiKernel/implements.h"
410
411 #endif // GAUDIKERNEL_IINTERFACE_H
System.h
operator<<
std::ostream & operator<<(std::ostream &s, const InterfaceID &id)
ostream operator for InterfaceID. Needed by PluginSvc
Definition: IInterface.h:384
Gaudi::interface_list_append
Definition: IInterface.h:166
Kernel.h
Gaudi::interface_list
Definition: IInterface.h:98
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:32
Gaudi::majorMatch_t::operator()
constexpr bool operator()(InterfaceId< IFACE, major, minor1 >, InterfaceId< IFACE, major, minor2 >) const
Definition: IInterface.h:263
Gaudi::fullMatch_t::operator()
constexpr bool operator()(InterfaceId< IFACE1, major1, minor1 >, InterfaceId< IFACE2, major2, minor2 >) const
Definition: IInterface.h:244
IInterface::NO_INTERFACE
Requested interface is not available.
Definition: IInterface.h:335
Gaudi::iid_cast_details::iid_cast_t<>::operator()
constexpr void * operator()(const InterfaceID &, P *) const
Definition: IInterface.h:188
Gaudi::Cast
const TARGET * Cast(const IInterface *i)
Cast a IInterface pointer to an IInterface specialization (TARGET).
Definition: IInterface.h:357
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:329
Gaudi::fullMatch_t
Definition: IInterface.h:241
StatusCode::isSuccess
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
GaudiPython.Bindings.SUCCESS
SUCCESS
Definition: Bindings.py:56
InterfaceID::hash32
static unsigned int hash32(const char *key)
Jenkins one-at-time hash function – see https://en.wikipedia.org/wiki/Jenkins_hash_function.
Definition: IInterface.h:74
InterfaceID::m_id
unsigned long m_id
Definition: IInterface.h:89
isValidInterface
bool isValidInterface(I *i)
Templated function that throws an exception if the version if the interface implemented by the object...
Definition: IInterface.h:374
Gaudi::InterfaceId::interface_type
INTERFACE interface_type
interface type
Definition: IInterface.h:224
Gaudi::InterfaceId::majorVersion
static constexpr unsigned long majorVersion()
Definition: IInterface.h:229
Gaudi::InterfaceId
Class to handle automatically the versioning of the interfaces when they are inheriting from other in...
Definition: IInterface.h:222
pp_cast
DEST ** pp_cast(SRC **ptr)
Small function to be used instead of the construct (void**)&pointer, which produces, on gcc 4.1 optimized, the warning warning: dereferencing type-punned pointer will break strict-aliasing rules The assempler code produced is equivalent to the one with the direct cast.
Definition: IInterface.h:402
std::type_info
Gaudi::interface_list_cat
Definition: IInterface.h:146
std::string
STL class.
Gaudi::meta::detail::inherit_from
Definition: IInterface.h:112
IInterface::i_cast
virtual void * i_cast(const InterfaceID &) const =0
main cast function
InterfaceID
Interface ID class.
Definition: IInterface.h:29
Gaudi::meta::id_
Definition: IInterface.h:105
InterfaceID::m_minor_ver
unsigned long m_minor_ver
Definition: IInterface.h:91
detail
TupleObj.h GaudiAlg/TupleObj.h namespace with few technical implementations.
Gaudi::fullMatch_t::operator()
constexpr bool operator()(InterfaceId< IFACE, major, minor >, InterfaceId< IFACE, major, minor >) const
Definition: IInterface.h:249
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
IInterface
Definition of the basic interface.
Definition: IInterface.h:277
gaudirun.type
type
Definition: gaudirun.py:133
implements.h
extends.h
Gaudi::meta::detail::append1
Definition: IInterface.h:116
InterfaceID::minorVersion
constexpr unsigned long minorVersion() const
get the minor version of the interface
Definition: IInterface.h:58
Gaudi::majorMatch_t
Definition: IInterface.h:255
extend_interfaces.h
InterfaceID::versionMatch
constexpr bool versionMatch(const InterfaceID &iid) const
check compatibility.
Definition: IInterface.h:62
IInterface::VERSMISMATCH
Requested interface version is incompatible.
Definition: IInterface.h:337
Gaudi::InterfaceId::name
static std::string name()
Definition: IInterface.h:228
Gaudi::InterfaceId::minorVersion
static constexpr unsigned long minorVersion()
Definition: IInterface.h:230
std::vector< std::string >
InterfaceID::m_major_ver
unsigned long m_major_ver
Definition: IInterface.h:90
IInterface::release
virtual unsigned long release()=0
Release Interface instance.
Gaudi::majorMatch_t::operator()
constexpr bool operator()(InterfaceId< IFACE1, major1, minor1 >, InterfaceId< IFACE2, major2, minor2 >) const
Definition: IInterface.h:258
Gaudi::iid_cast_details::iid_cast_t< I, Is... >::operator()
void * operator()(const InterfaceID &tid, P *ptr) const
Definition: IInterface.h:197
Gaudi::iid_cast_details::iid_cast_t
Definition: IInterface.h:183
Gaudi::InterfaceId::interfaceID
static const InterfaceID & interfaceID()
Definition: IInterface.h:234
IInterface::Status
Status
Return status.
Definition: IInterface.h:331
InterfaceID::id
constexpr unsigned long id() const
get the interface identifier
Definition: IInterface.h:54
gaudirun.s
string s
Definition: gaudirun.py:253
compareOutputFiles.pp
pp
Definition: compareOutputFiles.py:467
InterfaceID::operator==
constexpr bool operator==(const InterfaceID &iid) const
compare operator
Definition: IInterface.h:72
IInterface::addRef
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
IInterface::ext_iids
Gaudi::interface_list< iid > ext_iids
Extra interfaces.
Definition: IInterface.h:284
Gaudi::getInterfaceNames
std::vector< std::string > getInterfaceNames(Gaudi::interface_list< Is... >)
Definition: IInterface.h:206
Gaudi::iid_cast_details::void_cast
constexpr void * void_cast(const I *i)
Definition: IInterface.h:177
StatusCode.h
InterfaceID::majorVersion
constexpr unsigned long majorVersion() const
get the major version of the interface
Definition: IInterface.h:56
queryInterface
StatusCode queryInterface(const InterfaceID &iid, void **pinterface) override
Gaudi::meta::detail::appendN
Definition: IInterface.h:128
InterfaceID::fullMatch
constexpr bool fullMatch(const InterfaceID &iid) const
check full compatibility.
Definition: IInterface.h:67
AlgsExecutionStates::State
State
Execution states of the algorithms.
Definition: AlgsExecutionStates.h:32
InterfaceID::InterfaceID
InterfaceID(const char *name, unsigned long major, unsigned long minor=0)
constructor from components
Definition: IInterface.h:45
Gaudi::iid_cast
void * iid_cast(const InterfaceID &tid, Gaudi::interface_list< Is... >, P *ptr)
Definition: IInterface.h:212
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:110
std::ostream
STL class.
IInterface::interfaceID
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:287
Gaudi::InterfaceId::iids
typename Gaudi::interface_list_append< typename interface_type::ext_iids, InterfaceId >::type iids
List of interfaces.
Definition: IInterface.h:226
Gaudi
Helper functions to set/get the application return code.
Definition: __init__.py:1
IInterface::iid
Gaudi::InterfaceId< IInterface, 0, 0 > iid
Interface ID.
Definition: IInterface.h:281
Gaudi::InterfaceId::TypeInfo
static const std::type_info & TypeInfo()
Definition: IInterface.h:232