2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
5 * @file AthLinks/DataLink.icc
6 * @author scott snyder <snyder@bnl.gov>
8 * @brief Object reference supporting deferred reading from StoreGate.
12 #include "AthLinks/exceptions.h"
13 #include "AthenaKernel/StorableConversions.h"
14 #include "SGTools/DataProxy.h"
15 #include "AthenaKernel/ExtendedEventContext.h"
19 * @brief Return the CLID for the class that we reference.
21 template <typename STORABLE>
23 const CLID& DataLink<STORABLE>::classID()
25 return ClassID_traits<value_type>::ID();
30 * @brief Default constructor --- gives the equivalent of a NULL pointer.
32 template <typename STORABLE>
34 DataLink<STORABLE>::DataLink()
40 * @brief Constructor --- link to a STORABLE using a transient ref to it.
41 * @param data The object to which to link.
42 * @param sg Associated store; if 0, use the global default.
44 template <typename STORABLE>
46 DataLink<STORABLE>::DataLink(const_reference data, IProxyDict* sg/*=0*/)
47 : DataLinkBase (&data, classID(), sg)
53 * @brief Constructor --- link to a STORABLE using a transient ref to it.
54 * @param data The object to which to link.
55 * @param ctx Event context for this link.
57 template <typename STORABLE>
59 DataLink<STORABLE>::DataLink(const_reference data, const EventContext& ctx)
60 : DataLinkBase (&data, classID(), Atlas::getExtendedEventContext(ctx).proxy())
66 * @brief Constructor --- link to a STORABLE using a transient pointer to it.
67 * @param data The object to which to link.
68 * @param sg Associated store; if 0, use the global default.
70 template <typename STORABLE>
72 DataLink<STORABLE>::DataLink(const_pointer pdata, IProxyDict* sg/*=0*/)
73 : DataLinkBase (pdata, classID(), sg)
79 * @brief Constructor --- link to a STORABLE using a transient pointer to it.
80 * @param data The object to which to link.
81 * @param ctx Event context for this link.
83 template <typename STORABLE>
85 DataLink<STORABLE>::DataLink(const_pointer pdata, const EventContext& ctx)
86 : DataLinkBase (pdata, classID(), Atlas::getExtendedEventContext(ctx).proxy())
92 * @brief Constructor --- link to a STORABLE using a string ID.
93 * @param dataID Key of the object.
94 * @param sg Associated store; if 0, use the global default.
96 template <typename STORABLE>
98 DataLink<STORABLE>::DataLink(const ID_type& dataID,
99 IProxyDict* sg /*=0*/)
100 : DataLinkBase (dataID, classID(), sg)
106 * @brief Constructor --- link to a STORABLE using a string ID.
107 * @param dataID Key of the object.
108 * @param ctx Event context for this link.
110 template <typename STORABLE>
112 DataLink<STORABLE>::DataLink(const ID_type& dataID,
113 const EventContext& ctx)
114 : DataLinkBase (dataID, classID(), Atlas::getExtendedEventContext(ctx).proxy())
120 * @brief Constructor --- link to a STORABLE using a hashed ID.
121 * @param key Hashed key of the object.
122 * @param sg Associated store; if 0, use the global default.
124 * May throw @c ExcCLIDMismatch.
126 template <typename STORABLE>
128 DataLink<STORABLE>::DataLink(sgkey_t key, IProxyDict* sg /*=0*/)
129 : DataLinkBase (key, classID(), sg)
135 * @brief Constructor --- link to a STORABLE using a hashed ID.
136 * @param key Hashed key of the object.
137 * @param ctx Event context for this link.
139 * May throw @c ExcCLIDMismatch.
141 template <typename STORABLE>
143 DataLink<STORABLE>::DataLink(sgkey_t key, const EventContext& ctx)
144 : DataLinkBase (key, classID(), Atlas::getExtendedEventContext(ctx).proxy())
150 * @brief Constructor from a hashed key and a proxy holder object.
151 * Used internally for EL -> DL conversion.
152 * @param key Hashed key of the object.
153 * @param holder Internal holder object for the proxy.
155 template <typename STORABLE>
157 DataLink<STORABLE>::DataLink(sgkey_t key, const SG::DataProxyHolder& holder)
158 : DataLinkBase (key, holder)
164 * @brief Set the link to an object given by a reference.
165 * @param data The object to which to link.
166 * @param sg Associated store.
168 * If @c sg is 0, then we take the store from whatever the link's currently
169 * set to. If the link has no current store, then we take the global
172 template <typename STORABLE>
174 void DataLink<STORABLE>::toStorableObject(const_reference data,
175 IProxyDict* sg /*= 0*/)
177 DataLinkBase::toStorableObject (&data, classID(), sg);
182 * @brief Set the link to an object given by a reference.
183 * @param data The object to which to link.
184 * @param ctx Event context for this link.
186 template <typename STORABLE>
188 void DataLink<STORABLE>::toStorableObject(const_reference data,
189 const EventContext& ctx)
191 DataLinkBase::toStorableObject (&data, classID(), Atlas::getExtendedEventContext(ctx).proxy());
196 * @brief Set the link to an object given by a string key.
197 * @param dataID Key of the object.
198 * @param sg Associated store.
200 * If @c sg is 0, then we take the store from whatever the link's currently
201 * set to. If the link has no current store, then we take the global
204 template <typename STORABLE>
206 void DataLink<STORABLE>::toIdentifiedObject(const ID_type& dataID,
207 IProxyDict* sg /*= 0*/)
209 DataLinkBase::toIdentifiedObject (dataID, classID(), sg);
214 * @brief Set the link to an object given by a string key.
215 * @param dataID Key of the object.
216 * @param ctx Event context for this link.
218 template <typename STORABLE>
220 void DataLink<STORABLE>::toIdentifiedObject(const ID_type& dataID,
221 const EventContext& ctx)
223 DataLinkBase::toIdentifiedObject (dataID, classID(), Atlas::getExtendedEventContext(ctx).proxy());
228 * @brief Set the link to an object given by a hashed key.
229 * @param key Hashed key of the object.
230 * @param sg Associated store.
232 * If @c sg is 0, then we take the store from whatever the link's currently
233 * set to. If the link has no current store, then we take the global
236 * May throw @c ExcCLIDMismatch.
238 template <typename STORABLE>
240 void DataLink<STORABLE>::toIdentifiedObject(sgkey_t key,
241 IProxyDict* sg /*= 0*/)
243 DataLinkBase::toIdentifiedObject (key, classID(), sg);
248 * @brief Set the link to an object given by a hashed key.
249 * @param key Hashed key of the object.
250 * @param ctx Event context for this link.
252 * May throw @c ExcCLIDMismatch.
254 template <typename STORABLE>
256 void DataLink<STORABLE>::toIdentifiedObject(sgkey_t key,
257 const EventContext& ctx)
259 DataLinkBase::toIdentifiedObject (key, classID(), Atlas::getExtendedEventContext(ctx).proxy());
264 * @brief Set the link to the default object of this type.
265 * @param sg Associated store.
267 * If @c sg is 0, then we take the store from whatever the link's currently
268 * set to. If the link has no current store, then we take the global
271 * Note that this is _not_ the same as clearing the link
272 * (use @c clear for that). This produces a link that will resolve
273 * to any object in SG of the given type, provided that there is only
274 * one of them. (An attempt to dereference an ambiguous default link
275 * will give an error.)
277 template <typename STORABLE>
279 void DataLink<STORABLE>::toDefaultObject (IProxyDict* sg /*= 0*/)
281 DataLinkBase::toIdentifiedObject (SG::DEFAULTKEY, classID(), sg);
286 * @brief Set the link to the default object of this type.
287 * @param ctx Event context for this link.
289 * Note that this is _not_ the same as clearing the link
290 * (use @c clear for that). This produces a link that will resolve
291 * to any object in SG of the given type, provided that there is only
292 * one of them. (An attempt to dereference an ambiguous default link
293 * will give an error.)
295 template <typename STORABLE>
297 void DataLink<STORABLE>::toDefaultObject (const EventContext& ctx)
299 DataLinkBase::toIdentifiedObject (SG::DEFAULTKEY, classID(), Atlas::getExtendedEventContext(ctx).proxy());
304 * @brief Finish initialization after link has been read.
305 * @param sg Associated store.
307 * This should be called after a link has been read by root
308 * in order to set the proxy pointer.
311 * If @c sg is 0, then we use the global default store.
313 template <typename STORABLE>
315 bool DataLink<STORABLE>::toTransient (IProxyDict* sg /*= 0*/)
317 return DataLinkBase::toTransient (sg);
322 * @brief Finish initialization after link has been read.
323 * @param ctx Event context for this link.
325 * This should be called after a link has been read by root
326 * in order to set the proxy pointer.
329 template <typename STORABLE>
331 bool DataLink<STORABLE>::toTransient (const EventContext& ctx)
333 return DataLinkBase::toTransient (Atlas::getExtendedEventContext(ctx).proxy());
338 * @brief Finish initialization like the link as just been read from root,
339 * but with a specified key.
340 * @param dataID Key of the object.
341 * @param sg Associated store.
343 * The link should be clear before this is called.
346 * If @c sg is 0, then we use the global default store.
348 template <typename STORABLE>
350 bool DataLink<STORABLE>::toTransient (const ID_type& dataID,
351 IProxyDict* sg /*= 0*/)
353 return DataLinkBase::toTransient (dataID, classID(), sg);
358 * @brief Finish initialization like the link as just been read from root,
359 * but with a specified key.
360 * @param dataID Key of the object.
361 * @param ctx Event context for this link.
363 * The link should be clear before this is called.
366 template <typename STORABLE>
368 bool DataLink<STORABLE>::toTransient (const ID_type& dataID,
369 const EventContext& ctx)
371 return DataLinkBase::toTransient (dataID, classID(), Atlas::getExtendedEventContext(ctx).proxy());
376 * @brief Return a pointer to the currently-referenced object.
377 * @return A pointer to an object of the type given by @a clid,
378 * or null on a failure (or if the reference is null).
380 template <typename STORABLE>
382 typename DataLink<STORABLE>::const_pointer
383 DataLink<STORABLE>::getDataPtr() const
385 return reinterpret_cast<const_pointer> (this->storable());
390 * @brief Return a pointer to the currently-referenced object.
391 * @return A pointer to an object of the type given by @a clid,
392 * or null on a failure (or if the reference is null).
394 template <typename STORABLE>
396 typename DataLink<STORABLE>::pointer
397 DataLink<STORABLE>::getDataNonConstPtr()
399 return reinterpret_cast<pointer> (this->storableNonConst());
404 * @brief Dereference the link.
406 template< typename STORABLE>
408 typename DataLink< STORABLE >::const_reference
409 DataLink< STORABLE >::operator* () const
411 const_pointer p = this->getDataPtr();
419 * @brief Dereference the link.
421 template< typename STORABLE>
423 typename DataLink< STORABLE >::const_pointer
424 DataLink< STORABLE >::operator->() const
426 return this->getDataPtr();
431 * @brief Dereference the link.
433 template< typename STORABLE>
435 DataLink<STORABLE>::operator const_pointer() const
442 * @brief Dereference the link.
444 template< typename STORABLE>
446 typename DataLink< STORABLE >::const_pointer
447 DataLink< STORABLE >::cptr() const
449 return this->getDataPtr();
454 * @brief Test to see if the link is dereferencable.
456 template <typename STORABLE>
459 DataLink<STORABLE>::isValid() const
461 return 0 != getDataPtr();
466 * @brief True if the link is not dereferencable.
468 template <typename STORABLE>
470 bool DataLink<STORABLE>::operator!() const
477 * @brief Return a (void) pointer to the currently-referenced object.
478 * @return A pointer to an object of the type given by @a clid,
479 * or null on a failure (or if the reference is null).
481 template <typename STORABLE>
483 const void* DataLink<STORABLE>::storable() const
485 typedef STORABLE* fn_t (SG::DataProxy*);
486 fn_t* fn = &SG::DataProxy_cast<STORABLE>;
487 return this->storableBase (reinterpret_cast<castfn_t*> (fn), classID(), true);
492 * @brief Return a (void) pointer to the currently-referenced object.
493 * @return A pointer to an object of the type given by @a clid,
494 * or null on a failure (or if the reference is null).
496 template <typename STORABLE>
498 void* DataLink<STORABLE>::storableNonConst()
500 typedef STORABLE* fn_t (SG::DataProxy*);
501 fn_t* fn = &SG::DataProxy_cast<STORABLE>;
502 return this->storableBase (reinterpret_cast<castfn_t*> (fn), classID(), false);