Gaudi Framework, version v23r5

Home   Generated: Wed Nov 28 2012
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
IInterface.h
Go to the documentation of this file.
1 // $Id: IInterface.h,v 1.14 2008/01/29 08:28:14 marcocle Exp $
2 #ifndef GAUDIKERNEL_IINTERFACE_H
3 #define GAUDIKERNEL_IINTERFACE_H
4 
5 // Include files
6 #include "GaudiKernel/Kernel.h"
7 #include "GaudiKernel/System.h"
9 #include <ostream>
10 
11 #ifndef __GCCXML__
12 // Meta Programming Library (MPL) headers
13 #include <boost/mpl/set.hpp>
14 #include <boost/mpl/insert.hpp>
15 #include <boost/mpl/fold.hpp>
16 #include <boost/mpl/for_each.hpp>
17 namespace mpl = boost::mpl;
18 #endif
19 
20 #include <typeinfo>
21 
22 #ifndef __GCCXML__
23 
24 #define DeclareInterfaceID(name, major, minor) \
25  static const InterfaceID &interfaceID(){ return iid::interfaceID(); } \
26  typedef Gaudi::InterfaceId< name , major , minor > iid; \
27  typedef iid::iids::type ext_iids
28 #else
29 // GCCXML work-around
30 #define DeclareInterfaceID(name, major, minor) \
31  static const InterfaceID &interfaceID(){ static InterfaceID xx(0UL,0UL,0UL); return xx; }
32 #endif
33 
34 #ifndef __GCCXML__
35 
36 #define DeclareInterfaceIDMultiBase(name, major, minor) \
37  static const InterfaceID &interfaceID(){ return iid::interfaceID(); } \
38  typedef Gaudi::InterfaceId< name , major , minor > iid
39 #else
40 // GCCXML work-around
41 #define DeclareInterfaceIDMultiBase(name, major, minor) \
42  static const InterfaceID &interfaceID(){ static InterfaceID xx(0UL,0UL,0UL); return xx; }
43 #endif
44 
57 public:
58 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NEW_INTERFACES)
59 
60  InterfaceID( unsigned long lid ) : m_id( lid & 0xFFFF ),
61  m_major_ver( (lid & 0xFF000000)>>24 ),
62  m_minor_ver( (lid & 0xFF0000)>> 16 ) { }
63 #endif
64 
65  InterfaceID( unsigned long id, unsigned long major, unsigned long minor = 0)
66  : m_id( id ), m_major_ver( major ), m_minor_ver( minor ) { }
68  InterfaceID( const char* name, unsigned long major, unsigned long minor = 0)
69  : m_id( hash32(name) ), m_major_ver( major ), m_minor_ver( minor ) { }
72 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NEW_INTERFACES)
73 
74  operator unsigned long() const {
75  return (m_major_ver << 24) + (m_minor_ver << 16) + m_id;
76  }
77 #endif
78 
79  unsigned long id() const { return m_id; }
81  unsigned long majorVersion() const { return m_major_ver; }
83  unsigned long minorVersion() const { return m_minor_ver; }
87  bool versionMatch( const InterfaceID& iid ) const {
88  return ( id() == iid.id() &&
89  majorVersion() == iid.majorVersion() &&
90  minorVersion() >= iid.minorVersion() );
91  }
93  bool fullMatch( const InterfaceID& iid ) const {
94  return ( id() == iid.id() &&
95  majorVersion() == iid.majorVersion() &&
96  minorVersion() == iid.minorVersion() );
97  }
99  bool operator == (const InterfaceID& iid ) const { return fullMatch(iid); }
101  static unsigned int hash32(const char* key) {
102  unsigned int hash;
103  const char* k;
104  for (hash = 0, k = key; *k; k++) {
105  hash += *k; hash += (hash << 10); hash ^= (hash >> 6);
106  }
107  hash += (hash << 3); hash ^= (hash >> 11); hash += (hash << 15);
108  return hash;
109  }
110 
111 private:
112  unsigned long m_id;
113  unsigned long m_major_ver;
114  unsigned long m_minor_ver;
115 };
116 
117 namespace Gaudi {
121  template <typename INTERFACE, unsigned long majVers, unsigned long minVers>
122  class InterfaceId {
123  public:
125  typedef INTERFACE iface_type;
126 
127 #ifndef __GCCXML__
128 
129  typedef mpl::insert<typename iface_type::ext_iids, InterfaceId> iids;
130 #endif
131 
132  static inline std::string name() { return System::typeinfoName(typeid(INTERFACE)); }
133 
134  static inline unsigned long majorVersion(){return majVers;}
135  static inline unsigned long minorVersion(){return minVers;}
136 
137  static inline const std::type_info &TypeInfo() {
138 #ifndef __GCCXML__
139  return typeid(typename iids::type);
140 #else
141  return typeid(INTERFACE); // avoid compilation errors
142 #endif
143  }
144 
145  static const InterfaceID& interfaceID()
146  {
147  static InterfaceID s_iid(name().c_str(),majVers,minVers);
148  return s_iid;
149  }
150 
151  };
152 }
153 
162 public:
163 #ifndef __GCCXML__
164 
165  typedef Gaudi::InterfaceId<IInterface,0,0> iid;
166 
168  typedef mpl::set1<iid> ext_iids;
169 #endif
170 
172  static inline const InterfaceID &interfaceID(){
173 #ifndef __GCCXML__
174  return iid::interfaceID();
175 #else
176  // GCCXML work-around
177  static InterfaceID xx(0UL,0UL,0UL);
178  return xx;
179 #endif
180  }
181 
183  virtual void *i_cast(const InterfaceID &) const
184 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NEW_INTERFACES)
185  {return 0;}
186 #else
187  = 0;
188 #endif
189 
191  virtual std::vector<std::string> getInterfaceNames() const
192 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NEW_INTERFACES)
193  {return std::vector<std::string>();}
194 #else
195  = 0;
196 #endif
197 
199  virtual unsigned long addRef() = 0;
200 
202  virtual unsigned long release() = 0;
203 
205  virtual unsigned long refCount() const
206 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NEW_INTERFACES)
207  { IInterface* ths = const_cast<IInterface*>(this);
208  ths->addRef();
209  return ths->release(); } // new method, so we need a default implementation for v20 compatibility
210 #else
211  = 0;
212 #endif
213 
215  virtual StatusCode queryInterface(const InterfaceID &ti, void** pp) = 0;
216 
218  enum Status {
220  SUCCESS = 1,
226  LAST_ERROR
227  };
228 
230  virtual ~IInterface() {}
231 };
232 
233 namespace Gaudi {
235  template <typename TARGET>
236  TARGET* Cast(IInterface *i){
237  return reinterpret_cast<TARGET*>(i->i_cast(TARGET::interfaceID()));
238  }
241  template <typename TARGET>
242  const TARGET* Cast(const IInterface *i){
243  return reinterpret_cast<const TARGET*>(i->i_cast(TARGET::interfaceID()));
244  }
245 }
246 
257 template <class I>
258 bool isValidInterface( I* i) {
259  void* ii;
260  StatusCode sc = i->queryInterface( I::interfaceID(), &ii);
261  return sc.isSuccess();
262 }
263 
264 //#ifdef GAUDI_V20_COMPAT
267  s << "IID_" << id.id();
268  return s;
269 }
270 //#endif
271 
282 template <class DEST,class SRC>
283 inline DEST** pp_cast(SRC** ptr){
284  return reinterpret_cast<DEST**>(ptr);
285 }
286 
288 #include "GaudiKernel/implements.h"
289 #include "GaudiKernel/extends.h"
290 
291 #endif // GAUDIKERNEL_IINTERFACE_H

Generated at Wed Nov 28 2012 12:17:13 for Gaudi Framework, version v23r5 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004