| The Gaudi Framework v29r0 (ff2e7097)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

SmartRef.h
Go to the documentation of this file.
1 // ====================================================================
2 // SmartRef.h
3 // --------------------------------------------------------------------
4 //
5 // Package : Kernel
6 //
7 // Author : Markus Frank
8 //
9 // ====================================================================
10 #ifndef KERNEL_SMARTREF_H
11 #define KERNEL_SMARTREF_H 1
12
13 // Include files
14 #include "GaudiKernel/ContainedObject.h"
15 #include "GaudiKernel/SmartRefBase.h"
16
17 #include <typeinfo>
18
19 // Forward declarations
20 template <class TYPE>
21 class SmartRefArray;
22 template <class TYPE>
23 class SmartRefList;
24 template <class TYPE>
25 class SmartRefMap;
26
65 template <class TYPE>
66 class SmartRef
67 {
69 friend class SmartRefArray<TYPE>;
70 friend class SmartRefList<TYPE>;
71 friend class SmartRefMap<TYPE>;
72
73 public:
74 enum { VALID = StreamBuffer::VALID, INVALID = StreamBuffer::INVALID };
76 typedef TYPE entry_type;
77
78 protected:
79 SmartRefBase m_base;
81 mutable const TYPE* m_target = nullptr;
82
83 public:
85 SmartRef()
86 {
87 m_base.m_hintID = INVALID;
88 m_base.m_linkID = INVALID;
89 _setEnvironment( 0, 0 );
90 }
92 SmartRef( TYPE* pObject )
93 {
94 m_base.m_hintID = INVALID;
95 m_base.m_linkID = INVALID;
96 m_target = pObject;
97 _setEnvironment( 0, 0 );
98 }
100 SmartRef( const TYPE* pObject )
101 {
102 m_base.m_hintID = INVALID;
103 m_base.m_linkID = INVALID;
104 m_target = const_cast<TYPE*>( pObject );
105 _setEnvironment( 0, 0 );
106 }
108 SmartRef( const SmartRef& copy )
109 {
110 m_base.m_hintID = copy.m_base.m_hintID;
111 m_base.m_linkID = copy.m_base.m_linkID;
112 m_target = copy.m_target;
113 _setEnvironment( copy.m_base.m_data, copy.m_base.m_contd );
114 }
116 SmartRef( long hint, long link, TYPE* obj = nullptr )
117 {
118 m_base.m_hintID = hint;
119 m_base.m_linkID = link;
120 m_target = obj;
121 _setEnvironment( 0, 0 );
122 }
124 SmartRef( const ContainedObject* pObj, long hint, long link, TYPE* obj = nullptr )
125 {
126 m_base.m_hintID = hint;
127 m_base.m_linkID = link;
128 m_target = obj;
129 const DataObject* src = ( 0 == pObj ) ? 0 : pObj->parent();
130 _setEnvironment( src, pObj );
131 }
133 SmartRef( const DataObject* pObj, long hint, long link, TYPE* obj = nullptr )
134 {
135 m_base.m_hintID = hint;
136 m_base.m_linkID = link;
137 m_target = obj;
138 _setEnvironment( pObj, 0 );
139 }
141 SmartRef( const DataObject* pObj, long hint, TYPE* obj = nullptr )
142 {
143 m_base.m_hintID = hint;
144 m_base.m_linkID = INVALID;
145 m_target = obj;
146 _setEnvironment( pObj, 0 );
147 }
149 // virtual ~SmartRef() {
150 //}
152 bool shouldFollowLink( const DataObject* /* typ */ ) const { return ( !m_target && m_base.m_hintID != INVALID ); }
154 bool shouldFollowLink( const ContainedObject* /* typ */ ) const
155 {
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 {
178 if ( m_target && c.m_target ) return m_target == c.m_target;
179 if ( !m_target && !c.m_target ) return m_base.isEqual( m_target, c.m_base );
180 if ( m_target && !c.m_target ) return m_base.isEqualEx( m_target, c.m_base );
181 if ( !m_target && c.m_target ) return c.m_base.isEqualEx( c.m_target, m_base );
182 return false;
183 }
185 bool operator!=( const SmartRef<TYPE>& c ) const { return !( this->operator==( c ) ); }
187 const SmartRef<TYPE>& _setEnvironment( const DataObject* pObj, const ContainedObject* pContd ) const
188 {
189 m_base.m_data = pObj;
190 m_base.m_contd = pContd;
191 m_base.setObjectType( data() );
192 return *this;
193 }
195 SmartRef<TYPE>& _setEnvironment( const DataObject* pObj, const ContainedObject* pContd )
196 {
197 m_base.m_data = pObj;
198 m_base.m_contd = pContd;
199 m_base.setObjectType( data() );
200 return *this;
201 }
203 SmartRef<TYPE>& operator()( ContainedObject* pObj )
204 {
205 const DataObject* src = ( pObj ? pObj->parent() : nullptr );
206 return _setEnvironment( src, pObj );
207 }
209 const SmartRef<TYPE>& operator()( const ContainedObject* pObj ) const
210 {
211 const DataObject* src = ( pObj ? pObj->parent() : nullptr );
212 return _setEnvironment( src, pObj );
213 }
215 SmartRef<TYPE>& operator()( DataObject* pObj ) { return _setEnvironment( pObj, nullptr ); }
217 const SmartRef<TYPE>& operator()( const DataObject* pObj ) const { return _setEnvironment( pObj, nullptr ); }
219 SmartRef<TYPE>& operator=( const SmartRef<TYPE>& c )
220 {
221 m_target = c.m_target;
222 m_base.m_hintID = c.m_base.m_hintID;
223 m_base.m_linkID = c.m_base.m_linkID;
224 return _setEnvironment( c.m_base.m_data, c.m_base.m_contd );
225 }
227 SmartRef<TYPE>& operator=( TYPE* pObject )
228 {
229 m_target = pObject;
230 m_base.m_hintID = INVALID;
231 m_base.m_linkID = INVALID;
232 return *this;
233 }
235 TYPE& operator*() { return *SmartRef<TYPE>::target(); }
237 const TYPE& operator*() const { return *SmartRef<TYPE>::target(); }
239 TYPE* operator->() { return SmartRef<TYPE>::target(); }
241 const TYPE* operator->() const { return SmartRef<TYPE>::target(); }
243 operator const TYPE*() const { return SmartRef<TYPE>::target(); }
245 operator TYPE*() { return SmartRef<TYPE>::target(); }
247 StreamBuffer& writeRef( StreamBuffer& s ) const;
249 StreamBuffer& readRef( StreamBuffer& s );
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 //
259 // Inline function necessary to be outside the class definition.
260 // Mostly this stuff cannot go into the class definition because
261 // G++ has problems recognizing proper templates if they are not
262 // completely defined.
263 //
264 // M.Frank
265 //
266
268 template <class TYPE>
269 inline TYPE* SmartRef<TYPE>::target()
270 {
271 if ( !m_target ) {
272 m_target = dynamic_cast<const TYPE*>( m_base.accessData( m_target ) );
273 }
274 return const_cast<TYPE*>( m_target );
275 }
276
278 template <class TYPE>
279 inline const TYPE* SmartRef<TYPE>::target() const
280 {
281 if ( !m_target ) {
282 m_target = dynamic_cast<const TYPE*>( m_base.accessData( m_target ) );
283 }
284 return m_target;
285 }
286
288 template <class TYPE>
289 inline StreamBuffer& SmartRef<TYPE>::writeRef( StreamBuffer& s ) const
290 {
291 m_base.writeObject( m_target, s );
292 return s;
293 }
294
296 template <class TYPE>
297 inline StreamBuffer& SmartRef<TYPE>::readRef( StreamBuffer& s )
298 {
299 m_target = dynamic_cast<TYPE*>( m_base.readObject( m_target, s ) );
300 return s;
301 }
302
304 template <class TYPE>
305 inline bool operator==( const SmartRef<TYPE>& ref, int )
306 {
307 return ref.target() == nullptr;
308 }
309
311 template <class TYPE>
312 inline bool operator==( int, const SmartRef<TYPE>& ref )
313 {
314 return ref.target() == nullptr;
315 }
316
318 template <class TYPE>
319 inline bool operator!=( const SmartRef<TYPE>& ref, int )
320 {
321 return ref.target() != nullptr;
322 }
323
325 template <class TYPE>
326 inline bool operator!=( int, const SmartRef<TYPE>& ref )
327 {
328 return ref.target() != nullptr;
329 }
330
331 #endif // KERNEL_SMARTREF_H
SmartRef::SmartRef
SmartRef(const DataObject *pObj, long hint, TYPE *obj=nullptr)
Constructor for references to DataObjects passing environment.
Definition: SmartRef.h:141
SmartRef::operator=
SmartRef< TYPE > & operator=(const SmartRef< TYPE > &c)
Assignment.
Definition: SmartRef.h:219
SmartRefList
Definition: SmartRef.h:23
SmartRef::entry_type
TYPE entry_type
Entry type definition.
Definition: SmartRef.h:76
SmartRef::SmartRef
SmartRef()
Standard Constructor.
Definition: SmartRef.h:85
SmartRef::shouldFollowLink
bool shouldFollowLink(const DataObject *) const
Standard destructor.
Definition: SmartRef.h:152
SmartRefBase::isEqual
bool isEqual(const ContainedObject *, const SmartRefBase &c) const
Equality operator for ContainedObject like references.
Definition: SmartRefBase.h:73
gaudirun.c
c
Definition: gaudirun.py:407
SmartRefBase::isEqualEx
bool isEqualEx(const DataObject *pObj, const SmartRefBase &c) const
Extended equality check.
Definition: SmartRefBase.cpp:72
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:154
SmartRefBase::accessData
const ContainedObject * accessData(const ContainedObject *typ) const
Load on demand of ContainedObject like references.
Definition: SmartRefBase.cpp:34
SmartRef::operator->
const TYPE * operator->() const
Dereference operator to const object.
Definition: SmartRef.h:241
SmartRefBase::m_data
const DataObject * m_data
Object data: Pointer to the identifiable object the link originates.
Definition: SmartRefBase.h:55
SmartRef::path
const std::string & path() const
Return the path of the linked object inside the data store.
Definition: SmartRef.h:174
StreamBuffer
The stream buffer is a small object collecting object data.
Definition: StreamBuffer.h:41
SmartRefBase::m_hintID
long m_hintID
Object data: ID of the link hint to the identifiable object.
Definition: SmartRefBase.h:51
SmartRef::target
const TYPE * target() const
Access to the object.
Definition: SmartRef.h:279
SmartRef::operator()
const SmartRef< TYPE > & operator()(const DataObject *pObj) const
operator() const: assigns parent object for serialisation
Definition: SmartRef.h:217
SmartRefBase::writeObject
void writeObject(const DataObject *pObject, StreamBuffer &s) const
Output streamer for DataObject like references.
Definition: SmartRefBase.h:83
SmartRefBase::setObjectType
void setObjectType(const ContainedObject *) const
Definition: SmartRefBase.h:103
SmartRef::SmartRef
SmartRef(const DataObject *pObj, long hint, long link, TYPE *obj=nullptr)
Constructor for references to contained objects passing environment.
Definition: SmartRef.h:133
SmartRef::SmartRef
SmartRef(const SmartRef &copy)
Copy Constructor.
Definition: SmartRef.h:108
SmartRef::type
const std::type_info * type() const
Access to embedded type.
Definition: SmartRef.h:165
SmartRef::linkID
long linkID() const
Access link id:
Definition: SmartRef.h:161
SmartRef
Kernel objects: SmartRef.
Definition: SmartRef.h:66
SmartRefBase
User example objects: SmartRefBase.
Definition: SmartRefBase.h:47
SmartRefBase.h
SmartRef::_setEnvironment
SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd)
Set the environment (CONST)
Definition: SmartRef.h:195
SmartRefMap
Definition: SmartRef.h:25
ContainedObject::parent
const ObjectContainerBase * parent() const
Access to parent object.
Definition: ContainedObject.h:52
std::type_info
SmartRef::VALID
Definition: SmartRef.h:74
SmartRef::operator!=
bool operator!=(const SmartRef< TYPE > &c) const
NON-Equality operator.
Definition: SmartRef.h:185
std::string
STL class.
compareRootHistos.ref
string ref
Definition: compareRootHistos.py:14
SmartRefBase::m_contd
const ContainedObject * m_contd
Object data: Pointer to the Contained object (if applicable)
Definition: SmartRefBase.h:57
SmartRef::data
TYPE * data()
Access to raw data pointer.
Definition: SmartRef.h:167
SmartRefBase::path
const std::string & path() const
Shortcut to access the path to the linked object.
Definition: SmartRefBase.cpp:96
SmartRef::SmartRef
SmartRef(const ContainedObject *pObj, long hint, long link, TYPE *obj=nullptr)
Constructor for references to contained objects passing environment.
Definition: SmartRef.h:124
SmartRef::operator()
SmartRef< TYPE > & operator()(DataObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:215
SmartRef::operator()
const SmartRef< TYPE > & operator()(const ContainedObject *pObj) const
operator() const: assigns parent object for serialisation
Definition: SmartRef.h:209
SmartRef::operator==
bool operator==(const SmartRef< TYPE > &c) const
Equality operator.
Definition: SmartRef.h:176
SmartRef::operator()
SmartRef< TYPE > & operator()(ContainedObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:203
SmartRef::m_base
SmartRefBase m_base
Definition: SmartRef.h:79
SmartRef::operator=
SmartRef< TYPE > & operator=(TYPE *pObject)
Assignment.
Definition: SmartRef.h:227
SmartRef::data
const TYPE * data() const
Definition: SmartRef.h:168
SmartRef::m_target
const TYPE * m_target
Pointer to target data object.
Definition: SmartRef.h:81
SmartRefBase::m_linkID
long m_linkID
Object data: ID of the object within the identifiable container (if any)
Definition: SmartRefBase.h:53
SmartRef::SmartRef
SmartRef(long hint, long link, TYPE *obj=nullptr)
Constructor.
Definition: SmartRef.h:116
ContainedObject
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
Definition: ContainedObject.h:31
SmartRefArray
Definition: SmartRef.h:21
SmartRef::hintID
long hintID() const
Access hint id:
Definition: SmartRef.h:159
SmartRef::operator*
TYPE & operator*()
Dereference operator.
Definition: SmartRef.h:235
gaudirun.s
string s
Definition: gaudirun.py:253
SmartRef::operator->
TYPE * operator->()
Dereference operator.
Definition: SmartRef.h:239
SmartRefBase::readObject
DataObject * readObject(const DataObject *, StreamBuffer &s) const
Input streamer for DataObject like references.
Definition: SmartRefBase.h:90
ContainedObject.h
StreamBuffer::VALID
Definition: StreamBuffer.h:119
StreamBuffer::INVALID
Definition: StreamBuffer.h:119
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:26
SmartRef::writeRef
StreamBuffer & writeRef(StreamBuffer &s) const
Write the reference to the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:289
DataObject
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:29
SmartRef::operator>>
friend StreamBuffer & operator>>(StreamBuffer &_s, SmartRef< TYPE > &ptr)
Input Streamer operator.
Definition: SmartRef.h:255
SmartRef::readRef
StreamBuffer & readRef(StreamBuffer &s)
Read the reference from the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:297
SmartRef::operator*
const TYPE & operator*() const
Dereference operator.
Definition: SmartRef.h:237
SmartRef::_setEnvironment
const SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd) const
Set the environment (CONST)
Definition: SmartRef.h:187
SmartRef::SmartRef
SmartRef(const TYPE *pObject)
Standard Constructor with initialisation from const object.
Definition: SmartRef.h:100
SmartRef::SmartRef
SmartRef(TYPE *pObject)
Standard Constructor with initialisation.
Definition: SmartRef.h:92
SmartRef::INVALID
Definition: SmartRef.h:74