The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
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#include <utility>
17
18template <class TYPE>
20template <class TYPE>
22template <class TYPE>
24
63template <class TYPE>
64class SmartRef final {
66 friend class SmartRefArray<TYPE>;
67 friend class SmartRefList<TYPE>;
68 friend class SmartRefMap<TYPE>;
69
72 mutable const TYPE* m_target = nullptr;
73
74public:
77 typedef TYPE entry_type;
78
80 SmartRef( const TYPE* pObject = nullptr ) {
81 m_base.m_hintID = INVALID;
82 m_base.m_linkID = INVALID;
83 m_target = pObject;
84 _setEnvironment( nullptr, nullptr );
85 }
86
87 SmartRef( const SmartRef& copy ) {
88 m_base.m_hintID = copy.m_base.m_hintID;
89 m_base.m_linkID = copy.m_base.m_linkID;
90 m_target = copy.m_target;
92 }
93
94 SmartRef( long hint, long link, const TYPE* obj = nullptr ) {
95 m_base.m_hintID = hint;
96 m_base.m_linkID = link;
97 m_target = obj;
98 _setEnvironment( nullptr, nullptr );
99 }
100
101 SmartRef( const ContainedObject* pObj, long hint, long link, const TYPE* obj = nullptr ) {
102 m_base.m_hintID = hint;
103 m_base.m_linkID = link;
104 m_target = obj;
105 const DataObject* src = ( pObj ? pObj->parent() : nullptr );
106 _setEnvironment( src, pObj );
107 }
108
109 SmartRef( const DataObject* pObj, long hint, long link, const TYPE* obj = nullptr ) {
110 m_base.m_hintID = hint;
111 m_base.m_linkID = link;
112 m_target = obj;
113 _setEnvironment( pObj, nullptr );
114 }
115
116 SmartRef( const DataObject* pObj, long hint, const TYPE* obj = nullptr ) {
117 m_base.m_hintID = hint;
118 m_base.m_linkID = INVALID;
119 m_target = obj;
120 _setEnvironment( pObj, nullptr );
121 }
122
123 bool shouldFollowLink( const DataObject* ) const { return !m_target && m_base.m_hintID != INVALID; }
125 bool shouldFollowLink( const ContainedObject* ) const {
126 return !m_target && m_base.m_hintID != INVALID && m_base.m_linkID != INVALID;
127 }
128
129 long hintID() const { return m_base.m_hintID; }
131 long linkID() const { return m_base.m_linkID; }
133 void set( DataObject* pObj, long hint_id, long link_id ) { m_base.set( pObj, hint_id, link_id ); }
135 const std::type_info* type() const { return &typeid( TYPE ); }
137 TYPE* data() { return const_cast<TYPE*>( m_target ); }
138 const TYPE* data() const { return m_target; }
140 const TYPE* target() const {
141 if ( !m_target ) { m_target = dynamic_cast<const TYPE*>( m_base.accessData( m_target ) ); }
142 return m_target;
143 }
144
145 TYPE* target() {
146 if ( !m_target ) { m_target = dynamic_cast<const TYPE*>( m_base.accessData( m_target ) ); }
147 return const_cast<TYPE*>( m_target );
148 }
149
150 const std::string& path() const { return m_base.path(); }
152 bool operator==( const SmartRef<TYPE>& c ) const {
153 if ( m_target && c.m_target ) return m_target == c.m_target;
154 if ( !m_target && !c.m_target ) return m_base.isEqual( m_target, c.m_base );
155 if ( m_target && !c.m_target ) return m_base.isEqualEx( m_target, c.m_base );
156 if ( !m_target && c.m_target ) return c.m_base.isEqualEx( c.m_target, m_base );
157 return false;
158 }
159 bool operator==( SmartRef<TYPE>& rhs ) { return std::as_const( *this ) == std::as_const( rhs ); }
160
161 friend bool operator==( SmartRef<TYPE>& lhs, TYPE* rhs ) { return lhs.target() == rhs; }
162 friend bool operator==( const SmartRef<TYPE>& lhs, const TYPE* rhs ) { return lhs.target() == rhs; }
163
165 explicit operator bool() const { return target() != nullptr; }
166
168 const SmartRef<TYPE>& _setEnvironment( const DataObject* pObj, const ContainedObject* pContd ) const {
169 m_base.m_data = pObj;
170 m_base.m_contd = pContd;
171 m_base.setObjectType( data() );
172 return *this;
173 }
174
176 m_base.m_data = pObj;
177 m_base.m_contd = pContd;
178 m_base.setObjectType( data() );
179 return *this;
180 }
181
183 const DataObject* src = ( pObj ? pObj->parent() : nullptr );
184 return _setEnvironment( src, pObj );
185 }
186
187 const SmartRef<TYPE>& operator()( const ContainedObject* pObj ) const {
188 const DataObject* src = ( pObj ? pObj->parent() : nullptr );
189 return _setEnvironment( src, pObj );
190 }
191
192 SmartRef<TYPE>& operator()( DataObject* pObj ) { return _setEnvironment( pObj, nullptr ); }
194 const SmartRef<TYPE>& operator()( const DataObject* pObj ) const { return _setEnvironment( pObj, nullptr ); }
197 m_target = c.m_target;
198 m_base.m_hintID = c.m_base.m_hintID;
199 m_base.m_linkID = c.m_base.m_linkID;
200 return _setEnvironment( c.m_base.m_data, c.m_base.m_contd );
201 }
202
203 SmartRef<TYPE>& operator=( const TYPE* pObject ) {
204 m_target = pObject;
205 m_base.m_hintID = INVALID;
206 m_base.m_linkID = INVALID;
207 return *this;
208 }
209
210 TYPE& operator*() { return *SmartRef<TYPE>::target(); }
212 const TYPE& operator*() const { return *SmartRef<TYPE>::target(); }
214 TYPE* operator->() { return SmartRef<TYPE>::target(); }
216 const TYPE* operator->() const { return SmartRef<TYPE>::target(); }
218 operator const TYPE*() const { return SmartRef<TYPE>::target(); }
220 operator TYPE*() { return SmartRef<TYPE>::target(); }
223 m_base.writeObject( m_target, s );
224 return s;
225 }
226
228 m_target = dynamic_cast<const TYPE*>( m_base.readObject( m_target, s ) );
229 return s;
230 }
231
232 // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
233 friend StreamBuffer& operator<<( StreamBuffer& _s, const SmartRef<TYPE>& ptr ) { return ptr.writeRef( _s ); }
235 // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
236 friend StreamBuffer& operator>>( StreamBuffer& _s, SmartRef<TYPE>& ptr ) { return ptr.readRef( _s ); }
237};
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
const ObjectContainerBase * parent() const
Access to parent object.
A DataObject is the base class of any identifiable object on any data store.
Definition DataObject.h:37
User example objects: SmartRefBase.
long m_linkID
Object data: ID of the object within the identifiable container (if any)
long m_hintID
Object data: ID of the link hint to the identifiable object.
const ContainedObject * m_contd
Object data: Pointer to the Contained object (if applicable)
const DataObject * m_data
Object data: Pointer to the identifiable object the link originates.
TYPE * target()
Access to the object.
Definition SmartRef.h:145
SmartRef(long hint, long link, const TYPE *obj=nullptr)
Constructor.
Definition SmartRef.h:94
SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd)
Set the environment (CONST)
Definition SmartRef.h:175
TYPE & operator*()
Dereference operator.
Definition SmartRef.h:210
SmartRef(const TYPE *pObject=nullptr)
Standard Constructor.
Definition SmartRef.h:80
bool operator==(const SmartRef< TYPE > &c) const
Equality operator.
Definition SmartRef.h:152
const TYPE * operator->() const
Dereference operator to const object.
Definition SmartRef.h:216
@ INVALID
Definition SmartRef.h:75
const TYPE * target() const
Access to the object.
Definition SmartRef.h:140
const TYPE * data() const
Definition SmartRef.h:138
SmartRef(const DataObject *pObj, long hint, long link, const TYPE *obj=nullptr)
Constructor for references to contained objects passing environment.
Definition SmartRef.h:109
StreamBuffer & readRef(StreamBuffer &s)
Read the reference from the stream buffer (needed due to stupid G++ compiler)
Definition SmartRef.h:227
StreamBuffer & writeRef(StreamBuffer &s) const
Write the reference to the stream buffer (needed due to stupid G++ compiler)
Definition SmartRef.h:222
bool operator==(SmartRef< TYPE > &rhs)
Definition SmartRef.h:159
const std::type_info * type() const
Access to embedded type.
Definition SmartRef.h:135
friend StreamBuffer & operator<<(StreamBuffer &_s, const SmartRef< TYPE > &ptr)
Output Streamer operator.
Definition SmartRef.h:233
SmartRef< TYPE > & operator=(const SmartRef< TYPE > &c)
Assignment.
Definition SmartRef.h:196
void set(DataObject *pObj, long hint_id, long link_id)
Setup smart reference when reading. Must be allowed from external sources.
Definition SmartRef.h:133
const TYPE & operator*() const
Dereference operator.
Definition SmartRef.h:212
bool shouldFollowLink(const DataObject *) const
Check if link should be followed: link must be valid and object not yet loaded.
Definition SmartRef.h:123
SmartRef(const SmartRef &copy)
Copy Constructor.
Definition SmartRef.h:87
const SmartRef< TYPE > & operator()(const DataObject *pObj) const
operator() const: assigns parent object for serialisation
Definition SmartRef.h:194
SmartRef(const DataObject *pObj, long hint, const TYPE *obj=nullptr)
Constructor for references to DataObjects passing environment.
Definition SmartRef.h:116
SmartRef(const ContainedObject *pObj, long hint, long link, const TYPE *obj=nullptr)
Constructor for references to contained objects passing environment.
Definition SmartRef.h:101
SmartRefBase m_base
Definition SmartRef.h:70
TYPE entry_type
Entry type definition.
Definition SmartRef.h:77
const std::string & path() const
Return the path of the linked object inside the data store.
Definition SmartRef.h:150
SmartRef< TYPE > & operator()(ContainedObject *pObj)
operator(): assigns parent object for serialisation
Definition SmartRef.h:182
long hintID() const
Access hint id:
Definition SmartRef.h:129
SmartRef< TYPE > & operator()(DataObject *pObj)
operator(): assigns parent object for serialisation
Definition SmartRef.h:192
const TYPE * m_target
Pointer to target data object.
Definition SmartRef.h:72
long linkID() const
Access link id:
Definition SmartRef.h:131
friend bool operator==(SmartRef< TYPE > &lhs, TYPE *rhs)
Definition SmartRef.h:161
bool shouldFollowLink(const ContainedObject *) const
Check if link should be followed: link must be valid and object not yet loaded.
Definition SmartRef.h:125
const SmartRef< TYPE > & operator()(const ContainedObject *pObj) const
operator() const: assigns parent object for serialisation
Definition SmartRef.h:187
const SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd) const
Set the environment (CONST)
Definition SmartRef.h:168
friend bool operator==(const SmartRef< TYPE > &lhs, const TYPE *rhs)
Definition SmartRef.h:162
friend StreamBuffer & operator>>(StreamBuffer &_s, SmartRef< TYPE > &ptr)
Input Streamer operator.
Definition SmartRef.h:236
TYPE * data()
Access to raw data pointer.
Definition SmartRef.h:137
SmartRef< TYPE > & operator=(const TYPE *pObject)
Assignment.
Definition SmartRef.h:203
TYPE * operator->()
Dereference operator.
Definition SmartRef.h:214
The stream buffer is a small object collecting object data.