| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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"
7 #include "GaudiKernel/StatusCode.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
23 #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
35 #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
55 class GAUDI_API InterfaceID {
56 public:
57 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NEW_INTERFACES)
58
59 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
64 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 ) { }
70 ~InterfaceID() { }
71 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NEW_INTERFACES)
72
73 operator unsigned long() const {
74 return (m_major_ver << 24) + (m_minor_ver << 16) + m_id;
75 }
76 #endif
77
78 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
128 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
160 class GAUDI_API IInterface {
161 public:
162 #ifndef __GCCXML__
163
164 typedef Gaudi::InterfaceId<IInterface,0,0> iid;
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,
221 NO_INTERFACE,
223 VERSMISMATCH,
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
286 #include "GaudiKernel/extend_interfaces.h"
287 #include "GaudiKernel/implements.h"
288 #include "GaudiKernel/extends.h"
289
290 #endif // GAUDIKERNEL_IINTERFACE_H