ATLAS Offline Software
Loading...
Searching...
No Matches
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
22namespace 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 */
35template <class T>
36inline
37ReadHandle<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 */
48template <class T>
49inline
50ReadHandle<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 */
63template <class T>
64inline
65ReadHandle<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 */
78template <class T>
79inline
80ReadHandle<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 */
96template <class T>
97inline
98ReadHandle<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 */
115template <class T>
116inline
117ReadHandle<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 */
131template <class T>
132inline
133ReadHandle<T>::ReadHandle (SG::DataProxy* proxy)
134 : VarHandleBase (proxy, Gaudi::DataHandle::Reader)
135{
136}
137
138
139/**
140 * @brief Copy constructor.
141 */
142template <class T>
143inline
144ReadHandle<T>::ReadHandle(const ReadHandle& h)
145 : VarHandleBase(h)
146{
147}
148
149
150/**
151 * @brief Move constructor.
152 */
153template <class T>
154inline
155ReadHandle<T>::ReadHandle(ReadHandle&& h)
156 : VarHandleBase(std::move(h))
157{
158}
159
160
161/**
162 * @brief Assignment operator.
163 */
164template <class T>
165ReadHandle<T>&
166ReadHandle<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 */
176template <class T>
177inline
178ReadHandle<T>&
179ReadHandle<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 */
191template <class T>
192inline
193typename ReadHandle<T>::const_pointer_type
194ReadHandle<T>::operator->()
195{
196 return checkedCPtr();
197}
198
199
200/**
201 * @brief Dereference the pointer.
202 * Throws ExcNullReadHandle on failure.
203 */
204template <class T>
205inline
206typename ReadHandle<T>::const_reference_type
207ReadHandle<T>::operator*()
208{
209 return *checkedCPtr();
210}
211
212
213/**
214 * @brief Dereference the pointer.
215 * Returns nullptr on failure.
216 */
217template <class T>
218inline
219typename ReadHandle<T>::const_pointer_type
220ReadHandle<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 */
230template <class T>
231inline
232typename ReadHandle<T>::const_pointer_type
233ReadHandle<T>::ptr()
234{
235 return cptr();
236}
237
238
239/**
240 * @brief Return the cached pointer directly; no lookup.
241 */
242template <class T>
243inline
244typename ReadHandle<T>::const_pointer_type
245ReadHandle<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 */
254template <class T>
255inline
256bool 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 */
265template <class T>
266inline
267typename ReadHandle<T>::const_pointer_type
268ReadHandle<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 */
278template <class T>
279inline
280typename ReadHandle<T>::const_pointer_type
281ReadHandle<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 */
295template <class T>
296StatusCode 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 */
309template <class T>
310inline
311ReadHandle<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 */
322template <class T>
323inline
324typename ReadHandle<T>::const_pointer_type
325ReadHandle<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 */
341template <class T>
342ReadHandle<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 */
359template <class T>
360ReadHandle<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 */
374template <class T>
375inline
376const 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 */
390template <class T>
391inline
392const 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 */
409template <class T>
410inline
411StatusCode 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