Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v28r2p1 (f1a77ff4)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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
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:
71  typedef TYPE entry_type;
72 protected:
75  mutable const TYPE* m_target = nullptr;
76 public:
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;
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  }
194  m_base.m_data = pObj;
195  m_base.m_contd = pContd;
196  m_base.setObjectType(data());
197  return *this;
198  }
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  }
211  return _setEnvironment(pObj,nullptr);
212  }
214  const SmartRef<TYPE>& operator() (const DataObject* pObj) const {
215  return _setEnvironment(pObj,nullptr);
216  }
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(); }
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
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
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
289  return s;
290 }
291 
293 template <class TYPE> inline
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(const DataObject *pObj, long hint, TYPE *obj=nullptr)
Constructor for references to DataObjects passing environment.
Definition: SmartRef.h:127
SmartRef< TYPE > & operator=(const SmartRef< TYPE > &c)
Assignment.
Definition: SmartRef.h:218
TYPE entry_type
Entry type definition.
Definition: SmartRef.h:71
SmartRef()
Standard Constructor.
Definition: SmartRef.h:78
bool shouldFollowLink(const DataObject *) const
Standard destructor.
Definition: SmartRef.h:137
bool isEqual(const ContainedObject *, const SmartRefBase &c) const
Equality operator for ContainedObject like references.
Definition: SmartRefBase.h:72
bool isEqualEx(const DataObject *pObj, const SmartRefBase &c) const
Extended equality check.
bool shouldFollowLink(const ContainedObject *) const
Check if link should be followed: link must be valid and object not yet loaded.
Definition: SmartRef.h:141
const ContainedObject * accessData(const ContainedObject *typ) const
Load on demand of ContainedObject like references.
const TYPE * operator->() const
Dereference operator to const object.
Definition: SmartRef.h:237
const DataObject * m_data
Object data: Pointer to the identifiable object the link originates.
Definition: SmartRefBase.h:54
const std::string & path() const
Return the path of the linked object inside the data store.
Definition: SmartRef.h:172
The stream buffer is a small object collecting object data.
Definition: StreamBuffer.h:41
long m_hintID
Object data: ID of the link hint to the identifiable object.
Definition: SmartRefBase.h:50
const TYPE * target() const
Access to the object.
Definition: SmartRef.h:278
void writeObject(const DataObject *pObject, StreamBuffer &s) const
Output streamer for DataObject like references.
Definition: SmartRefBase.h:81
void setObjectType(const ContainedObject *) const
Definition: SmartRefBase.h:100
SmartRef(const DataObject *pObj, long hint, long link, TYPE *obj=nullptr)
Constructor for references to contained objects passing environment.
Definition: SmartRef.h:120
SmartRef(const SmartRef &copy)
Copy Constructor.
Definition: SmartRef.h:98
const std::type_info * type() const
Access to embedded type.
Definition: SmartRef.h:157
long linkID() const
Access link id:
Definition: SmartRef.h:149
Kernel objects: SmartRef.
Definition: SmartRef.h:62
User example objects: SmartRefBase.
Definition: SmartRefBase.h:47
SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd)
Set the environment (CONST)
Definition: SmartRef.h:193
const ObjectContainerBase * parent() const
Access to parent object.
bool operator!=(const SmartRef< TYPE > &c) const
NON-Equality operator.
Definition: SmartRef.h:182
STL class.
const ContainedObject * m_contd
Object data: Pointer to the Contained object (if applicable)
Definition: SmartRefBase.h:56
TYPE * data()
Access to raw data pointer.
Definition: SmartRef.h:161
const std::string & path() const
Shortcut to access the path to the linked object.
SmartRef(const ContainedObject *pObj, long hint, long link, TYPE *obj=nullptr)
Constructor for references to contained objects passing environment.
Definition: SmartRef.h:112
bool operator==(const SmartRef< TYPE > &c) const
Equality operator.
Definition: SmartRef.h:174
SmartRef< TYPE > & operator()(ContainedObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:200
SmartRefBase m_base
Definition: SmartRef.h:73
SmartRef< TYPE > & operator=(TYPE *pObject)
Assignment.
Definition: SmartRef.h:224
const TYPE * data() const
Definition: SmartRef.h:164
const TYPE * m_target
Pointer to target data object.
Definition: SmartRef.h:75
long m_linkID
Object data: ID of the object within the identifiable container (if any)
Definition: SmartRefBase.h:52
SmartRef(long hint, long link, TYPE *obj=nullptr)
Constructor.
Definition: SmartRef.h:105
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
long hintID() const
Access hint id:
Definition: SmartRef.h:145
TYPE & operator*()
Dereference operator.
Definition: SmartRef.h:231
string s
Definition: gaudirun.py:245
TYPE * operator->()
Dereference operator.
Definition: SmartRef.h:235
DataObject * readObject(const DataObject *, StreamBuffer &s) const
Input streamer for DataObject like references.
Definition: SmartRefBase.h:89
void set(DataObject *pObj, long hint_id, long link_id)
Setup smart reference when reading. Must be allowed from external sources.
StreamBuffer & writeRef(StreamBuffer &s) const
Write the reference to the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:287
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
friend StreamBuffer & operator>>(StreamBuffer &_s, SmartRef< TYPE > &ptr)
Input Streamer operator.
Definition: SmartRef.h:253
StreamBuffer & readRef(StreamBuffer &s)
Read the reference from the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:294
const TYPE & operator*() const
Dereference operator.
Definition: SmartRef.h:233
const SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd) const
Set the environment (CONST)
Definition: SmartRef.h:186
SmartRef(const TYPE *pObject)
Standard Constructor with initialisation from const object.
Definition: SmartRef.h:91
SmartRef(TYPE *pObject)
Standard Constructor with initialisation.
Definition: SmartRef.h:84