| The Gaudi Framework v28r2p1 (f1a77ff4)

- 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/System.h"
7 #include "GaudiKernel/StatusCode.h"
8 #include <type_traits>
9 #include <typeinfo>
10 #include <ostream>
11
12
14 #define DeclareInterfaceID(iface, major, minor) \
15 static const InterfaceID &interfaceID() { return iid::interfaceID(); } \
16 using iid = Gaudi::InterfaceId< iface , major , minor > ; \
17 using ext_iids = iid::iids
18
30 class GAUDI_API InterfaceID final {
31 public:
32 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NEW_INTERFACES)
33 constexpr InterfaceID( unsigned long lid ) : m_id( lid & 0xFFFF ),
35 m_major_ver( (lid & 0xFF000000)>>24 ),
36 m_minor_ver( (lid & 0xFF0000)>> 16 ) { }
37 #endif
38 constexpr InterfaceID( unsigned long id, unsigned long major, unsigned long minor = 0)
40 : m_id( id ), m_major_ver( major ), m_minor_ver( minor ) { }
42 InterfaceID( const char* name, unsigned long major, unsigned long minor = 0)
43 : m_id( hash32(name) ), m_major_ver( major ), m_minor_ver( minor ) { }
44 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NEW_INTERFACES)
45 constexpr operator unsigned long() const {
47 return (m_major_ver << 24) + (m_minor_ver << 16) + m_id;
48 }
49 #endif
50 constexpr unsigned long id() const { return m_id; }
53 constexpr unsigned long majorVersion() const { return m_major_ver; }
55 constexpr unsigned long minorVersion() const { return m_minor_ver; }
59 constexpr bool versionMatch( const InterfaceID& iid ) const {
60 return ( id() == iid.id() &&
61 majorVersion() == iid.majorVersion() &&
62 minorVersion() >= iid.minorVersion() );
63 }
65 constexpr bool fullMatch( const InterfaceID& iid ) const {
66 return ( id() == iid.id() &&
67 majorVersion() == iid.majorVersion() &&
68 minorVersion() == iid.minorVersion() );
69 }
71 constexpr bool operator == (const InterfaceID& iid ) const { return fullMatch(iid); }
73 static unsigned int hash32(const char* key) {
74 unsigned int hash = 0;
75 for (const char* k = key; *k; ++k) {
76 hash += *k; hash += (hash << 10); hash ^= (hash >> 6);
77 }
78 hash += (hash << 3); hash ^= (hash >> 11); hash += (hash << 15);
79 return hash;
80 }
81
82 private:
83 unsigned long m_id;
84 unsigned long m_major_ver;
85 unsigned long m_minor_ver;
86 };
87
88 namespace Gaudi {
89
90 template <typename... I> struct interface_list { };
91
92 namespace meta {
93 // identity T -> type = T; note that id_<T> is a complete type, even if T is not
94 template <typename T> struct id_
95 { using type = T; };
96
97 namespace detail
98 {
99 template <typename... Is>
100 struct inherit_from : Is... { };
101
102 template <typename List, typename I>
103 struct append1{ };
104
105 // interpose an id_<I> as id_<I> is a complete type, even if I is not... and we need complete
106 // types to inherit from
107 template <typename... Is, typename I>
108 struct append1<interface_list<Is...>, I>
109 : id_<typename std::conditional<std::is_base_of< id_<I>, inherit_from<id_<Is>...> >::value,
110 interface_list<Is...>,
111 interface_list<Is...,I> >::type> { };
112
113 template <typename, typename>
114 struct appendN { };
115
116 template <typename State>
117 struct appendN<interface_list<>, State >
118 : id_<State> { };
119
120 template <typename... Is, typename I, typename List>
121 struct appendN<interface_list<I,Is...>, List>
122 : appendN<interface_list<Is... >, typename append1<List, I>::type> { };
123 }
124
125 template <typename... Is>
126 using unique_interface_list = detail::appendN<interface_list<Is...>, interface_list<>>;
127 }
128
129 // interface_list concatenation
130 template <typename... I> struct interface_list_cat;
131
132 // identity
133 template <typename... I>
134 struct interface_list_cat<interface_list<I...>>
135 : meta::unique_interface_list< I... > { };
136
137 // binary op
138 template <typename... I1, typename... I2>
139 struct interface_list_cat<interface_list<I1...>,interface_list<I2...>>
140 : meta::unique_interface_list<I1...,I2...> { };
141
142 // induction of binary op
143 template <typename... I1, typename... I2, typename... Others>
144 struct interface_list_cat<interface_list<I1...>,interface_list<I2...>, Others...>
145 : interface_list_cat< interface_list<I1...,I2...>, Others... > { };
146
147 // append is a special case of concatenation...
148 template <typename... I> struct interface_list_append;
149
150 template <typename... Is, typename I>
151 struct interface_list_append<interface_list<Is...>,I>
152 : interface_list_cat< interface_list<Is...>, interface_list<I> > { };
153
154 // helpers for implementation of interface cast
155 namespace iid_cast_details {
156 template <typename I>
157 constexpr void* void_cast(const I* i)
158 { return const_cast<I*>(i); }
159
160 template <typename ... Is> struct iid_cast_t;
161
162 template <> struct iid_cast_t<> {
163 template <typename P>
164 constexpr void* operator()(const InterfaceID&, P*) const { return nullptr ; }
165 };
166
167 template <typename I, typename... Is> struct iid_cast_t<I,Is...> {
168 template <typename P>
169 inline void* operator()(const InterfaceID& tid, P* ptr) const {
170 return tid.versionMatch(I::interfaceID()) ? void_cast<typename I::interface_type>(ptr)
171 : iid_cast_t<Is...>{}(tid,ptr);
172 }
173 };
174 }
175
176 template <typename... Is>
177 std::vector<std::string> getInterfaceNames( Gaudi::interface_list<Is...> ) {
178 return { Is::name()... };
179 }
180
181 template <typename...Is,typename P>
182 inline void* iid_cast(const InterfaceID& tid, Gaudi::interface_list<Is...>, P* ptr ) {
183 constexpr auto iid_cast_ = iid_cast_details::iid_cast_t<Is...>{};
184 return iid_cast_(tid,ptr);
185 }
186
190 template <typename INTERFACE, unsigned long majVers, unsigned long minVers>
191 struct InterfaceId final {
193 using interface_type = INTERFACE;
195 using iids = typename Gaudi::interface_list_append<typename interface_type::ext_iids,InterfaceId>::type ;
196
197 static inline std::string name() { return System::typeinfoName(typeid(INTERFACE)); }
198 static constexpr unsigned long majorVersion() { return majVers; }
199 static constexpr unsigned long minorVersion() { return minVers; }
200
201 static inline const std::type_info &TypeInfo() {
202 return typeid(typename iids::type);
203 }
204
205 static inline const InterfaceID& interfaceID() {
206 static const InterfaceID s_iid(name().c_str(),majVers,minVers);
207 return s_iid;
208 }
209 };
210
211 constexpr struct fullMatch_t {
212 template <typename IFACE1, typename IFACE2, unsigned long major1, unsigned long major2, unsigned long minor1, unsigned long minor2>
213 constexpr bool operator()(InterfaceId<IFACE1,major1,minor1>, InterfaceId<IFACE2,major2,minor2>) const { return false; }
214 template <typename IFACE, unsigned long major, unsigned long minor>
215 constexpr bool operator()(InterfaceId<IFACE,major,minor>, InterfaceId<IFACE,major,minor>) const { return true; }
216 } fullMatch{};
217
218 constexpr struct majorMatch_t {
219 template <typename IFACE1, typename IFACE2, unsigned long major1, unsigned long major2, unsigned long minor1, unsigned long minor2>
220 constexpr bool operator()(InterfaceId<IFACE1,major1,minor1>, InterfaceId<IFACE2,major2,minor2>) const { return false; }
221 template <typename IFACE, unsigned long major, unsigned long minor1, unsigned long minor2>
222 constexpr bool operator()(InterfaceId<IFACE,major,minor1>, InterfaceId<IFACE,major,minor2>) const { return true; }
223 } majorMatch{};
224
225 }
226
234 class GAUDI_API IInterface {
235 public:
237 using iid = Gaudi::InterfaceId<IInterface,0,0>;
238
240 using ext_iids = Gaudi::interface_list<iid>;
241
243 static inline const InterfaceID &interfaceID(){
244 return iid::interfaceID();
245 }
246
248 virtual void *i_cast(const InterfaceID &) const
249 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NEW_INTERFACES)
250 {return nullptr;}
251 #else
252 = 0;
253 #endif
254
256 virtual std::vector<std::string> getInterfaceNames() const
257 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NEW_INTERFACES)
258 {return {};}
259 #else
260 = 0;
261 #endif
262
264 virtual unsigned long addRef() = 0;
265
267 virtual unsigned long release() = 0;
268
270 virtual unsigned long refCount() const
271 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NEW_INTERFACES)
272 { IInterface* ths = const_cast<IInterface*>(this);
273 ths->addRef();
274 return ths->release(); } // new method, so we need a default implementation for v20 compatibility
275 #else
276 = 0;
277 #endif
278
280 virtual StatusCode queryInterface(const InterfaceID &ti, void** pp) = 0;
281
283 enum Status {
285 SUCCESS = 1,
287 NO_INTERFACE,
289 VERSMISMATCH,
291 LAST_ERROR
292 };
293
295 virtual ~IInterface() = default;
296 };
297
298 namespace Gaudi {
300 template <typename TARGET>
301 TARGET* Cast(IInterface *i){
302 return reinterpret_cast<TARGET*>(i->i_cast(TARGET::interfaceID()));
303 }
306 template <typename TARGET>
307 const TARGET* Cast(const IInterface *i){
308 return reinterpret_cast<const TARGET*>(i->i_cast(TARGET::interfaceID()));
309 }
310 }
311
322 template <class I>
323 bool isValidInterface( I* i) {
324 void* ii = nullptr;;
325 StatusCode sc = i->queryInterface( I::interfaceID(), &ii );
326 return sc.isSuccess();
327 }
328
329 //#ifdef GAUDI_V20_COMPAT
331 inline std::ostream& operator << ( std::ostream& s, const InterfaceID& id) {
332 s << "IID_" << id.id();
333 return s;
334 }
335 //#endif
336
347 template <class DEST,class SRC>
348 inline DEST** pp_cast(SRC** ptr){
349 return reinterpret_cast<DEST**>(ptr);
350 }
351
352 #include "GaudiKernel/extend_interfaces.h"
353 #include "GaudiKernel/implements.h"
354 #include "GaudiKernel/extends.h"
355
356 #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:331
Gaudi::interface_list_append
Definition: IInterface.h:148
Kernel.h
Gaudi::interface_list
Definition: IInterface.h:90
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:28
Gaudi::majorMatch_t::operator()
constexpr bool operator()(InterfaceId< IFACE, major, minor1 >, InterfaceId< IFACE, major, minor2 >) const
Definition: IInterface.h:222
Gaudi::fullMatch_t::operator()
constexpr bool operator()(InterfaceId< IFACE1, major1, minor1 >, InterfaceId< IFACE2, major2, minor2 >) const
Definition: IInterface.h:213
IInterface::NO_INTERFACE
Requested interface is not available.
Definition: IInterface.h:287
Gaudi::iid_cast_details::iid_cast_t<>::operator()
constexpr void * operator()(const InterfaceID &, P *) const
Definition: IInterface.h:164
Gaudi::Cast
const TARGET * Cast(const IInterface *i)
Cast a IInterface pointer to an IInterface specialization (TARGET).
Definition: IInterface.h:307
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:299
Gaudi::fullMatch_t
Definition: IInterface.h:211
StatusCode::isSuccess
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
GaudiPython.Bindings.SUCCESS
SUCCESS
Definition: Bindings.py:52
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:73
InterfaceID::m_id
unsigned long m_id
Definition: IInterface.h:83
isValidInterface
bool isValidInterface(I *i)
Templated function that throws an exception if the version if the interface implemented by the object...
Definition: IInterface.h:323
Gaudi::InterfaceId::interface_type
INTERFACE interface_type
interface type
Definition: IInterface.h:193
Gaudi::InterfaceId::majorVersion
static constexpr unsigned long majorVersion()
Definition: IInterface.h:198
Gaudi::InterfaceId
Class to handle automatically the versioning of the interfaces when they are inheriting from other in...
Definition: IInterface.h:191
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:348
std::type_info
Gaudi::interface_list_cat
Definition: IInterface.h:130
std::string
STL class.
Gaudi::meta::detail::inherit_from
Definition: IInterface.h:100
IInterface::i_cast
virtual void * i_cast(const InterfaceID &) const =0
main cast function
InterfaceID
Interface ID class.
Definition: IInterface.h:30
Gaudi::meta::id_
Definition: IInterface.h:94
InterfaceID::m_minor_ver
unsigned long m_minor_ver
Definition: IInterface.h:85
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:215
operator==
bool operator==(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition: Allocator.h:249
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
IInterface
Definition of the basic interface.
Definition: IInterface.h:234
gaudirun.type
type
Definition: gaudirun.py:125
implements.h
extends.h
Gaudi::meta::detail::append1
Definition: IInterface.h:103
InterfaceID::minorVersion
constexpr unsigned long minorVersion() const
get the minor version of the interface
Definition: IInterface.h:55
Gaudi::majorMatch_t
Definition: IInterface.h:218
extend_interfaces.h
InterfaceID::versionMatch
constexpr bool versionMatch(const InterfaceID &iid) const
check compatibility.
Definition: IInterface.h:59
IInterface::VERSMISMATCH
Requested interface version is incompatible.
Definition: IInterface.h:289
Gaudi::InterfaceId::name
static std::string name()
Definition: IInterface.h:197
Gaudi::InterfaceId::minorVersion
static constexpr unsigned long minorVersion()
Definition: IInterface.h:199
std::vector< std::string >
InterfaceID::m_major_ver
unsigned long m_major_ver
Definition: IInterface.h:84
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:220
Gaudi::iid_cast_details::iid_cast_t< I, Is... >::operator()
void * operator()(const InterfaceID &tid, P *ptr) const
Definition: IInterface.h:169
Gaudi::iid_cast_details::iid_cast_t
Definition: IInterface.h:160
Gaudi::InterfaceId::interfaceID
static const InterfaceID & interfaceID()
Definition: IInterface.h:205
IInterface::Status
Status
Return status.
Definition: IInterface.h:283
InterfaceID::id
constexpr unsigned long id() const
get the interface identifier
Definition: IInterface.h:51
gaudirun.s
string s
Definition: gaudirun.py:245
compareOutputFiles.pp
pp
Definition: compareOutputFiles.py:417
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:240
Gaudi::getInterfaceNames
std::vector< std::string > getInterfaceNames(Gaudi::interface_list< Is... >)
Definition: IInterface.h:177
Gaudi::iid_cast_details::void_cast
constexpr void * void_cast(const I *i)
Definition: IInterface.h:157
IInterface::iid
Gaudi::InterfaceId< IInterface, 0, 0 > iid
Interface ID.
Definition: IInterface.h:237
StatusCode.h
InterfaceID::majorVersion
constexpr unsigned long majorVersion() const
get the major version of the interface
Definition: IInterface.h:53
queryInterface
StatusCode queryInterface(const InterfaceID &iid, void **pinterface) override
Gaudi::meta::detail::appendN
Definition: IInterface.h:114
InterfaceID::fullMatch
constexpr bool fullMatch(const InterfaceID &iid) const
check full compatibility.
Definition: IInterface.h:65
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:42
Gaudi::iid_cast
void * iid_cast(const InterfaceID &tid, Gaudi::interface_list< Is... >, P *ptr)
Definition: IInterface.h:182
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:107
std::ostream
STL class.
IInterface::interfaceID
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:243
Gaudi
Helper functions to set/get the application return code.
Definition: __init__.py:1
Gaudi::InterfaceId::iids
typename Gaudi::interface_list_append< typename interface_type::ext_iids, InterfaceId >::type iids
List of interfaces.
Definition: IInterface.h:195
Gaudi::InterfaceId::TypeInfo
static const std::type_info & TypeInfo()
Definition: IInterface.h:201