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