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"
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>
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>
127  }
128 
129  // interface_list concatenation
130  template <typename... I> struct interface_list_cat;
131 
132  // identity
133  template <typename... I>
135  : meta::unique_interface_list< I... > { };
136 
137  // binary op
138  template <typename... I1, typename... 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>
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>
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;
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>
214  template <typename IFACE, unsigned long major, unsigned long minor>
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>
221  template <typename IFACE, unsigned long major, unsigned long minor1, unsigned long minor2>
223  } majorMatch{};
224 
225 }
226 
235 public:
238 
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 
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,
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
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 
353 #include "GaudiKernel/implements.h"
354 #include "GaudiKernel/extends.h"
355 
356 #endif // GAUDIKERNEL_IINTERFACE_H
std::ostream & operator<<(std::ostream &s, const InterfaceID &id)
ostream operator for InterfaceID. Needed by PluginSvc
Definition: IInterface.h:331
constexpr bool operator()(InterfaceId< IFACE, major, minor1 >, InterfaceId< IFACE, major, minor2 >) const
Definition: IInterface.h:222
constexpr bool operator()(InterfaceId< IFACE1, major1, minor1 >, InterfaceId< IFACE2, major2, minor2 >) const
Definition: IInterface.h:213
Requested interface is not available.
Definition: IInterface.h:287
constexpr void * operator()(const InterfaceID &, P *) const
Definition: IInterface.h:164
const TARGET * Cast(const IInterface *i)
Cast a IInterface pointer to an IInterface specialization (TARGET).
Definition: IInterface.h:307
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:299
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
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
unsigned long m_id
Definition: IInterface.h:83
bool isValidInterface(I *i)
Templated function that throws an exception if the version if the interface implemented by the object...
Definition: IInterface.h:323
INTERFACE interface_type
interface type
Definition: IInterface.h:193
static constexpr unsigned long majorVersion()
Definition: IInterface.h:198
Class to handle automatically the versioning of the interfaces when they are inheriting from other in...
Definition: IInterface.h:191
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
STL class.
virtual void * i_cast(const InterfaceID &) const =0
main cast function
Interface ID class.
Definition: IInterface.h:30
unsigned long m_minor_ver
Definition: IInterface.h:85
TupleObj.h GaudiAlg/TupleObj.h namespace with few technical implementations.
constexpr bool operator()(InterfaceId< IFACE, major, minor >, InterfaceId< IFACE, major, minor >) const
Definition: IInterface.h:215
bool operator==(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition: Allocator.h:249
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
Definition of the basic interface.
Definition: IInterface.h:234
constexpr unsigned long minorVersion() const
get the minor version of the interface
Definition: IInterface.h:55
constexpr bool versionMatch(const InterfaceID &iid) const
check compatibility.
Definition: IInterface.h:59
Requested interface version is incompatible.
Definition: IInterface.h:289
static std::string name()
Definition: IInterface.h:197
static constexpr unsigned long minorVersion()
Definition: IInterface.h:199
unsigned long m_major_ver
Definition: IInterface.h:84
virtual unsigned long release()=0
Release Interface instance.
constexpr bool operator()(InterfaceId< IFACE1, major1, minor1 >, InterfaceId< IFACE2, major2, minor2 >) const
Definition: IInterface.h:220
void * operator()(const InterfaceID &tid, P *ptr) const
Definition: IInterface.h:169
static const InterfaceID & interfaceID()
Definition: IInterface.h:205
Status
Return status.
Definition: IInterface.h:283
constexpr unsigned long id() const
get the interface identifier
Definition: IInterface.h:51
string s
Definition: gaudirun.py:245
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
Gaudi::interface_list< iid > ext_iids
Extra interfaces.
Definition: IInterface.h:240
std::vector< std::string > getInterfaceNames(Gaudi::interface_list< Is... >)
Definition: IInterface.h:177
constexpr void * void_cast(const I *i)
Definition: IInterface.h:157
Gaudi::InterfaceId< IInterface, 0, 0 > iid
Interface ID.
Definition: IInterface.h:237
constexpr unsigned long majorVersion() const
get the major version of the interface
Definition: IInterface.h:53
StatusCode queryInterface(const InterfaceID &iid, void **pinterface) override
constexpr bool fullMatch(const InterfaceID &iid) const
check full compatibility.
Definition: IInterface.h:65
State
Execution states of the algorithms.
InterfaceID(const char *name, unsigned long major, unsigned long minor=0)
constructor from components
Definition: IInterface.h:42
void * iid_cast(const InterfaceID &tid, Gaudi::interface_list< Is... >, P *ptr)
Definition: IInterface.h:182
#define GAUDI_API
Definition: Kernel.h:107
STL class.
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:243
Helper functions to set/get the application return code.
Definition: __init__.py:1
typename Gaudi::interface_list_append< typename interface_type::ext_iids, InterfaceId >::type iids
List of interfaces.
Definition: IInterface.h:195
static const std::type_info & TypeInfo()
Definition: IInterface.h:201