2 Copyright (C) 2002-2025 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.
37 ReadHandle<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.
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 )
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.
65 ReadHandle<T>::ReadHandle(const std::string& sgkey,
66 const EventContext& ctx)
67 : ReadHandle(sgkey, StoreID::storeName(StoreID::EVENT_STORE), ctx)
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.
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 )
90 * @brief Constructor from a ReadHandleKey.
91 * @param key The key object holding the clid/key/store.
93 * This will raise an exception if the StoreGate key is blank,
94 * or if the event store cannot be found.
98 ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key)
99 : VarHandleBase (key, nullptr)
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.
109 * This will raise an exception if the StoreGate key is blank,
110 * or if the event store cannot be found.
112 * If the default event store has been requested, then the thread-specific
113 * store from the event context will be used.
117 ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key,
118 const EventContext& ctx)
119 : VarHandleBase (key, &ctx)
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).
129 * This handle will be bound to the given proxy.
133 ReadHandle<T>::ReadHandle (SG::DataProxy* proxy)
134 : VarHandleBase (proxy, Gaudi::DataHandle::Reader)
140 * @brief Copy constructor.
144 ReadHandle<T>::ReadHandle(const ReadHandle& h)
151 * @brief Move constructor.
155 ReadHandle<T>::ReadHandle(ReadHandle&& h)
156 : VarHandleBase(std::move(h))
162 * @brief Assignment operator.
166 ReadHandle<T>::ReadHandle::operator= (const ReadHandle& h)
169 this->VarHandleBase::operator=(h);
174 * @brief Move operator.
179 ReadHandle<T>::ReadHandle::operator= (ReadHandle&& h)
182 this->VarHandleBase::operator=(std::move(h));
188 * @brief Dereference the pointer.
189 * Throws ExcNullReadHandle on failure.
193 typename ReadHandle<T>::const_pointer_type
194 ReadHandle<T>::operator->()
196 return checkedCPtr();
201 * @brief Dereference the pointer.
202 * Throws ExcNullReadHandle on failure.
206 typename ReadHandle<T>::const_reference_type
207 ReadHandle<T>::operator*()
209 return *checkedCPtr();
214 * @brief Dereference the pointer.
215 * Returns nullptr on failure.
219 typename ReadHandle<T>::const_pointer_type
220 ReadHandle<T>::cptr()
222 return reinterpret_cast<const_pointer_type>(this->typeless_cptr());
227 * @brief Dereference the pointer.
228 * Returns nullptr on failure.
232 typename ReadHandle<T>::const_pointer_type
240 * @brief Return the cached pointer directly; no lookup.
244 typename ReadHandle<T>::const_pointer_type
245 ReadHandle<T>::cachedPtr() const
247 return reinterpret_cast<const_pointer_type>(this->m_ptr);
252 * @brief Can the handle be successfully dereferenced?
256 bool ReadHandle<T>::isValid()
258 return 0 != this->typeless_dataPointer(true);
263 * @brief Dereference the pointer, but don't cache anything.
267 typename ReadHandle<T>::const_pointer_type
268 ReadHandle<T>::get() const
270 return reinterpret_cast<const_pointer_type> (this->get_impl (nullptr));
275 * @brief Dereference the pointer, but don't cache anything.
276 * @param ctx The event context to use.
280 typename ReadHandle<T>::const_pointer_type
281 ReadHandle<T>::get (const EventContext& ctx) const
283 return reinterpret_cast<const_pointer_type> (this->get_impl (&ctx));
288 * @brief Make an alias.
289 * @param key Alternate key by which the referenced object should be known.
291 * The current handle should be valid and referencing an object.
293 * The object will also be known by the name given in @c key.
296 StatusCode ReadHandle<T>::alias (const WriteHandleKey<T>& key)
299 return StatusCode::FAILURE;
300 return symLink_impl (this->clid(), key.key());
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.
311 ReadHandle<T>::ReadHandle (const VarHandleKey& key, const EventContext* ctx)
312 : VarHandleBase (key, ctx)
314 // cppcheck-suppress missingReturn; false positive
319 * @brief Helper: dereference the pointer.
320 * Throws ExcNullReadHandle on failure.
324 typename ReadHandle<T>::const_pointer_type
325 ReadHandle<T>::checkedCPtr()
327 const_pointer_type p = this->cptr();
329 throwExcNullReadHandle (clid(), key(), store());
335 * @brief Return a @c ReadHandle referencing @c key.
336 * @param key The key object holding the clid/key/store.
338 * This will raise an exception if the StoreGate key is blank,
339 * or if the event store cannot be found.
342 ReadHandle<T> makeHandle (const ReadHandleKey<T>& key)
344 return ReadHandle<T> (key);
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.
353 * This will raise an exception if the StoreGate key is blank,
354 * or if the event store cannot be found.
356 * If the default event store has been requested, then the thread-specific
357 * store from the event context will be used.
360 ReadHandle<T> makeHandle (const ReadHandleKey<T>& key,
361 const EventContext& ctx)
363 return ReadHandle<T> (key, ctx);
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.
372 * Returns the object. Returns nullptr if the key is null or if there's an error.
376 const T* get (const ReadHandleKey<T>& key)
378 if (key.key().empty()) return nullptr;
379 ReadHandle<T> h (key);
385 * @brief Convenience function to retrieve an object given a @c ReadHandleKey.
386 * @param key The key to retrieve.
388 * Returns the object. Returns nullptr if the key is null or if there's an error.
392 const T* get (const ReadHandleKey<T>& key,
393 const EventContext& ctx)
395 if (key.key().empty()) return nullptr;
396 ReadHandle<T> h (key, ctx);
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.
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.
411 StatusCode get (const T*& ptr,
412 const ReadHandleKey<T>& key,
413 const EventContext& ctx)
416 return (ptr || key.empty()) ? StatusCode::SUCCESS : StatusCode::FAILURE;
422 #endif //> !STOREGATE_SG_READHANDLE_ICC