Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r16 (ea80daf8)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
IInterface.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-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 GAUDIKERNEL_IINTERFACE_H
12 #define GAUDIKERNEL_IINTERFACE_H
13 
14 // Include files
15 #include "GaudiKernel/Kernel.h"
16 #include "GaudiKernel/StatusCode.h"
17 #include "GaudiKernel/System.h"
18 #include <ostream>
19 #include <type_traits>
20 #include <typeinfo>
21 
23 #define DeclareInterfaceID( iface, major, minor ) \
24  static const InterfaceID& interfaceID() { return iid::interfaceID(); } \
25  using iid = Gaudi::InterfaceId<iface, major, minor>; \
26  using ext_iids = typename iid::iids
27 
39 class GAUDI_API InterfaceID final {
40 public:
41 #if defined( GAUDI_V20_COMPAT ) && !defined( G21_NEW_INTERFACES )
42  constexpr InterfaceID( unsigned long lid )
44  : m_id( lid & 0xFFFF ), m_major_ver( ( lid & 0xFF000000 ) >> 24 ), m_minor_ver( ( lid & 0xFF0000 ) >> 16 ) {}
45 #endif
46  constexpr InterfaceID( unsigned long id, unsigned long major, unsigned long minor = 0 )
48  : m_id( id ), m_major_ver( major ), m_minor_ver( minor ) {}
50  InterfaceID( const char* name, unsigned long major, unsigned long minor = 0 )
51  : m_id( hash32( name ) ), m_major_ver( major ), m_minor_ver( minor ) {}
52 #if defined( GAUDI_V20_COMPAT ) && !defined( G21_NEW_INTERFACES )
53  constexpr operator unsigned long() const { return ( m_major_ver << 24 ) + ( m_minor_ver << 16 ) + m_id; }
55 #endif
56  constexpr unsigned long id() const { return m_id; }
59  constexpr unsigned long majorVersion() const { return m_major_ver; }
61  constexpr unsigned long minorVersion() const { return m_minor_ver; }
65  constexpr bool versionMatch( const InterfaceID& iid ) const {
66  return ( id() == iid.id() && majorVersion() == iid.majorVersion() && minorVersion() >= iid.minorVersion() );
67  }
69  constexpr bool fullMatch( const InterfaceID& iid ) const {
70  return ( id() == iid.id() && majorVersion() == iid.majorVersion() && minorVersion() == iid.minorVersion() );
71  }
73  constexpr bool operator==( const InterfaceID& iid ) const { return fullMatch( iid ); }
75  static unsigned int hash32( const char* key ) {
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  template <typename... I>
97  struct interface_list {};
98 
99  namespace meta {
100  // identity T -> type = T; note that id_<T> is a complete type, even if T is not
101  template <typename T>
102  struct id_ {
103  using type = T;
104  };
105 
106  namespace detail {
107  template <typename... Is>
108  struct inherit_from : Is... {};
109 
110  template <typename List, typename I>
111  struct append1 {};
112 
113  // interpose an id_<I> as id_<I> is a complete type, even if I is not... and we need complete
114  // types to inherit from
115  template <typename... Is, typename I>
116  struct append1<interface_list<Is...>, I>
117  : id_<std::conditional_t<std::is_base_of_v<id_<I>, inherit_from<id_<Is>...>>, interface_list<Is...>,
118  interface_list<Is..., I>>> {};
119 
120  template <typename, typename>
121  struct appendN {};
122 
123  template <typename State>
124  struct appendN<interface_list<>, State> : id_<State> {};
125 
126  template <typename... Is, typename I, typename List>
127  struct appendN<interface_list<I, Is...>, List> : appendN<interface_list<Is...>, typename append1<List, I>::type> {
128  };
129  } // namespace detail
130 
131  template <typename... Is>
133  } // namespace meta
134 
135  // interface_list concatenation
136  template <typename... I>
138 
139  // identity
140  template <typename... I>
142 
143  // binary op
144  template <typename... I1, typename... I2>
146  };
147 
148  // induction of binary op
149  template <typename... I1, typename... I2, typename... Others>
150  struct interface_list_cat<interface_list<I1...>, interface_list<I2...>, Others...>
151  : interface_list_cat<interface_list<I1..., I2...>, Others...> {};
152 
153  // append is a special case of concatenation...
154  template <typename... I>
156 
157  template <typename... Is, typename I>
159  : interface_list_cat<interface_list<Is...>, interface_list<I>> {};
160 
161  template <typename... Is>
163  return { Is::name()... };
164  }
165 
166  // gcc9 has a false positive warning -- see https://godbolt.org/z/cyjtrr -- gcc10,clang are happy...
167 #if defined( __GNUC__ ) && __GNUC__ < 10
168 # pragma GCC diagnostic push
169 # pragma GCC diagnostic ignored "-Wparentheses"
170 #endif
171  template <typename... Is, typename P>
172  void* iid_cast( const InterfaceID& tid, Gaudi::interface_list<Is...>, P* ptr ) {
173  const void* target = nullptr;
174  ( ( tid.versionMatch( Is::interfaceID() ) &&
175  ( target = static_cast<typename Is::interface_type const*>( ptr ), true ) ) ||
176  ... );
177  return const_cast<void*>( target );
178  }
179 #if defined( __GNUC__ ) && __GNUC__ < 10
180 # pragma GCC diagnostic pop
181 #endif
182 
186  template <typename INTERFACE, unsigned long majVers, unsigned long minVers>
187  struct InterfaceId final {
189  using interface_type = INTERFACE;
192 
193  static inline std::string name() { return System::typeinfoName( typeid( INTERFACE ) ); }
194  static constexpr unsigned long majorVersion() { return majVers; }
195  static constexpr unsigned long minorVersion() { return minVers; }
196 
197  static inline const std::type_info& TypeInfo() { return typeid( typename iids::type ); }
198 
199  static inline const InterfaceID& interfaceID() {
200  static const InterfaceID s_iid( name().c_str(), majVers, minVers );
201  return s_iid;
202  }
203  };
204 
205  constexpr struct fullMatch_t {
206  template <typename IFACE1, typename IFACE2, unsigned long major1, unsigned long major2, unsigned long minor1,
207  unsigned long minor2>
209  return false;
210  }
211  template <typename IFACE, unsigned long major, unsigned long minor>
213  return true;
214  }
216 
217  constexpr struct majorMatch_t {
218  template <typename IFACE1, typename IFACE2, unsigned long major1, unsigned long major2, unsigned long minor1,
219  unsigned long minor2>
221  return false;
222  }
223  template <typename IFACE, unsigned long major, unsigned long minor1, unsigned long minor2>
225  return true;
226  }
228 } // namespace Gaudi
229 
238 public:
241 
244 
246  static inline const InterfaceID& interfaceID() { return iid::interfaceID(); }
247 
249  virtual void* i_cast( const InterfaceID& ) const
250 #if defined( GAUDI_V20_COMPAT ) && !defined( G21_NEW_INTERFACES )
251  {
252  return nullptr;
253  }
254 #else
255  = 0;
256 #endif
257 
260 #if defined( GAUDI_V20_COMPAT ) && !defined( G21_NEW_INTERFACES )
261  {
262  return {};
263  }
264 #else
265  = 0;
266 #endif
267 
269  virtual unsigned long addRef() = 0;
270 
272  virtual unsigned long release() = 0;
273 
275  virtual unsigned long refCount() const
276 #if defined( GAUDI_V20_COMPAT ) && !defined( G21_NEW_INTERFACES )
277  {
278  IInterface* ths = const_cast<IInterface*>( this );
279  ths->addRef();
280  return ths->release();
281  } // new method, so we need a default implementation for v20 compatibility
282 #else
283  = 0;
284 #endif
285 
287  virtual StatusCode queryInterface( const InterfaceID& ti, void** pp ) = 0;
288 
290  enum class Status : StatusCode::code_t {
292  FAILURE = 0,
294  SUCCESS = 1,
296  NO_INTERFACE,
298  VERSMISMATCH,
300  LAST_ERROR
301  };
302 
304  virtual ~IInterface() = default;
305 };
306 
308 
309 namespace Gaudi {
311  template <typename TARGET>
312  TARGET* Cast( IInterface* i ) {
313  return reinterpret_cast<TARGET*>( i->i_cast( TARGET::interfaceID() ) );
314  }
317  template <typename TARGET>
318  const TARGET* Cast( const IInterface* i ) {
319  return reinterpret_cast<const TARGET*>( i->i_cast( TARGET::interfaceID() ) );
320  }
321 } // namespace Gaudi
322 
333 template <class IFace>
334 bool isValidInterface( IFace* i ) {
335  void* ii = nullptr;
336  return i->queryInterface( IFace::interfaceID(), &ii ).isSuccess();
337 }
338 
339 //#ifdef GAUDI_V20_COMPAT
342  s << "IID_" << id.id();
343  return s;
344 }
345 //#endif
346 
357 template <class DEST, class SRC>
358 inline DEST** pp_cast( SRC** ptr ) {
359  return reinterpret_cast<DEST**>( ptr );
360 }
361 
363 #include "GaudiKernel/extends.h"
364 #include "GaudiKernel/implements.h"
365 
366 #endif // GAUDIKERNEL_IINTERFACE_H
GaudiPython.Bindings.FAILURE
FAILURE
Definition: Bindings.py:89
extend_interfaces.h
Gaudi::InterfaceId
Class to handle automatically the versioning of the interfaces when they are inheriting from other in...
Definition: IInterface.h:187
InterfaceID::fullMatch
constexpr bool fullMatch(const InterfaceID &iid) const
check full compatibility.
Definition: IInterface.h:69
std::string
STL class.
Gaudi::fullMatch
constexpr struct Gaudi::fullMatch_t fullMatch
Gaudi::InterfaceId::majorVersion
static constexpr unsigned long majorVersion()
Definition: IInterface.h:194
System.h
Properties.long
long
(c) Copyright 1998-2020 CERN for the benefit of the LHCb and ATLAS collaborations # # This software i...
Definition: Properties.py:15
gaudirun.s
string s
Definition: gaudirun.py:348
std::vector< std::string >
std::type_info
Gaudi::meta::detail::appendN
Definition: IInterface.h:121
InterfaceID::m_id
unsigned long m_id
Definition: IInterface.h:89
Gaudi::fullMatch_t
Definition: IInterface.h:205
detail
Gaudi::interface_list_cat
Definition: IInterface.h:137
IInterface::queryInterface
virtual StatusCode queryInterface(const InterfaceID &ti, void **pp)=0
Set the void** to the pointer to the requested interface of the instance.
Gaudi::InterfaceId::minorVersion
static constexpr unsigned long minorVersion()
Definition: IInterface.h:195
extends.h
StatusCode::code_t
unsigned long code_t
type of StatusCode value
Definition: StatusCode.h:67
Gaudi::interface_list_append
Definition: IInterface.h:155
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:313
StatusCode.h
Gaudi::meta::detail::inherit_from
Definition: IInterface.h:108
InterfaceID::id
constexpr unsigned long id() const
get the interface identifier
Definition: IInterface.h:57
operator<<
std::ostream & operator<<(std::ostream &s, const InterfaceID &id)
ostream operator for InterfaceID. Needed by PluginSvc
Definition: IInterface.h:341
compareOutputFiles.target
target
Definition: compareOutputFiles.py:498
IInterface::Status
Status
Return status.
Definition: IInterface.h:290
Gaudi::StateMachine::State
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm,...
Definition: StateMachine.h:22
IInterface::~IInterface
virtual ~IInterface()=default
Virtual destructor.
Gaudi::InterfaceId::interfaceID
static const InterfaceID & interfaceID()
Definition: IInterface.h:199
TimingHistograms.name
name
Definition: TimingHistograms.py:25
StatusCode
Definition: StatusCode.h:65
IInterface::i_cast
virtual void * i_cast(const InterfaceID &) const =0
main cast function
IInterface::interfaceID
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:246
InterfaceID::m_minor_ver
unsigned long m_minor_ver
Definition: IInterface.h:91
std::ostream
STL class.
InterfaceID::majorVersion
constexpr unsigned long majorVersion() const
get the major version of the interface
Definition: IInterface.h:59
Gaudi::InterfaceId::interface_type
INTERFACE interface_type
interface type
Definition: IInterface.h:189
InterfaceID::m_major_ver
unsigned long m_major_ver
Definition: IInterface.h:90
InterfaceID::operator==
constexpr bool operator==(const InterfaceID &iid) const
compare operator
Definition: IInterface.h:73
Gaudi::meta::id_< State >::type
State type
Definition: IInterface.h:103
Gaudi::meta::id_
Definition: IInterface.h:102
Gaudi::fullMatch_t::operator()
constexpr bool operator()(InterfaceId< IFACE, major, minor >, InterfaceId< IFACE, major, minor >) const
Definition: IInterface.h:212
Gaudi::InterfaceId::name
static std::string name()
Definition: IInterface.h:193
Gaudi::interface_list
Definition: IInterface.h:97
Gaudi::majorMatch
constexpr struct Gaudi::majorMatch_t majorMatch
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
pp_cast
DEST ** pp_cast(SRC **ptr)
Small function to be used instead of the construct (void**)&pointer, which produces,...
Definition: IInterface.h:358
InterfaceID::minorVersion
constexpr unsigned long minorVersion() const
get the minor version of the interface
Definition: IInterface.h:61
Gaudi::InterfaceId::TypeInfo
static const std::type_info & TypeInfo()
Definition: IInterface.h:197
gaudirun.type
type
Definition: gaudirun.py:162
Gaudi::majorMatch_t
Definition: IInterface.h:217
Gaudi::fullMatch_t::operator()
constexpr bool operator()(InterfaceId< IFACE1, major1, minor1 >, InterfaceId< IFACE2, major2, minor2 >) const
Definition: IInterface.h:208
STATUSCODE_ENUM_DECL
#define STATUSCODE_ENUM_DECL(ENUM)
Declare an enum to be used as StatusCode value.
Definition: StatusCode.h:286
Gaudi::meta::detail::append1
Definition: IInterface.h:111
Gaudi::iid_cast
void * iid_cast(const InterfaceID &tid, Gaudi::interface_list< Is... >, P *ptr)
Definition: IInterface.h:172
Gaudi::getInterfaceNames
std::vector< std::string > getInterfaceNames(Gaudi::interface_list< Is... >)
Definition: IInterface.h:162
Kernel.h
IInterface
Definition: IInterface.h:237
InterfaceID::versionMatch
constexpr bool versionMatch(const InterfaceID &iid) const
check compatibility.
Definition: IInterface.h:65
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:75
IInterface::getInterfaceNames
virtual std::vector< std::string > getInterfaceNames() const =0
Returns a vector of strings containing the names of all the implemented interfaces.
isValidInterface
bool isValidInterface(IFace *i)
Templated function that throws an exception if the version if the interface implemented by the object...
Definition: IInterface.h:334
implements.h
Gaudi::InterfaceId::iids
typename Gaudi::interface_list_append< typename interface_type::ext_iids, InterfaceId >::type iids
List of interfaces.
Definition: IInterface.h:191
Gaudi::majorMatch_t::operator()
constexpr bool operator()(InterfaceId< IFACE1, major1, minor1 >, InterfaceId< IFACE2, major2, minor2 >) const
Definition: IInterface.h:220
Gaudi::majorMatch_t::operator()
constexpr bool operator()(InterfaceId< IFACE, major, minor1 >, InterfaceId< IFACE, major, minor2 >) const
Definition: IInterface.h:224
InterfaceID::InterfaceID
InterfaceID(const char *name, unsigned long major, unsigned long minor=0)
constructor from components
Definition: IInterface.h:50
InterfaceID
Definition: IInterface.h:39
bug_38882.SUCCESS
SUCCESS
Definition: bug_38882.py:24
IInterface::refCount
virtual unsigned long refCount() const =0
Current reference count.
IInterface::release
virtual unsigned long release()=0
Release Interface instance.
compareOutputFiles.pp
pp
Definition: compareOutputFiles.py:516
ProduceConsume.key
key
Definition: ProduceConsume.py:81
Gaudi::Cast
TARGET * Cast(IInterface *i)
Cast a IInterface pointer to an IInterface specialization (TARGET).
Definition: IInterface.h:312
IInterface::addRef
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81