2 Copyright (C) 2002-2018 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 with full arguments.
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 )
59 * @brief Constructor from a ReadHandleKey.
60 * @param key The key object holding the clid/key/store.
62 * This will raise an exception if the StoreGate key is blank,
63 * or if the event store cannot be found.
67 ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key)
68 : VarHandleBase (key, nullptr)
74 * @brief Constructor from a ReadHandleKey and an explicit event context.
75 * @param key The key object holding the clid/key.
76 * @param ctx The event context.
78 * This will raise an exception if the StoreGate key is blank,
79 * or if the event store cannot be found.
81 * If the default event store has been requested, then the thread-specific
82 * store from the event context will be used.
86 ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key,
87 const EventContext& ctx)
88 : VarHandleBase (key, &ctx)
94 * @brief Constructor from a DataProxy.
95 * @param proxy The proxy to which to bind.
96 * @param mode Mode of this handle (read/write/update).
98 * This handle will be bound to the given proxy.
102 ReadHandle<T>::ReadHandle (SG::DataProxy* proxy)
103 : VarHandleBase (proxy, Gaudi::DataHandle::Reader)
109 * @brief Copy constructor.
113 ReadHandle<T>::ReadHandle(const ReadHandle& h)
120 * @brief Move constructor.
124 ReadHandle<T>::ReadHandle(ReadHandle&& h)
125 : VarHandleBase(std::move(h))
131 * @brief Assignment operator.
135 ReadHandle<T>::ReadHandle::operator= (const ReadHandle& h)
138 this->VarHandleBase::operator=(h);
143 * @brief Move operator.
148 ReadHandle<T>::ReadHandle::operator= (ReadHandle&& h)
151 this->VarHandleBase::operator=(std::move(h));
157 * @brief Dereference the pointer.
158 * Throws ExcNullReadHandle on failure.
162 typename ReadHandle<T>::const_pointer_type
163 ReadHandle<T>::operator->()
165 return checkedCPtr();
170 * @brief Dereference the pointer.
171 * Throws ExcNullReadHandle on failure.
175 typename ReadHandle<T>::const_reference_type
176 ReadHandle<T>::operator*()
178 return *checkedCPtr();
183 * @brief Dereference the pointer.
184 * Returns nullptr on failure.
188 typename ReadHandle<T>::const_pointer_type
189 ReadHandle<T>::cptr()
191 return reinterpret_cast<const_pointer_type>(this->typeless_cptr());
196 * @brief Dereference the pointer.
197 * Returns nullptr on failure.
201 typename ReadHandle<T>::const_pointer_type
209 * @brief Return the cached pointer directly; no lookup.
213 typename ReadHandle<T>::const_pointer_type
214 ReadHandle<T>::cachedPtr() const
216 return reinterpret_cast<const_pointer_type>(this->m_ptr);
221 * @brief Can the handle be successfully dereferenced?
225 bool ReadHandle<T>::isValid()
227 return 0 != this->typeless_dataPointer(true);
232 * @brief Dereference the pointer, but don't cache anything.
236 typename ReadHandle<T>::const_pointer_type
237 ReadHandle<T>::get() const
239 return reinterpret_cast<const_pointer_type> (this->get_impl (nullptr));
244 * @brief Dereference the pointer, but don't cache anything.
245 * @param ctx The event context to use.
249 typename ReadHandle<T>::const_pointer_type
250 ReadHandle<T>::get (const EventContext& ctx) const
252 return reinterpret_cast<const_pointer_type> (this->get_impl (&ctx));
257 * @brief Make an alias.
258 * @param key Alternate key by which the referenced object should be known.
260 * The current handle should be valid and referencing an object.
262 * The object will also be known by the name given in @c key.
265 StatusCode ReadHandle<T>::alias (const WriteHandleKey<T>& key)
268 return StatusCode::FAILURE;
269 return symLink_impl (this->clid(), key.key());
274 * @brief Protected constructor used by WriteDecorHandle.
275 * @param key The key object holding the clid/key.
276 * @param ctx The event context, or nullptr to use the global default.
280 ReadHandle<T>::ReadHandle (const VarHandleKey& key, const EventContext* ctx)
281 : VarHandleBase (key, ctx)
283 // cppcheck-suppress missingReturn; false positive
288 * @brief Helper: dereference the pointer.
289 * Throws ExcNullReadHandle on failure.
293 typename ReadHandle<T>::const_pointer_type
294 ReadHandle<T>::checkedCPtr()
296 const_pointer_type p = this->cptr();
298 throwExcNullReadHandle (clid(), key(), store());
304 * @brief Return a @c ReadHandle referencing @c key.
305 * @param key The key object holding the clid/key/store.
307 * This will raise an exception if the StoreGate key is blank,
308 * or if the event store cannot be found.
311 ReadHandle<T> makeHandle (const ReadHandleKey<T>& key)
313 return ReadHandle<T> (key);
318 * @brief Return a @c ReadHandle referencing @c key for an explicit context.
319 * @param key The key object holding the clid/key/store.
320 * @param ctx The event context.
322 * This will raise an exception if the StoreGate key is blank,
323 * or if the event store cannot be found.
325 * If the default event store has been requested, then the thread-specific
326 * store from the event context will be used.
329 ReadHandle<T> makeHandle (const ReadHandleKey<T>& key,
330 const EventContext& ctx)
332 return ReadHandle<T> (key, ctx);
337 * @brief Convenience function to retrieve an object given a @c ReadHandleKey.
338 * @param key The key to retrieve.
339 * @param ctx The event context.
341 * Returns the object. Returns nullptr if the key is null or if there's an error.
345 const T* get (const ReadHandleKey<T>& key)
347 if (key.key().empty()) return nullptr;
348 ReadHandle<T> h (key);
354 * @brief Convenience function to retrieve an object given a @c ReadHandleKey.
355 * @param key The key to retrieve.
357 * Returns the object. Returns nullptr if the key is null or if there's an error.
361 const T* get (const ReadHandleKey<T>& key,
362 const EventContext& ctx)
364 if (key.key().empty()) return nullptr;
365 ReadHandle<T> h (key, ctx);
373 #endif //> !STOREGATE_SG_READHANDLE_ICC