Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
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>
22 template <class TYPE>
24 template <class TYPE>
26 
65 template <class TYPE>
66 class SmartRef {
68  friend class SmartRefArray<TYPE>;
69  friend class SmartRefList<TYPE>;
70  friend class SmartRefMap<TYPE>;
71 
72 public:
75  typedef TYPE entry_type;
76 
77 protected:
80  mutable const TYPE* m_target = nullptr;
81 
82 public:
85  m_base.m_hintID = INVALID;
86  m_base.m_linkID = INVALID;
87  _setEnvironment( 0, 0 );
88  }
90  SmartRef( TYPE* pObject ) {
91  m_base.m_hintID = INVALID;
92  m_base.m_linkID = INVALID;
93  m_target = pObject;
94  _setEnvironment( 0, 0 );
95  }
97  SmartRef( const TYPE* pObject ) {
98  m_base.m_hintID = INVALID;
99  m_base.m_linkID = INVALID;
100  m_target = const_cast<TYPE*>( pObject );
101  _setEnvironment( 0, 0 );
102  }
104  SmartRef( const SmartRef& copy ) {
105  m_base.m_hintID = copy.m_base.m_hintID;
106  m_base.m_linkID = copy.m_base.m_linkID;
107  m_target = copy.m_target;
109  }
111  SmartRef( long hint, long link, TYPE* obj = nullptr ) {
112  m_base.m_hintID = hint;
113  m_base.m_linkID = link;
114  m_target = obj;
115  _setEnvironment( 0, 0 );
116  }
118  SmartRef( const ContainedObject* pObj, long hint, long link, TYPE* obj = nullptr ) {
119  m_base.m_hintID = hint;
120  m_base.m_linkID = link;
121  m_target = obj;
122  const DataObject* src = ( 0 == pObj ) ? 0 : pObj->parent();
123  _setEnvironment( src, pObj );
124  }
126  SmartRef( const DataObject* pObj, long hint, long link, TYPE* obj = nullptr ) {
127  m_base.m_hintID = hint;
128  m_base.m_linkID = link;
129  m_target = obj;
130  _setEnvironment( pObj, 0 );
131  }
133  SmartRef( const DataObject* pObj, long hint, TYPE* obj = nullptr ) {
134  m_base.m_hintID = hint;
135  m_base.m_linkID = INVALID;
136  m_target = obj;
137  _setEnvironment( pObj, 0 );
138  }
140  // virtual ~SmartRef() {
141  //}
143  bool shouldFollowLink( const DataObject* /* typ */ ) const { return ( !m_target && m_base.m_hintID != INVALID ); }
145  bool shouldFollowLink( const ContainedObject* /* typ */ ) const {
146  return ( !m_target && m_base.m_hintID != INVALID && m_base.m_linkID != INVALID );
147  }
149  long hintID() const { return m_base.m_hintID; }
151  long linkID() const { return m_base.m_linkID; }
153  void set( DataObject* pObj, long hint_id, long link_id ) { m_base.set( pObj, hint_id, link_id ); }
155  const std::type_info* type() const { return &typeid( TYPE ); }
157  TYPE* data() { return const_cast<TYPE*>( m_target ); }
158  const TYPE* data() const { return m_target; }
160  const TYPE* target() const;
162  TYPE* target();
164  inline const std::string& path() const { return m_base.path(); }
166  bool operator==( const SmartRef<TYPE>& c ) const {
167  if ( m_target && c.m_target ) return m_target == c.m_target;
168  if ( !m_target && !c.m_target ) return m_base.isEqual( m_target, c.m_base );
169  if ( m_target && !c.m_target ) return m_base.isEqualEx( m_target, c.m_base );
170  if ( !m_target && c.m_target ) return c.m_base.isEqualEx( c.m_target, m_base );
171  return false;
172  }
174  bool operator!=( const SmartRef<TYPE>& c ) const { return !( this->operator==( c ) ); }
176  const SmartRef<TYPE>& _setEnvironment( const DataObject* pObj, const ContainedObject* pContd ) const {
177  m_base.m_data = pObj;
178  m_base.m_contd = pContd;
179  m_base.setObjectType( data() );
180  return *this;
181  }
183  SmartRef<TYPE>& _setEnvironment( const DataObject* pObj, const ContainedObject* pContd ) {
184  m_base.m_data = pObj;
185  m_base.m_contd = pContd;
186  m_base.setObjectType( data() );
187  return *this;
188  }
191  const DataObject* src = ( pObj ? pObj->parent() : nullptr );
192  return _setEnvironment( src, pObj );
193  }
195  const SmartRef<TYPE>& operator()( const ContainedObject* pObj ) const {
196  const DataObject* src = ( pObj ? pObj->parent() : nullptr );
197  return _setEnvironment( src, pObj );
198  }
200  SmartRef<TYPE>& operator()( DataObject* pObj ) { return _setEnvironment( pObj, nullptr ); }
202  const SmartRef<TYPE>& operator()( const DataObject* pObj ) const { return _setEnvironment( pObj, nullptr ); }
205  m_target = c.m_target;
206  m_base.m_hintID = c.m_base.m_hintID;
207  m_base.m_linkID = c.m_base.m_linkID;
208  return _setEnvironment( c.m_base.m_data, c.m_base.m_contd );
209  }
211  SmartRef<TYPE>& operator=( TYPE* pObject ) {
212  m_target = pObject;
213  m_base.m_hintID = INVALID;
214  m_base.m_linkID = INVALID;
215  return *this;
216  }
218  TYPE& operator*() { return *SmartRef<TYPE>::target(); }
220  const TYPE& operator*() const { return *SmartRef<TYPE>::target(); }
222  TYPE* operator->() { return SmartRef<TYPE>::target(); }
224  const TYPE* operator->() const { return SmartRef<TYPE>::target(); }
226  operator const TYPE*() const { return SmartRef<TYPE>::target(); }
228  operator TYPE*() { return SmartRef<TYPE>::target(); }
230  StreamBuffer& writeRef( StreamBuffer& s ) const;
234  // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
235  friend StreamBuffer& operator<<( StreamBuffer& _s, const SmartRef<TYPE>& ptr ) { return ptr.writeRef( _s ); }
237  // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
238  friend StreamBuffer& operator>>( StreamBuffer& _s, SmartRef<TYPE>& ptr ) { return ptr.readRef( _s ); }
239 };
240 
241 //
242 // Inline function necessary to be outside the class definition.
243 // Mostly this stuff cannot go into the class definition because
244 // G++ has problems recognizing proper templates if they are not
245 // completely defined.
246 //
247 // M.Frank
248 //
249 
251 template <class TYPE>
252 inline TYPE* SmartRef<TYPE>::target() {
253  if ( !m_target ) { m_target = dynamic_cast<const TYPE*>( m_base.accessData( m_target ) ); }
254  return const_cast<TYPE*>( m_target );
255 }
256 
258 template <class TYPE>
259 inline const TYPE* SmartRef<TYPE>::target() const {
260  if ( !m_target ) { m_target = dynamic_cast<const TYPE*>( m_base.accessData( m_target ) ); }
261  return m_target;
262 }
263 
265 template <class TYPE>
268  return s;
269 }
270 
272 template <class TYPE>
274  m_target = dynamic_cast<TYPE*>( m_base.readObject( m_target, s ) );
275  return s;
276 }
277 
279 template <class TYPE>
280 inline bool operator==( const SmartRef<TYPE>& ref, int ) {
281  return ref.target() == nullptr;
282 }
283 
285 template <class TYPE>
286 inline bool operator==( int, const SmartRef<TYPE>& ref ) {
287  return ref.target() == nullptr;
288 }
289 
291 template <class TYPE>
292 inline bool operator!=( const SmartRef<TYPE>& ref, int ) {
293  return ref.target() != nullptr;
294 }
295 
297 template <class TYPE>
298 inline bool operator!=( int, const SmartRef<TYPE>& ref ) {
299  return ref.target() != nullptr;
300 }
301 
302 #endif // KERNEL_SMARTREF_H
SmartRef(const DataObject *pObj, long hint, TYPE *obj=nullptr)
Constructor for references to DataObjects passing environment.
Definition: SmartRef.h:133
SmartRef< TYPE > & operator=(const SmartRef< TYPE > &c)
Assignment.
Definition: SmartRef.h:204
TYPE entry_type
Entry type definition.
Definition: SmartRef.h:75
SmartRef()
Standard Constructor.
Definition: SmartRef.h:84
bool shouldFollowLink(const DataObject *) const
Standard destructor.
Definition: SmartRef.h:143
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:145
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:224
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:164
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:259
const SmartRef< TYPE > & operator()(const DataObject *pObj) const
operator() const: assigns parent object for serialisation
Definition: SmartRef.h:202
void writeObject(const DataObject *pObject, StreamBuffer &s) const
Output streamer for DataObject like references.
Definition: SmartRefBase.h:80
void setObjectType(const ContainedObject *) const
Definition: SmartRefBase.h:97
SmartRef(const DataObject *pObj, long hint, long link, TYPE *obj=nullptr)
Constructor for references to contained objects passing environment.
Definition: SmartRef.h:126
SmartRef(const SmartRef &copy)
Copy Constructor.
Definition: SmartRef.h:104
const std::type_info * type() const
Access to embedded type.
Definition: SmartRef.h:155
long linkID() const
Access link id:
Definition: SmartRef.h:151
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:183
const ObjectContainerBase * parent() const
Access to parent object.
bool operator!=(const SmartRef< TYPE > &c) const
NON-Equality operator.
Definition: SmartRef.h:174
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:157
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:118
SmartRef< TYPE > & operator()(DataObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:200
const SmartRef< TYPE > & operator()(const ContainedObject *pObj) const
operator() const: assigns parent object for serialisation
Definition: SmartRef.h:195
bool operator==(const SmartRef< TYPE > &c) const
Equality operator.
Definition: SmartRef.h:166
SmartRef< TYPE > & operator()(ContainedObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:190
SmartRefBase m_base
Definition: SmartRef.h:78
SmartRef< TYPE > & operator=(TYPE *pObject)
Assignment.
Definition: SmartRef.h:211
const TYPE * data() const
Definition: SmartRef.h:158
const TYPE * m_target
Pointer to target data object.
Definition: SmartRef.h:80
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:111
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
long hintID() const
Access hint id:
Definition: SmartRef.h:149
TYPE & operator*()
Dereference operator.
Definition: SmartRef.h:218
string s
Definition: gaudirun.py:312
TYPE * operator->()
Dereference operator.
Definition: SmartRef.h:222
DataObject * readObject(const DataObject *, StreamBuffer &s) const
Input streamer for DataObject like references.
Definition: SmartRefBase.h:86
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:266
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:238
StreamBuffer & readRef(StreamBuffer &s)
Read the reference from the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:273
const TYPE & operator*() const
Dereference operator.
Definition: SmartRef.h:220
const SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd) const
Set the environment (CONST)
Definition: SmartRef.h:176
SmartRef(const TYPE *pObject)
Standard Constructor with initialisation from const object.
Definition: SmartRef.h:97
SmartRef(TYPE *pObject)
Standard Constructor with initialisation.
Definition: SmartRef.h:90