| The Gaudi Framework v32r1 (f65d50dc)

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 {
68 friend class SmartRefArray<TYPE>;
69 friend class SmartRefList<TYPE>;
70 friend class SmartRefMap<TYPE>;
71
72 public:
73 enum { VALID = StreamBuffer::VALID, INVALID = StreamBuffer::INVALID };
75 typedef TYPE entry_type;
76
77 protected:
78 SmartRefBase m_base;
80 mutable const TYPE* m_target = nullptr;
81
82 public:
84 SmartRef() {
85 m_base.m_hintID = INVALID;
86 m_base.m_linkID = INVALID;
87 _setEnvironment( 0, 0 );
88 }
90 SmartRef( TYPE* pObject ) {
91 m_base.m_hintID = INVALID;
92 m_base.m_linkID = INVALID;
93 m_target = pObject;
94 _setEnvironment( 0, 0 );
95 }
97 SmartRef( const TYPE* pObject ) {
98 m_base.m_hintID = INVALID;
99 m_base.m_linkID = INVALID;
100 m_target = const_cast<TYPE*>( pObject );
101 _setEnvironment( 0, 0 );
102 }
104 SmartRef( const SmartRef& copy ) {
105 m_base.m_hintID = copy.m_base.m_hintID;
106 m_base.m_linkID = copy.m_base.m_linkID;
107 m_target = copy.m_target;
108 _setEnvironment( copy.m_base.m_data, copy.m_base.m_contd );
109 }
111 SmartRef( long hint, long link, TYPE* obj = nullptr ) {
112 m_base.m_hintID = hint;
113 m_base.m_linkID = link;
114 m_target = obj;
115 _setEnvironment( 0, 0 );
116 }
118 SmartRef( const ContainedObject* pObj, long hint, long link, TYPE* obj = nullptr ) {
119 m_base.m_hintID = hint;
120 m_base.m_linkID = link;
121 m_target = obj;
122 const DataObject* src = ( 0 == pObj ) ? 0 : pObj->parent();
123 _setEnvironment( src, pObj );
124 }
126 SmartRef( const DataObject* pObj, long hint, long link, TYPE* obj = nullptr ) {
127 m_base.m_hintID = hint;
128 m_base.m_linkID = link;
129 m_target = obj;
130 _setEnvironment( pObj, 0 );
131 }
133 SmartRef( const DataObject* pObj, long hint, TYPE* obj = nullptr ) {
134 m_base.m_hintID = hint;
135 m_base.m_linkID = INVALID;
136 m_target = obj;
137 _setEnvironment( pObj, 0 );
138 }
140 // virtual ~SmartRef() {
141 //}
143 bool shouldFollowLink( const DataObject* /* typ */ ) const { return ( !m_target && m_base.m_hintID != INVALID ); }
145 bool shouldFollowLink( const ContainedObject* /* typ */ ) const {
146 return ( !m_target && m_base.m_hintID != INVALID && m_base.m_linkID != INVALID );
147 }
149 long hintID() const { return m_base.m_hintID; }
151 long linkID() const { return m_base.m_linkID; }
153 void set( DataObject* pObj, long hint_id, long link_id ) { m_base.set( pObj, hint_id, link_id ); }
155 const std::type_info* type() const { return &typeid( TYPE ); }
157 TYPE* data() { return const_cast<TYPE*>( m_target ); }
158 const TYPE* data() const { return m_target; }
160 const TYPE* target() const;
162 TYPE* target();
164 inline const std::string& path() const { return m_base.path(); }
166 bool operator==( const SmartRef<TYPE>& c ) const {
167 if ( m_target && c.m_target ) return m_target == c.m_target;
168 if ( !m_target && !c.m_target ) return m_base.isEqual( m_target, c.m_base );
169 if ( m_target && !c.m_target ) return m_base.isEqualEx( m_target, c.m_base );
170 if ( !m_target && c.m_target ) return c.m_base.isEqualEx( c.m_target, m_base );
171 return false;
172 }
174 bool operator!=( const SmartRef<TYPE>& c ) const { return !( this->operator==( c ) ); }
176 const SmartRef<TYPE>& _setEnvironment( const DataObject* pObj, const ContainedObject* pContd ) const {
177 m_base.m_data = pObj;
178 m_base.m_contd = pContd;
179 m_base.setObjectType( data() );
180 return *this;
181 }
183 SmartRef<TYPE>& _setEnvironment( const DataObject* pObj, const ContainedObject* pContd ) {
184 m_base.m_data = pObj;
185 m_base.m_contd = pContd;
186 m_base.setObjectType( data() );
187 return *this;
188 }
190 SmartRef<TYPE>& operator()( ContainedObject* pObj ) {
191 const DataObject* src = ( pObj ? pObj->parent() : nullptr );
192 return _setEnvironment( src, pObj );
193 }
195 const SmartRef<TYPE>& operator()( const ContainedObject* pObj ) const {
196 const DataObject* src = ( pObj ? pObj->parent() : nullptr );
197 return _setEnvironment( src, pObj );
198 }
200 SmartRef<TYPE>& operator()( DataObject* pObj ) { return _setEnvironment( pObj, nullptr ); }
202 const SmartRef<TYPE>& operator()( const DataObject* pObj ) const { return _setEnvironment( pObj, nullptr ); }
204 SmartRef<TYPE>& operator=( const SmartRef<TYPE>& c ) {
205 m_target = c.m_target;
206 m_base.m_hintID = c.m_base.m_hintID;
207 m_base.m_linkID = c.m_base.m_linkID;
208 return _setEnvironment( c.m_base.m_data, c.m_base.m_contd );
209 }
211 SmartRef<TYPE>& operator=( TYPE* pObject ) {
212 m_target = pObject;
213 m_base.m_hintID = INVALID;
214 m_base.m_linkID = INVALID;
215 return *this;
216 }
218 TYPE& operator*() { return *SmartRef<TYPE>::target(); }
220 const TYPE& operator*() const { return *SmartRef<TYPE>::target(); }
222 TYPE* operator->() { return SmartRef<TYPE>::target(); }
224 const TYPE* operator->() const { return SmartRef<TYPE>::target(); }
226 operator const TYPE*() const { return SmartRef<TYPE>::target(); }
228 operator TYPE*() { return SmartRef<TYPE>::target(); }
230 StreamBuffer& writeRef( StreamBuffer& s ) const;
232 StreamBuffer& readRef( StreamBuffer& s );
234 // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
235 friend StreamBuffer& operator<<( StreamBuffer& _s, const SmartRef<TYPE>& ptr ) { return ptr.writeRef( _s ); }
237 // MCl: it is "_s" instead of the most common "s" to avoid a fake icc remark #1599
238 friend StreamBuffer& operator>>( StreamBuffer& _s, SmartRef<TYPE>& ptr ) { return ptr.readRef( _s ); }
239 };
240
241 //
242 // Inline function necessary to be outside the class definition.
243 // Mostly this stuff cannot go into the class definition because
244 // G++ has problems recognizing proper templates if they are not
245 // completely defined.
246 //
247 // M.Frank
248 //
249
251 template <class TYPE>
252 inline TYPE* SmartRef<TYPE>::target() {
253 if ( !m_target ) { m_target = dynamic_cast<const TYPE*>( m_base.accessData( m_target ) ); }
254 return const_cast<TYPE*>( m_target );
255 }
256
258 template <class TYPE>
259 inline const TYPE* SmartRef<TYPE>::target() const {
260 if ( !m_target ) { m_target = dynamic_cast<const TYPE*>( m_base.accessData( m_target ) ); }
261 return m_target;
262 }
263
265 template <class TYPE>
266 inline StreamBuffer& SmartRef<TYPE>::writeRef( StreamBuffer& s ) const {
267 m_base.writeObject( m_target, s );
268 return s;
269 }
270
272 template <class TYPE>
273 inline StreamBuffer& SmartRef<TYPE>::readRef( StreamBuffer& s ) {
274 m_target = dynamic_cast<TYPE*>( m_base.readObject( m_target, s ) );
275 return s;
276 }
277
279 template <class TYPE>
280 inline bool operator==( const SmartRef<TYPE>& ref, int ) {
281 return ref.target() == nullptr;
282 }
283
285 template <class TYPE>
286 inline bool operator==( int, const SmartRef<TYPE>& ref ) {
287 return ref.target() == nullptr;
288 }
289
291 template <class TYPE>
292 inline bool operator!=( const SmartRef<TYPE>& ref, int ) {
293 return ref.target() != nullptr;
294 }
295
297 template <class TYPE>
298 inline bool operator!=( int, const SmartRef<TYPE>& ref ) {
299 return ref.target() != nullptr;
300 }
301
302 #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:133
SmartRef::operator=
SmartRef< TYPE > & operator=(const SmartRef< TYPE > &c)
Assignment.
Definition: SmartRef.h:204
SmartRef::operator!=
bool operator!=(const SmartRef< TYPE > &c) const
NON-Equality operator.
Definition: SmartRef.h:174
SmartRefList
Definition: SmartRef.h:23
SmartRef::entry_type
TYPE entry_type
Entry type definition.
Definition: SmartRef.h:75
SmartRef::SmartRef
SmartRef()
Standard Constructor.
Definition: SmartRef.h:84
SmartRef::shouldFollowLink
bool shouldFollowLink(const DataObject *) const
Standard destructor.
Definition: SmartRef.h:143
gaudirun.c
c
Definition: gaudirun.py:487
SmartRefBase::m_data
const DataObject * m_data
Object data: Pointer to the identifiable object the link originates.
Definition: SmartRefBase.h:54
SmartRef::writeRef
StreamBuffer & writeRef(StreamBuffer &s) const
Write the reference to the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:266
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:50
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:153
SmartRefBase::isEqualEx
bool isEqualEx(const DataObject *pObj, const SmartRefBase &c) const
Extended equality check.
Definition: SmartRefBase.cpp:67
SmartRef::INVALID
Definition: SmartRef.h:73
SmartRef::SmartRef
SmartRef(const DataObject *pObj, long hint, long link, TYPE *obj=nullptr)
Constructor for references to contained objects passing environment.
Definition: SmartRef.h:126
SmartRef::SmartRef
SmartRef(const SmartRef &copy)
Copy Constructor.
Definition: SmartRef.h:104
SmartRefBase::path
const std::string & path() const
Shortcut to access the path to the linked object.
Definition: SmartRefBase.cpp:89
SmartRef::_setEnvironment
const SmartRef< TYPE > & _setEnvironment(const DataObject *pObj, const ContainedObject *pContd) const
Set the environment (CONST)
Definition: SmartRef.h:176
SmartRef::operator<<
friend StreamBuffer & operator<<(StreamBuffer &_s, const SmartRef< TYPE > &ptr)
Output Streamer operator.
Definition: SmartRef.h:235
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:183
SmartRefMap
Definition: SmartRef.h:25
SmartRef::VALID
Definition: SmartRef.h:73
std::type_info
std::string
STL class.
compareRootHistos.ref
string ref
Definition: compareRootHistos.py:14
SmartRef::target
const TYPE * target() const
Access to the object.
Definition: SmartRef.h:259
SmartRef::type
const std::type_info * type() const
Access to embedded type.
Definition: SmartRef.h:155
SmartRefBase::m_contd
const ContainedObject * m_contd
Object data: Pointer to the Contained object (if applicable)
Definition: SmartRefBase.h:56
SmartRef::data
TYPE * data()
Access to raw data pointer.
Definition: SmartRef.h:157
SmartRef::SmartRef
SmartRef(const ContainedObject *pObj, long hint, long link, TYPE *obj=nullptr)
Constructor for references to contained objects passing environment.
Definition: SmartRef.h:118
SmartRef::operator()
SmartRef< TYPE > & operator()(DataObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:200
SmartRefBase::isEqual
bool isEqual(const ContainedObject *, const SmartRefBase &c) const
Equality operator for ContainedObject like references.
Definition: SmartRefBase.h:72
operator!=
bool operator!=(const SmartRef< TYPE > &ref, int)
Friend helper to check for object existence (will load object)
Definition: SmartRef.h:292
SmartRef::operator()
SmartRef< TYPE > & operator()(ContainedObject *pObj)
operator(): assigns parent object for serialisation
Definition: SmartRef.h:190
SmartRef::operator()
const SmartRef< TYPE > & operator()(const ContainedObject *pObj) const
operator() const: assigns parent object for serialisation
Definition: SmartRef.h:195
SmartRef::m_base
SmartRefBase m_base
Definition: SmartRef.h:78
SmartRef::operator=
SmartRef< TYPE > & operator=(TYPE *pObject)
Assignment.
Definition: SmartRef.h:211
SmartRef::operator->
const TYPE * operator->() const
Dereference operator to const object.
Definition: SmartRef.h:224
SmartRef::m_target
const TYPE * m_target
Pointer to target data object.
Definition: SmartRef.h:80
SmartRefBase::m_linkID
long m_linkID
Object data: ID of the object within the identifiable container (if any)
Definition: SmartRefBase.h:52
SmartRef::SmartRef
SmartRef(long hint, long link, TYPE *obj=nullptr)
Constructor.
Definition: SmartRef.h:111
ContainedObject
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
Definition: ContainedObject.h:31
SmartRef::operator *
TYPE & operator *()
Dereference operator.
Definition: SmartRef.h:218
operator==
bool operator==(const SmartRef< TYPE > &ref, int)
Friend helper to check for object existence (will load object)
Definition: SmartRef.h:280
SmartRef::data
const TYPE * data() const
Definition: SmartRef.h:158
SmartRefArray
Definition: SmartRef.h:21
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:145
SmartRef::operator==
bool operator==(const SmartRef< TYPE > &c) const
Equality operator.
Definition: SmartRef.h:166
gaudirun.s
string s
Definition: gaudirun.py:316
SmartRef::operator->
TYPE * operator->()
Dereference operator.
Definition: SmartRef.h:222
ContainedObject.h
ContainedObject::parent
const ObjectContainerBase * parent() const
Access to parent object.
Definition: ContainedObject.h:51
StreamBuffer::VALID
Definition: StreamBuffer.h:111
StreamBuffer::INVALID
Definition: StreamBuffer.h:111
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::path
const std::string & path() const
Return the path of the linked object inside the data store.
Definition: SmartRef.h:164
DataObject
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
SmartRef::operator>>
friend StreamBuffer & operator>>(StreamBuffer &_s, SmartRef< TYPE > &ptr)
Input Streamer operator.
Definition: SmartRef.h:238
SmartRef::readRef
StreamBuffer & readRef(StreamBuffer &s)
Read the reference from the stream buffer (needed due to stupid G++ compiler)
Definition: SmartRef.h:273
SmartRefBase::setObjectType
void setObjectType(const ContainedObject *) const
Definition: SmartRefBase.h:97
SmartRef::SmartRef
SmartRef(const TYPE *pObject)
Standard Constructor with initialisation from const object.
Definition: SmartRef.h:97
SmartRef::linkID
long linkID() const
Access link id:
Definition: SmartRef.h:151
SmartRef::operator()
const SmartRef< TYPE > & operator()(const DataObject *pObj) const
operator() const: assigns parent object for serialisation
Definition: SmartRef.h:202
SmartRef::hintID
long hintID() const
Access hint id:
Definition: SmartRef.h:149
SmartRef::SmartRef
SmartRef(TYPE *pObject)
Standard Constructor with initialisation.
Definition: SmartRef.h:90