ATLAS Offline Software
DataLinkBase.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 /**
5  * @file AthLinks/DataLinkBase.icc
6  * @author scott snyder <snyder@bnl.gov>
7  * @date Mar, 2014
8  * @brief Type-independent part of @c DataLink; holds the persistent state.
9  */
10 
11 #include "AthenaKernel/proxyDictFromEventContext.h"
12 
13 /**
14  * @brief Test to see if we're in the default state.
15  */
16 inline
17 bool DataLinkBase::isDefault() const
18 {
19  return m_persKey == 0 && m_proxy.isDefault();
20 }
21 
22 
23 /**
24  * @brief Return the SG key that we reference, as a string.
25  *
26  * Returns a null string on failure.
27  */
28 inline
29 const DataLinkBase::ID_type& DataLinkBase::dataID() const
30 {
31  return m_proxy.dataID();
32 }
33 
34 
35 /**
36  * @brief Return the SG key that we reference, as a hash.
37  *
38  * Returns 0 on failure.
39  */
40 inline
41 DataLinkBase::sgkey_t DataLinkBase::key() const
42 {
43  return m_persKey;
44 }
45 
46 
47 /**
48  * @brief Clear the link (make it null).
49  */
50 inline
51 void DataLinkBase::clear()
52 {
53  m_persKey = 0;
54  m_proxy.clear();
55 }
56 
57 
58 /**
59  * @brief Return the @c DataProxy for this link.
60  * @param nothrow If true, return 0 on failure instead of throwing
61  * an exception.
62  *
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
67  * we're referencing.
68  */
69 inline
70 SG::DataProxy* DataLinkBase::proxy (bool nothrow /*= false*/) const
71 {
72  return m_proxy.proxy (nothrow);
73 }
74 
75 
76 /**
77  * @brief Return the data source for this reference.
78  */
79 inline
80 IProxyDict* DataLinkBase::source() const
81 {
82  return m_proxy.source();
83 }
84 
85 
86 /**
87  * @brief Finish initialization after link has been read.
88  * @param sg Associated store.
89  *
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.
93  *
94  * If @c sg is 0, then we use the global default store.
95  */
96 inline
97 bool DataLinkBase::toTransient (IProxyDict* sg /*= 0*/)
98 {
99  m_proxy.toTransient (m_persKey, sg);
100  return true;
101 }
102 
103 
104 /**
105  * @brief Finish initialization after link has been read.
106  * @param ctx Event context for this link.
107  *
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.
111  */
112 inline
113 bool DataLinkBase::toTransient (const EventContext& ctx)
114 {
115  m_proxy.toTransient (m_persKey, Atlas::proxyDictFromEventContext(ctx));
116  return true;
117 }
118 
119 
120 /**
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.
126  *
127  * The link should be clear before this is called.
128  * Returns true.
129  *
130  * If @c sg is 0, then we use the global default store.
131  */
132 inline
133 bool DataLinkBase::toTransient (const ID_type& dataID,
134  CLID link_clid,
135  IProxyDict* sg /*= 0*/)
136 {
137  if (!isDefault()) SG::throwExcBadToTransient();
138  m_persKey = m_proxy.toTransient (dataID, link_clid, sg);
139  return true;
140 }
141 
142 
143 /**
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.
149  *
150  * The link should be clear before this is called.
151  * Returns true.
152  */
153 inline
154 bool DataLinkBase::toTransient (const ID_type& dataID,
155  CLID link_clid,
156  const EventContext& ctx)
157 {
158  if (!isDefault()) SG::throwExcBadToTransient();
159  m_persKey = m_proxy.toTransient (dataID, link_clid, Atlas::proxyDictFromEventContext(ctx));
160  return true;
161 }
162 
163 
164 /**
165  * @brief Prepare this link for writing.
166  *
167  * One of the @c toPersistent methods should be called before
168  * trying to write the link with root.
169  *
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.
173  *
174  * Returns true.
175  *
176  * This version does not perform link remapping.
177  */
178 inline
179 bool DataLinkBase::toPersistent()
180 {
181  size_t dum = 0;
182  m_proxy.toPersistent (m_persKey, dum);
183  return true;
184 }
185 
186 
187 /**
188  * @brief Prepare this link for writing.
189  *
190  * One of the @c toPersistent methods should be called before
191  * trying to write the link with root.
192  *
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.
196  *
197  * This version does not perform link remapping.
198  *
199  * Returns true.
200  */
201 inline
202 bool DataLinkBase::toPersistentNoRemap()
203 {
204  m_proxy.toPersistentNoRemap (m_persKey);
205  return true;
206 }
207 
208 
209 /**
210  * @brief Compare for equality.
211  */
212 inline
213 bool DataLinkBase::operator== (const DataLinkBase& other) const
214 {
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);
218 }
219 
220 
221 /**
222  * @brief Compare for inequality.
223  */
224 inline
225 bool DataLinkBase::operator!= (const DataLinkBase& other) const
226 {
227  return !(*this == other);
228 }
229 
230 
231 //****************************************************************************
232 
233 
234 
235 /**
236  * @brief Default constructor.
237  */
238 inline
239 DataLinkBase::DataLinkBase()
240  : m_persKey(0)
241 {
242 }
243 
244 
245 /**
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.
250  *
251  * If @c sg is 0, we take the global default.
252  *
253  * May throw @c ExcCLIDMismatch.
254  */
255 inline
256 DataLinkBase::DataLinkBase (const_pointer_t obj,
257  CLID link_clid,
258  IProxyDict* sg)
259 {
260  toStorableObject (obj, link_clid, sg);
261 }
262 
263 
264 /**
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.
269  *
270  * If @c sg is 0, we take the global default.
271  *
272  * May throw @c ExcCLIDMismatch.
273  */
274 inline
275 DataLinkBase::DataLinkBase (const ID_type& dataID,
276  CLID link_clid,
277  IProxyDict* sg)
278 {
279  toIdentifiedObject (dataID, link_clid, sg);
280 }
281 
282 
283 /**
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.
288  *
289  * If @c sg is 0, we take the global default.
290  *
291  * May throw @c ExcCLIDMismatch.
292  */
293 inline
294 DataLinkBase::DataLinkBase (sgkey_t key, CLID link_clid, IProxyDict* sg)
295 {
296  toIdentifiedObject (key, link_clid, sg);
297 }
298 
299 
300 /**
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.
305  */
306 inline
307 DataLinkBase::DataLinkBase (sgkey_t key, const SG::DataProxyHolder& holder)
308  : m_persKey (key),
309  m_proxy (holder)
310 {
311 }
312 
313 
314 /**
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.
320  *
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
323  * default.
324  *
325  * May throw @c ExcCLIDMismatch.
326  */
327 inline
328 void DataLinkBase::toStorableObject (const_pointer_t obj,
329  CLID link_clid,
330  IProxyDict* sg)
331 {
332  m_persKey = m_proxy.toStorableObject (obj, link_clid, sg);
333 }
334 
335 
336 /**
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.
342  *
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
345  * default.
346  */
347 inline
348 void DataLinkBase::toIdentifiedObject (const ID_type& dataID,
349  CLID link_clid,
350  IProxyDict* sg)
351 {
352  m_persKey = m_proxy.toIdentifiedObject (dataID, link_clid, sg);
353 }
354 
355 
356 /**
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.
361  *
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
364  * default.
365  *
366  * May throw @c ExcCLIDMismatch.
367  */
368 inline
369 void DataLinkBase::toIdentifiedObject (sgkey_t key,
370  CLID link_clid,
371  IProxyDict* sg)
372 {
373  m_persKey = key;
374  m_proxy.toIdentifiedObject (key, link_clid, sg);
375 }
376 
377 
378 /**
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).
388  */
389 inline
390 void* DataLinkBase::storableBase (castfn_t* castfn,
391  const CLID& clid,
392  bool isConst) const
393 {
394  return m_proxy.storableBase (castfn, clid, isConst);
395 }
396 
397 
398 /**
399  * @brief Throw a @c ExcInvalidLink exception for this link.
400  * @param sgkey The hashed key for this link.
401  *
402  * This will fill in parameters for the exception message from the proxy.
403  */
404 inline
405 void DataLinkBase::throwInvalidLink() const
406 {
407  m_proxy.throwInvalidLink(m_persKey);
408 }