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 protected:
77 public:
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;
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  }
195  m_base.m_data = pObj;
196  m_base.m_contd = pContd;
197  m_base.setObjectType(data());
198  return *this;
199  }
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  }
212  return _setEnvironment(pObj,nullptr);
213  }
215  const SmartRef<TYPE>& operator() (const DataObject* pObj) const {
216  return _setEnvironment(pObj,nullptr);
217  }
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(); }
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
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
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
290  return s;
291 }
292 
294 template <class TYPE> inline
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(const DataObject *pObj, long hint, TYPE *obj=nullptr)
Constructor for references to DataObjects passing environment.
Definition: SmartRef.h:128
SmartRef< TYPE > & operator=(const SmartRef< TYPE > &c)
Assignment.
Definition: SmartRef.h:219
TYPE entry_type
Entry type definition.
Definition: SmartRef.h:71
SmartRef()
Standard Constructor.
Definition: SmartRef.h:79
bool shouldFollowLink(const DataObject *) const
Standard destructor.
Definition: SmartRef.h:138
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:142
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:238
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:173
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:279
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:121
SmartRef(const SmartRef &copy)
Copy Constructor.
Definition: SmartRef.h:99
const std::type_info * type() const
Access to embedded type.
Definition: SmartRef.h:158
long linkID() const
Access link id:
Definition: SmartRef.h:150
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:194
const ObjectContainerBase * parent() const
Access to parent object.
bool operator!=(const SmartRef< TYPE > &c) const
NON-Equality operator.
Definition: SmartRef.h:183
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:162
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:113
bool operator==(const SmartRef< TYPE > &c) const
Equality operator.
Definition: SmartRef.h:175
SmartRef< TYPE > & operator()(ContainedObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:201
SmartRefBase m_base
Definition: SmartRef.h:73
SmartRef< TYPE > & operator=(TYPE *pObject)
Assignment.
Definition: SmartRef.h:225
const TYPE * data() const
Definition: SmartRef.h:165
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:106
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
long hintID() const
Access hint id:
Definition: SmartRef.h:146
TYPE & operator*()
Dereference operator.
Definition: SmartRef.h:232
string s
Definition: gaudirun.py:245
TYPE * operator->()
Dereference operator.
Definition: SmartRef.h:236
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:288
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:254
StreamBuffer & readRef(StreamBuffer &s)
Read the reference from the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:295
const TYPE & operator*() const
Dereference operator.
Definition: SmartRef.h:234
const SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd) const
Set the environment (CONST)
Definition: SmartRef.h:187
SmartRef(const TYPE *pObject)
Standard Constructor with initialisation from const object.
Definition: SmartRef.h:92
SmartRef(TYPE *pObject)
Standard Constructor with initialisation.
Definition: SmartRef.h:85