Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r16 (ea80daf8)
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 {
171  if ( !m_target ) { m_target = dynamic_cast<const TYPE*>( m_base.accessData( m_target ) ); }
172  return m_target;
173  }
175  TYPE* target() {
176  if ( !m_target ) { m_target = dynamic_cast<const TYPE*>( m_base.accessData( m_target ) ); }
177  return const_cast<TYPE*>( m_target );
178  }
180  const std::string& path() const { return m_base.path(); }
182  bool operator==( const SmartRef<TYPE>& c ) const {
183  if ( m_target && c.m_target ) return m_target == c.m_target;
184  if ( !m_target && !c.m_target ) return m_base.isEqual( m_target, c.m_base );
185  if ( m_target && !c.m_target ) return m_base.isEqualEx( m_target, c.m_base );
186  if ( !m_target && c.m_target ) return c.m_base.isEqualEx( c.m_target, m_base );
187  return false;
188  }
189  /*[[deprecated]]*/ friend bool operator==( const SmartRef<TYPE>& ref, int ) { return ref.target() == nullptr; }
190  friend bool operator==( const SmartRef<TYPE>& ref, std::nullptr_t ) { return ref.target() == nullptr; }
191 
193  /*[[deprecated]]*/ friend bool operator==( int, const SmartRef<TYPE>& ref ) { return ref.target() == nullptr; }
194  friend bool operator==( std::nullptr_t, const SmartRef<TYPE>& ref ) { return ref.target() == nullptr; }
196  bool operator!=( const SmartRef<TYPE>& c ) const { return !( this->operator==( c ) ); }
197 
198  /*[[deprecated]]*/ friend bool operator!=( const SmartRef<TYPE>& ref, int ) { return ref.target() != nullptr; }
199 
200  friend bool operator!=( const SmartRef<TYPE>& ref, std::nullptr_t ) { return ref.target() != nullptr; }
201 
203  /*[[deprecated]]*/ friend bool operator!=( int, const SmartRef<TYPE>& ref ) { return ref.target() != nullptr; }
204 
205  friend bool operator!=( std::nullptr_t, const SmartRef<TYPE>& ref ) { return ref.target() != nullptr; }
206 
208  explicit operator bool() const { return target() != nullptr; }
209 
211  const SmartRef<TYPE>& _setEnvironment( const DataObject* pObj, const ContainedObject* pContd ) const {
212  m_base.m_data = pObj;
213  m_base.m_contd = pContd;
215  return *this;
216  }
218  SmartRef<TYPE>& _setEnvironment( const DataObject* pObj, const ContainedObject* pContd ) {
219  m_base.m_data = pObj;
220  m_base.m_contd = pContd;
222  return *this;
223  }
226  const DataObject* src = ( pObj ? pObj->parent() : nullptr );
227  return _setEnvironment( src, pObj );
228  }
230  const SmartRef<TYPE>& operator()( const ContainedObject* pObj ) const {
231  const DataObject* src = ( pObj ? pObj->parent() : nullptr );
232  return _setEnvironment( src, pObj );
233  }
235  SmartRef<TYPE>& operator()( DataObject* pObj ) { return _setEnvironment( pObj, nullptr ); }
237  const SmartRef<TYPE>& operator()( const DataObject* pObj ) const { return _setEnvironment( pObj, nullptr ); }
240  m_target = c.m_target;
241  m_base.m_hintID = c.m_base.m_hintID;
242  m_base.m_linkID = c.m_base.m_linkID;
243  return _setEnvironment( c.m_base.m_data, c.m_base.m_contd );
244  }
246  SmartRef<TYPE>& operator=( TYPE* pObject ) {
247  m_target = pObject;
250  return *this;
251  }
253  TYPE& operator*() { return *SmartRef<TYPE>::target(); }
255  const TYPE& operator*() const { return *SmartRef<TYPE>::target(); }
257  TYPE* operator->() { return SmartRef<TYPE>::target(); }
259  const TYPE* operator->() const { return SmartRef<TYPE>::target(); }
261  operator const TYPE*() const { return SmartRef<TYPE>::target(); }
263  operator TYPE*() { return SmartRef<TYPE>::target(); }
267  return s;
268  }
271  m_target = dynamic_cast<TYPE*>( m_base.readObject( m_target, s ) );
272  return s;
273  }
275  // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
276  friend StreamBuffer& operator<<( StreamBuffer& _s, const SmartRef<TYPE>& ptr ) { return ptr.writeRef( _s ); }
278  // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
279  friend StreamBuffer& operator>>( StreamBuffer& _s, SmartRef<TYPE>& ptr ) { return ptr.readRef( _s ); }
280 };
281 
282 #endif // KERNEL_SMARTREF_H
SmartRef::operator()
const SmartRef< TYPE > & operator()(const ContainedObject *pObj) const
operator() const: assigns parent object for serialisation
Definition: SmartRef.h:230
SmartRef::m_base
SmartRefBase m_base
Definition: SmartRef.h:88
SmartRef::operator->
const TYPE * operator->() const
Dereference operator to const object.
Definition: SmartRef.h:259
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
SmartRef::operator!=
friend bool operator!=(const SmartRef< TYPE > &ref, std::nullptr_t)
Definition: SmartRef.h:200
std::string
STL class.
SmartRef::operator==
friend bool operator==(const SmartRef< TYPE > &ref, int)
Definition: SmartRef.h:189
SmartRef::operator()
SmartRef< TYPE > & operator()(ContainedObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:225
SmartRef::operator=
SmartRef< TYPE > & operator=(TYPE *pObject)
Assignment.
Definition: SmartRef.h:246
SmartRef::m_target
const TYPE * m_target
Pointer to target data object.
Definition: SmartRef.h:90
gaudirun.s
string s
Definition: gaudirun.py:348
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:253
SmartRef::operator->
TYPE * operator->()
Dereference operator.
Definition: SmartRef.h:257
SmartRefArray
Definition: SmartRef.h:31
SmartRef::operator==
bool operator==(const SmartRef< TYPE > &c) const
Equality operator.
Definition: SmartRef.h:182
SmartRef::operator==
friend bool operator==(int, const SmartRef< TYPE > &ref)
Friend helper to check for object existence (will load object)
Definition: SmartRef.h:193
SmartRef::data
const TYPE * data() const
Definition: SmartRef.h:168
gaudirun.c
c
Definition: gaudirun.py:527
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
SmartRef::operator!=
friend bool operator!=(int, const SmartRef< TYPE > &ref)
Friend helper to check for object existence (will load object)
Definition: SmartRef.h:203
SmartRefBase::m_linkID
long m_linkID
Object data: ID of the object within the identifiable container (if any)
Definition: SmartRefBase.h:62
SmartRef::readRef
StreamBuffer & readRef(StreamBuffer &s)
Read the reference from the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:270
SmartRefBase::readObject
DataObject * readObject(const DataObject *, StreamBuffer &s) const
Input streamer for DataObject like references.
Definition: SmartRefBase.h:96
std::nullptr_t
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
SmartRefBase::writeObject
void writeObject(const DataObject *pObject, StreamBuffer &s) const
Output streamer for DataObject like references.
Definition: SmartRefBase.h:90
SmartRef::path
const std::string & path() const
Return the path of the linked object inside the data store.
Definition: SmartRef.h:180
SmartRef::target
TYPE * target()
Access to the object.
Definition: SmartRef.h:175
SmartRef::operator!=
friend bool operator!=(const SmartRef< TYPE > &ref, int)
Definition: SmartRef.h:198
SmartRef::operator>>
friend StreamBuffer & operator>>(StreamBuffer &_s, SmartRef< TYPE > &ptr)
Input Streamer operator.
Definition: SmartRef.h:279
SmartRef::operator!=
bool operator!=(const SmartRef< TYPE > &c) const
NON-Equality operator.
Definition: SmartRef.h:196
SmartRef::operator=
SmartRef< TYPE > & operator=(const SmartRef< TYPE > &c)
Assignment.
Definition: SmartRef.h:239
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:237
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:92
SmartRefBase::path
const std::string & path() const
Shortcut to access the path to the linked object.
Definition: SmartRefBase.cpp:93
SmartRefBase::m_data
const DataObject * m_data
Object data: Pointer to the identifiable object the link originates.
Definition: SmartRefBase.h:64
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
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
SmartRef::operator==
friend bool operator==(std::nullptr_t, const SmartRef< TYPE > &ref)
Definition: SmartRef.h:194
SmartRefBase::setObjectType
void setObjectType(const ContainedObject *) const
Definition: SmartRefBase.h:107
SmartRef::operator==
friend bool operator==(const SmartRef< TYPE > &ref, std::nullptr_t)
Definition: SmartRef.h:190
SmartRef::_setEnvironment
const SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd) const
Set the environment (CONST)
Definition: SmartRef.h:211
SmartRef::INVALID
@ INVALID
Definition: SmartRef.h:83
SmartRef::operator*
const TYPE & operator*() const
Dereference operator.
Definition: SmartRef.h:255
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::operator!=
friend bool operator!=(std::nullptr_t, const SmartRef< TYPE > &ref)
Definition: SmartRef.h:205
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:218
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
SmartRefBase::accessData
const ContainedObject * accessData(const ContainedObject *typ) const
Load on demand of ContainedObject like references.
Definition: SmartRefBase.cpp:43
SmartRef::writeRef
StreamBuffer & writeRef(StreamBuffer &s) const
Write the reference to the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:265
SmartRef::operator<<
friend StreamBuffer & operator<<(StreamBuffer &_s, const SmartRef< TYPE > &ptr)
Output Streamer operator.
Definition: SmartRef.h:276
SmartRef
Kernel objects: SmartRef.
Definition: SmartRef.h:76
SmartRefBase.h
compareRootHistos.ref
ref
Definition: compareRootHistos.py:28
SmartRefMap
Definition: SmartRef.h:35
SmartRefBase::isEqualEx
bool isEqualEx(const DataObject *pObj, const SmartRefBase &c) const
Extended equality check.
Definition: SmartRefBase.cpp:71
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:235
SmartRefBase::isEqual
bool isEqual(const ContainedObject *, const SmartRefBase &c) const
Equality operator for ContainedObject like references.
Definition: SmartRefBase.h:82
ContainedObject
Definition: ContainedObject.h:41
SmartRef::target
const TYPE * target() const
Access to the object.
Definition: SmartRef.h:170
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