| The Gaudi Framework v28r2p1 (f1a77ff4)

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

- File List
- File Members

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 = nullptr;
76 public:
78 SmartRef() {
79 m_base.m_hintID = INVALID;
80 m_base.m_linkID = INVALID;
81 _setEnvironment(0, 0);
82 }
84 SmartRef(TYPE* pObject) {
85 m_base.m_hintID = INVALID;
86 m_base.m_linkID = INVALID;
87 m_target = pObject;
88 _setEnvironment(0, 0);
89 }
91 SmartRef(const TYPE* pObject) {
92 m_base.m_hintID = INVALID;
93 m_base.m_linkID = INVALID;
94 m_target = const_cast<TYPE*>(pObject);
95 _setEnvironment(0, 0);
96 }
98 SmartRef(const SmartRef& copy) {
99 m_base.m_hintID = copy.m_base.m_hintID;
100 m_base.m_linkID = copy.m_base.m_linkID;
101 m_target = copy.m_target;
102 _setEnvironment(copy.m_base.m_data, copy.m_base.m_contd);
103 }
105 SmartRef(long hint, long link, TYPE* obj = nullptr) {
106 m_base.m_hintID = hint;
107 m_base.m_linkID = link;
108 m_target = obj;
109 _setEnvironment(0, 0);
110 }
112 SmartRef(const ContainedObject* pObj, long hint, long link, TYPE* obj = nullptr) {
113 m_base.m_hintID = hint;
114 m_base.m_linkID = link;
115 m_target = obj;
116 const DataObject* src = (0==pObj) ? 0 : pObj->parent();
117 _setEnvironment(src, pObj);
118 }
120 SmartRef(const DataObject* pObj, long hint, long link, TYPE* obj = nullptr) {
121 m_base.m_hintID = hint;
122 m_base.m_linkID = link;
123 m_target = obj;
124 _setEnvironment(pObj, 0);
125 }
127 SmartRef(const DataObject* pObj, long hint, TYPE* obj = nullptr) {
128 m_base.m_hintID = hint;
129 m_base.m_linkID = INVALID;
130 m_target = obj;
131 _setEnvironment(pObj, 0);
132 }
134 //virtual ~SmartRef() {
135 //}
137 bool shouldFollowLink(const DataObject* /* typ */) const {
138 return (!m_target && m_base.m_hintID != INVALID );
139 }
141 bool shouldFollowLink(const ContainedObject* /* typ */) const {
142 return (!m_target && m_base.m_hintID != INVALID && m_base.m_linkID != INVALID );
143 }
145 long hintID() const {
146 return m_base.m_hintID;
147 }
149 long linkID() const {
150 return m_base.m_linkID;
151 }
153 void set(DataObject* pObj, long hint_id, long link_id) {
154 m_base.set(pObj, hint_id, link_id);
155 }
157 const std::type_info* type() const {
158 return &typeid(TYPE);
159 }
161 TYPE* data() {
162 return const_cast<TYPE*>(m_target);
163 }
164 const TYPE* data() const {
165 return m_target;
166 }
168 const TYPE* target() const;
170 TYPE* target();
172 inline const std::string &path() const { return m_base.path(); }
174 bool operator==(const SmartRef<TYPE>& c) const {
175 if ( m_target && c.m_target ) return m_target == c.m_target;
176 if ( !m_target && !c.m_target ) return m_base.isEqual(m_target,c.m_base);
177 if ( m_target && !c.m_target ) return m_base.isEqualEx(m_target, c.m_base);
178 if ( !m_target && c.m_target ) return c.m_base.isEqualEx(c.m_target, m_base);
179 return false;
180 }
182 bool operator!=(const SmartRef<TYPE>& c) const {
183 return !(this->operator==(c));
184 }
186 const SmartRef<TYPE>& _setEnvironment(const DataObject* pObj, const ContainedObject* pContd) const {
187 m_base.m_data = pObj;
188 m_base.m_contd = pContd;
189 m_base.setObjectType(data());
190 return *this;
191 }
193 SmartRef<TYPE>& _setEnvironment(const DataObject* pObj, const ContainedObject* pContd) {
194 m_base.m_data = pObj;
195 m_base.m_contd = pContd;
196 m_base.setObjectType(data());
197 return *this;
198 }
200 SmartRef<TYPE>& operator() (ContainedObject* pObj) {
201 const DataObject* src = ( pObj ? pObj->parent() : nullptr );
202 return _setEnvironment(src, pObj);
203 }
205 const SmartRef<TYPE>& operator() (const ContainedObject* pObj) const {
206 const DataObject* src = ( pObj ? pObj->parent() : nullptr );
207 return _setEnvironment(src, pObj);
208 }
210 SmartRef<TYPE>& operator() (DataObject* pObj) {
211 return _setEnvironment(pObj,nullptr);
212 }
214 const SmartRef<TYPE>& operator() (const DataObject* pObj) const {
215 return _setEnvironment(pObj,nullptr);
216 }
218 SmartRef<TYPE>& operator=(const SmartRef<TYPE>& c) {
219 m_target = c.m_target;
220 m_base.m_hintID = c.m_base.m_hintID;
221 m_base.m_linkID = c.m_base.m_linkID;
222 return _setEnvironment(c.m_base.m_data, c.m_base.m_contd); }
224 SmartRef<TYPE>& operator=(TYPE* pObject) {
225 m_target = pObject;
226 m_base.m_hintID = INVALID;
227 m_base.m_linkID = INVALID;
228 return *this;
229 }
231 TYPE& operator*() { return *SmartRef<TYPE>::target(); }
233 const TYPE& operator*() const { return *SmartRef<TYPE>::target(); }
235 TYPE* operator->() { return SmartRef<TYPE>::target(); }
237 const TYPE* operator->() const { return SmartRef<TYPE>::target(); }
239 operator const TYPE* () const { return SmartRef<TYPE>::target(); }
241 operator TYPE* () { return SmartRef<TYPE>::target(); }
243 StreamBuffer& writeRef(StreamBuffer& s) const;
245 StreamBuffer& readRef(StreamBuffer& s);
247 // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
248 friend StreamBuffer& operator<< (StreamBuffer& _s, const SmartRef<TYPE>& ptr) {
249 return ptr.writeRef(_s);
250 }
252 // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
253 friend StreamBuffer& operator>> (StreamBuffer& _s, SmartRef<TYPE>& ptr) {
254 return ptr.readRef(_s);
255 }
256 };
257
258 //
259 // Inline function necessary to be outside the class definition.
260 // Mostly this stuff cannot go into the class definition because
261 // G++ has problems recognizing proper templates if they are not
262 // completely defined.
263 //
264 // M.Frank
265 //
266
268 template <class TYPE> inline
269 TYPE* SmartRef<TYPE>::target() {
270 if ( !m_target ) {
271 m_target = dynamic_cast<const TYPE*>(m_base.accessData(m_target));
272 }
273 return const_cast<TYPE*>(m_target);
274 }
275
277 template <class TYPE> inline
278 const TYPE* SmartRef<TYPE>::target() const {
279 if ( !m_target ) {
280 m_target = dynamic_cast<const TYPE*>(m_base.accessData(m_target));
281 }
282 return m_target;
283 }
284
286 template <class TYPE> inline
287 StreamBuffer& SmartRef<TYPE>::writeRef (StreamBuffer& s) const {
288 m_base.writeObject(m_target, s);
289 return s;
290 }
291
293 template <class TYPE> inline
294 StreamBuffer& SmartRef<TYPE>::readRef (StreamBuffer& s) {
295 m_target = dynamic_cast<TYPE*>( m_base.readObject(m_target, s) );
296 return s;
297 }
298
300 template <class TYPE> inline
301 bool operator == (const SmartRef<TYPE>& ref, int) {
302 return ref.target() == nullptr;
303 }
304
306 template <class TYPE> inline
307 bool operator == (int, const SmartRef<TYPE>& ref) {
308 return ref.target() == nullptr;
309 }
310
312 template <class TYPE> inline
313 bool operator != (const SmartRef<TYPE>& ref, int) {
314 return ref.target() != nullptr;
315 }
316
318 template <class TYPE> inline
319 bool operator != (int, const SmartRef<TYPE>& ref) {
320 return ref.target() != nullptr;
321 }
322
323 #endif // KERNEL_SMARTREF_H
SmartRef::SmartRef
SmartRef(const DataObject *pObj, long hint, TYPE *obj=nullptr)
Constructor for references to DataObjects passing environment.
Definition: SmartRef.h:127
SmartRef::operator=
SmartRef< TYPE > & operator=(const SmartRef< TYPE > &c)
Assignment.
Definition: SmartRef.h:218
SmartRefList
Definition: SmartRef.h:21
SmartRef::entry_type
TYPE entry_type
Entry type definition.
Definition: SmartRef.h:71
SmartRef::SmartRef
SmartRef()
Standard Constructor.
Definition: SmartRef.h:78
SmartRef::shouldFollowLink
bool shouldFollowLink(const DataObject *) const
Standard destructor.
Definition: SmartRef.h:137
SmartRefBase::isEqual
bool isEqual(const ContainedObject *, const SmartRefBase &c) const
Equality operator for ContainedObject like references.
Definition: SmartRefBase.h:72
gaudirun.c
c
Definition: gaudirun.py:391
SmartRefBase::isEqualEx
bool isEqualEx(const DataObject *pObj, const SmartRefBase &c) const
Extended equality check.
Definition: SmartRefBase.cpp:69
SmartRef::shouldFollowLink
bool shouldFollowLink(const ContainedObject *) const
Check if link should be followed: link must be valid and object not yet loaded.
Definition: SmartRef.h:141
SmartRefBase::accessData
const ContainedObject * accessData(const ContainedObject *typ) const
Load on demand of ContainedObject like references.
Definition: SmartRefBase.cpp:33
SmartRef::operator->
const TYPE * operator->() const
Dereference operator to const object.
Definition: SmartRef.h:237
SmartRefBase::m_data
const DataObject * m_data
Object data: Pointer to the identifiable object the link originates.
Definition: SmartRefBase.h:54
SmartRef::path
const std::string & path() const
Return the path of the linked object inside the data store.
Definition: SmartRef.h:172
StreamBuffer
The stream buffer is a small object collecting object data.
Definition: StreamBuffer.h:41
SmartRefBase::m_hintID
long m_hintID
Object data: ID of the link hint to the identifiable object.
Definition: SmartRefBase.h:50
SmartRef::target
const TYPE * target() const
Access to the object.
Definition: SmartRef.h:278
SmartRefBase::writeObject
void writeObject(const DataObject *pObject, StreamBuffer &s) const
Output streamer for DataObject like references.
Definition: SmartRefBase.h:81
SmartRefBase::setObjectType
void setObjectType(const ContainedObject *) const
Definition: SmartRefBase.h:100
SmartRef::SmartRef
SmartRef(const DataObject *pObj, long hint, long link, TYPE *obj=nullptr)
Constructor for references to contained objects passing environment.
Definition: SmartRef.h:120
SmartRef::SmartRef
SmartRef(const SmartRef &copy)
Copy Constructor.
Definition: SmartRef.h:98
SmartRef::type
const std::type_info * type() const
Access to embedded type.
Definition: SmartRef.h:157
SmartRef::linkID
long linkID() const
Access link id:
Definition: SmartRef.h:149
SmartRef
Kernel objects: SmartRef.
Definition: SmartRef.h:62
SmartRefBase
User example objects: SmartRefBase.
Definition: SmartRefBase.h:47
SmartRefBase.h
SmartRef::_setEnvironment
SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd)
Set the environment (CONST)
Definition: SmartRef.h:193
SmartRefMap
Definition: SmartRef.h:22
ContainedObject::parent
const ObjectContainerBase * parent() const
Access to parent object.
Definition: ContainedObject.h:49
std::type_info
SmartRef::VALID
Definition: SmartRef.h:69
SmartRef::operator!=
bool operator!=(const SmartRef< TYPE > &c) const
NON-Equality operator.
Definition: SmartRef.h:182
std::string
STL class.
compareRootHistos.ref
string ref
Definition: compareRootHistos.py:14
SmartRefBase::m_contd
const ContainedObject * m_contd
Object data: Pointer to the Contained object (if applicable)
Definition: SmartRefBase.h:56
SmartRef::data
TYPE * data()
Access to raw data pointer.
Definition: SmartRef.h:161
SmartRefBase::path
const std::string & path() const
Shortcut to access the path to the linked object.
Definition: SmartRefBase.cpp:91
SmartRef::SmartRef
SmartRef(const ContainedObject *pObj, long hint, long link, TYPE *obj=nullptr)
Constructor for references to contained objects passing environment.
Definition: SmartRef.h:112
SmartRef::operator==
bool operator==(const SmartRef< TYPE > &c) const
Equality operator.
Definition: SmartRef.h:174
SmartRef::operator()
SmartRef< TYPE > & operator()(ContainedObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:200
SmartRef::m_base
SmartRefBase m_base
Definition: SmartRef.h:73
SmartRef::operator=
SmartRef< TYPE > & operator=(TYPE *pObject)
Assignment.
Definition: SmartRef.h:224
SmartRef::data
const TYPE * data() const
Definition: SmartRef.h:164
SmartRef::m_target
const TYPE * m_target
Pointer to target data object.
Definition: SmartRef.h:75
SmartRefBase::m_linkID
long m_linkID
Object data: ID of the object within the identifiable container (if any)
Definition: SmartRefBase.h:52
SmartRef::SmartRef
SmartRef(long hint, long link, TYPE *obj=nullptr)
Constructor.
Definition: SmartRef.h:105
ContainedObject
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
Definition: ContainedObject.h:29
SmartRefArray
Definition: SmartRef.h:20
SmartRef::hintID
long hintID() const
Access hint id:
Definition: SmartRef.h:145
SmartRef::operator*
TYPE & operator*()
Dereference operator.
Definition: SmartRef.h:231
gaudirun.s
string s
Definition: gaudirun.py:245
SmartRef::operator->
TYPE * operator->()
Dereference operator.
Definition: SmartRef.h:235
SmartRefBase::readObject
DataObject * readObject(const DataObject *, StreamBuffer &s) const
Input streamer for DataObject like references.
Definition: SmartRefBase.h:89
ContainedObject.h
StreamBuffer::VALID
Definition: StreamBuffer.h:117
StreamBuffer::INVALID
Definition: StreamBuffer.h:117
SmartRefBase::set
void set(DataObject *pObj, long hint_id, long link_id)
Setup smart reference when reading. Must be allowed from external sources.
Definition: SmartRefBase.cpp:26
SmartRef::writeRef
StreamBuffer & writeRef(StreamBuffer &s) const
Write the reference to the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:287
DataObject
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
SmartRef::operator>>
friend StreamBuffer & operator>>(StreamBuffer &_s, SmartRef< TYPE > &ptr)
Input Streamer operator.
Definition: SmartRef.h:253
SmartRef::readRef
StreamBuffer & readRef(StreamBuffer &s)
Read the reference from the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:294
SmartRef::operator*
const TYPE & operator*() const
Dereference operator.
Definition: SmartRef.h:233
SmartRef::_setEnvironment
const SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd) const
Set the environment (CONST)
Definition: SmartRef.h:186
SmartRef::SmartRef
SmartRef(const TYPE *pObject)
Standard Constructor with initialisation from const object.
Definition: SmartRef.h:91
SmartRef::SmartRef
SmartRef(TYPE *pObject)
Standard Constructor with initialisation.
Definition: SmartRef.h:84
SmartRef::INVALID
Definition: SmartRef.h:69