The Gaudi Framework  v30r3 (a5ef0a68)
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>
22 template <class TYPE>
24 template <class TYPE>
26 
65 template <class TYPE>
66 class SmartRef
67 {
69  friend class SmartRefArray<TYPE>;
70  friend class SmartRefList<TYPE>;
71  friend class SmartRefMap<TYPE>;
72 
73 public:
76  typedef TYPE entry_type;
77 
78 protected:
81  mutable const TYPE* m_target = nullptr;
82 
83 public:
86  {
87  m_base.m_hintID = INVALID;
88  m_base.m_linkID = INVALID;
89  _setEnvironment( 0, 0 );
90  }
92  SmartRef( TYPE* pObject )
93  {
94  m_base.m_hintID = INVALID;
95  m_base.m_linkID = INVALID;
96  m_target = pObject;
97  _setEnvironment( 0, 0 );
98  }
100  SmartRef( const TYPE* pObject )
101  {
102  m_base.m_hintID = INVALID;
103  m_base.m_linkID = INVALID;
104  m_target = const_cast<TYPE*>( pObject );
105  _setEnvironment( 0, 0 );
106  }
108  SmartRef( const SmartRef& copy )
109  {
110  m_base.m_hintID = copy.m_base.m_hintID;
111  m_base.m_linkID = copy.m_base.m_linkID;
112  m_target = copy.m_target;
114  }
116  SmartRef( long hint, long link, TYPE* obj = nullptr )
117  {
118  m_base.m_hintID = hint;
119  m_base.m_linkID = link;
120  m_target = obj;
121  _setEnvironment( 0, 0 );
122  }
124  SmartRef( const ContainedObject* pObj, long hint, long link, TYPE* obj = nullptr )
125  {
126  m_base.m_hintID = hint;
127  m_base.m_linkID = link;
128  m_target = obj;
129  const DataObject* src = ( 0 == pObj ) ? 0 : pObj->parent();
130  _setEnvironment( src, pObj );
131  }
133  SmartRef( const DataObject* pObj, long hint, long link, TYPE* obj = nullptr )
134  {
135  m_base.m_hintID = hint;
136  m_base.m_linkID = link;
137  m_target = obj;
138  _setEnvironment( pObj, 0 );
139  }
141  SmartRef( const DataObject* pObj, long hint, TYPE* obj = nullptr )
142  {
143  m_base.m_hintID = hint;
144  m_base.m_linkID = INVALID;
145  m_target = obj;
146  _setEnvironment( pObj, 0 );
147  }
149  // virtual ~SmartRef() {
150  //}
152  bool shouldFollowLink( const DataObject* /* typ */ ) const { return ( !m_target && m_base.m_hintID != INVALID ); }
154  bool shouldFollowLink( const ContainedObject* /* typ */ ) const
155  {
156  return ( !m_target && m_base.m_hintID != INVALID && m_base.m_linkID != INVALID );
157  }
159  long hintID() const { return m_base.m_hintID; }
161  long linkID() const { return m_base.m_linkID; }
163  void set( DataObject* pObj, long hint_id, long link_id ) { m_base.set( pObj, hint_id, link_id ); }
165  const std::type_info* type() const { return &typeid( TYPE ); }
167  TYPE* data() { return const_cast<TYPE*>( m_target ); }
168  const TYPE* data() const { return m_target; }
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  {
178  if ( m_target && c.m_target ) return m_target == c.m_target;
179  if ( !m_target && !c.m_target ) return m_base.isEqual( m_target, c.m_base );
180  if ( m_target && !c.m_target ) return m_base.isEqualEx( m_target, c.m_base );
181  if ( !m_target && c.m_target ) return c.m_base.isEqualEx( c.m_target, m_base );
182  return false;
183  }
185  bool operator!=( const SmartRef<TYPE>& c ) const { return !( this->operator==( c ) ); }
187  const SmartRef<TYPE>& _setEnvironment( const DataObject* pObj, const ContainedObject* pContd ) const
188  {
189  m_base.m_data = pObj;
190  m_base.m_contd = pContd;
191  m_base.setObjectType( data() );
192  return *this;
193  }
196  {
197  m_base.m_data = pObj;
198  m_base.m_contd = pContd;
199  m_base.setObjectType( data() );
200  return *this;
201  }
204  {
205  const DataObject* src = ( pObj ? pObj->parent() : nullptr );
206  return _setEnvironment( src, pObj );
207  }
209  const SmartRef<TYPE>& operator()( const ContainedObject* pObj ) const
210  {
211  const DataObject* src = ( pObj ? pObj->parent() : nullptr );
212  return _setEnvironment( src, pObj );
213  }
215  SmartRef<TYPE>& operator()( DataObject* pObj ) { return _setEnvironment( pObj, nullptr ); }
217  const SmartRef<TYPE>& operator()( const DataObject* pObj ) const { return _setEnvironment( pObj, nullptr ); }
220  {
221  m_target = c.m_target;
222  m_base.m_hintID = c.m_base.m_hintID;
223  m_base.m_linkID = c.m_base.m_linkID;
224  return _setEnvironment( c.m_base.m_data, c.m_base.m_contd );
225  }
227  SmartRef<TYPE>& operator=( TYPE* pObject )
228  {
229  m_target = pObject;
230  m_base.m_hintID = INVALID;
231  m_base.m_linkID = INVALID;
232  return *this;
233  }
235  TYPE& operator*() { return *SmartRef<TYPE>::target(); }
237  const TYPE& operator*() const { return *SmartRef<TYPE>::target(); }
239  TYPE* operator->() { return SmartRef<TYPE>::target(); }
241  const TYPE* operator->() const { return SmartRef<TYPE>::target(); }
243  operator const TYPE*() const { return SmartRef<TYPE>::target(); }
245  operator TYPE*() { return SmartRef<TYPE>::target(); }
247  StreamBuffer& writeRef( StreamBuffer& s ) const;
251  // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
252  friend StreamBuffer& operator<<( StreamBuffer& _s, const SmartRef<TYPE>& ptr ) { return ptr.writeRef( _s ); }
254  // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
255  friend StreamBuffer& operator>>( StreamBuffer& _s, SmartRef<TYPE>& ptr ) { return ptr.readRef( _s ); }
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>
270 {
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>
279 inline const TYPE* SmartRef<TYPE>::target() const
280 {
281  if ( !m_target ) {
282  m_target = dynamic_cast<const TYPE*>( m_base.accessData( m_target ) );
283  }
284  return m_target;
285 }
286 
288 template <class TYPE>
290 {
292  return s;
293 }
294 
296 template <class TYPE>
298 {
299  m_target = dynamic_cast<TYPE*>( m_base.readObject( m_target, s ) );
300  return s;
301 }
302 
304 template <class TYPE>
305 inline bool operator==( const SmartRef<TYPE>& ref, int )
306 {
307  return ref.target() == nullptr;
308 }
309 
311 template <class TYPE>
312 inline bool operator==( int, const SmartRef<TYPE>& ref )
313 {
314  return ref.target() == nullptr;
315 }
316 
318 template <class TYPE>
319 inline bool operator!=( const SmartRef<TYPE>& ref, int )
320 {
321  return ref.target() != nullptr;
322 }
323 
325 template <class TYPE>
326 inline bool operator!=( int, const SmartRef<TYPE>& ref )
327 {
328  return ref.target() != nullptr;
329 }
330 
331 #endif // KERNEL_SMARTREF_H
SmartRef(const DataObject *pObj, long hint, TYPE *obj=nullptr)
Constructor for references to DataObjects passing environment.
Definition: SmartRef.h:141
SmartRef< TYPE > & operator=(const SmartRef< TYPE > &c)
Assignment.
Definition: SmartRef.h:219
TYPE entry_type
Entry type definition.
Definition: SmartRef.h:76
SmartRef()
Standard Constructor.
Definition: SmartRef.h:85
bool shouldFollowLink(const DataObject *) const
Standard destructor.
Definition: SmartRef.h:152
bool isEqual(const ContainedObject *, const SmartRefBase &c) const
Equality operator for ContainedObject like references.
Definition: SmartRefBase.h:73
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:154
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:241
const DataObject * m_data
Object data: Pointer to the identifiable object the link originates.
Definition: SmartRefBase.h:55
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:41
long m_hintID
Object data: ID of the link hint to the identifiable object.
Definition: SmartRefBase.h:51
const TYPE * target() const
Access to the object.
Definition: SmartRef.h:279
const SmartRef< TYPE > & operator()(const DataObject *pObj) const
operator() const: assigns parent object for serialisation
Definition: SmartRef.h:217
void writeObject(const DataObject *pObject, StreamBuffer &s) const
Output streamer for DataObject like references.
Definition: SmartRefBase.h:83
void setObjectType(const ContainedObject *) const
Definition: SmartRefBase.h:103
SmartRef(const DataObject *pObj, long hint, long link, TYPE *obj=nullptr)
Constructor for references to contained objects passing environment.
Definition: SmartRef.h:133
SmartRef(const SmartRef &copy)
Copy Constructor.
Definition: SmartRef.h:108
const std::type_info * type() const
Access to embedded type.
Definition: SmartRef.h:165
long linkID() const
Access link id:
Definition: SmartRef.h:161
Kernel objects: SmartRef.
Definition: SmartRef.h:66
User example objects: SmartRefBase.
Definition: SmartRefBase.h:47
SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd)
Set the environment (CONST)
Definition: SmartRef.h:195
const ObjectContainerBase * parent() const
Access to parent object.
bool operator!=(const SmartRef< TYPE > &c) const
NON-Equality operator.
Definition: SmartRef.h:185
STL class.
const ContainedObject * m_contd
Object data: Pointer to the Contained object (if applicable)
Definition: SmartRefBase.h:57
TYPE * data()
Access to raw data pointer.
Definition: SmartRef.h:167
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:124
SmartRef< TYPE > & operator()(DataObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:215
const SmartRef< TYPE > & operator()(const ContainedObject *pObj) const
operator() const: assigns parent object for serialisation
Definition: SmartRef.h:209
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:203
SmartRefBase m_base
Definition: SmartRef.h:79
SmartRef< TYPE > & operator=(TYPE *pObject)
Assignment.
Definition: SmartRef.h:227
const TYPE * data() const
Definition: SmartRef.h:168
const TYPE * m_target
Pointer to target data object.
Definition: SmartRef.h:81
long m_linkID
Object data: ID of the object within the identifiable container (if any)
Definition: SmartRefBase.h:53
SmartRef(long hint, long link, TYPE *obj=nullptr)
Constructor.
Definition: SmartRef.h:116
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
long hintID() const
Access hint id:
Definition: SmartRef.h:159
TYPE & operator*()
Dereference operator.
Definition: SmartRef.h:235
string s
Definition: gaudirun.py:253
TYPE * operator->()
Dereference operator.
Definition: SmartRef.h:239
DataObject * readObject(const DataObject *, StreamBuffer &s) const
Input streamer for DataObject like references.
Definition: SmartRefBase.h:90
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:289
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:255
StreamBuffer & readRef(StreamBuffer &s)
Read the reference from the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:297
const TYPE & operator*() const
Dereference operator.
Definition: SmartRef.h:237
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:100
SmartRef(TYPE *pObject)
Standard Constructor with initialisation.
Definition: SmartRef.h:92