2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
5 * @file AthLinks/DataLinkBase.icc
6 * @author scott snyder <snyder@bnl.gov>
8 * @brief Type-independent part of @c DataLink; holds the persistent state.
11 #include "AthenaKernel/proxyDictFromEventContext.h"
14 * @brief Test to see if we're in the default state.
17 bool DataLinkBase::isDefault() const
19 return m_persKey == 0 && m_proxy.isDefault();
24 * @brief Return the SG key that we reference, as a string.
26 * Returns a null string on failure.
29 const DataLinkBase::ID_type& DataLinkBase::dataID() const
31 return m_proxy.dataID();
36 * @brief Return the SG key that we reference, as a hash.
38 * Returns 0 on failure.
41 DataLinkBase::sgkey_t DataLinkBase::key() const
48 * @brief Clear the link (make it null).
51 void DataLinkBase::clear()
59 * @brief Return the @c DataProxy for this link.
60 * @param nothrow If true, return 0 on failure instead of throwing
63 * If this is a null link, we return 0.
64 * If we're directly referencing an object that's not in StoreGate,
65 * either return 0 or throw @c ExcPointerNotInSG, depending
66 * on @c nothrow. Otherwise, return the proxy for the object
70 SG::DataProxy* DataLinkBase::proxy (bool nothrow /*= false*/) const
72 return m_proxy.proxy (nothrow);
77 * @brief Return the data source for this reference.
80 IProxyDict* DataLinkBase::source() const
82 return m_proxy.source();
87 * @brief Finish initialization after link has been read.
88 * @param sg Associated store.
90 * This should be called after a link has been read by root
91 * in order to set the proxy pointer.
92 * Returns true on success.
94 * If @c sg is 0, then we use the global default store.
97 bool DataLinkBase::toTransient (IProxyDict* sg /*= 0*/)
99 m_proxy.toTransient (m_persKey, sg);
105 * @brief Finish initialization after link has been read.
106 * @param ctx Event context for this link.
108 * This should be called after a link has been read by root
109 * in order to set the proxy pointer.
110 * Returns true on success.
113 bool DataLinkBase::toTransient (const EventContext& ctx)
115 m_proxy.toTransient (m_persKey, Atlas::proxyDictFromEventContext(ctx));
121 * @brief Finish initialization like the link as just been read from root,
122 * but with a specified key.
123 * @param dataID Key of the object.
124 * @param link_clid CLID of the link being set.
125 * @param sg Associated store.
127 * The link should be clear before this is called.
130 * If @c sg is 0, then we use the global default store.
133 bool DataLinkBase::toTransient (const ID_type& dataID,
135 IProxyDict* sg /*= 0*/)
137 if (!isDefault()) SG::throwExcBadToTransient();
138 m_persKey = m_proxy.toTransient (dataID, link_clid, sg);
144 * @brief Finish initialization like the link as just been read from root,
145 * but with a specified key.
146 * @param dataID Key of the object.
147 * @param link_clid CLID of the link being set.
148 * @param ctx Event context for this link.
150 * The link should be clear before this is called.
154 bool DataLinkBase::toTransient (const ID_type& dataID,
156 const EventContext& ctx)
158 if (!isDefault()) SG::throwExcBadToTransient();
159 m_persKey = m_proxy.toTransient (dataID, link_clid, Atlas::proxyDictFromEventContext(ctx));
165 * @brief Prepare this link for writing.
167 * One of the @c toPersistent methods should be called before
168 * trying to write the link with root.
170 * Ensures the hashed SG key to be saved is correct.
171 * If this link is referencing an object not in SG,
172 * we throw @c ExcPointerNotInSG.
176 * This version does not perform link remapping.
179 bool DataLinkBase::toPersistent()
182 m_proxy.toPersistent (m_persKey, dum);
188 * @brief Prepare this link for writing.
190 * One of the @c toPersistent methods should be called before
191 * trying to write the link with root.
193 * Ensures the hashed SG key to be saved is correct.
194 * If this link is referencing an object not in SG,
195 * we throw @c ExcPointerNotInSG.
197 * This version does not perform link remapping.
202 bool DataLinkBase::toPersistentNoRemap()
204 m_proxy.toPersistentNoRemap (m_persKey);
210 * @brief Compare for equality.
213 bool DataLinkBase::operator== (const DataLinkBase& other) const
215 if (m_persKey != 0 && other.m_persKey != 0)
216 return SG::sgkeyEqual (m_persKey, other.m_persKey);
217 return (m_proxy == other.m_proxy);
222 * @brief Compare for inequality.
225 bool DataLinkBase::operator!= (const DataLinkBase& other) const
227 return !(*this == other);
231 //****************************************************************************
236 * @brief Default constructor.
239 DataLinkBase::DataLinkBase()
246 * @brief Constructor from pointer to object.
247 * @param obj Pointer to the object.
248 * @param link_clid CLID of the link being set.
249 * @param sg Associated store.
251 * If @c sg is 0, we take the global default.
253 * May throw @c ExcCLIDMismatch.
256 DataLinkBase::DataLinkBase (const_pointer_t obj,
260 toStorableObject (obj, link_clid, sg);
265 * @brief Constructor from a string key.
266 * @param dataID Key of the object.
267 * @param link_clid CLID of the link being set.
268 * @param sg Associated store.
270 * If @c sg is 0, we take the global default.
272 * May throw @c ExcCLIDMismatch.
275 DataLinkBase::DataLinkBase (const ID_type& dataID,
279 toIdentifiedObject (dataID, link_clid, sg);
284 * @brief Constructor from a hashed key.
285 * @param key Hashed key of the object.
286 * @param link_clid CLID of the link being set.
287 * @param sg Associated store.
289 * If @c sg is 0, we take the global default.
291 * May throw @c ExcCLIDMismatch.
294 DataLinkBase::DataLinkBase (sgkey_t key, CLID link_clid, IProxyDict* sg)
296 toIdentifiedObject (key, link_clid, sg);
301 * @brief Constructor from a hashed key and a proxy holder object.
302 * Used internally for EL -> DL conversion.
303 * @param key Hashed key of the object.
304 * @param holder Internal holder object for the proxy.
307 DataLinkBase::DataLinkBase (sgkey_t key, const SG::DataProxyHolder& holder)
315 * @brief Set the link to an object given by a pointer.
316 * @param obj Pointer to the object.
317 * @param link_clid CLID of the link being set.
318 * @param sg Associated store.
319 * @returns The SG key for this object.
321 * If @c sg is 0, then we take the store from whatever the link's currently
322 * set to. If the link has no current store, then we take the global
325 * May throw @c ExcCLIDMismatch.
328 void DataLinkBase::toStorableObject (const_pointer_t obj,
332 m_persKey = m_proxy.toStorableObject (obj, link_clid, sg);
337 * @brief Set the link to an object given by a string key.
338 * @param dataID Key of the object.
339 * @param link_clid CLID of the link being set.
340 * @param sg Associated store.
341 * @returns The SG key for this object.
343 * If @c sg is 0, then we take the store from whatever the link's currently
344 * set to. If the link has no current store, then we take the global
348 void DataLinkBase::toIdentifiedObject (const ID_type& dataID,
352 m_persKey = m_proxy.toIdentifiedObject (dataID, link_clid, sg);
357 * @brief Set the link to an object given by a hashed key.
358 * @param key Hashed key of the object.
359 * @param link_clid CLID of the link being set.
360 * @param sg Associated store.
362 * If @c sg is 0, then we take the store from whatever the link's currently
363 * set to. If the link has no current store, then we take the global
366 * May throw @c ExcCLIDMismatch.
369 void DataLinkBase::toIdentifiedObject (sgkey_t key,
374 m_proxy.toIdentifiedObject (key, link_clid, sg);
379 * @brief Return a pointer to the currently-referenced object.
380 * @param castfn Function to do the cast from data proxy to object.
381 * If 0, use a dynamic cast.
382 * @param clid The CLID of the desired object.
383 * This is used to determine how the returned pointer
384 * is to be converted.
385 * @param isConst True if the returned object will be treated as const.
386 * @return A pointer to an object of the type given by @a clid,
387 * or null on a failure (or if the reference is null).
390 void* DataLinkBase::storableBase (castfn_t* castfn,
394 return m_proxy.storableBase (castfn, clid, isConst);
399 * @brief Throw a @c ExcInvalidLink exception for this link.
400 * @param sgkey The hashed key for this link.
402 * This will fill in parameters for the exception message from the proxy.
405 void DataLinkBase::throwInvalidLink() const
407 m_proxy.throwInvalidLink(m_persKey);