ATLAS Offline Software
StoreGate/StoreGate/ReadHandle.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // $Id: ReadHandle.icc 797637 2017-02-17 02:32:11Z ssnyder $
6 /**
7  * @file StoreGate/ReadHandle.icc
8  * @author S. Binet, P. Calafiura, scott snyder <snyder@bnl.gov>
9  * @date Updated: Feb, 2016
10  * @brief Handle class for reading from StoreGate.
11  */
12 
13 #ifndef STOREGATE_SG_READHANDLE_ICC
14 #define STOREGATE_SG_READHANDLE_ICC 1
15 
16 
17 #include "StoreGate/exceptions.h"
18 #include "AthenaKernel/ClassID_traits.h"
19 #include <stdexcept>
20 
21 
22 namespace SG {
23 
24 
25 //************************************************************************
26 // Constructors, etc.
27 //
28 
29 
30 /**
31  * @brief Default constructor.
32  *
33  * The handle will not be usable until a non-blank key is assigned.
34  */
35 template <class T>
36 inline
37 ReadHandle<T>::ReadHandle()
38  : VarHandleBase(ClassID_traits<T>::ID(), Gaudi::DataHandle::Reader)
39 {
40 }
41 
42 
43 /**
44  * @brief Constructor specifying the key as a string.
45  * @param sgkey StoreGate key of the referenced object.
46  * @param storename Name of the referenced event store.
47  */
48 template <class T>
49 inline
50 ReadHandle<T>::ReadHandle(const std::string& sgkey,
51  const std::string& storename /*= "StoreGateSvc"*/)
52  : VarHandleBase( ClassID_traits<T>::ID(),
53  sgkey, Gaudi::DataHandle::Reader, storename, nullptr )
54 {
55 }
56 
57 
58 /**
59  * @brief Constructor specifying the key as a string, with context.
60  * @param sgkey StoreGate key of the referenced object.
61  * @param ctx The event context.
62  */
63 template <class T>
64 inline
65 ReadHandle<T>::ReadHandle(const std::string& sgkey,
66  const EventContext& ctx)
67  : ReadHandle(sgkey, StoreID::storeName(StoreID::EVENT_STORE), ctx)
68 {
69 }
70 
71 
72 /**
73  * @brief Constructor specifying the key as a string, with context.
74  * @param sgkey StoreGate key of the referenced object.
75  * @param storename Name of the referenced event store.
76  * @param ctx The event context.
77  */
78 template <class T>
79 inline
80 ReadHandle<T>::ReadHandle(const std::string& sgkey,
81  const std::string& storename,
82  const EventContext& ctx)
83  : VarHandleBase( ClassID_traits<T>::ID(),
84  sgkey, Gaudi::DataHandle::Reader, storename, &ctx )
85 {
86 }
87 
88 
89 /**
90  * @brief Constructor from a ReadHandleKey.
91  * @param key The key object holding the clid/key/store.
92  *
93  * This will raise an exception if the StoreGate key is blank,
94  * or if the event store cannot be found.
95  */
96 template <class T>
97 inline
98 ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key)
99  : VarHandleBase (key, nullptr)
100 {
101 }
102 
103 
104 /**
105  * @brief Constructor from a ReadHandleKey and an explicit event context.
106  * @param key The key object holding the clid/key.
107  * @param ctx The event context.
108  *
109  * This will raise an exception if the StoreGate key is blank,
110  * or if the event store cannot be found.
111  *
112  * If the default event store has been requested, then the thread-specific
113  * store from the event context will be used.
114  */
115 template <class T>
116 inline
117 ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key,
118  const EventContext& ctx)
119  : VarHandleBase (key, &ctx)
120 {
121 }
122 
123 
124 /**
125  * @brief Constructor from a DataProxy.
126  * @param proxy The proxy to which to bind.
127  * @param mode Mode of this handle (read/write/update).
128  *
129  * This handle will be bound to the given proxy.
130  */
131 template <class T>
132 inline
133 ReadHandle<T>::ReadHandle (SG::DataProxy* proxy)
134  : VarHandleBase (proxy, Gaudi::DataHandle::Reader)
135 {
136 }
137 
138 
139 /**
140  * @brief Copy constructor.
141  */
142 template <class T>
143 inline
144 ReadHandle<T>::ReadHandle(const ReadHandle& h)
145  : VarHandleBase(h)
146 {
147 }
148 
149 
150 /**
151  * @brief Move constructor.
152  */
153 template <class T>
154 inline
155 ReadHandle<T>::ReadHandle(ReadHandle&& h)
156  : VarHandleBase(std::move(h))
157 {
158 }
159 
160 
161 /**
162  * @brief Assignment operator.
163  */
164 template <class T>
165 ReadHandle<T>&
166 ReadHandle<T>::ReadHandle::operator= (const ReadHandle& h)
167 {
168  if (this != &h)
169  this->VarHandleBase::operator=(h);
170  return *this;
171 }
172 
173 /**
174  * @brief Move operator.
175  */
176 template <class T>
177 inline
178 ReadHandle<T>&
179 ReadHandle<T>::ReadHandle::operator= (ReadHandle&& h)
180 {
181  if (this != &h)
182  this->VarHandleBase::operator=(std::move(h));
183  return *this;
184 }
185 
186 
187 /**
188  * @brief Dereference the pointer.
189  * Throws ExcNullReadHandle on failure.
190  */
191 template <class T>
192 inline
193 typename ReadHandle<T>::const_pointer_type
194 ReadHandle<T>::operator->()
195 {
196  return checkedCPtr();
197 }
198 
199 
200 /**
201  * @brief Dereference the pointer.
202  * Throws ExcNullReadHandle on failure.
203  */
204 template <class T>
205 inline
206 typename ReadHandle<T>::const_reference_type
207 ReadHandle<T>::operator*()
208 {
209  return *checkedCPtr();
210 }
211 
212 
213 /**
214  * @brief Dereference the pointer.
215  * Returns nullptr on failure.
216  */
217 template <class T>
218 inline
219 typename ReadHandle<T>::const_pointer_type
220 ReadHandle<T>::cptr()
221 {
222  return reinterpret_cast<const_pointer_type>(this->typeless_cptr());
223 }
224 
225 
226 /**
227  * @brief Dereference the pointer.
228  * Returns nullptr on failure.
229  */
230 template <class T>
231 inline
232 typename ReadHandle<T>::const_pointer_type
233 ReadHandle<T>::ptr()
234 {
235  return cptr();
236 }
237 
238 
239 /**
240  * @brief Return the cached pointer directly; no lookup.
241  */
242 template <class T>
243 inline
244 typename ReadHandle<T>::const_pointer_type
245 ReadHandle<T>::cachedPtr() const
246 {
247  return reinterpret_cast<const_pointer_type>(this->m_ptr);
248 }
249 
250 
251 /**
252  * @brief Can the handle be successfully dereferenced?
253  */
254 template <class T>
255 inline
256 bool ReadHandle<T>::isValid()
257 {
258  return 0 != this->typeless_dataPointer(true);
259 }
260 
261 
262 /**
263  * @brief Dereference the pointer, but don't cache anything.
264  */
265 template <class T>
266 inline
267 typename ReadHandle<T>::const_pointer_type
268 ReadHandle<T>::get() const
269 {
270  return reinterpret_cast<const_pointer_type> (this->get_impl (nullptr));
271 }
272 
273 
274 /**
275  * @brief Dereference the pointer, but don't cache anything.
276  * @param ctx The event context to use.
277  */
278 template <class T>
279 inline
280 typename ReadHandle<T>::const_pointer_type
281 ReadHandle<T>::get (const EventContext& ctx) const
282 {
283  return reinterpret_cast<const_pointer_type> (this->get_impl (&ctx));
284 }
285 
286 
287 /**
288  * @brief Make an alias.
289  * @param key Alternate key by which the referenced object should be known.
290  *
291  * The current handle should be valid and referencing an object.
292  *
293  * The object will also be known by the name given in @c key.
294  */
295 template <class T>
296 StatusCode ReadHandle<T>::alias (const WriteHandleKey<T>& key)
297 {
298  if (!cptr())
299  return StatusCode::FAILURE;
300  return symLink_impl (this->clid(), key.key());
301 }
302 
303 
304 /**
305  * @brief Protected constructor used by WriteDecorHandle.
306  * @param key The key object holding the clid/key.
307  * @param ctx The event context, or nullptr to use the global default.
308  */
309 template <class T>
310 inline
311 ReadHandle<T>::ReadHandle (const VarHandleKey& key, const EventContext* ctx)
312  : VarHandleBase (key, ctx)
313 {
314  // cppcheck-suppress missingReturn; false positive
315 }
316 
317 
318 /**
319  * @brief Helper: dereference the pointer.
320  * Throws ExcNullReadHandle on failure.
321  */
322 template <class T>
323 inline
324 typename ReadHandle<T>::const_pointer_type
325 ReadHandle<T>::checkedCPtr()
326 {
327  const_pointer_type p = this->cptr();
328  if (!p)
329  throwExcNullReadHandle (clid(), key(), store());
330  return p;
331 }
332 
333 
334 /**
335  * @brief Return a @c ReadHandle referencing @c key.
336  * @param key The key object holding the clid/key/store.
337  *
338  * This will raise an exception if the StoreGate key is blank,
339  * or if the event store cannot be found.
340  */
341 template <class T>
342 ReadHandle<T> makeHandle (const ReadHandleKey<T>& key)
343 {
344  return ReadHandle<T> (key);
345 }
346 
347 
348 /**
349  * @brief Return a @c ReadHandle referencing @c key for an explicit context.
350  * @param key The key object holding the clid/key/store.
351  * @param ctx The event context.
352  *
353  * This will raise an exception if the StoreGate key is blank,
354  * or if the event store cannot be found.
355  *
356  * If the default event store has been requested, then the thread-specific
357  * store from the event context will be used.
358  */
359 template <class T>
360 ReadHandle<T> makeHandle (const ReadHandleKey<T>& key,
361  const EventContext& ctx)
362 {
363  return ReadHandle<T> (key, ctx);
364 }
365 
366 
367 /**
368  * @brief Convenience function to retrieve an object given a @c ReadHandleKey.
369  * @param key The key to retrieve.
370  * @param ctx The event context.
371  *
372  * Returns the object. Returns nullptr if the key is null or if there's an error.
373  */
374 template <class T>
375 inline
376 const T* get (const ReadHandleKey<T>& key)
377 {
378  if (key.key().empty()) return nullptr;
379  ReadHandle<T> h (key);
380  return h.get();
381 }
382 
383 
384 /**
385  * @brief Convenience function to retrieve an object given a @c ReadHandleKey.
386  * @param key The key to retrieve.
387  *
388  * Returns the object. Returns nullptr if the key is null or if there's an error.
389  */
390 template <class T>
391 inline
392 const T* get (const ReadHandleKey<T>& key,
393  const EventContext& ctx)
394 {
395  if (key.key().empty()) return nullptr;
396  ReadHandle<T> h (key, ctx);
397  return h.get();
398 }
399 
400 
401 /**
402  * @brief Convenience function to retrieve an object given a @c ReadHandleKey.
403  * @param ptr Pointer to the retrieved object.
404  * @param key The key to retrieve.
405  *
406  * In case of error, sets @c ptr to nullptr and returns FAILURE. In case of an
407  * empty key, sets @c ptr to nullptr and returns SUCCESS.
408  */
409 template <class T>
410 inline
411 StatusCode get (const T*& ptr,
412  const ReadHandleKey<T>& key,
413  const EventContext& ctx)
414 {
415  ptr = get(key, ctx);
416  return (ptr || key.empty()) ? StatusCode::SUCCESS : StatusCode::FAILURE;
417 }
418 
419 } /* namespace SG */
420 
421 
422 #endif //> !STOREGATE_SG_READHANDLE_ICC