The Gaudi Framework  master (37c0b60a)
SmartRef.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 final {
78  friend class SmartRefArray<TYPE>;
79  friend class SmartRefList<TYPE>;
80  friend class SmartRefMap<TYPE>;
81 
84  mutable const TYPE* m_target = nullptr;
85 
86 public:
89  typedef TYPE entry_type;
90 
92  SmartRef( const TYPE* pObject = nullptr ) {
95  m_target = pObject;
96  _setEnvironment( nullptr, nullptr );
97  }
99  SmartRef( const SmartRef& copy ) {
100  m_base.m_hintID = copy.m_base.m_hintID;
101  m_base.m_linkID = copy.m_base.m_linkID;
102  m_target = copy.m_target;
103  _setEnvironment( copy.m_base.m_data, copy.m_base.m_contd );
104  }
106  SmartRef( long hint, long link, const TYPE* obj = nullptr ) {
107  m_base.m_hintID = hint;
108  m_base.m_linkID = link;
109  m_target = obj;
110  _setEnvironment( nullptr, nullptr );
111  }
113  SmartRef( const ContainedObject* pObj, long hint, long link, const TYPE* obj = nullptr ) {
114  m_base.m_hintID = hint;
115  m_base.m_linkID = link;
116  m_target = obj;
117  const DataObject* src = ( pObj ? pObj->parent() : nullptr );
118  _setEnvironment( src, pObj );
119  }
121  SmartRef( const DataObject* pObj, long hint, long link, const TYPE* obj = nullptr ) {
122  m_base.m_hintID = hint;
123  m_base.m_linkID = link;
124  m_target = obj;
125  _setEnvironment( pObj, nullptr );
126  }
128  SmartRef( const DataObject* pObj, long hint, const TYPE* obj = nullptr ) {
129  m_base.m_hintID = hint;
131  m_target = obj;
132  _setEnvironment( pObj, nullptr );
133  }
135  bool shouldFollowLink( const DataObject* ) const { return !m_target && m_base.m_hintID != INVALID; }
137  bool shouldFollowLink( const ContainedObject* ) const {
138  return !m_target && m_base.m_hintID != INVALID && m_base.m_linkID != INVALID;
139  }
141  long hintID() const { return m_base.m_hintID; }
143  long linkID() const { return m_base.m_linkID; }
145  void set( DataObject* pObj, long hint_id, long link_id ) { m_base.set( pObj, hint_id, link_id ); }
147  const std::type_info* type() const { return &typeid( TYPE ); }
149  TYPE* data() { return const_cast<TYPE*>( m_target ); }
150  const TYPE* data() const { return m_target; }
152  const TYPE* target() const {
153  if ( !m_target ) { m_target = dynamic_cast<const TYPE*>( m_base.accessData( m_target ) ); }
154  return m_target;
155  }
157  TYPE* target() {
158  if ( !m_target ) { m_target = dynamic_cast<const TYPE*>( m_base.accessData( m_target ) ); }
159  return const_cast<TYPE*>( m_target );
160  }
162  const std::string& path() const { return m_base.path(); }
164  bool operator==( const SmartRef<TYPE>& c ) const {
165  if ( m_target && c.m_target ) return m_target == c.m_target;
166  if ( !m_target && !c.m_target ) return m_base.isEqual( m_target, c.m_base );
167  if ( m_target && !c.m_target ) return m_base.isEqualEx( m_target, c.m_base );
168  if ( !m_target && c.m_target ) return c.m_base.isEqualEx( c.m_target, m_base );
169  return false;
170  }
171  friend bool operator==( const SmartRef<TYPE>& ref, std::nullptr_t ) { return ref.target() == nullptr; }
172 
174  friend bool operator==( std::nullptr_t, const SmartRef<TYPE>& ref ) { return ref.target() == nullptr; }
176  bool operator!=( const SmartRef<TYPE>& c ) const { return !( this->operator==( c ) ); }
177 
178  friend bool operator!=( const SmartRef<TYPE>& ref, std::nullptr_t ) { return ref.target() != nullptr; }
179 
181  friend bool operator!=( std::nullptr_t, const SmartRef<TYPE>& ref ) { return ref.target() != nullptr; }
182 
184  explicit operator bool() const { return target() != nullptr; }
185 
187  const SmartRef<TYPE>& _setEnvironment( const DataObject* pObj, const ContainedObject* pContd ) const {
188  m_base.m_data = pObj;
189  m_base.m_contd = pContd;
191  return *this;
192  }
194  SmartRef<TYPE>& _setEnvironment( const DataObject* pObj, const ContainedObject* pContd ) {
195  m_base.m_data = pObj;
196  m_base.m_contd = pContd;
198  return *this;
199  }
202  const DataObject* src = ( pObj ? pObj->parent() : nullptr );
203  return _setEnvironment( src, pObj );
204  }
206  const SmartRef<TYPE>& operator()( const ContainedObject* pObj ) const {
207  const DataObject* src = ( pObj ? pObj->parent() : nullptr );
208  return _setEnvironment( src, pObj );
209  }
211  SmartRef<TYPE>& operator()( DataObject* pObj ) { return _setEnvironment( pObj, nullptr ); }
213  const SmartRef<TYPE>& operator()( const DataObject* pObj ) const { return _setEnvironment( pObj, nullptr ); }
216  m_target = c.m_target;
217  m_base.m_hintID = c.m_base.m_hintID;
218  m_base.m_linkID = c.m_base.m_linkID;
219  return _setEnvironment( c.m_base.m_data, c.m_base.m_contd );
220  }
222  SmartRef<TYPE>& operator=( const TYPE* pObject ) {
223  m_target = pObject;
226  return *this;
227  }
229  TYPE& operator*() { return *SmartRef<TYPE>::target(); }
231  const TYPE& operator*() const { return *SmartRef<TYPE>::target(); }
233  TYPE* operator->() { return SmartRef<TYPE>::target(); }
235  const TYPE* operator->() const { return SmartRef<TYPE>::target(); }
237  operator const TYPE*() const { return SmartRef<TYPE>::target(); }
239  operator TYPE*() { return SmartRef<TYPE>::target(); }
243  return s;
244  }
247  m_target = dynamic_cast<const TYPE*>( m_base.readObject( m_target, s ) );
248  return s;
249  }
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 #endif // KERNEL_SMARTREF_H
SmartRef::operator()
const SmartRef< TYPE > & operator()(const ContainedObject *pObj) const
operator() const: assigns parent object for serialisation
Definition: SmartRef.h:206
SmartRef::m_base
SmartRefBase m_base
Definition: SmartRef.h:82
SmartRef::operator->
const TYPE * operator->() const
Dereference operator to const object.
Definition: SmartRef.h:235
SmartRef::operator!=
friend bool operator!=(const SmartRef< TYPE > &ref, std::nullptr_t)
Definition: SmartRef.h:178
std::string
STL class.
SmartRef::operator()
SmartRef< TYPE > & operator()(ContainedObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:201
SmartRef::m_target
const TYPE * m_target
Pointer to target data object.
Definition: SmartRef.h:84
SmartRef::operator=
SmartRef< TYPE > & operator=(const TYPE *pObject)
Assignment.
Definition: SmartRef.h:222
SmartRef::VALID
@ VALID
Definition: SmartRef.h:87
gaudirun.s
string s
Definition: gaudirun.py:346
std::type_info
SmartRef::operator*
TYPE & operator*()
Dereference operator.
Definition: SmartRef.h:229
SmartRef::operator->
TYPE * operator->()
Dereference operator.
Definition: SmartRef.h:233
SmartRefArray
Definition: SmartRef.h:31
SmartRef::operator==
bool operator==(const SmartRef< TYPE > &c) const
Equality operator.
Definition: SmartRef.h:164
SmartRef::data
const TYPE * data() const
Definition: SmartRef.h:150
gaudirun.c
c
Definition: gaudirun.py:525
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:137
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::readRef
StreamBuffer & readRef(StreamBuffer &s)
Read the reference from the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:246
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:141
SmartRef::linkID
long linkID() const
Access link id:
Definition: SmartRef.h:143
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:162
SmartRef::INVALID
@ INVALID
Definition: SmartRef.h:87
SmartRef::target
TYPE * target()
Access to the object.
Definition: SmartRef.h:157
SmartRef::operator>>
friend StreamBuffer & operator>>(StreamBuffer &_s, SmartRef< TYPE > &ptr)
Input Streamer operator.
Definition: SmartRef.h:255
SmartRef::operator!=
bool operator!=(const SmartRef< TYPE > &c) const
NON-Equality operator.
Definition: SmartRef.h:176
SmartRef::operator=
SmartRef< TYPE > & operator=(const SmartRef< TYPE > &c)
Assignment.
Definition: SmartRef.h:215
SmartRef::operator()
const SmartRef< TYPE > & operator()(const DataObject *pObj) const
operator() const: assigns parent object for serialisation
Definition: SmartRef.h:213
GaudiPython.Bindings.nullptr
nullptr
Definition: Bindings.py:87
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::entry_type
TYPE entry_type
Entry type definition.
Definition: SmartRef.h:89
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
Check if link should be followed: link must be valid and object not yet loaded.
Definition: SmartRef.h:135
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:145
ContainedObject::parent
const ObjectContainerBase * parent() const
Access to parent object.
Definition: ContainedObject.h:63
SmartRef::operator==
friend bool operator==(std::nullptr_t, const SmartRef< TYPE > &ref)
Friend helper to check for object existence (will load object)
Definition: SmartRef.h:174
SmartRef::SmartRef
SmartRef(const TYPE *pObject=nullptr)
Standard Constructor.
Definition: SmartRef.h:92
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:171
SmartRef::_setEnvironment
const SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd) const
Set the environment (CONST)
Definition: SmartRef.h:187
SmartRef::operator*
const TYPE & operator*() const
Dereference operator.
Definition: SmartRef.h:231
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)
Friend helper to check for object existence (will load object)
Definition: SmartRef.h:181
SmartRef::SmartRef
SmartRef(const SmartRef &copy)
Copy Constructor.
Definition: SmartRef.h:99
SmartRef::_setEnvironment
SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd)
Set the environment (CONST)
Definition: SmartRef.h:194
DataObject
Definition: DataObject.h:36
SmartRef::SmartRef
SmartRef(const DataObject *pObj, long hint, long link, const TYPE *obj=nullptr)
Constructor for references to contained objects passing environment.
Definition: SmartRef.h:121
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:241
SmartRef::SmartRef
SmartRef(long hint, long link, const TYPE *obj=nullptr)
Constructor.
Definition: SmartRef.h:106
SmartRef::operator<<
friend StreamBuffer & operator<<(StreamBuffer &_s, const SmartRef< TYPE > &ptr)
Output Streamer operator.
Definition: SmartRef.h:252
SmartRef
Kernel objects: SmartRef.
Definition: SmartRef.h:76
SmartRefBase.h
compareRootHistos.ref
ref
Definition: compareRootHistos.py:27
SmartRef::SmartRef
SmartRef(const DataObject *pObj, long hint, const TYPE *obj=nullptr)
Constructor for references to DataObjects passing environment.
Definition: SmartRef.h:128
SmartRef::SmartRef
SmartRef(const ContainedObject *pObj, long hint, long link, const TYPE *obj=nullptr)
Constructor for references to contained objects passing environment.
Definition: SmartRef.h:113
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:149
ContainedObject.h
SmartRef::operator()
SmartRef< TYPE > & operator()(DataObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:211
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:152
SmartRefList
Definition: SmartRef.h:33
SmartRef::type
const std::type_info * type() const
Access to embedded type.
Definition: SmartRef.h:147
StreamBuffer::INVALID
@ INVALID
Definition: StreamBuffer.h:121