All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 <ostream>
9 
10 #ifndef __GCCXML__
11 // Meta Programming Library (MPL) headers
12 #include <boost/mpl/set.hpp>
13 #include <boost/mpl/insert.hpp>
14 #include <boost/mpl/fold.hpp>
15 #include <boost/mpl/for_each.hpp>
16 namespace mpl = boost::mpl;
17 #endif
18 
19 #include <typeinfo>
20 
21 #ifndef __GCCXML__
22 #define DeclareInterfaceID(name, major, minor) \
24  static const InterfaceID &interfaceID(){ return iid::interfaceID(); } \
25  typedef Gaudi::InterfaceId< name , major , minor > iid; \
26  typedef iid::iids::type ext_iids
27 #else
28 // GCCXML work-around
29 #define DeclareInterfaceID(name, major, minor) \
30  static const InterfaceID &interfaceID(){ static InterfaceID xx(0UL,0UL,0UL); return xx; }
31 #endif
32 
33 #ifndef __GCCXML__
34 #define DeclareInterfaceIDMultiBase(name, major, minor) \
36  static const InterfaceID &interfaceID(){ return iid::interfaceID(); } \
37  typedef Gaudi::InterfaceId< name , major , minor > iid
38 #else
39 // GCCXML work-around
40 #define DeclareInterfaceIDMultiBase(name, major, minor) \
41  static const InterfaceID &interfaceID(){ static InterfaceID xx(0UL,0UL,0UL); return xx; }
42 #endif
43 
56 public:
57 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NEW_INTERFACES)
58  InterfaceID( unsigned long lid ) : m_id( lid & 0xFFFF ),
60  m_major_ver( (lid & 0xFF000000)>>24 ),
61  m_minor_ver( (lid & 0xFF0000)>> 16 ) { }
62 #endif
63  InterfaceID( unsigned long id, unsigned long major, unsigned long minor = 0)
65  : m_id( id ), m_major_ver( major ), m_minor_ver( minor ) { }
67  InterfaceID( const char* name, unsigned long major, unsigned long minor = 0)
68  : m_id( hash32(name) ), m_major_ver( major ), m_minor_ver( minor ) { }
71 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NEW_INTERFACES)
72  operator unsigned long() const {
74  return (m_major_ver << 24) + (m_minor_ver << 16) + m_id;
75  }
76 #endif
77  unsigned long id() const { return m_id; }
80  unsigned long majorVersion() const { return m_major_ver; }
82  unsigned long minorVersion() const { return m_minor_ver; }
86  bool versionMatch( const InterfaceID& iid ) const {
87  return ( id() == iid.id() &&
88  majorVersion() == iid.majorVersion() &&
89  minorVersion() >= iid.minorVersion() );
90  }
92  bool fullMatch( const InterfaceID& iid ) const {
93  return ( id() == iid.id() &&
94  majorVersion() == iid.majorVersion() &&
95  minorVersion() == iid.minorVersion() );
96  }
98  bool operator == (const InterfaceID& iid ) const { return fullMatch(iid); }
100  static unsigned int hash32(const char* key) {
101  unsigned int hash;
102  const char* k;
103  for (hash = 0, k = key; *k; k++) {
104  hash += *k; hash += (hash << 10); hash ^= (hash >> 6);
105  }
106  hash += (hash << 3); hash ^= (hash >> 11); hash += (hash << 15);
107  return hash;
108  }
109 
110 private:
111  unsigned long m_id;
112  unsigned long m_major_ver;
113  unsigned long m_minor_ver;
114 };
115 
116 namespace Gaudi {
120  template <typename INTERFACE, unsigned long majVers, unsigned long minVers>
121  class InterfaceId {
122  public:
124  typedef INTERFACE iface_type;
125 
126 #ifndef __GCCXML__
127  typedef mpl::insert<typename iface_type::ext_iids, InterfaceId> iids;
129 #endif
130 
131  static inline std::string name() { return System::typeinfoName(typeid(INTERFACE)); }
132 
133  static inline unsigned long majorVersion(){return majVers;}
134  static inline unsigned long minorVersion(){return minVers;}
135 
136  static inline const std::type_info &TypeInfo() {
137 #ifndef __GCCXML__
138  return typeid(typename iids::type);
139 #else
140  return typeid(INTERFACE); // avoid compilation errors
141 #endif
142  }
143 
144  static const InterfaceID& interfaceID()
145  {
146  static InterfaceID s_iid(name().c_str(),majVers,minVers);
147  return s_iid;
148  }
149 
150  };
151 }
152 
161 public:
162 #ifndef __GCCXML__
165 
167  typedef mpl::set1<iid> ext_iids;
168 #endif
169 
171  static inline const InterfaceID &interfaceID(){
172 #ifndef __GCCXML__
173  return iid::interfaceID();
174 #else
175  // GCCXML work-around
176  static InterfaceID xx(0UL,0UL,0UL);
177  return xx;
178 #endif
179  }
180 
182  virtual void *i_cast(const InterfaceID &) const
183 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NEW_INTERFACES)
184  {return 0;}
185 #else
186  = 0;
187 #endif
188 
190  virtual std::vector<std::string> getInterfaceNames() const
191 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NEW_INTERFACES)
192  {return std::vector<std::string>();}
193 #else
194  = 0;
195 #endif
196 
198  virtual unsigned long addRef() = 0;
199 
201  virtual unsigned long release() = 0;
202 
204  virtual unsigned long refCount() const
205 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NEW_INTERFACES)
206  { IInterface* ths = const_cast<IInterface*>(this);
207  ths->addRef();
208  return ths->release(); } // new method, so we need a default implementation for v20 compatibility
209 #else
210  = 0;
211 #endif
212 
214  virtual StatusCode queryInterface(const InterfaceID &ti, void** pp) = 0;
215 
217  enum Status {
219  SUCCESS = 1,
225  LAST_ERROR
226  };
227 
229  virtual ~IInterface() {}
230 };
231 
232 namespace Gaudi {
234  template <typename TARGET>
235  TARGET* Cast(IInterface *i){
236  return reinterpret_cast<TARGET*>(i->i_cast(TARGET::interfaceID()));
237  }
240  template <typename TARGET>
241  const TARGET* Cast(const IInterface *i){
242  return reinterpret_cast<const TARGET*>(i->i_cast(TARGET::interfaceID()));
243  }
244 }
245 
256 template <class I>
257 bool isValidInterface( I* i) {
258  void* ii;
259  StatusCode sc = i->queryInterface( I::interfaceID(), &ii);
260  return sc.isSuccess();
261 }
262 
263 //#ifdef GAUDI_V20_COMPAT
265 inline std::ostream& operator << ( std::ostream& s, const InterfaceID& id) {
266  s << "IID_" << id.id();
267  return s;
268 }
269 //#endif
270 
281 template <class DEST,class SRC>
282 inline DEST** pp_cast(SRC** ptr){
283  return reinterpret_cast<DEST**>(ptr);
284 }
285 
287 #include "GaudiKernel/implements.h"
288 #include "GaudiKernel/extends.h"
289 
290 #endif // GAUDIKERNEL_IINTERFACE_H
mpl::set1< iid > ext_iids
Extra interfaces.
Definition: IInterface.h:167
std::ostream & operator<<(std::ostream &s, const InterfaceID &id)
ostream operator for InterfaceID. Needed by PluginSvc
Definition: IInterface.h:265
INTERFACE iface_type
Interface type.
Definition: IInterface.h:124
Requested interface is not available.
Definition: IInterface.h:221
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:298
unsigned long id() const
get the interface identifier
Definition: IInterface.h:78
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
static unsigned int hash32(const char *key)
one-at-time hash function
Definition: IInterface.h:100
unsigned long m_id
Definition: IInterface.h:111
bool isValidInterface(I *i)
Templated function that throws an exception if the version if the interface implemented by the object...
Definition: IInterface.h:257
unsigned long majorVersion() const
get the major version of the interface
Definition: IInterface.h:80
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:282
Gaudi::InterfaceId< IInterface, 0, 0 > iid
Interface ID.
Definition: IInterface.h:164
virtual void * i_cast(const InterfaceID &) const =0
main cast function
Interface ID class.
Definition: IInterface.h:55
static const InterfaceID & interfaceID()
Definition: IInterface.h:144
unsigned long m_minor_ver
Definition: IInterface.h:113
string type
Definition: gaudirun.py:126
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:30
unsigned long minorVersion() const
get the minor version of the interface
Definition: IInterface.h:82
virtual ~IInterface()
Virtual destructor.
Definition: IInterface.h:229
Definition of the basic interface.
Definition: IInterface.h:160
bool versionMatch(const InterfaceID &iid) const
check compatibility.
Definition: IInterface.h:86
~InterfaceID()
destructor
Definition: IInterface.h:70
Requested interface version is incompatible.
Definition: IInterface.h:223
unsigned long m_major_ver
Definition: IInterface.h:112
virtual unsigned long release()=0
Release Interface instance.
Status
Return status.
Definition: IInterface.h:217
static std::string name()
Definition: IInterface.h:131
string s
Definition: gaudirun.py:210
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
TARGET * Cast(IInterface *i)
Cast a IInterface pointer to an IInterface specialization (TARGET).
Definition: IInterface.h:235
static const std::type_info & TypeInfo()
Definition: IInterface.h:136
This is a number of static methods for bootstrapping the Gaudi framework.
Definition: Bootstrap.h:14
static unsigned long majorVersion()
Definition: IInterface.h:133
InterfaceID(const char *name, unsigned long major, unsigned long minor=0)
constructor from components
Definition: IInterface.h:67
#define GAUDI_API
Definition: Kernel.h:108
Class to handle automatically the versioning of the interfaces when they are inheriting from other in...
Definition: IInterface.h:121
list i
Definition: ana.py:128
mpl::insert< typename iface_type::ext_iids, InterfaceId > iids
List of interfaces.
Definition: IInterface.h:128
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:171
bool fullMatch(const InterfaceID &iid) const
check full compatibility.
Definition: IInterface.h:92
static unsigned long minorVersion()
Definition: IInterface.h:134