Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r1 (3e2fb5a8)
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 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // ====================================================================
12 // SmartRef.h
13 // --------------------------------------------------------------------
14 //
15 // Package : Kernel
16 //
17 // Author : Markus Frank
18 //
19 // ====================================================================
20 #ifndef KERNEL_SMARTREF_H
21 #define KERNEL_SMARTREF_H 1
22 
23 // Include files
26 
27 #include <typeinfo>
28 
29 // Forward declarations
30 template <class TYPE>
32 template <class TYPE>
34 template <class TYPE>
36 
75 template <class TYPE>
76 class SmartRef {
78  friend class SmartRefArray<TYPE>;
79  friend class SmartRefList<TYPE>;
80  friend class SmartRefMap<TYPE>;
81 
82 public:
85  typedef TYPE entry_type;
86 
87 protected:
90  mutable const TYPE* m_target = nullptr;
91 
92 public:
97  _setEnvironment( 0, 0 );
98  }
100  SmartRef( TYPE* pObject ) {
103  m_target = pObject;
104  _setEnvironment( 0, 0 );
105  }
107  SmartRef( const TYPE* pObject ) {
110  m_target = const_cast<TYPE*>( pObject );
111  _setEnvironment( 0, 0 );
112  }
114  SmartRef( const SmartRef& copy ) {
115  m_base.m_hintID = copy.m_base.m_hintID;
116  m_base.m_linkID = copy.m_base.m_linkID;
117  m_target = copy.m_target;
118  _setEnvironment( copy.m_base.m_data, copy.m_base.m_contd );
119  }
121  SmartRef( 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( 0, 0 );
126  }
128  SmartRef( const ContainedObject* pObj, long hint, long link, TYPE* obj = nullptr ) {
129  m_base.m_hintID = hint;
130  m_base.m_linkID = link;
131  m_target = obj;
132  const DataObject* src = ( 0 == pObj ) ? 0 : pObj->parent();
133  _setEnvironment( src, pObj );
134  }
136  SmartRef( const DataObject* pObj, long hint, long link, TYPE* obj = nullptr ) {
137  m_base.m_hintID = hint;
138  m_base.m_linkID = link;
139  m_target = obj;
140  _setEnvironment( pObj, 0 );
141  }
143  SmartRef( const DataObject* pObj, long hint, TYPE* obj = nullptr ) {
144  m_base.m_hintID = hint;
146  m_target = obj;
147  _setEnvironment( pObj, 0 );
148  }
150  // virtual ~SmartRef() {
151  //}
153  bool shouldFollowLink( const DataObject* /* typ */ ) const { return ( !m_target && m_base.m_hintID != INVALID ); }
155  bool shouldFollowLink( const ContainedObject* /* typ */ ) const {
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  if ( m_target && c.m_target ) return m_target == c.m_target;
178  if ( !m_target && !c.m_target ) return m_base.isEqual( m_target, c.m_base );
179  if ( m_target && !c.m_target ) return m_base.isEqualEx( m_target, c.m_base );
180  if ( !m_target && c.m_target ) return c.m_base.isEqualEx( c.m_target, m_base );
181  return false;
182  }
184  bool operator!=( const SmartRef<TYPE>& c ) const { return !( this->operator==( c ) ); }
186  const SmartRef<TYPE>& _setEnvironment( const DataObject* pObj, const ContainedObject* pContd ) const {
187  m_base.m_data = pObj;
188  m_base.m_contd = pContd;
190  return *this;
191  }
193  SmartRef<TYPE>& _setEnvironment( const DataObject* pObj, const ContainedObject* pContd ) {
194  m_base.m_data = pObj;
195  m_base.m_contd = pContd;
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  }
210  SmartRef<TYPE>& operator()( DataObject* pObj ) { return _setEnvironment( pObj, nullptr ); }
212  const SmartRef<TYPE>& operator()( const DataObject* pObj ) const { return _setEnvironment( pObj, nullptr ); }
215  m_target = c.m_target;
216  m_base.m_hintID = c.m_base.m_hintID;
217  m_base.m_linkID = c.m_base.m_linkID;
218  return _setEnvironment( c.m_base.m_data, c.m_base.m_contd );
219  }
221  SmartRef<TYPE>& operator=( TYPE* pObject ) {
222  m_target = pObject;
225  return *this;
226  }
228  TYPE& operator*() { return *SmartRef<TYPE>::target(); }
230  const TYPE& operator*() const { return *SmartRef<TYPE>::target(); }
232  TYPE* operator->() { return SmartRef<TYPE>::target(); }
234  const TYPE* operator->() const { return SmartRef<TYPE>::target(); }
236  operator const TYPE*() const { return SmartRef<TYPE>::target(); }
238  operator TYPE*() { return SmartRef<TYPE>::target(); }
244  // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
245  friend StreamBuffer& operator<<( StreamBuffer& _s, const SmartRef<TYPE>& ptr ) { return ptr.writeRef( _s ); }
247  // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
248  friend StreamBuffer& operator>>( StreamBuffer& _s, SmartRef<TYPE>& ptr ) { return ptr.readRef( _s ); }
249 };
250 
251 //
252 // Inline function necessary to be outside the class definition.
253 // Mostly this stuff cannot go into the class definition because
254 // G++ has problems recognizing proper templates if they are not
255 // completely defined.
256 //
257 // M.Frank
258 //
259 
261 template <class TYPE>
262 inline TYPE* SmartRef<TYPE>::target() {
263  if ( !m_target ) { m_target = dynamic_cast<const TYPE*>( m_base.accessData( m_target ) ); }
264  return const_cast<TYPE*>( m_target );
265 }
266 
268 template <class TYPE>
269 inline const TYPE* SmartRef<TYPE>::target() const {
270  if ( !m_target ) { m_target = dynamic_cast<const TYPE*>( m_base.accessData( m_target ) ); }
271  return m_target;
272 }
273 
275 template <class TYPE>
277  m_base.writeObject( m_target, s );
278  return s;
279 }
280 
282 template <class TYPE>
284  m_target = dynamic_cast<TYPE*>( m_base.readObject( m_target, s ) );
285  return s;
286 }
287 
289 template <class TYPE>
290 inline bool operator==( const SmartRef<TYPE>& ref, int ) {
291  return ref.target() == nullptr;
292 }
293 
295 template <class TYPE>
296 inline bool operator==( int, const SmartRef<TYPE>& ref ) {
297  return ref.target() == nullptr;
298 }
299 
301 template <class TYPE>
302 inline bool operator!=( const SmartRef<TYPE>& ref, int ) {
303  return ref.target() != nullptr;
304 }
305 
307 template <class TYPE>
308 inline bool operator!=( int, const SmartRef<TYPE>& ref ) {
309  return ref.target() != nullptr;
310 }
311 
312 #endif // KERNEL_SMARTREF_H
SmartRef::operator()
const SmartRef< TYPE > & operator()(const ContainedObject *pObj) const
operator() const: assigns parent object for serialisation
Definition: SmartRef.h:205
SmartRef::m_base
SmartRefBase m_base
Definition: SmartRef.h:88
SmartRef::operator->
const TYPE * operator->() const
Dereference operator to const object.
Definition: SmartRef.h:234
SmartRef::SmartRef
SmartRef(const ContainedObject *pObj, long hint, long link, TYPE *obj=nullptr)
Constructor for references to contained objects passing environment.
Definition: SmartRef.h:128
std::string
STL class.
SmartRef::operator()
SmartRef< TYPE > & operator()(ContainedObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:200
SmartRef::operator=
SmartRef< TYPE > & operator=(TYPE *pObject)
Assignment.
Definition: SmartRef.h:221
SmartRef::m_target
const TYPE * m_target
Pointer to target data object.
Definition: SmartRef.h:90
gaudirun.s
string s
Definition: gaudirun.py:328
std::type_info
SmartRef::SmartRef
SmartRef(long hint, long link, TYPE *obj=nullptr)
Constructor.
Definition: SmartRef.h:121
SmartRef::operator*
TYPE & operator*()
Dereference operator.
Definition: SmartRef.h:228
SmartRef::operator->
TYPE * operator->()
Dereference operator.
Definition: SmartRef.h:232
SmartRefArray
Definition: SmartRef.h:31
SmartRef::operator==
bool operator==(const SmartRef< TYPE > &c) const
Equality operator.
Definition: SmartRef.h:176
SmartRef::data
const TYPE * data() const
Definition: SmartRef.h:168
gaudirun.c
c
Definition: gaudirun.py:509
SmartRef::shouldFollowLink
bool shouldFollowLink(const ContainedObject *) const
Check if link should be followed: link must be valid and object not yet loaded.
Definition: SmartRef.h:155
SmartRefBase
User example objects: SmartRefBase.
Definition: SmartRefBase.h:57
StreamBuffer
Definition: StreamBuffer.h:52
SmartRefBase::m_linkID
long m_linkID
Object data: ID of the object within the identifiable container (if any)
Definition: SmartRefBase.h:62
SmartRef::hintID
long hintID() const
Access hint id:
Definition: SmartRef.h:159
SmartRef::linkID
long linkID() const
Access link id:
Definition: SmartRef.h:161
SmartRef::SmartRef
SmartRef(TYPE *pObject)
Standard Constructor with initialisation.
Definition: SmartRef.h:100
SmartRef::path
const std::string & path() const
Return the path of the linked object inside the data store.
Definition: SmartRef.h:174
SmartRef::operator>>
friend StreamBuffer & operator>>(StreamBuffer &_s, SmartRef< TYPE > &ptr)
Input Streamer operator.
Definition: SmartRef.h:248
SmartRef::readRef
StreamBuffer & readRef(StreamBuffer &s)
Read the reference from the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:283
SmartRef::operator!=
bool operator!=(const SmartRef< TYPE > &c) const
NON-Equality operator.
Definition: SmartRef.h:184
SmartRef::operator=
SmartRef< TYPE > & operator=(const SmartRef< TYPE > &c)
Assignment.
Definition: SmartRef.h:214
SmartRef::SmartRef
SmartRef(const TYPE *pObject)
Standard Constructor with initialisation from const object.
Definition: SmartRef.h:107
SmartRef::VALID
@ VALID
Definition: SmartRef.h:83
SmartRef::operator()
const SmartRef< TYPE > & operator()(const DataObject *pObj) const
operator() const: assigns parent object for serialisation
Definition: SmartRef.h:212
SmartRef::SmartRef
SmartRef(const DataObject *pObj, long hint, TYPE *obj=nullptr)
Constructor for references to DataObjects passing environment.
Definition: SmartRef.h:143
GaudiPython.Bindings.nullptr
nullptr
Definition: Bindings.py:74
SmartRefBase::path
const std::string & path() const
Shortcut to access the path to the linked object.
Definition: SmartRefBase.cpp:99
SmartRefBase::m_data
const DataObject * m_data
Object data: Pointer to the identifiable object the link originates.
Definition: SmartRefBase.h:64
SmartRef::writeRef
StreamBuffer & writeRef(StreamBuffer &s) const
Write the reference to the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:276
compareRootHistos.ref
string ref
Definition: compareRootHistos.py:25
SmartRefBase::set
void set(DataObject *pObj, long hint_id, long link_id)
Setup smart reference when reading. Must be allowed from external sources.
Definition: SmartRefBase.cpp:36
SmartRef::SmartRef
SmartRef()
Standard Constructor.
Definition: SmartRef.h:94
SmartRef::entry_type
TYPE entry_type
Entry type definition.
Definition: SmartRef.h:85
SmartRefBase::m_hintID
long m_hintID
Object data: ID of the link hint to the identifiable object.
Definition: SmartRefBase.h:60
SmartRef::shouldFollowLink
bool shouldFollowLink(const DataObject *) const
Standard destructor.
Definition: SmartRef.h:153
operator!=
bool operator!=(const SmartRef< TYPE > &ref, int)
Friend helper to check for object existence (will load object)
Definition: SmartRef.h:302
SmartRef::set
void set(DataObject *pObj, long hint_id, long link_id)
Setup smart reference when reading. Must be allowed from external sources.
Definition: SmartRef.h:163
ContainedObject::parent
const ObjectContainerBase * parent() const
Access to parent object.
Definition: ContainedObject.h:62
SmartRefBase::setObjectType
void setObjectType(const ContainedObject *) const
Definition: SmartRefBase.h:107
operator==
bool operator==(const SmartRef< TYPE > &ref, int)
Friend helper to check for object existence (will load object)
Definition: SmartRef.h:290
SmartRef::_setEnvironment
const SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd) const
Set the environment (CONST)
Definition: SmartRef.h:186
SmartRef::INVALID
@ INVALID
Definition: SmartRef.h:83
SmartRef::operator*
const TYPE & operator*() const
Dereference operator.
Definition: SmartRef.h:230
SmartRefBase::m_contd
const ContainedObject * m_contd
Object data: Pointer to the Contained object (if applicable)
Definition: SmartRefBase.h:66
StreamBuffer::VALID
@ VALID
Definition: StreamBuffer.h:121
SmartRef::SmartRef
SmartRef(const SmartRef &copy)
Copy Constructor.
Definition: SmartRef.h:114
SmartRef::_setEnvironment
SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd)
Set the environment (CONST)
Definition: SmartRef.h:193
DataObject
Definition: DataObject.h:40
SmartRef::SmartRef
SmartRef(const DataObject *pObj, long hint, long link, TYPE *obj=nullptr)
Constructor for references to contained objects passing environment.
Definition: SmartRef.h:136
SmartRef::operator<<
friend StreamBuffer & operator<<(StreamBuffer &_s, const SmartRef< TYPE > &ptr)
Output Streamer operator.
Definition: SmartRef.h:245
SmartRef
Kernel objects: SmartRef.
Definition: SmartRef.h:76
SmartRefBase.h
SmartRef::target
TYPE * target()
Access to the object.
Definition: SmartRef.h:262
SmartRefMap
Definition: SmartRef.h:35
SmartRefBase::isEqualEx
bool isEqualEx(const DataObject *pObj, const SmartRefBase &c) const
Extended equality check.
Definition: SmartRefBase.cpp:77
SmartRef::data
TYPE * data()
Access to raw data pointer.
Definition: SmartRef.h:167
ContainedObject.h
SmartRef::operator()
SmartRef< TYPE > & operator()(DataObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:210
SmartRefBase::isEqual
bool isEqual(const ContainedObject *, const SmartRefBase &c) const
Equality operator for ContainedObject like references.
Definition: SmartRefBase.h:82
SmartRef::target
const TYPE * target() const
Access to the object.
Definition: SmartRef.h:269
ContainedObject
Definition: ContainedObject.h:41
SmartRefList
Definition: SmartRef.h:33
SmartRef::type
const std::type_info * type() const
Access to embedded type.
Definition: SmartRef.h:165
StreamBuffer::INVALID
@ INVALID
Definition: StreamBuffer.h:121