2 Copyright (C) 2002-2025 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 "SGTools/DataProxy_cast.h"
14 #include "AthenaKernel/proxyDictFromEventContext.h"
18 * @brief Return the CLID for the class that we reference.
20 template <typename STORABLE>
22 const CLID& DataLink<STORABLE>::classID()
24 return ClassID_traits<value_type>::ID();
29 * @brief Default constructor --- gives the equivalent of a NULL pointer.
31 template <typename STORABLE>
33 DataLink<STORABLE>::DataLink()
39 * @brief Constructor --- link to a STORABLE using a transient ref to it.
40 * @param data The object to which to link.
41 * @param sg Associated store; if 0, use the global default.
43 template <typename STORABLE>
45 DataLink<STORABLE>::DataLink(const_reference data, IProxyDict* sg/*=0*/)
46 : DataLinkBase (&data, classID(), sg)
52 * @brief Constructor --- link to a STORABLE using a transient ref to it.
53 * @param data The object to which to link.
54 * @param ctx Event context for this link.
56 template <typename STORABLE>
58 DataLink<STORABLE>::DataLink(const_reference data, const EventContext& ctx)
59 : DataLinkBase (&data, classID(), Atlas::proxyDictFromEventContext(ctx))
65 * @brief Constructor --- link to a STORABLE using a transient pointer to it.
66 * @param data The object to which to link.
67 * @param sg Associated store; if 0, use the global default.
69 template <typename STORABLE>
71 DataLink<STORABLE>::DataLink(const_pointer pdata, IProxyDict* sg/*=0*/)
72 : DataLinkBase (pdata, classID(), sg)
78 * @brief Constructor --- link to a STORABLE using a transient pointer to it.
79 * @param data The object to which to link.
80 * @param ctx Event context for this link.
82 template <typename STORABLE>
84 DataLink<STORABLE>::DataLink(const_pointer pdata, const EventContext& ctx)
85 : DataLinkBase (pdata, classID(), Atlas::proxyDictFromEventContext(ctx))
91 * @brief Constructor --- link to a STORABLE using a string ID.
92 * @param dataID Key of the object.
93 * @param sg Associated store; if 0, use the global default.
95 template <typename STORABLE>
97 DataLink<STORABLE>::DataLink(const ID_type& dataID,
98 IProxyDict* sg /*=0*/)
99 : DataLinkBase (dataID, classID(), sg)
105 * @brief Constructor --- link to a STORABLE using a string ID.
106 * @param dataID Key of the object.
107 * @param ctx Event context for this link.
109 template <typename STORABLE>
111 DataLink<STORABLE>::DataLink(const ID_type& dataID,
112 const EventContext& ctx)
113 : DataLinkBase (dataID, classID(), Atlas::proxyDictFromEventContext(ctx))
119 * @brief Constructor --- link to a STORABLE using a hashed ID.
120 * @param key Hashed key of the object.
121 * @param sg Associated store; if 0, use the global default.
123 * May throw @c ExcCLIDMismatch.
125 template <typename STORABLE>
127 DataLink<STORABLE>::DataLink(sgkey_t key, IProxyDict* sg /*=0*/)
128 : DataLinkBase (key, classID(), sg)
134 * @brief Constructor --- link to a STORABLE using a hashed ID.
135 * @param key Hashed key of the object.
136 * @param ctx Event context for this link.
138 * May throw @c ExcCLIDMismatch.
140 template <typename STORABLE>
142 DataLink<STORABLE>::DataLink(sgkey_t key, const EventContext& ctx)
143 : DataLinkBase (key, classID(), Atlas::proxyDictFromEventContext(ctx))
149 * @brief Constructor from a hashed key and a proxy holder object.
150 * Used internally for EL -> DL conversion.
151 * @param key Hashed key of the object.
152 * @param holder Internal holder object for the proxy.
154 template <typename STORABLE>
156 DataLink<STORABLE>::DataLink(sgkey_t key, const SG::DataProxyHolder& holder)
157 : DataLinkBase (key, holder)
163 * @brief Set the link to an object given by a reference.
164 * @param data The object to which to link.
165 * @param sg Associated store.
167 * If @c sg is 0, then we take the store from whatever the link's currently
168 * set to. If the link has no current store, then we take the global
171 template <typename STORABLE>
173 void DataLink<STORABLE>::toStorableObject(const_reference data,
174 IProxyDict* sg /*= 0*/)
176 DataLinkBase::toStorableObject (&data, classID(), sg);
181 * @brief Set the link to an object given by a reference.
182 * @param data The object to which to link.
183 * @param ctx Event context for this link.
185 template <typename STORABLE>
187 void DataLink<STORABLE>::toStorableObject(const_reference data,
188 const EventContext& ctx)
190 DataLinkBase::toStorableObject (&data, classID(), Atlas::proxyDictFromEventContext(ctx));
195 * @brief Set the link to an object given by a string key.
196 * @param dataID Key of the object.
197 * @param sg Associated store.
199 * If @c sg is 0, then we take the store from whatever the link's currently
200 * set to. If the link has no current store, then we take the global
203 template <typename STORABLE>
205 void DataLink<STORABLE>::toIdentifiedObject(const ID_type& dataID,
206 IProxyDict* sg /*= 0*/)
208 DataLinkBase::toIdentifiedObject (dataID, classID(), sg);
213 * @brief Set the link to an object given by a string key.
214 * @param dataID Key of the object.
215 * @param ctx Event context for this link.
217 template <typename STORABLE>
219 void DataLink<STORABLE>::toIdentifiedObject(const ID_type& dataID,
220 const EventContext& ctx)
222 DataLinkBase::toIdentifiedObject (dataID, classID(), Atlas::proxyDictFromEventContext(ctx));
227 * @brief Set the link to an object given by a hashed key.
228 * @param key Hashed key of the object.
229 * @param sg Associated store.
231 * If @c sg is 0, then we take the store from whatever the link's currently
232 * set to. If the link has no current store, then we take the global
235 * May throw @c ExcCLIDMismatch.
237 template <typename STORABLE>
239 void DataLink<STORABLE>::toIdentifiedObject(sgkey_t key,
240 IProxyDict* sg /*= 0*/)
242 DataLinkBase::toIdentifiedObject (key, classID(), sg);
247 * @brief Set the link to an object given by a hashed key.
248 * @param key Hashed key of the object.
249 * @param ctx Event context for this link.
251 * May throw @c ExcCLIDMismatch.
253 template <typename STORABLE>
255 void DataLink<STORABLE>::toIdentifiedObject(sgkey_t key,
256 const EventContext& ctx)
258 DataLinkBase::toIdentifiedObject (key, classID(), Atlas::proxyDictFromEventContext(ctx));
263 * @brief Set the link to the default object of this type.
264 * @param sg Associated store.
266 * If @c sg is 0, then we take the store from whatever the link's currently
267 * set to. If the link has no current store, then we take the global
270 * Note that this is _not_ the same as clearing the link
271 * (use @c clear for that). This produces a link that will resolve
272 * to any object in SG of the given type, provided that there is only
273 * one of them. (An attempt to dereference an ambiguous default link
274 * will give an error.)
276 template <typename STORABLE>
278 void DataLink<STORABLE>::toDefaultObject (IProxyDict* sg /*= 0*/)
280 DataLinkBase::toIdentifiedObject (SG::DEFAULTKEY, classID(), sg);
285 * @brief Set the link to the default object of this type.
286 * @param ctx Event context for this link.
288 * Note that this is _not_ the same as clearing the link
289 * (use @c clear for that). This produces a link that will resolve
290 * to any object in SG of the given type, provided that there is only
291 * one of them. (An attempt to dereference an ambiguous default link
292 * will give an error.)
294 template <typename STORABLE>
296 void DataLink<STORABLE>::toDefaultObject (const EventContext& ctx)
298 DataLinkBase::toIdentifiedObject (SG::DEFAULTKEY, classID(),
299 Atlas::proxyDictFromEventContext(ctx));
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::proxyDictFromEventContext(ctx));
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::proxyDictFromEventContext(ctx));
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);