ATLAS Offline Software
Loading...
Searching...
No Matches
AthToolSupport/AsgDataHandles/AsgDataHandles/ReadHandle.icc
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3*/
4
5/**
6 * @file AsgDataHandles/ReadHandle.icc
7 * @author Nils Krumnack <Nils.Erik.Krumnack@cern.h>
8 * @author S. Binet, P. Calafiura, scott snyder <snyder@bnl.gov> (for original)
9 * @brief Handle class for reading from StoreGate.
10 */
11
12#ifndef ASG_DATA_HANDLES_READ_HANDLE_ICC
13#define ASG_DATA_HANDLES_READ_HANDLE_ICC
14
15
16#include "xAODRootAccessInterfaces/TActiveEvent.h"
17#include "xAODRootAccessInterfaces/TVirtualEvent.h"
18#include <stdexcept>
19
20
21namespace SG {
22
23
24//************************************************************************
25// Constructors, etc.
26//
27
28
29// /**
30// * @brief Default constructor.
31// *
32// * The handle will not be usable until a non-blank key is assigned.
33// */
34// template <class T>
35// inline
36// ReadHandle<T>::ReadHandle()
37// : VarHandleBase(ClassID_traits<T>::ID(), Gaudi::DataHandle::Reader)
38// {
39// }
40
41
42/**
43 * @brief Constructor with full arguments.
44 * @param sgkey StoreGate key of the referenced object.
45 */
46template <class T>
47inline
48ReadHandle<T>::ReadHandle(const std::string& sgkey)
49 : VarHandleBase (sgkey)
50{
51}
52
53
54/**
55 * @brief Constructor from a ReadHandleKey.
56 * @param key The key object holding the clid/key/store.
57 *
58 * This will raise an exception if the StoreGate key is blank,
59 * or if the event store cannot be found.
60 */
61template <class T>
62inline
63ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key)
64 : VarHandleBase (key, nullptr)
65{
66}
67
68
69/**
70 * @brief Constructor from a ReadHandleKey and an explicit event context.
71 * @param key The key object holding the clid/key.
72 * @param ctx The event context.
73 *
74 * This will raise an exception if the StoreGate key is blank,
75 * or if the event store cannot be found.
76 *
77 * If the default event store has been requested, then the thread-specific
78 * store from the event context will be used.
79 */
80template <class T>
81inline
82ReadHandle<T>::ReadHandle (const ReadHandleKey<T>& key,
83 const EventContext& ctx)
84 : VarHandleBase (key, &ctx)
85{
86}
87
88
89/**
90 * @brief Dereference the pointer.
91 * Throws ExcNullReadHandle on failure.
92 */
93template <class T>
94inline
95typename ReadHandle<T>::const_pointer_type
96ReadHandle<T>::operator->()
97{
98 return checkedCPtr();
99}
100
101
102/**
103 * @brief Dereference the pointer.
104 * Throws ExcNullReadHandle on failure.
105 */
106template <class T>
107inline
108typename ReadHandle<T>::const_reference_type
109ReadHandle<T>::operator*()
110{
111 return *checkedCPtr();
112}
113
114
115/**
116 * @brief Dereference the pointer.
117 * Returns nullptr on failure.
118 */
119template <class T>
120inline
121typename ReadHandle<T>::const_pointer_type
122ReadHandle<T>::cptr()
123{
124 return getCPtr();
125}
126
127
128/**
129 * @brief Dereference the pointer.
130 * Returns nullptr on failure.
131 */
132template <class T>
133inline
134typename ReadHandle<T>::const_pointer_type
135ReadHandle<T>::ptr()
136{
137 return cptr();
138}
139
140
141// /**
142// * @brief Return the cached pointer directly; no lookup.
143// */
144// template <class T>
145// inline
146// typename ReadHandle<T>::const_pointer_type
147// ReadHandle<T>::cachedPtr() const
148// {
149// return reinterpret_cast<const_pointer_type>(this->m_ptr);
150// }
151
152
153/**
154 * @brief Can the handle be successfully dereferenced?
155 */
156template <class T>
157inline
158bool ReadHandle<T>::isValid()
159{
160 return isPresent_impl(key());
161}
162
163
164/**
165 * @brief Dereference the pointer, but don't cache anything.
166 */
167template <class T>
168inline
169typename ReadHandle<T>::const_pointer_type
170ReadHandle<T>::get() const
171{
172 return getCPtr();
173}
174
175
176/**
177 * @brief Dereference the pointer, but don't cache anything.
178 * @param ctx The event context to use.
179 */
180template <class T>
181inline
182typename ReadHandle<T>::const_pointer_type
183ReadHandle<T>::get (const EventContext& /*ctx*/) const
184{
185 return cptr();
186}
187
188
189/**
190* @brief Is the referenced object present in SG?
191*
192* Const method; the handle does not change as a result of this.
193*/
194template <class T>
195bool ReadHandle<T>::isPresent() const
196{
197return isPresent_impl (key());
198}
199
200
201/**
202 * @brief Is the referenced object present in SG?
203 * @param key SG key to test.
204 *
205 * Const method; the handle does not change as a result of this.
206 */
207template <class T>
208bool ReadHandle<T>::isPresent_impl (const std::string& key) const
209{
210 const T *result = nullptr;
211 xAOD::TVirtualEvent* event = xAOD::TActiveEvent::event();
212 if(event == nullptr)
213 throw std::runtime_error ("No active event present! ReadHandles cannot be used.");
214 return event->retrieve (result, key, true);
215}
216
217
218/**
219 * @brief Helper: dereference the pointer.
220 * Throws ExcNullReadHandle on failure.
221 */
222template <class T>
223inline
224typename ReadHandle<T>::const_pointer_type
225ReadHandle<T>::checkedCPtr()
226{
227 const_pointer_type p = this->cptr();
228 if (!p)
229 throw std::runtime_error ("failed to read object: " + key());
230 // throwExcNullReadHandle (clid(), key(), store());
231 return p;
232}
233
234
235/**
236 * @brief Helper: dereference the pointer.
237 * Throws ExcNullReadHandle on failure.
238 */
239template <class T>
240inline
241typename ReadHandle<T>::const_pointer_type
242ReadHandle<T>::getCPtr() const
243{
244 const T *result = nullptr;
245 (void) xAOD::TActiveEvent::event()->retrieve (result, key(), true);
246 return result;
247}
248
249
250/**
251 * @brief Return a @c ReadHandle referencing @c key.
252 * @param key The key object holding the clid/key/store.
253 *
254 * This will raise an exception if the StoreGate key is blank,
255 * or if the event store cannot be found.
256 */
257template <class T>
258ReadHandle<T> makeHandle (const ReadHandleKey<T>& key)
259{
260 return ReadHandle<T> (key);
261}
262
263
264/**
265 * @brief Return a @c ReadHandle referencing @c key for an explicit context.
266 * @param key The key object holding the clid/key/store.
267 * @param ctx The event context.
268 *
269 * This will raise an exception if the StoreGate key is blank,
270 * or if the event store cannot be found.
271 *
272 * If the default event store has been requested, then the thread-specific
273 * store from the event context will be used.
274 */
275template <class T>
276ReadHandle<T> makeHandle (const ReadHandleKey<T>& key,
277 const EventContext& ctx)
278{
279 return ReadHandle<T> (key, ctx);
280}
281
282
283/**
284 * @brief Convenience function to retrieve an object given a @c ReadHandleKey.
285 * @param key The key to retrieve.
286 * @param ctx The event context.
287 *
288 * Returns the object. Returns nullptr if the key is null or if there's an error.
289 */
290template <class T>
291inline
292const T* get (const ReadHandleKey<T>& key)
293{
294 if (key.key().empty()) return nullptr;
295 ReadHandle<T> h (key);
296 return h.get();
297}
298
299
300/**
301 * @brief Convenience function to retrieve an object given a @c ReadHandleKey.
302 * @param key The key to retrieve.
303 *
304 * Returns the object. Returns nullptr if the key is null or if there's an error.
305 */
306template <class T>
307inline
308const T* get (const ReadHandleKey<T>& key,
309 const EventContext& ctx)
310{
311 if (key.key().empty()) return nullptr;
312 ReadHandle<T> h (key, ctx);
313 return h.get();
314}
315
316
317/**
318 * @brief Protected constructor used by WriteDecorHandle.
319 * @param key The key object holding the clid/key.
320 * @param ctx The event context, or nullptr to use the global default.
321 */
322template <class T>
323inline
324ReadHandle<T>::ReadHandle (const VarHandleKey& key, const EventContext* ctx)
325 : VarHandleBase (key, ctx)
326{
327}
328
329
330} /* namespace SG */
331
332
333#endif