2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
5// $Id: ReadHandle.icc 797637 2017-02-17 02:32:11Z ssnyder $
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.
13#ifndef STOREGATE_SG_READHANDLE_ICC
14#define STOREGATE_SG_READHANDLE_ICC 1
17#include "StoreGate/exceptions.h"
18#include "AthenaKernel/ClassID_traits.h"
25//************************************************************************
31 * @brief Default constructor.
33 * The handle will not be usable until a non-blank key is assigned.
37ReadHandle<T>::ReadHandle()
38 : VarHandleBase(ClassID_traits<T>::ID(), Gaudi::DataHandle::Reader)
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.
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 )
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.
65ReadHandle<T>::ReadHandle(const std::string& sgkey, const EventContext& ctx)
66 : ReadHandle(sgkey, StoreID::storeName(StoreID::EVENT_STORE), ctx)
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.
78ReadHandle<T>::ReadHandle(const std::string& sgkey, IProxyDict* sg)
79 : VarHandleBase(ClassID_traits<T>::ID(), sgkey, Gaudi::DataHandle::Reader, sg)
81 // cppcheck-suppress missingReturn; false positive
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.
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 )
103 * @brief Constructor from a ReadHandleKey.
104 * @param key The key object holding the clid/key/store.
106 * This will raise an exception if the StoreGate key is blank,
107 * or if the event store cannot be found.
111ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key)
112 : VarHandleBase (key, nullptr)
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.
122 * This will raise an exception if the StoreGate key is blank,
123 * or if the event store cannot be found.
125 * If the default event store has been requested, then the thread-specific
126 * store from the event context will be used.
130ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key,
131 const EventContext& ctx)
132 : VarHandleBase (key, &ctx)
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).
142 * This handle will be bound to the given proxy.
146ReadHandle<T>::ReadHandle (SG::DataProxy* proxy)
147 : VarHandleBase (proxy, Gaudi::DataHandle::Reader)
153 * @brief Copy constructor.
157ReadHandle<T>::ReadHandle(const ReadHandle& h)
164 * @brief Move constructor.
168ReadHandle<T>::ReadHandle(ReadHandle&& h) noexcept
169 : VarHandleBase(std::move(h))
175 * @brief Assignment operator.
179ReadHandle<T>::ReadHandle::operator= (const ReadHandle& h)
182 this->VarHandleBase::operator=(h);
187 * @brief Move operator.
192ReadHandle<T>::ReadHandle::operator= (ReadHandle&& h) noexcept
195 this->VarHandleBase::operator=(std::move(h));
201 * @brief Dereference the pointer.
202 * Throws ExcNullReadHandle on failure.
206typename ReadHandle<T>::const_pointer_type
207ReadHandle<T>::operator->()
209 return checkedCPtr();
214 * @brief Dereference the pointer.
215 * Throws ExcNullReadHandle on failure.
219typename ReadHandle<T>::const_reference_type
220ReadHandle<T>::operator*()
222 return *checkedCPtr();
227 * @brief Dereference the pointer.
228 * Returns nullptr on failure.
232typename ReadHandle<T>::const_pointer_type
235 return reinterpret_cast<const_pointer_type>(this->typeless_cptr());
240 * @brief Dereference the pointer.
241 * Returns nullptr on failure.
245typename ReadHandle<T>::const_pointer_type
253 * @brief Return the cached pointer directly; no lookup.
257typename ReadHandle<T>::const_pointer_type
258ReadHandle<T>::cachedPtr() const
260 return reinterpret_cast<const_pointer_type>(this->m_ptr);
265 * @brief Can the handle be successfully dereferenced?
269bool ReadHandle<T>::isValid()
271 return 0 != this->typeless_dataPointer(true);
276 * @brief Dereference the pointer, but don't cache anything.
280typename ReadHandle<T>::const_pointer_type
281ReadHandle<T>::get() const
283 return reinterpret_cast<const_pointer_type> (this->get_impl (nullptr));
288 * @brief Dereference the pointer, but don't cache anything.
289 * @param ctx The event context to use.
293typename ReadHandle<T>::const_pointer_type
294ReadHandle<T>::get (const EventContext& ctx) const
296 return reinterpret_cast<const_pointer_type> (this->get_impl (&ctx));
301 * @brief Make an alias.
302 * @param key Alternate key by which the referenced object should be known.
304 * The current handle should be valid and referencing an object.
306 * The object will also be known by the name given in @c key.
309StatusCode ReadHandle<T>::alias (const WriteHandleKey<T>& key)
312 return StatusCode::FAILURE;
313 return symLink_impl (this->clid(), key.key());
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.
324ReadHandle<T>::ReadHandle (const VarHandleKey& key, const EventContext* ctx)
325 : VarHandleBase (key, ctx)
327 // cppcheck-suppress missingReturn; false positive
332 * @brief Helper: dereference the pointer.
333 * Throws ExcNullReadHandle on failure.
337typename ReadHandle<T>::const_pointer_type
338ReadHandle<T>::checkedCPtr()
340 const_pointer_type p = this->cptr();
342 throwExcNullReadHandle (clid(), key(), store());
348 * @brief Return a @c ReadHandle referencing @c key.
349 * @param key The key object holding the clid/key/store.
351 * This will raise an exception if the StoreGate key is blank,
352 * or if the event store cannot be found.
355ReadHandle<T> makeHandle (const ReadHandleKey<T>& key)
357 return ReadHandle<T> (key);
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.
366 * This will raise an exception if the StoreGate key is blank,
367 * or if the event store cannot be found.
369 * If the default event store has been requested, then the thread-specific
370 * store from the event context will be used.
373ReadHandle<T> makeHandle (const ReadHandleKey<T>& key,
374 const EventContext& ctx)
376 return ReadHandle<T> (key, ctx);
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.
385 * Returns the object. Returns nullptr if the key is null or if there's an error.
389const T* get (const ReadHandleKey<T>& key)
391 if (key.key().empty()) return nullptr;
392 ReadHandle<T> h (key);
398 * @brief Convenience function to retrieve an object given a @c ReadHandleKey.
399 * @param key The key to retrieve.
401 * Returns the object. Returns nullptr if the key is null or if there's an error.
405const T* get (const ReadHandleKey<T>& key,
406 const EventContext& ctx)
408 if (key.key().empty()) return nullptr;
409 ReadHandle<T> h (key, ctx);
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.
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.
424StatusCode get (const T*& ptr,
425 const ReadHandleKey<T>& key,
426 const EventContext& ctx)
429 return (ptr || key.empty()) ? StatusCode::SUCCESS : StatusCode::FAILURE;
435#endif //> !STOREGATE_SG_READHANDLE_ICC