| The Gaudi Framework v27r1

- 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 protected:
77 public:
79 SmartRef() {
80 m_base.m_hintID = INVALID;
81 m_base.m_linkID = INVALID;
82 _setEnvironment(0, 0);
83 }
85 SmartRef(TYPE* pObject) {
86 m_base.m_hintID = INVALID;
87 m_base.m_linkID = INVALID;
88 m_target = pObject;
89 _setEnvironment(0, 0);
90 }
92 SmartRef(const TYPE* pObject) {
93 m_base.m_hintID = INVALID;
94 m_base.m_linkID = INVALID;
95 m_target = const_cast<TYPE*>(pObject);
96 _setEnvironment(0, 0);
97 }
99 SmartRef(const SmartRef& copy) {
100 m_base.m_hintID = copy.m_base.m_hintID;
101 m_base.m_linkID = copy.m_base.m_linkID;
102 m_target = copy.m_target;
103 _setEnvironment(copy.m_base.m_data, copy.m_base.m_contd);
104 }
106 SmartRef(long hint, long link, TYPE* obj = nullptr) {
107 m_base.m_hintID = hint;
108 m_base.m_linkID = link;
109 m_target = obj;
110 _setEnvironment(0, 0);
111 }
113 SmartRef(const ContainedObject* pObj, long hint, long link, TYPE* obj = nullptr) {
114 m_base.m_hintID = hint;
115 m_base.m_linkID = link;
116 m_target = obj;
117 const DataObject* src = (0==pObj) ? 0 : pObj->parent();
118 _setEnvironment(src, pObj);
119 }
121 SmartRef(const DataObject* pObj, long hint, long link, TYPE* obj = nullptr) {
122 m_base.m_hintID = hint;
123 m_base.m_linkID = link;
124 m_target = obj;
125 _setEnvironment(pObj, 0);
126 }
128 SmartRef(const DataObject* pObj, long hint, TYPE* obj = nullptr) {
129 m_base.m_hintID = hint;
130 m_base.m_linkID = INVALID;
131 m_target = obj;
132 _setEnvironment(pObj, 0);
133 }
135 //virtual ~SmartRef() {
136 //}
138 bool shouldFollowLink(const DataObject* /* typ */) const {
139 return (!m_target && m_base.m_hintID != INVALID );
140 }
142 bool shouldFollowLink(const ContainedObject* /* typ */) const {
143 return (!m_target && m_base.m_hintID != INVALID && m_base.m_linkID != INVALID );
144 }
146 long hintID() const {
147 return m_base.m_hintID;
148 }
150 long linkID() const {
151 return m_base.m_linkID;
152 }
154 void set(DataObject* pObj, long hint_id, long link_id) {
155 m_base.set(pObj, hint_id, link_id);
156 }
158 const std::type_info* type() const {
159 return &typeid(TYPE);
160 }
162 TYPE* data() {
163 return const_cast<TYPE*>(m_target);
164 }
165 const TYPE* data() const {
166 return m_target;
167 }
169 const TYPE* target() const;
171 TYPE* target();
173 inline const std::string &path() const { return m_base.path(); }
175 bool operator==(const SmartRef<TYPE>& c) const {
176 if ( m_target && c.m_target ) return m_target == c.m_target;
177 if ( !m_target && !c.m_target ) return m_base.isEqual(m_target,c.m_base);
178 if ( m_target && !c.m_target ) return m_base.isEqualEx(m_target, c.m_base);
179 if ( !m_target && c.m_target ) return c.m_base.isEqualEx(c.m_target, m_base);
180 return false;
181 }
183 bool operator!=(const SmartRef<TYPE>& c) const {
184 return !(this->operator==(c));
185 }
187 const SmartRef<TYPE>& _setEnvironment(const DataObject* pObj, const ContainedObject* pContd) const {
188 m_base.m_data = pObj;
189 m_base.m_contd = pContd;
190 m_base.setObjectType(data());
191 return *this;
192 }
194 SmartRef<TYPE>& _setEnvironment(const DataObject* pObj, const ContainedObject* pContd) {
195 m_base.m_data = pObj;
196 m_base.m_contd = pContd;
197 m_base.setObjectType(data());
198 return *this;
199 }
201 SmartRef<TYPE>& operator() (ContainedObject* pObj) {
202 const DataObject* src = ( pObj ? pObj->parent() : nullptr );
203 return _setEnvironment(src, pObj);
204 }
206 const SmartRef<TYPE>& operator() (const ContainedObject* pObj) const {
207 const DataObject* src = ( pObj ? pObj->parent() : nullptr );
208 return _setEnvironment(src, pObj);
209 }
211 SmartRef<TYPE>& operator() (DataObject* pObj) {
212 return _setEnvironment(pObj,nullptr);
213 }
215 const SmartRef<TYPE>& operator() (const DataObject* pObj) const {
216 return _setEnvironment(pObj,nullptr);
217 }
219 SmartRef<TYPE>& operator=(const SmartRef<TYPE>& c) {
220 m_target = c.m_target;
221 m_base.m_hintID = c.m_base.m_hintID;
222 m_base.m_linkID = c.m_base.m_linkID;
223 return _setEnvironment(c.m_base.m_data, c.m_base.m_contd); }
225 SmartRef<TYPE>& operator=(TYPE* pObject) {
226 m_target = pObject;
227 m_base.m_hintID = INVALID;
228 m_base.m_linkID = INVALID;
229 return *this;
230 }
232 TYPE& operator*() { return *SmartRef<TYPE>::target(); }
234 const TYPE& operator*() const { return *SmartRef<TYPE>::target(); }
236 TYPE* operator->() { return SmartRef<TYPE>::target(); }
238 const TYPE* operator->() const { return SmartRef<TYPE>::target(); }
240 operator const TYPE* () const { return SmartRef<TYPE>::target(); }
242 operator TYPE* () { return SmartRef<TYPE>::target(); }
244 StreamBuffer& writeRef(StreamBuffer& s) const;
246 StreamBuffer& readRef(StreamBuffer& s);
248 // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
249 friend StreamBuffer& operator<< (StreamBuffer& _s, const SmartRef<TYPE>& ptr) {
250 return ptr.writeRef(_s);
251 }
253 // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
254 friend StreamBuffer& operator>> (StreamBuffer& _s, SmartRef<TYPE>& ptr) {
255 return ptr.readRef(_s);
256 }
257 };
258
259 //
260 // Inline function necessary to be outside the class definition.
261 // Mostly this stuff cannot go into the class definition because
262 // G++ has problems recognizing proper templates if they are not
263 // completely defined.
264 //
265 // M.Frank
266 //
267
269 template <class TYPE> inline
270 TYPE* SmartRef<TYPE>::target() {
271 if ( !m_target ) {
272 m_target = dynamic_cast<const TYPE*>(m_base.accessData(m_target));
273 }
274 return const_cast<TYPE*>(m_target);
275 }
276
278 template <class TYPE> inline
279 const TYPE* SmartRef<TYPE>::target() const {
280 if ( !m_target ) {
281 m_target = dynamic_cast<const TYPE*>(m_base.accessData(m_target));
282 }
283 return m_target;
284 }
285
287 template <class TYPE> inline
288 StreamBuffer& SmartRef<TYPE>::writeRef (StreamBuffer& s) const {
289 m_base.writeObject(m_target, s);
290 return s;
291 }
292
294 template <class TYPE> inline
295 StreamBuffer& SmartRef<TYPE>::readRef (StreamBuffer& s) {
296 m_target = dynamic_cast<TYPE*>( m_base.readObject(m_target, s) );
297 return s;
298 }
299
301 template <class TYPE> inline
302 bool operator == (const SmartRef<TYPE>& ref, int) {
303 return ref.target() == nullptr;
304 }
305
307 template <class TYPE> inline
308 bool operator == (int, const SmartRef<TYPE>& ref) {
309 return ref.target() == nullptr;
310 }
311
313 template <class TYPE> inline
314 bool operator != (const SmartRef<TYPE>& ref, int) {
315 return ref.target() != nullptr;
316 }
317
319 template <class TYPE> inline
320 bool operator != (int, const SmartRef<TYPE>& ref) {
321 return ref.target() != nullptr;
322 }
323
324 #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:128
SmartRef::operator=
SmartRef< TYPE > & operator=(const SmartRef< TYPE > &c)
Assignment.
Definition: SmartRef.h:219
SmartRef::INVALID
Definition: SmartRef.h:69
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:79
SmartRef::shouldFollowLink
bool shouldFollowLink(const DataObject *) const
Standard destructor.
Definition: SmartRef.h:138
SmartRefBase::isEqual
bool isEqual(const ContainedObject *, const SmartRefBase &c) const
Equality operator for ContainedObject like references.
Definition: SmartRefBase.h:72
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:142
SmartRef::operator->
const TYPE * operator->() const
Dereference operator to const object.
Definition: SmartRef.h:238
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:173
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:279
gaudirun.c
tuple c
Definition: gaudirun.py:391
SmartRef::set
void set(DataObject *pObj, long hint_id, long link_id)
Setup smart reference when reading. Must be allowed from external sources.
Definition: SmartRef.h:154
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:121
SmartRef::SmartRef
SmartRef(const SmartRef &copy)
Copy Constructor.
Definition: SmartRef.h:99
SmartRef::type
const std::type_info * type() const
Access to embedded type.
Definition: SmartRef.h:158
SmartRef::linkID
long linkID() const
Access link id:
Definition: SmartRef.h:150
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:194
SmartRefMap
Definition: SmartRef.h:22
ContainedObject::parent
const ObjectContainerBase * parent() const
Access to parent object.
Definition: ContainedObject.h:49
std::type_info
SmartRef::operator!=
bool operator!=(const SmartRef< TYPE > &c) const
NON-Equality operator.
Definition: SmartRef.h:183
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:162
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:113
operator!=
bool operator!=(const SmartRef< TYPE > &ref, int)
Friend helper to check for object existence (will load object)
Definition: SmartRef.h:314
SmartRef::operator==
bool operator==(const SmartRef< TYPE > &c) const
Equality operator.
Definition: SmartRef.h:175
SmartRef::operator()
SmartRef< TYPE > & operator()(ContainedObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:201
SmartRef::m_base
SmartRefBase m_base
Definition: SmartRef.h:73
SmartRef::operator=
SmartRef< TYPE > & operator=(TYPE *pObject)
Assignment.
Definition: SmartRef.h:225
SmartRef::data
const TYPE * data() const
Definition: SmartRef.h:165
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:106
ContainedObject
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
Definition: ContainedObject.h:29
operator==
bool operator==(const SmartRef< TYPE > &ref, int)
Friend helper to check for object existence (will load object)
Definition: SmartRef.h:302
SmartRefArray
Definition: SmartRef.h:20
SmartRef::hintID
long hintID() const
Access hint id:
Definition: SmartRef.h:146
SmartRef::operator*
TYPE & operator*()
Dereference operator.
Definition: SmartRef.h:232
gaudirun.s
string s
Definition: gaudirun.py:245
SmartRef::operator->
TYPE * operator->()
Dereference operator.
Definition: SmartRef.h:236
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:288
SmartRef::VALID
Definition: SmartRef.h:69
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:254
SmartRef::readRef
StreamBuffer & readRef(StreamBuffer &s)
Read the reference from the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:295
SmartRef::operator*
const TYPE & operator*() const
Dereference operator.
Definition: SmartRef.h:234
SmartRef::_setEnvironment
const SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd) const
Set the environment (CONST)
Definition: SmartRef.h:187
SmartRef::SmartRef
SmartRef(const TYPE *pObject)
Standard Constructor with initialisation from const object.
Definition: SmartRef.h:92
SmartRef::SmartRef
SmartRef(TYPE *pObject)
Standard Constructor with initialisation.
Definition: SmartRef.h:85