ATLAS Offline Software
Loading...
Searching...
No Matches
StoreGate/StoreGate/ReadHandle.icc
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 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, const EventContext& ctx)
66 : ReadHandle(sgkey, StoreID::storeName(StoreID::EVENT_STORE), ctx)
67{
68}
69
70
71/**
72 * @brief Constructor specifying the key as a string, with data store.
73 * @param sgkey StoreGate key of the referenced object.
74 * @param sg Data store.
75 */
76template <class T>
77inline
78ReadHandle<T>::ReadHandle(const std::string& sgkey, IProxyDict* sg)
79 : VarHandleBase(ClassID_traits<T>::ID(), sgkey, Gaudi::DataHandle::Reader, sg)
80{
81 // cppcheck-suppress missingReturn; false positive
82}
83
84
85/**
86 * @brief Constructor specifying the key as a string, with context.
87 * @param sgkey StoreGate key of the referenced object.
88 * @param storename Name of the referenced event store.
89 * @param ctx The event context.
90 */
91template <class T>
92inline
93ReadHandle<T>::ReadHandle(const std::string& sgkey,
94 const std::string& storename,
95 const EventContext& ctx)
96 : VarHandleBase( ClassID_traits<T>::ID(),
97 sgkey, Gaudi::DataHandle::Reader, storename, &ctx )
98{
99}
100
101
102/**
103 * @brief Constructor from a ReadHandleKey.
104 * @param key The key object holding the clid/key/store.
105 *
106 * This will raise an exception if the StoreGate key is blank,
107 * or if the event store cannot be found.
108 */
109template <class T>
110inline
111ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key)
112 : VarHandleBase (key, nullptr)
113{
114}
115
116
117/**
118 * @brief Constructor from a ReadHandleKey and an explicit event context.
119 * @param key The key object holding the clid/key.
120 * @param ctx The event context.
121 *
122 * This will raise an exception if the StoreGate key is blank,
123 * or if the event store cannot be found.
124 *
125 * If the default event store has been requested, then the thread-specific
126 * store from the event context will be used.
127 */
128template <class T>
129inline
130ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key,
131 const EventContext& ctx)
132 : VarHandleBase (key, &ctx)
133{
134}
135
136
137/**
138 * @brief Constructor from a DataProxy.
139 * @param proxy The proxy to which to bind.
140 * @param mode Mode of this handle (read/write/update).
141 *
142 * This handle will be bound to the given proxy.
143 */
144template <class T>
145inline
146ReadHandle<T>::ReadHandle (SG::DataProxy* proxy)
147 : VarHandleBase (proxy, Gaudi::DataHandle::Reader)
148{
149}
150
151
152/**
153 * @brief Copy constructor.
154 */
155template <class T>
156inline
157ReadHandle<T>::ReadHandle(const ReadHandle& h)
158 : VarHandleBase(h)
159{
160}
161
162
163/**
164 * @brief Move constructor.
165 */
166template <class T>
167inline
168ReadHandle<T>::ReadHandle(ReadHandle&& h) noexcept
169 : VarHandleBase(std::move(h))
170{
171}
172
173
174/**
175 * @brief Assignment operator.
176 */
177template <class T>
178ReadHandle<T>&
179ReadHandle<T>::ReadHandle::operator= (const ReadHandle& h)
180{
181 if (this != &h)
182 this->VarHandleBase::operator=(h);
183 return *this;
184}
185
186/**
187 * @brief Move operator.
188 */
189template <class T>
190inline
191ReadHandle<T>&
192ReadHandle<T>::ReadHandle::operator= (ReadHandle&& h) noexcept
193{
194 if (this != &h)
195 this->VarHandleBase::operator=(std::move(h));
196 return *this;
197}
198
199
200/**
201 * @brief Dereference the pointer.
202 * Throws ExcNullReadHandle on failure.
203 */
204template <class T>
205inline
206typename ReadHandle<T>::const_pointer_type
207ReadHandle<T>::operator->()
208{
209 return checkedCPtr();
210}
211
212
213/**
214 * @brief Dereference the pointer.
215 * Throws ExcNullReadHandle on failure.
216 */
217template <class T>
218inline
219typename ReadHandle<T>::const_reference_type
220ReadHandle<T>::operator*()
221{
222 return *checkedCPtr();
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>::cptr()
234{
235 return reinterpret_cast<const_pointer_type>(this->typeless_cptr());
236}
237
238
239/**
240 * @brief Dereference the pointer.
241 * Returns nullptr on failure.
242 */
243template <class T>
244inline
245typename ReadHandle<T>::const_pointer_type
246ReadHandle<T>::ptr()
247{
248 return cptr();
249}
250
251
252/**
253 * @brief Return the cached pointer directly; no lookup.
254 */
255template <class T>
256inline
257typename ReadHandle<T>::const_pointer_type
258ReadHandle<T>::cachedPtr() const
259{
260 return reinterpret_cast<const_pointer_type>(this->m_ptr);
261}
262
263
264/**
265 * @brief Can the handle be successfully dereferenced?
266 */
267template <class T>
268inline
269bool ReadHandle<T>::isValid()
270{
271 return 0 != this->typeless_dataPointer(true);
272}
273
274
275/**
276 * @brief Dereference the pointer, but don't cache anything.
277 */
278template <class T>
279inline
280typename ReadHandle<T>::const_pointer_type
281ReadHandle<T>::get() const
282{
283 return reinterpret_cast<const_pointer_type> (this->get_impl (nullptr));
284}
285
286
287/**
288 * @brief Dereference the pointer, but don't cache anything.
289 * @param ctx The event context to use.
290 */
291template <class T>
292inline
293typename ReadHandle<T>::const_pointer_type
294ReadHandle<T>::get (const EventContext& ctx) const
295{
296 return reinterpret_cast<const_pointer_type> (this->get_impl (&ctx));
297}
298
299
300/**
301 * @brief Make an alias.
302 * @param key Alternate key by which the referenced object should be known.
303 *
304 * The current handle should be valid and referencing an object.
305 *
306 * The object will also be known by the name given in @c key.
307 */
308template <class T>
309StatusCode ReadHandle<T>::alias (const WriteHandleKey<T>& key)
310{
311 if (!cptr())
312 return StatusCode::FAILURE;
313 return symLink_impl (this->clid(), key.key());
314}
315
316
317/**
318 * @brief Protected constructor used by WriteDecorHandle.
319 * @param key The key object holding the clid/key.
320 * @param ctx The event context, or nullptr to use the global default.
321 */
322template <class T>
323inline
324ReadHandle<T>::ReadHandle (const VarHandleKey& key, const EventContext* ctx)
325 : VarHandleBase (key, ctx)
326{
327 // cppcheck-suppress missingReturn; false positive
328}
329
330
331/**
332 * @brief Helper: dereference the pointer.
333 * Throws ExcNullReadHandle on failure.
334 */
335template <class T>
336inline
337typename ReadHandle<T>::const_pointer_type
338ReadHandle<T>::checkedCPtr()
339{
340 const_pointer_type p = this->cptr();
341 if (!p)
342 throwExcNullReadHandle (clid(), key(), store());
343 return p;
344}
345
346
347/**
348 * @brief Return a @c ReadHandle referencing @c key.
349 * @param key The key object holding the clid/key/store.
350 *
351 * This will raise an exception if the StoreGate key is blank,
352 * or if the event store cannot be found.
353 */
354template <class T>
355ReadHandle<T> makeHandle (const ReadHandleKey<T>& key)
356{
357 return ReadHandle<T> (key);
358}
359
360
361/**
362 * @brief Return a @c ReadHandle referencing @c key for an explicit context.
363 * @param key The key object holding the clid/key/store.
364 * @param ctx The event context.
365 *
366 * This will raise an exception if the StoreGate key is blank,
367 * or if the event store cannot be found.
368 *
369 * If the default event store has been requested, then the thread-specific
370 * store from the event context will be used.
371 */
372template <class T>
373ReadHandle<T> makeHandle (const ReadHandleKey<T>& key,
374 const EventContext& ctx)
375{
376 return ReadHandle<T> (key, ctx);
377}
378
379
380/**
381 * @brief Convenience function to retrieve an object given a @c ReadHandleKey.
382 * @param key The key to retrieve.
383 * @param ctx The event context.
384 *
385 * Returns the object. Returns nullptr if the key is null or if there's an error.
386 */
387template <class T>
388inline
389const T* get (const ReadHandleKey<T>& key)
390{
391 if (key.key().empty()) return nullptr;
392 ReadHandle<T> h (key);
393 return h.get();
394}
395
396
397/**
398 * @brief Convenience function to retrieve an object given a @c ReadHandleKey.
399 * @param key The key to retrieve.
400 *
401 * Returns the object. Returns nullptr if the key is null or if there's an error.
402 */
403template <class T>
404inline
405const T* get (const ReadHandleKey<T>& key,
406 const EventContext& ctx)
407{
408 if (key.key().empty()) return nullptr;
409 ReadHandle<T> h (key, ctx);
410 return h.get();
411}
412
413
414/**
415 * @brief Convenience function to retrieve an object given a @c ReadHandleKey.
416 * @param ptr Pointer to the retrieved object.
417 * @param key The key to retrieve.
418 *
419 * In case of error, sets @c ptr to nullptr and returns FAILURE. In case of an
420 * empty key, sets @c ptr to nullptr and returns SUCCESS.
421 */
422template <class T>
423inline
424StatusCode get (const T*& ptr,
425 const ReadHandleKey<T>& key,
426 const EventContext& ctx)
427{
428 ptr = get(key, ctx);
429 return (ptr || key.empty()) ? StatusCode::SUCCESS : StatusCode::FAILURE;
430}
431
432} /* namespace SG */
433
434
435#endif //> !STOREGATE_SG_READHANDLE_ICC