2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
5 * @file StoreGate/ReadHandle.icc
6 * @author S. Binet, P. Calafiura, scott snyder <snyder@bnl.gov>
7 * @date Updated: Feb, 2016
8 * @brief Handle class for reading from StoreGate.
11 #ifndef STOREGATE_SG_READHANDLE_ICC
12 #define STOREGATE_SG_READHANDLE_ICC 1
15 #include "StoreGate/exceptions.h"
16 #include "AthenaKernel/ClassID_traits.h"
23 //************************************************************************
29 * @brief Default constructor.
31 * The handle will not be usable until a non-blank key is assigned.
35 ReadHandle<T>::ReadHandle()
36 : VarHandleBase(ClassID_traits<T>::ID(), Gaudi::DataHandle::Reader)
42 * @brief Constructor with full arguments.
43 * @param sgkey StoreGate key of the referenced object.
44 * @param storename Name of the referenced event store.
48 ReadHandle<T>::ReadHandle(const std::string& sgkey,
49 const std::string& storename /*= "StoreGateSvc"*/)
50 : VarHandleBase( ClassID_traits<T>::ID(),
51 sgkey, Gaudi::DataHandle::Reader, storename )
57 * @brief Constructor from a ReadHandleKey.
58 * @param key The key object holding the clid/key/store.
60 * This will raise an exception if the StoreGate key is blank,
61 * or if the event store cannot be found.
65 ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key)
66 : VarHandleBase (key, nullptr)
72 * @brief Constructor from a ReadHandleKey and an explicit event context.
73 * @param key The key object holding the clid/key.
74 * @param ctx The event context.
76 * This will raise an exception if the StoreGate key is blank,
77 * or if the event store cannot be found.
79 * If the default event store has been requested, then the thread-specific
80 * store from the event context will be used.
84 ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key,
85 const EventContext& ctx)
86 : VarHandleBase (key, &ctx)
92 * @brief Constructor from a DataProxy.
93 * @param proxy The proxy to which to bind.
94 * @param mode Mode of this handle (read/write/update).
96 * This handle will be bound to the given proxy.
100 ReadHandle<T>::ReadHandle (SG::DataProxy* proxy)
101 : VarHandleBase (proxy, Gaudi::DataHandle::Reader)
107 * @brief Copy constructor.
111 ReadHandle<T>::ReadHandle(const ReadHandle& h)
118 * @brief Move constructor.
122 ReadHandle<T>::ReadHandle(ReadHandle&& h)
123 : VarHandleBase(std::move(h))
129 * @brief Assignment operator.
133 ReadHandle<T>::ReadHandle::operator= (const ReadHandle& h)
136 this->VarHandleBase::operator=(h);
141 * @brief Move operator.
146 ReadHandle<T>::ReadHandle::operator= (ReadHandle&& h)
149 this->VarHandleBase::operator=(std::move(h));
155 * @brief Dereference the pointer.
156 * Throws ExcNullReadHandle on failure.
160 typename ReadHandle<T>::const_pointer_type
161 ReadHandle<T>::operator->()
163 return checkedCPtr();
168 * @brief Dereference the pointer.
169 * Throws ExcNullReadHandle on failure.
173 typename ReadHandle<T>::const_reference_type
174 ReadHandle<T>::operator*()
176 return *checkedCPtr();
181 * @brief Dereference the pointer.
182 * Returns nullptr on failure.
186 typename ReadHandle<T>::const_pointer_type
187 ReadHandle<T>::cptr()
189 return reinterpret_cast<const_pointer_type>(this->typeless_cptr());
194 * @brief Dereference the pointer.
195 * Returns nullptr on failure.
199 typename ReadHandle<T>::const_pointer_type
207 * @brief Return the cached pointer directly; no lookup.
211 typename ReadHandle<T>::const_pointer_type
212 ReadHandle<T>::cachedPtr() const
214 return reinterpret_cast<const_pointer_type>(this->m_ptr);
219 * @brief Can the handle be successfully dereferenced?
223 bool ReadHandle<T>::isValid()
225 return 0 != this->typeless_dataPointer(true);
230 * @brief Dereference the pointer, but don't cache anything.
234 typename ReadHandle<T>::const_pointer_type
235 ReadHandle<T>::get() const
237 return reinterpret_cast<const_pointer_type> (this->get_impl (nullptr));
242 * @brief Dereference the pointer, but don't cache anything.
243 * @param ctx The event context to use.
247 typename ReadHandle<T>::const_pointer_type
248 ReadHandle<T>::get (const EventContext& ctx) const
250 return reinterpret_cast<const_pointer_type> (this->get_impl (&ctx));
255 * @brief Make an alias.
256 * @param key Alternate key by which the referenced object should be known.
258 * The current handle should be valid and referencing an object.
260 * The object will also be known by the name given in @c key.
263 StatusCode ReadHandle<T>::alias (const WriteHandleKey<T>& key)
266 return StatusCode::FAILURE;
267 return symLink_impl (this->clid(), key.key());
272 * @brief Protected constructor used by WriteDecorHandle.
273 * @param key The key object holding the clid/key.
274 * @param ctx The event context, or nullptr to use the global default.
278 ReadHandle<T>::ReadHandle (const VarHandleKey& key, const EventContext* ctx)
279 : VarHandleBase (key, ctx)
281 // cppcheck-suppress missingReturn; false positive
286 * @brief Helper: dereference the pointer.
287 * Throws ExcNullReadHandle on failure.
291 typename ReadHandle<T>::const_pointer_type
292 ReadHandle<T>::checkedCPtr()
294 const_pointer_type p = this->cptr();
296 throwExcNullReadHandle (clid(), key(), store());
302 * @brief Return a @c ReadHandle referencing @c key.
303 * @param key The key object holding the clid/key/store.
305 * This will raise an exception if the StoreGate key is blank,
306 * or if the event store cannot be found.
309 ReadHandle<T> makeHandle (const ReadHandleKey<T>& key)
311 return ReadHandle<T> (key);
316 * @brief Return a @c ReadHandle referencing @c key for an explicit context.
317 * @param key The key object holding the clid/key/store.
318 * @param ctx The event context.
320 * This will raise an exception if the StoreGate key is blank,
321 * or if the event store cannot be found.
323 * If the default event store has been requested, then the thread-specific
324 * store from the event context will be used.
327 ReadHandle<T> makeHandle (const ReadHandleKey<T>& key,
328 const EventContext& ctx)
330 return ReadHandle<T> (key, ctx);
335 * @brief Convenience function to retrieve an object given a @c ReadHandleKey.
336 * @param key The key to retrieve.
337 * @param ctx The event context.
339 * Returns the object. Returns nullptr if the key is null or if there's an error.
343 const T* get (const ReadHandleKey<T>& key)
345 if (key.key().empty()) return nullptr;
346 ReadHandle<T> h (key);
352 * @brief Convenience function to retrieve an object given a @c ReadHandleKey.
353 * @param key The key to retrieve.
355 * Returns the object. Returns nullptr if the key is null or if there's an error.
359 const T* get (const ReadHandleKey<T>& key,
360 const EventContext& ctx)
362 if (key.key().empty()) return nullptr;
363 ReadHandle<T> h (key, ctx);
371 #endif //> !STOREGATE_SG_READHANDLE_ICC