| # 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
SmartRef.h
Go to the documentation of this file.
1 // ====================================================================
2 // SmartRef.h
3 // --------------------------------------------------------------------
4 //
5 // Package : Kernel
6 //
7 // Author : Markus Frank
8 //
9 // ====================================================================
10 #ifndef KERNEL_SMARTREF_H
11 #define KERNEL_SMARTREF_H 1
12
13 // Include files
14 #include "GaudiKernel/SmartRefBase.h"
15 #include "GaudiKernel/ContainedObject.h"
16
17 #include <typeinfo>
18
19 // Forward declarations
20 template <class TYPE> class SmartRefArray;
21 template <class TYPE> class SmartRefList;
22 template <class TYPE> class SmartRefMap;
23
62 template <class TYPE> class SmartRef {
64 friend class SmartRefArray<TYPE>;
65 friend class SmartRefList<TYPE>;
66 friend class SmartRefMap<TYPE>;
67
68 public:
69 enum { VALID = StreamBuffer::VALID, INVALID = StreamBuffer::INVALID };
71 typedef TYPE entry_type;
72 protected:
73 SmartRefBase m_base;
75 mutable const TYPE* m_target;
76 protected:
77 public:
79 SmartRef() {
80 m_base.m_hintID = INVALID;
81 m_base.m_linkID = INVALID;
82 m_target = 0;
83 _setEnvironment(0, 0);
84 }
86 SmartRef(TYPE* pObject) {
87 m_base.m_hintID = INVALID;
88 m_base.m_linkID = INVALID;
89 m_target = pObject;
90 _setEnvironment(0, 0);
91 }
93 SmartRef(const TYPE* pObject) {
94 m_base.m_hintID = INVALID;
95 m_base.m_linkID = INVALID;
96 m_target = const_cast<TYPE*>(pObject);
97 _setEnvironment(0, 0);
98 }
100 SmartRef(const SmartRef& copy) {
101 m_base.m_hintID = copy.m_base.m_hintID;
102 m_base.m_linkID = copy.m_base.m_linkID;
103 m_target = copy.m_target;
104 _setEnvironment(copy.m_base.m_data, copy.m_base.m_contd);
105 }
107 SmartRef(long hint, long link, TYPE* obj = 0) {
108 m_base.m_hintID = hint;
109 m_base.m_linkID = link;
110 m_target = obj;
111 _setEnvironment(0, 0);
112 }
114 SmartRef(const ContainedObject* pObj, long hint, long link, TYPE* obj = 0) {
115 m_base.m_hintID = hint;
116 m_base.m_linkID = link;
117 m_target = obj;
118 const DataObject* src = (0==pObj) ? 0 : pObj->parent();
119 _setEnvironment(src, pObj);
120 }
122 SmartRef(const DataObject* pObj, long hint, long link, TYPE* obj = 0) {
123 m_base.m_hintID = hint;
124 m_base.m_linkID = link;
125 m_target = obj;
126 _setEnvironment(pObj, 0);
127 }
129 SmartRef(const DataObject* pObj, long hint, TYPE* obj = 0) {
130 m_base.m_hintID = hint;
131 m_base.m_linkID = INVALID;
132 m_target = obj;
133 _setEnvironment(pObj, 0);
134 }
136 //virtual ~SmartRef() {
137 //}
139 bool shouldFollowLink(const DataObject* /* typ */) const {
140 return (0 == m_target && m_base.m_hintID != INVALID );
141 }
143 bool shouldFollowLink(const ContainedObject* /* typ */) const {
144 return (0 == m_target && m_base.m_hintID != INVALID && m_base.m_linkID != INVALID );
145 }
147 long hintID() const {
148 return m_base.m_hintID;
149 }
151 long linkID() const {
152 return m_base.m_linkID;
153 }
155 void set(DataObject* pObj, long hint_id, long link_id) {
156 m_base.set(pObj, hint_id, link_id);
157 }
159 const std::type_info* type() const {
160 return &typeid(TYPE);
161 }
163 TYPE* data() {
164 return const_cast<TYPE*>(m_target);
165 }
166 const TYPE* data() const {
167 return m_target;
168 }
170 const TYPE* target() const;
172 TYPE* target();
174 inline const std::string &path() const { return m_base.path(); }
176 bool operator==(const SmartRef<TYPE>& c) const {
177 if ( 0 != m_target && 0 != c.m_target ) {
178 return m_target == c.m_target;
179 }
180 else if ( 0 == m_target && 0 == c.m_target ) {
181 return m_base.isEqual(m_target,c.m_base);
182 }
183 else if ( 0 != m_target && 0 == c.m_target ) {
184 return m_base.isEqualEx(m_target, c.m_base);
185 }
186 else if ( 0 == m_target && 0 != c.m_target ) {
187 return c.m_base.isEqualEx(c.m_target, m_base);
188 }
189 return false;
190 }
192 bool operator!=(const SmartRef<TYPE>& c) const {
193 return !(this->operator==(c));
194 }
196 const SmartRef<TYPE>& _setEnvironment(const DataObject* pObj, const ContainedObject* pContd) const {
197 m_base.m_data = pObj;
198 m_base.m_contd = pContd;
199 m_base.setObjectType(data());
200 return *this;
201 }
203 SmartRef<TYPE>& _setEnvironment(const DataObject* pObj, const ContainedObject* pContd) {
204 m_base.m_data = pObj;
205 m_base.m_contd = pContd;
206 m_base.setObjectType(data());
207 return *this;
208 }
210 SmartRef<TYPE>& operator() (ContainedObject* pObj) {
211 const DataObject* src = (0==pObj) ? 0 : pObj->parent();
212 return _setEnvironment(src, pObj);
213 }
215 const SmartRef<TYPE>& operator() (const ContainedObject* pObj) const {
216 const DataObject* src = (0==pObj) ? 0 : pObj->parent();
217 return _setEnvironment(src, pObj);
218 }
220 SmartRef<TYPE>& operator() (DataObject* pObj) {
221 return _setEnvironment(pObj,0);
222 }
224 const SmartRef<TYPE>& operator() (const DataObject* pObj) const {
225 return _setEnvironment(pObj,0);
226 }
228 SmartRef<TYPE>& operator=(const SmartRef<TYPE>& c) {
229 m_target = c.m_target;
230 m_base.m_hintID = c.m_base.m_hintID;
231 m_base.m_linkID = c.m_base.m_linkID;
232 return _setEnvironment(c.m_base.m_data, c.m_base.m_contd); }
234 SmartRef<TYPE>& operator=(TYPE* pObject) {
235 m_target = pObject;
236 m_base.m_hintID = INVALID;
237 m_base.m_linkID = INVALID;
238 return *this;
239 }
241 TYPE& operator*() { return *SmartRef<TYPE>::target(); }
243 const TYPE& operator*() const { return *SmartRef<TYPE>::target(); }
245 TYPE* operator->() { return SmartRef<TYPE>::target(); }
247 const TYPE* operator->() const { return SmartRef<TYPE>::target(); }
249 operator const TYPE* () const { return SmartRef<TYPE>::target(); }
251 operator TYPE* () { return SmartRef<TYPE>::target(); }
253 StreamBuffer& writeRef(StreamBuffer& s) const;
255 StreamBuffer& readRef(StreamBuffer& s);
257 // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
258 friend StreamBuffer& operator<< (StreamBuffer& _s, const SmartRef<TYPE>& ptr) {
259 return ptr.writeRef(_s);
260 }
262 // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
263 friend StreamBuffer& operator>> (StreamBuffer& _s, SmartRef<TYPE>& ptr) {
264 return ptr.readRef(_s);
265 }
266 };
267
268 //
269 // Inline function necessary to be outside the class definition.
270 // Mostly this stuff cannot go into the class definition because
271 // G++ has problems recognizing proper templates if they are not
272 // completely defined.
273 //
274 // M.Frank
275 //
276
278 template <class TYPE> inline
279 TYPE* SmartRef<TYPE>::target() {
280 if ( 0 == m_target ) {
281 m_target = dynamic_cast<const TYPE*>(m_base.accessData(m_target));
282 }
283 return const_cast<TYPE*>(m_target);
284 }
285
287 template <class TYPE> inline
288 const TYPE* SmartRef<TYPE>::target() const {
289 if ( 0 == m_target ) {
290 m_target = dynamic_cast<const TYPE*>(m_base.accessData(m_target));
291 }
292 return m_target;
293 }
294
296 template <class TYPE> inline
297 StreamBuffer& SmartRef<TYPE>::writeRef (StreamBuffer& s) const {
298 m_base.writeObject(m_target, s);
299 return s;
300 }
301
303 template <class TYPE> inline
304 StreamBuffer& SmartRef<TYPE>::readRef (StreamBuffer& s) {
305 m_target = dynamic_cast<TYPE*>( m_base.readObject(m_target, s) );
306 return s;
307 }
308
310 template <class TYPE> inline
311 bool operator == (const SmartRef<TYPE>& ref, int) {
312 const TYPE* obj = ref;
313 return obj == 0;
314 }
315
317 template <class TYPE> inline
318 bool operator == (int, const SmartRef<TYPE>& ref) {
319 const TYPE* obj = ref;
320 return obj == 0;
321 }
322
324 template <class TYPE> inline
325 bool operator != (const SmartRef<TYPE>& ref, int) {
326 const TYPE* obj = ref;
327 return obj != 0;
328 }
329
331 template <class TYPE> inline
332 bool operator != (int, const SmartRef<TYPE>& ref) {
333 const TYPE* obj = ref;
334 return obj != 0;
335 }
336
337 #endif // KERNEL_SMARTREF_H
338
339