The Gaudi Framework  master (42b00024)
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-2025 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:
42  constexpr InterfaceID( unsigned long id, unsigned long major, unsigned long minor = 0 )
43  : m_id( id ), m_major_ver( major ), m_minor_ver( minor ) {}
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 ) {}
48  constexpr unsigned long id() const { return m_id; }
50  constexpr unsigned long majorVersion() const { return m_major_ver; }
52  constexpr unsigned long minorVersion() const { return m_minor_ver; }
56  constexpr bool versionMatch( const InterfaceID& iid ) const {
57  return ( id() == iid.id() && majorVersion() == iid.majorVersion() && minorVersion() >= iid.minorVersion() );
58  }
60  constexpr bool fullMatch( const InterfaceID& iid ) const {
61  return ( id() == iid.id() && majorVersion() == iid.majorVersion() && minorVersion() == iid.minorVersion() );
62  }
64  constexpr bool operator==( const InterfaceID& iid ) const { return fullMatch( iid ); }
66  static unsigned int hash32( const char* key ) {
67  unsigned int hash = 0;
68  for ( const char* k = key; *k; ++k ) {
69  hash += *k;
70  hash += ( hash << 10 );
71  hash ^= ( hash >> 6 );
72  }
73  hash += ( hash << 3 );
74  hash ^= ( hash >> 11 );
75  hash += ( hash << 15 );
76  return hash;
77  }
78 
80  friend std::ostream& operator<<( std::ostream& s, const InterfaceID& id ) {
81  s << "IID_" << id.id();
82  return s;
83  }
84 
85 private:
86  unsigned long m_id;
87  unsigned long m_major_ver;
88  unsigned long m_minor_ver;
89 };
90 
91 namespace Gaudi {
92 
93  template <typename... I>
94  struct interface_list {};
95 
96  namespace meta {
97  // identity T -> type = T; note that id_<T> is a complete type, even if T is not
98  template <typename T>
99  struct id_ {
100  using type = T;
101  };
102 
103  namespace detail {
104  template <typename... Is>
105  struct inherit_from : Is... {};
106 
107  template <typename List, typename I>
108  struct append1 {};
109 
110  // interpose an id_<I> as id_<I> is a complete type, even if I is not... and we need complete
111  // types to inherit from
112  template <typename... Is, typename I>
113  struct append1<interface_list<Is...>, I>
114  : id_<std::conditional_t<std::is_base_of_v<id_<I>, inherit_from<id_<Is>...>>, interface_list<Is...>,
115  interface_list<Is..., I>>> {};
116 
117  template <typename, typename>
118  struct appendN {};
119 
120  template <typename State>
121  struct appendN<interface_list<>, State> : id_<State> {};
122 
123  template <typename... Is, typename I, typename List>
124  struct appendN<interface_list<I, Is...>, List> : appendN<interface_list<Is...>, typename append1<List, I>::type> {
125  };
126  } // namespace detail
127 
128  template <typename... Is>
130  } // namespace meta
131 
132  // interface_list concatenation
133  template <typename... I>
135 
136  // identity
137  template <typename... I>
139 
140  // binary op
141  template <typename... I1, typename... I2>
143  };
144 
145  // induction of binary op
146  template <typename... I1, typename... I2, typename... Others>
147  struct interface_list_cat<interface_list<I1...>, interface_list<I2...>, Others...>
148  : interface_list_cat<interface_list<I1..., I2...>, Others...> {};
149 
150  // append is a special case of concatenation...
151  template <typename... I>
153 
154  template <typename... Is, typename I>
156  : interface_list_cat<interface_list<Is...>, interface_list<I>> {};
157 
158  template <typename... Is>
159  std::vector<std::string> getInterfaceNames( Gaudi::interface_list<Is...> ) {
160  return { Is::name()... };
161  }
162 
163  template <typename... Is, typename P>
164  void* iid_cast( const InterfaceID& tid, Gaudi::interface_list<Is...>, P* ptr ) {
165  const void* target = nullptr;
166  ( ( tid.versionMatch( Is::interfaceID() ) &&
167  ( target = static_cast<typename Is::interface_type const*>( ptr ), true ) ) ||
168  ... );
169  return const_cast<void*>( target );
170  }
171 
175  template <typename INTERFACE, unsigned long majVers, unsigned long minVers>
176  struct InterfaceId final {
178  using interface_type = INTERFACE;
181 
182  static inline std::string name() { return System::typeinfoName( typeid( INTERFACE ) ); }
183  static constexpr unsigned long majorVersion() { return majVers; }
184  static constexpr unsigned long minorVersion() { return minVers; }
185 
186  static inline const std::type_info& TypeInfo() { return typeid( typename iids::type ); }
187 
188  static inline const InterfaceID& interfaceID() {
189  static const InterfaceID s_iid( name().c_str(), majVers, minVers );
190  return s_iid;
191  }
192  };
193 
194  constexpr struct fullMatch_t {
195  template <typename IFACE1, typename IFACE2, unsigned long major1, unsigned long major2, unsigned long minor1,
196  unsigned long minor2>
198  return false;
199  }
200  template <typename IFACE, unsigned long major, unsigned long minor>
202  return true;
203  }
205 
206  constexpr struct majorMatch_t {
207  template <typename IFACE1, typename IFACE2, unsigned long major1, unsigned long major2, unsigned long minor1,
208  unsigned long minor2>
210  return false;
211  }
212  template <typename IFACE, unsigned long major, unsigned long minor1, unsigned long minor2>
214  return true;
215  }
217 } // namespace Gaudi
218 
227 public:
230 
233 
235  static inline const InterfaceID& interfaceID() { return iid::interfaceID(); }
236 
238  virtual void* i_cast( const InterfaceID& ) const = 0;
239 
241  virtual std::vector<std::string> getInterfaceNames() const = 0;
242 
244  virtual unsigned long addRef() = 0;
245 
247  virtual unsigned long release() = 0;
248 
250  virtual unsigned long refCount() const = 0;
251 
253  virtual StatusCode queryInterface( const InterfaceID& ti, void** pp ) = 0;
254 
256  enum class Status : StatusCode::code_t {
258  FAILURE = 0,
260  SUCCESS = 1,
262  NO_INTERFACE,
264  VERSMISMATCH,
266  LAST_ERROR
267  };
268 
270  virtual ~IInterface() = default;
271 };
272 
274 
275 namespace Gaudi {
277  template <typename TARGET>
278  TARGET* Cast( IInterface* i ) {
279  return reinterpret_cast<TARGET*>( i->i_cast( TARGET::interfaceID() ) );
280  }
283  template <typename TARGET>
284  const TARGET* Cast( const IInterface* i ) {
285  return reinterpret_cast<const TARGET*>( i->i_cast( TARGET::interfaceID() ) );
286  }
287 } // namespace Gaudi
288 
299 template <class IFace>
300 bool isValidInterface( IFace* i ) {
301  void* ii = nullptr;
302  return i->queryInterface( IFace::interfaceID(), &ii ).isSuccess();
303 }
304 
315 template <class DEST, class SRC>
316 inline DEST** pp_cast( SRC** ptr ) {
317  return reinterpret_cast<DEST**>( ptr );
318 }
319 
321 #include <GaudiKernel/extends.h>
322 #include <GaudiKernel/implements.h>
323 
324 #endif // GAUDIKERNEL_IINTERFACE_H
GaudiPython.Bindings.FAILURE
FAILURE
Definition: Bindings.py:84
extend_interfaces.h
Gaudi::InterfaceId
Class to handle automatically the versioning of the interfaces when they are inheriting from other in...
Definition: IInterface.h:176
InterfaceID::fullMatch
constexpr bool fullMatch(const InterfaceID &iid) const
check full compatibility.
Definition: IInterface.h:60
Gaudi::fullMatch
constexpr struct Gaudi::fullMatch_t fullMatch
Gaudi::InterfaceId::majorVersion
static constexpr unsigned long majorVersion()
Definition: IInterface.h:183
System.h
gaudirun.s
string s
Definition: gaudirun.py:346
Gaudi::meta::detail::appendN
Definition: IInterface.h:118
InterfaceID::InterfaceID
constexpr InterfaceID(unsigned long id, unsigned long major, unsigned long minor=0)
constructor from components
Definition: IInterface.h:42
InterfaceID::m_id
unsigned long m_id
Definition: IInterface.h:86
Gaudi::fullMatch_t
Definition: IInterface.h:194
detail
Definition: HistogramSvc.h:46
Gaudi::interface_list_cat
Definition: IInterface.h:134
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:184
extends.h
StatusCode::code_t
unsigned long code_t
type of StatusCode value
Definition: StatusCode.h:67
Gaudi::interface_list_append
Definition: IInterface.h:152
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:315
GaudiPartProp.tests.id
id
Definition: tests.py:111
StatusCode.h
Gaudi::meta::detail::inherit_from
Definition: IInterface.h:105
InterfaceID::id
constexpr unsigned long id() const
get the interface identifier
Definition: IInterface.h:48
compareOutputFiles.target
target
Definition: compareOutputFiles.py:489
IInterface::Status
Status
Return status.
Definition: IInterface.h:256
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:188
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:235
InterfaceID::m_minor_ver
unsigned long m_minor_ver
Definition: IInterface.h:88
InterfaceID::majorVersion
constexpr unsigned long majorVersion() const
get the major version of the interface
Definition: IInterface.h:50
Gaudi::InterfaceId::interface_type
INTERFACE interface_type
interface type
Definition: IInterface.h:178
InterfaceID::m_major_ver
unsigned long m_major_ver
Definition: IInterface.h:87
InterfaceID::operator==
constexpr bool operator==(const InterfaceID &iid) const
compare operator
Definition: IInterface.h:64
Gaudi::meta::id_< State >::type
State type
Definition: IInterface.h:100
Gaudi::meta::id_
Definition: IInterface.h:99
Gaudi::fullMatch_t::operator()
constexpr bool operator()(InterfaceId< IFACE, major, minor >, InterfaceId< IFACE, major, minor >) const
Definition: IInterface.h:201
Gaudi::InterfaceId::name
static std::string name()
Definition: IInterface.h:182
GaudiPython.Bindings.SUCCESS
SUCCESS
Definition: Bindings.py:83
Gaudi::interface_list
Definition: IInterface.h:94
Gaudi::majorMatch
constexpr struct Gaudi::majorMatch_t majorMatch
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
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:316
InterfaceID::minorVersion
constexpr unsigned long minorVersion() const
get the minor version of the interface
Definition: IInterface.h:52
Gaudi::InterfaceId::TypeInfo
static const std::type_info & TypeInfo()
Definition: IInterface.h:186
gaudirun.type
type
Definition: gaudirun.py:160
Gaudi::majorMatch_t
Definition: IInterface.h:206
Gaudi::fullMatch_t::operator()
constexpr bool operator()(InterfaceId< IFACE1, major1, minor1 >, InterfaceId< IFACE2, major2, minor2 >) const
Definition: IInterface.h:197
STATUSCODE_ENUM_DECL
#define STATUSCODE_ENUM_DECL(ENUM)
Declare an enum to be used as StatusCode value.
Definition: StatusCode.h:287
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
Gaudi::meta::detail::append1
Definition: IInterface.h:108
Gaudi::iid_cast
void * iid_cast(const InterfaceID &tid, Gaudi::interface_list< Is... >, P *ptr)
Definition: IInterface.h:164
Gaudi::getInterfaceNames
std::vector< std::string > getInterfaceNames(Gaudi::interface_list< Is... >)
Definition: IInterface.h:159
Kernel.h
IInterface
Definition: IInterface.h:226
InterfaceID::versionMatch
constexpr bool versionMatch(const InterfaceID &iid) const
check compatibility.
Definition: IInterface.h: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:66
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:300
implements.h
Gaudi::InterfaceId::iids
typename Gaudi::interface_list_append< typename interface_type::ext_iids, InterfaceId >::type iids
List of interfaces.
Definition: IInterface.h:180
Gaudi::majorMatch_t::operator()
constexpr bool operator()(InterfaceId< IFACE1, major1, minor1 >, InterfaceId< IFACE2, major2, minor2 >) const
Definition: IInterface.h:209
Gaudi::majorMatch_t::operator()
constexpr bool operator()(InterfaceId< IFACE, major, minor1 >, InterfaceId< IFACE, major, minor2 >) const
Definition: IInterface.h:213
InterfaceID::InterfaceID
InterfaceID(const char *name, unsigned long major, unsigned long minor=0)
constructor from components
Definition: IInterface.h:45
InterfaceID
Definition: IInterface.h:39
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:507
InterfaceID::operator<<
friend std::ostream & operator<<(std::ostream &s, const InterfaceID &id)
ostream operator for InterfaceID. Needed by PluginSvc
Definition: IInterface.h:80
ProduceConsume.key
key
Definition: ProduceConsume.py:84
Gaudi::Cast
TARGET * Cast(IInterface *i)
Cast a IInterface pointer to an IInterface specialization (TARGET).
Definition: IInterface.h:278
IInterface::addRef
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:84