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
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;
76 protected:
77 public:
82  m_target = 0;
83  _setEnvironment(0, 0);
84  }
86  SmartRef(TYPE* pObject) {
89  m_target = pObject;
90  _setEnvironment(0, 0);
91  }
93  SmartRef(const TYPE* pObject) {
96  m_target = const_cast<TYPE*>(pObject);
97  _setEnvironment(0, 0);
98  }
100  SmartRef(const SmartRef& copy) {
103  m_target = copy.m_target;
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;
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;
200  return *this;
201  }
204  m_base.m_data = pObj;
205  m_base.m_contd = pContd;
207  return *this;
208  }
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  }
221  return _setEnvironment(pObj,0);
222  }
224  const SmartRef<TYPE>& operator() (const DataObject* pObj) const {
225  return _setEnvironment(pObj,0);
226  }
229  m_target = c.m_target;
232  return _setEnvironment(c.m_base.m_data, c.m_base.m_contd); }
234  SmartRef<TYPE>& operator=(TYPE* pObject) {
235  m_target = pObject;
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(); }
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
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
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
298  m_base.writeObject(m_target, s);
299  return s;
300 }
301 
303 template <class TYPE> inline
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 
SmartRef< TYPE > & operator=(const SmartRef< TYPE > &c)
Assignment.
Definition: SmartRef.h:228
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:139
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:143
const TYPE * operator->() const
Dereference operator to const object.
Definition: SmartRef.h:247
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:174
The stream buffer is a small object collecting object data.
Definition: StreamBuffer.h:40
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:288
tuple c
Definition: gaudirun.py:341
void set(DataObject *pObj, long hint_id, long link_id)
Setup smart reference when reading. Must be allowed from external sources.
Definition: SmartRef.h:155
SmartRef(const DataObject *pObj, long hint, long link, TYPE *obj=0)
Constructor for references to contained objects passing environment.
Definition: SmartRef.h:122
SmartRef(long hint, long link, TYPE *obj=0)
Constructor.
Definition: SmartRef.h:107
void setObjectType(const ContainedObject *) const
Definition: SmartRefBase.h:100
SmartRef(const SmartRef &copy)
Copy Constructor.
Definition: SmartRef.h:100
const std::type_info * type() const
Access to embedded type.
Definition: SmartRef.h:159
long linkID() const
Access link id:
Definition: SmartRef.h:151
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:203
const ObjectContainerBase * parent() const
Access to parent object.
bool operator!=(const SmartRef< TYPE > &c) const
NON-Equality operator.
Definition: SmartRef.h:192
SmartRef(const ContainedObject *pObj, long hint, long link, TYPE *obj=0)
Constructor for references to contained objects passing environment.
Definition: SmartRef.h:114
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:163
const std::string & path() const
Shortcut to access the path to the linked object.
bool operator!=(const SmartRef< TYPE > &ref, int)
Friend helper to check for object existence (will load object)
Definition: SmartRef.h:325
bool operator==(const SmartRef< TYPE > &c) const
Equality operator.
Definition: SmartRef.h:176
SmartRef< TYPE > & operator()(ContainedObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:210
SmartRefBase m_base
Definition: SmartRef.h:73
SmartRef< TYPE > & operator=(TYPE *pObject)
Assignment.
Definition: SmartRef.h:234
const TYPE * data() const
Definition: SmartRef.h:166
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
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
bool operator==(const SmartRef< TYPE > &ref, int)
Friend helper to check for object existence (will load object)
Definition: SmartRef.h:311
SmartRef(const DataObject *pObj, long hint, TYPE *obj=0)
Constructor for references to DataObjects passing environment.
Definition: SmartRef.h:129
long hintID() const
Access hint id:
Definition: SmartRef.h:147
TYPE & operator*()
Dereference operator.
Definition: SmartRef.h:241
string s
Definition: gaudirun.py:210
TYPE * operator->()
Dereference operator.
Definition: SmartRef.h:245
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:297
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:31
friend StreamBuffer & operator>>(StreamBuffer &_s, SmartRef< TYPE > &ptr)
Input Streamer operator.
Definition: SmartRef.h:263
StreamBuffer & readRef(StreamBuffer &s)
Read the reference from the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:304
const TYPE & operator*() const
Dereference operator.
Definition: SmartRef.h:243
const SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd) const
Set the environment (CONST)
Definition: SmartRef.h:196
SmartRef(const TYPE *pObject)
Standard Constructor with initialisation from const object.
Definition: SmartRef.h:93
SmartRef(TYPE *pObject)
Standard Constructor with initialisation.
Definition: SmartRef.h:86