The Gaudi Framework  v40r0 (475e45c1)
SmartRef.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 #pragma once
12 
15 #include <typeinfo>
16 
17 template <class TYPE>
19 template <class TYPE>
21 template <class TYPE>
23 
62 template <class TYPE>
63 class SmartRef final {
65  friend class SmartRefArray<TYPE>;
66  friend class SmartRefList<TYPE>;
67  friend class SmartRefMap<TYPE>;
68 
71  mutable const TYPE* m_target = nullptr;
72 
73 public:
76  typedef TYPE entry_type;
77 
79  SmartRef( const TYPE* pObject = nullptr ) {
82  m_target = pObject;
83  _setEnvironment( nullptr, nullptr );
84  }
86  SmartRef( const SmartRef& copy ) {
89  m_target = copy.m_target;
91  }
93  SmartRef( long hint, long link, const TYPE* obj = nullptr ) {
94  m_base.m_hintID = hint;
95  m_base.m_linkID = link;
96  m_target = obj;
97  _setEnvironment( nullptr, nullptr );
98  }
100  SmartRef( const ContainedObject* pObj, long hint, long link, const TYPE* obj = nullptr ) {
101  m_base.m_hintID = hint;
102  m_base.m_linkID = link;
103  m_target = obj;
104  const DataObject* src = ( pObj ? pObj->parent() : nullptr );
105  _setEnvironment( src, pObj );
106  }
108  SmartRef( const DataObject* pObj, long hint, long link, const TYPE* obj = nullptr ) {
109  m_base.m_hintID = hint;
110  m_base.m_linkID = link;
111  m_target = obj;
112  _setEnvironment( pObj, nullptr );
113  }
115  SmartRef( const DataObject* pObj, long hint, const TYPE* obj = nullptr ) {
116  m_base.m_hintID = hint;
118  m_target = obj;
119  _setEnvironment( pObj, nullptr );
120  }
122  bool shouldFollowLink( const DataObject* ) const { return !m_target && m_base.m_hintID != INVALID; }
124  bool shouldFollowLink( const ContainedObject* ) const {
125  return !m_target && m_base.m_hintID != INVALID && m_base.m_linkID != INVALID;
126  }
128  long hintID() const { return m_base.m_hintID; }
130  long linkID() const { return m_base.m_linkID; }
132  void set( DataObject* pObj, long hint_id, long link_id ) { m_base.set( pObj, hint_id, link_id ); }
134  const std::type_info* type() const { return &typeid( TYPE ); }
136  TYPE* data() { return const_cast<TYPE*>( m_target ); }
137  const TYPE* data() const { return m_target; }
139  const TYPE* target() const {
140  if ( !m_target ) { m_target = dynamic_cast<const TYPE*>( m_base.accessData( m_target ) ); }
141  return m_target;
142  }
144  TYPE* target() {
145  if ( !m_target ) { m_target = dynamic_cast<const TYPE*>( m_base.accessData( m_target ) ); }
146  return const_cast<TYPE*>( m_target );
147  }
149  const std::string& path() const { return m_base.path(); }
151  bool operator==( const SmartRef<TYPE>& c ) const {
152  if ( m_target && c.m_target ) return m_target == c.m_target;
153  if ( !m_target && !c.m_target ) return m_base.isEqual( m_target, c.m_base );
154  if ( m_target && !c.m_target ) return m_base.isEqualEx( m_target, c.m_base );
155  if ( !m_target && c.m_target ) return c.m_base.isEqualEx( c.m_target, m_base );
156  return false;
157  }
158  friend bool operator==( const SmartRef<TYPE>& ref, std::nullptr_t ) { return ref.target() == nullptr; }
159 
161  friend bool operator==( std::nullptr_t, const SmartRef<TYPE>& ref ) { return ref.target() == nullptr; }
163  bool operator!=( const SmartRef<TYPE>& c ) const { return !( this->operator==( c ) ); }
164 
165  friend bool operator!=( const SmartRef<TYPE>& ref, std::nullptr_t ) { return ref.target() != nullptr; }
166 
168  friend bool operator!=( std::nullptr_t, const SmartRef<TYPE>& ref ) { return ref.target() != nullptr; }
169 
171  explicit operator bool() const { return target() != nullptr; }
172 
174  const SmartRef<TYPE>& _setEnvironment( const DataObject* pObj, const ContainedObject* pContd ) const {
175  m_base.m_data = pObj;
176  m_base.m_contd = pContd;
178  return *this;
179  }
181  SmartRef<TYPE>& _setEnvironment( const DataObject* pObj, const ContainedObject* pContd ) {
182  m_base.m_data = pObj;
183  m_base.m_contd = pContd;
185  return *this;
186  }
189  const DataObject* src = ( pObj ? pObj->parent() : nullptr );
190  return _setEnvironment( src, pObj );
191  }
193  const SmartRef<TYPE>& operator()( const ContainedObject* pObj ) const {
194  const DataObject* src = ( pObj ? pObj->parent() : nullptr );
195  return _setEnvironment( src, pObj );
196  }
198  SmartRef<TYPE>& operator()( DataObject* pObj ) { return _setEnvironment( pObj, nullptr ); }
200  const SmartRef<TYPE>& operator()( const DataObject* pObj ) const { return _setEnvironment( pObj, nullptr ); }
203  m_target = c.m_target;
204  m_base.m_hintID = c.m_base.m_hintID;
205  m_base.m_linkID = c.m_base.m_linkID;
206  return _setEnvironment( c.m_base.m_data, c.m_base.m_contd );
207  }
209  SmartRef<TYPE>& operator=( const TYPE* pObject ) {
210  m_target = pObject;
213  return *this;
214  }
216  TYPE& operator*() { return *SmartRef<TYPE>::target(); }
218  const TYPE& operator*() const { return *SmartRef<TYPE>::target(); }
220  TYPE* operator->() { return SmartRef<TYPE>::target(); }
222  const TYPE* operator->() const { return SmartRef<TYPE>::target(); }
224  operator const TYPE*() const { return SmartRef<TYPE>::target(); }
226  operator TYPE*() { return SmartRef<TYPE>::target(); }
230  return s;
231  }
234  m_target = dynamic_cast<const TYPE*>( m_base.readObject( m_target, s ) );
235  return s;
236  }
238  // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
239  friend StreamBuffer& operator<<( StreamBuffer& _s, const SmartRef<TYPE>& ptr ) { return ptr.writeRef( _s ); }
241  // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
242  friend StreamBuffer& operator>>( StreamBuffer& _s, SmartRef<TYPE>& ptr ) { return ptr.readRef( _s ); }
243 };
SmartRef::operator()
const SmartRef< TYPE > & operator()(const ContainedObject *pObj) const
operator() const: assigns parent object for serialisation
Definition: SmartRef.h:193
SmartRef::m_base
SmartRefBase m_base
Definition: SmartRef.h:69
SmartRef::operator->
const TYPE * operator->() const
Dereference operator to const object.
Definition: SmartRef.h:222
SmartRef::operator!=
friend bool operator!=(const SmartRef< TYPE > &ref, std::nullptr_t)
Definition: SmartRef.h:165
SmartRef::operator()
SmartRef< TYPE > & operator()(ContainedObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:188
SmartRef::m_target
const TYPE * m_target
Pointer to target data object.
Definition: SmartRef.h:71
SmartRef::operator=
SmartRef< TYPE > & operator=(const TYPE *pObject)
Assignment.
Definition: SmartRef.h:209
SmartRef::VALID
@ VALID
Definition: SmartRef.h:74
gaudirun.s
string s
Definition: gaudirun.py:346
SmartRef::operator*
TYPE & operator*()
Dereference operator.
Definition: SmartRef.h:216
SmartRef::operator->
TYPE * operator->()
Dereference operator.
Definition: SmartRef.h:220
SmartRefArray
Definition: SmartRef.h:18
SmartRef::operator==
bool operator==(const SmartRef< TYPE > &c) const
Equality operator.
Definition: SmartRef.h:151
SmartRef::data
const TYPE * data() const
Definition: SmartRef.h:137
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:124
SmartRefBase
User example objects: SmartRefBase.
Definition: SmartRefBase.h:45
StreamBuffer
Definition: StreamBuffer.h:48
SmartRefBase::m_linkID
long m_linkID
Object data: ID of the object within the identifiable container (if any)
Definition: SmartRefBase.h:50
SmartRef::readRef
StreamBuffer & readRef(StreamBuffer &s)
Read the reference from the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:233
SmartRefBase::readObject
DataObject * readObject(const DataObject *, StreamBuffer &s) const
Input streamer for DataObject like references.
Definition: SmartRefBase.h:84
SmartRef::hintID
long hintID() const
Access hint id:
Definition: SmartRef.h:128
SmartRef::linkID
long linkID() const
Access link id:
Definition: SmartRef.h:130
SmartRefBase::writeObject
void writeObject(const DataObject *pObject, StreamBuffer &s) const
Output streamer for DataObject like references.
Definition: SmartRefBase.h:78
SmartRef::path
const std::string & path() const
Return the path of the linked object inside the data store.
Definition: SmartRef.h:149
SmartRef::INVALID
@ INVALID
Definition: SmartRef.h:74
SmartRef::target
TYPE * target()
Access to the object.
Definition: SmartRef.h:144
SmartRef::operator>>
friend StreamBuffer & operator>>(StreamBuffer &_s, SmartRef< TYPE > &ptr)
Input Streamer operator.
Definition: SmartRef.h:242
SmartRef::operator!=
bool operator!=(const SmartRef< TYPE > &c) const
NON-Equality operator.
Definition: SmartRef.h:163
SmartRef::operator=
SmartRef< TYPE > & operator=(const SmartRef< TYPE > &c)
Assignment.
Definition: SmartRef.h:202
SmartRef::operator()
const SmartRef< TYPE > & operator()(const DataObject *pObj) const
operator() const: assigns parent object for serialisation
Definition: SmartRef.h:200
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:81
SmartRefBase::m_data
const DataObject * m_data
Object data: Pointer to the identifiable object the link originates.
Definition: SmartRefBase.h:52
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:24
SmartRef::entry_type
TYPE entry_type
Entry type definition.
Definition: SmartRef.h:76
SmartRefBase::m_hintID
long m_hintID
Object data: ID of the link hint to the identifiable object.
Definition: SmartRefBase.h:48
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:122
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:132
ContainedObject::parent
const ObjectContainerBase * parent() const
Access to parent object.
Definition: ContainedObject.h:59
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:161
SmartRef::SmartRef
SmartRef(const TYPE *pObject=nullptr)
Standard Constructor.
Definition: SmartRef.h:79
SmartRefBase::setObjectType
void setObjectType(const ContainedObject *) const
Definition: SmartRefBase.h:95
SmartRef::operator==
friend bool operator==(const SmartRef< TYPE > &ref, std::nullptr_t)
Definition: SmartRef.h:158
SmartRef::_setEnvironment
const SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd) const
Set the environment (CONST)
Definition: SmartRef.h:174
SmartRef::operator*
const TYPE & operator*() const
Dereference operator.
Definition: SmartRef.h:218
SmartRefBase::m_contd
const ContainedObject * m_contd
Object data: Pointer to the Contained object (if applicable)
Definition: SmartRefBase.h:54
StreamBuffer::VALID
@ VALID
Definition: StreamBuffer.h:117
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:168
SmartRef::SmartRef
SmartRef(const SmartRef &copy)
Copy Constructor.
Definition: SmartRef.h:86
SmartRef::_setEnvironment
SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd)
Set the environment (CONST)
Definition: SmartRef.h:181
DataObject
Definition: DataObject.h:37
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:108
SmartRefBase::accessData
const ContainedObject * accessData(const ContainedObject *typ) const
Load on demand of ContainedObject like references.
Definition: SmartRefBase.cpp:31
SmartRef::writeRef
StreamBuffer & writeRef(StreamBuffer &s) const
Write the reference to the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:228
SmartRef::SmartRef
SmartRef(long hint, long link, const TYPE *obj=nullptr)
Constructor.
Definition: SmartRef.h:93
SmartRef::operator<<
friend StreamBuffer & operator<<(StreamBuffer &_s, const SmartRef< TYPE > &ptr)
Output Streamer operator.
Definition: SmartRef.h:239
SmartRef
Kernel objects: SmartRef.
Definition: SmartRef.h:63
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:115
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:100
SmartRefMap
Definition: SmartRef.h:22
SmartRefBase::isEqualEx
bool isEqualEx(const DataObject *pObj, const SmartRefBase &c) const
Extended equality check.
Definition: SmartRefBase.cpp:59
SmartRef::data
TYPE * data()
Access to raw data pointer.
Definition: SmartRef.h:136
ContainedObject.h
SmartRef::operator()
SmartRef< TYPE > & operator()(DataObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:198
SmartRefBase::isEqual
bool isEqual(const ContainedObject *, const SmartRefBase &c) const
Equality operator for ContainedObject like references.
Definition: SmartRefBase.h:70
ContainedObject
Definition: ContainedObject.h:37
SmartRef::target
const TYPE * target() const
Access to the object.
Definition: SmartRef.h:139
SmartRefList
Definition: SmartRef.h:20
SmartRef::type
const std::type_info * type() const
Access to embedded type.
Definition: SmartRef.h:134
StreamBuffer::INVALID
@ INVALID
Definition: StreamBuffer.h:117