2 Copyright (C) 2002-2024 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/proxyDictFromEventContext.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::proxyDictFromEventContext(ctx))
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::proxyDictFromEventContext(ctx))
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::proxyDictFromEventContext(ctx))
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::proxyDictFromEventContext(ctx))
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::proxyDictFromEventContext(ctx));
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::proxyDictFromEventContext(ctx));
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::proxyDictFromEventContext(ctx));
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(),
300 Atlas::proxyDictFromEventContext(ctx));
305 * @brief Finish initialization after link has been read.
306 * @param sg Associated store.
308 * This should be called after a link has been read by root
309 * in order to set the proxy pointer.
312 * If @c sg is 0, then we use the global default store.
314 template <typename STORABLE>
316 bool DataLink<STORABLE>::toTransient (IProxyDict* sg /*= 0*/)
318 return DataLinkBase::toTransient (sg);
323 * @brief Finish initialization after link has been read.
324 * @param ctx Event context for this link.
326 * This should be called after a link has been read by root
327 * in order to set the proxy pointer.
330 template <typename STORABLE>
332 bool DataLink<STORABLE>::toTransient (const EventContext& ctx)
334 return DataLinkBase::toTransient (Atlas::proxyDictFromEventContext(ctx));
339 * @brief Finish initialization like the link as just been read from root,
340 * but with a specified key.
341 * @param dataID Key of the object.
342 * @param sg Associated store.
344 * The link should be clear before this is called.
347 * If @c sg is 0, then we use the global default store.
349 template <typename STORABLE>
351 bool DataLink<STORABLE>::toTransient (const ID_type& dataID,
352 IProxyDict* sg /*= 0*/)
354 return DataLinkBase::toTransient (dataID, classID(), sg);
359 * @brief Finish initialization like the link as just been read from root,
360 * but with a specified key.
361 * @param dataID Key of the object.
362 * @param ctx Event context for this link.
364 * The link should be clear before this is called.
367 template <typename STORABLE>
369 bool DataLink<STORABLE>::toTransient (const ID_type& dataID,
370 const EventContext& ctx)
372 return DataLinkBase::toTransient (dataID, classID(), Atlas::proxyDictFromEventContext(ctx));
377 * @brief Return a pointer to the currently-referenced object.
378 * @return A pointer to an object of the type given by @a clid,
379 * or null on a failure (or if the reference is null).
381 template <typename STORABLE>
383 typename DataLink<STORABLE>::const_pointer
384 DataLink<STORABLE>::getDataPtr() const
386 return reinterpret_cast<const_pointer> (this->storable());
391 * @brief Return a pointer to the currently-referenced object.
392 * @return A pointer to an object of the type given by @a clid,
393 * or null on a failure (or if the reference is null).
395 template <typename STORABLE>
397 typename DataLink<STORABLE>::pointer
398 DataLink<STORABLE>::getDataNonConstPtr()
400 return reinterpret_cast<pointer> (this->storableNonConst());
405 * @brief Dereference the link.
407 template< typename STORABLE>
409 typename DataLink< STORABLE >::const_reference
410 DataLink< STORABLE >::operator* () const
412 const_pointer p = this->getDataPtr();
420 * @brief Dereference the link.
422 template< typename STORABLE>
424 typename DataLink< STORABLE >::const_pointer
425 DataLink< STORABLE >::operator->() const
427 return this->getDataPtr();
432 * @brief Dereference the link.
434 template< typename STORABLE>
436 DataLink<STORABLE>::operator const_pointer() const
443 * @brief Dereference the link.
445 template< typename STORABLE>
447 typename DataLink< STORABLE >::const_pointer
448 DataLink< STORABLE >::cptr() const
450 return this->getDataPtr();
455 * @brief Test to see if the link is dereferencable.
457 template <typename STORABLE>
460 DataLink<STORABLE>::isValid() const
462 return 0 != getDataPtr();
467 * @brief True if the link is not dereferencable.
469 template <typename STORABLE>
471 bool DataLink<STORABLE>::operator!() const
478 * @brief Return a (void) pointer to the currently-referenced object.
479 * @return A pointer to an object of the type given by @a clid,
480 * or null on a failure (or if the reference is null).
482 template <typename STORABLE>
484 const void* DataLink<STORABLE>::storable() const
486 typedef STORABLE* fn_t (SG::DataProxy*);
487 fn_t* fn = &SG::DataProxy_cast<STORABLE>;
488 return this->storableBase (reinterpret_cast<castfn_t*> (fn), classID(), true);
493 * @brief Return a (void) pointer to the currently-referenced object.
494 * @return A pointer to an object of the type given by @a clid,
495 * or null on a failure (or if the reference is null).
497 template <typename STORABLE>
499 void* DataLink<STORABLE>::storableNonConst()
501 typedef STORABLE* fn_t (SG::DataProxy*);
502 fn_t* fn = &SG::DataProxy_cast<STORABLE>;
503 return this->storableBase (reinterpret_cast<castfn_t*> (fn), classID(), false);