ATLAS Offline Software
StoreGate/StoreGate/ReadHandle.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2018 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 
22 namespace 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  */
35 template <class T>
36 inline
37 ReadHandle<T>::ReadHandle()
38  : VarHandleBase(ClassID_traits<T>::ID(), Gaudi::DataHandle::Reader)
39 {
40 }
41 
42 
43 /**
44  * @brief Constructor with full arguments.
45  * @param sgkey StoreGate key of the referenced object.
46  * @param storename Name of the referenced event store.
47  */
48 template <class T>
49 inline
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 )
54 {
55 }
56 
57 
58 /**
59  * @brief Constructor from a ReadHandleKey.
60  * @param key The key object holding the clid/key/store.
61  *
62  * This will raise an exception if the StoreGate key is blank,
63  * or if the event store cannot be found.
64  */
65 template <class T>
66 inline
67 ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key)
68  : VarHandleBase (key, nullptr)
69 {
70 }
71 
72 
73 /**
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.
77  *
78  * This will raise an exception if the StoreGate key is blank,
79  * or if the event store cannot be found.
80  *
81  * If the default event store has been requested, then the thread-specific
82  * store from the event context will be used.
83  */
84 template <class T>
85 inline
86 ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key,
87  const EventContext& ctx)
88  : VarHandleBase (key, &ctx)
89 {
90 }
91 
92 
93 /**
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).
97  *
98  * This handle will be bound to the given proxy.
99  */
100 template <class T>
101 inline
102 ReadHandle<T>::ReadHandle (SG::DataProxy* proxy)
103  : VarHandleBase (proxy, Gaudi::DataHandle::Reader)
104 {
105 }
106 
107 
108 /**
109  * @brief Copy constructor.
110  */
111 template <class T>
112 inline
113 ReadHandle<T>::ReadHandle(const ReadHandle& h)
114  : VarHandleBase(h)
115 {
116 }
117 
118 
119 /**
120  * @brief Move constructor.
121  */
122 template <class T>
123 inline
124 ReadHandle<T>::ReadHandle(ReadHandle&& h)
125  : VarHandleBase(std::move(h))
126 {
127 }
128 
129 
130 /**
131  * @brief Assignment operator.
132  */
133 template <class T>
134 ReadHandle<T>&
135 ReadHandle<T>::ReadHandle::operator= (const ReadHandle& h)
136 {
137  if (this != &h)
138  this->VarHandleBase::operator=(h);
139  return *this;
140 }
141 
142 /**
143  * @brief Move operator.
144  */
145 template <class T>
146 inline
147 ReadHandle<T>&
148 ReadHandle<T>::ReadHandle::operator= (ReadHandle&& h)
149 {
150  if (this != &h)
151  this->VarHandleBase::operator=(std::move(h));
152  return *this;
153 }
154 
155 
156 /**
157  * @brief Dereference the pointer.
158  * Throws ExcNullReadHandle on failure.
159  */
160 template <class T>
161 inline
162 typename ReadHandle<T>::const_pointer_type
163 ReadHandle<T>::operator->()
164 {
165  return checkedCPtr();
166 }
167 
168 
169 /**
170  * @brief Dereference the pointer.
171  * Throws ExcNullReadHandle on failure.
172  */
173 template <class T>
174 inline
175 typename ReadHandle<T>::const_reference_type
176 ReadHandle<T>::operator*()
177 {
178  return *checkedCPtr();
179 }
180 
181 
182 /**
183  * @brief Dereference the pointer.
184  * Returns nullptr on failure.
185  */
186 template <class T>
187 inline
188 typename ReadHandle<T>::const_pointer_type
189 ReadHandle<T>::cptr()
190 {
191  return reinterpret_cast<const_pointer_type>(this->typeless_cptr());
192 }
193 
194 
195 /**
196  * @brief Dereference the pointer.
197  * Returns nullptr on failure.
198  */
199 template <class T>
200 inline
201 typename ReadHandle<T>::const_pointer_type
202 ReadHandle<T>::ptr()
203 {
204  return cptr();
205 }
206 
207 
208 /**
209  * @brief Return the cached pointer directly; no lookup.
210  */
211 template <class T>
212 inline
213 typename ReadHandle<T>::const_pointer_type
214 ReadHandle<T>::cachedPtr() const
215 {
216  return reinterpret_cast<const_pointer_type>(this->m_ptr);
217 }
218 
219 
220 /**
221  * @brief Can the handle be successfully dereferenced?
222  */
223 template <class T>
224 inline
225 bool ReadHandle<T>::isValid()
226 {
227  return 0 != this->typeless_dataPointer(true);
228 }
229 
230 
231 /**
232  * @brief Dereference the pointer, but don't cache anything.
233  */
234 template <class T>
235 inline
236 typename ReadHandle<T>::const_pointer_type
237 ReadHandle<T>::get() const
238 {
239  return reinterpret_cast<const_pointer_type> (this->get_impl (nullptr));
240 }
241 
242 
243 /**
244  * @brief Dereference the pointer, but don't cache anything.
245  * @param ctx The event context to use.
246  */
247 template <class T>
248 inline
249 typename ReadHandle<T>::const_pointer_type
250 ReadHandle<T>::get (const EventContext& ctx) const
251 {
252  return reinterpret_cast<const_pointer_type> (this->get_impl (&ctx));
253 }
254 
255 
256 /**
257  * @brief Make an alias.
258  * @param key Alternate key by which the referenced object should be known.
259  *
260  * The current handle should be valid and referencing an object.
261  *
262  * The object will also be known by the name given in @c key.
263  */
264 template <class T>
265 StatusCode ReadHandle<T>::alias (const WriteHandleKey<T>& key)
266 {
267  if (!cptr())
268  return StatusCode::FAILURE;
269  return symLink_impl (this->clid(), key.key());
270 }
271 
272 
273 /**
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.
277  */
278 template <class T>
279 inline
280 ReadHandle<T>::ReadHandle (const VarHandleKey& key, const EventContext* ctx)
281  : VarHandleBase (key, ctx)
282 {
283  // cppcheck-suppress missingReturn; false positive
284 }
285 
286 
287 /**
288  * @brief Helper: dereference the pointer.
289  * Throws ExcNullReadHandle on failure.
290  */
291 template <class T>
292 inline
293 typename ReadHandle<T>::const_pointer_type
294 ReadHandle<T>::checkedCPtr()
295 {
296  const_pointer_type p = this->cptr();
297  if (!p)
298  throwExcNullReadHandle (clid(), key(), store());
299  return p;
300 }
301 
302 
303 /**
304  * @brief Return a @c ReadHandle referencing @c key.
305  * @param key The key object holding the clid/key/store.
306  *
307  * This will raise an exception if the StoreGate key is blank,
308  * or if the event store cannot be found.
309  */
310 template <class T>
311 ReadHandle<T> makeHandle (const ReadHandleKey<T>& key)
312 {
313  return ReadHandle<T> (key);
314 }
315 
316 
317 /**
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.
321  *
322  * This will raise an exception if the StoreGate key is blank,
323  * or if the event store cannot be found.
324  *
325  * If the default event store has been requested, then the thread-specific
326  * store from the event context will be used.
327  */
328 template <class T>
329 ReadHandle<T> makeHandle (const ReadHandleKey<T>& key,
330  const EventContext& ctx)
331 {
332  return ReadHandle<T> (key, ctx);
333 }
334 
335 
336 /**
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.
340  *
341  * Returns the object. Returns nullptr if the key is null or if there's an error.
342  */
343 template <class T>
344 inline
345 const T* get (const ReadHandleKey<T>& key)
346 {
347  if (key.key().empty()) return nullptr;
348  ReadHandle<T> h (key);
349  return h.get();
350 }
351 
352 
353 /**
354  * @brief Convenience function to retrieve an object given a @c ReadHandleKey.
355  * @param key The key to retrieve.
356  *
357  * Returns the object. Returns nullptr if the key is null or if there's an error.
358  */
359 template <class T>
360 inline
361 const T* get (const ReadHandleKey<T>& key,
362  const EventContext& ctx)
363 {
364  if (key.key().empty()) return nullptr;
365  ReadHandle<T> h (key, ctx);
366  return h.get();
367 }
368 
369 
370 } /* namespace SG */
371 
372 
373 #endif //> !STOREGATE_SG_READHANDLE_ICC