ATLAS Offline Software
UpdateHandle.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: UpdateHandle.icc 797637 2017-02-17 02:32:11Z ssnyder $
6 /**
7  * @file StoreGate/UpdateHandle.icc
8  * @author S. Binet, P. Calafiura, scott snyder <snyder@bnl.gov>
9  * @date Updated: Feb, 2016
10  * @brief Handle class for modifying an existing object in StoreGate.
11  */
12 
13 #ifndef STOREGATE_SG_UPDATEHANDLE_ICC
14 #define STOREGATE_SG_UPDATEHANDLE_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  * @brief Default constructor.
27  *
28  * The handle will not be usable until a non-blank key is assigned.
29  */
30 template <class T>
31 inline
32 UpdateHandle<T>::UpdateHandle()
33  : VarHandleBase(ClassID_traits<T>::ID(), Gaudi::DataHandle::Reader)
34 {
35 }
36 
37 
38 /**
39  * @brief Constructor with full arguments.
40  * @param sgkey StoreGate key of the referenced object.
41  * @param storename Name of the referenced event store.
42  */
43 template <class T>
44 inline
45 UpdateHandle<T>::UpdateHandle(const std::string& sgkey,
46  const std::string& storename /*= "StoreGateSvc"*/)
47  : VarHandleBase( ClassID_traits<T>::ID(),
48  sgkey, Gaudi::DataHandle::Reader, storename )
49 {
50 }
51 
52 
53 /**
54  * @brief Constructor from an UpdateHandleKey.
55  * @param key The key object holding the clid/key/store.
56  *
57  * This will raise an exception if the StoreGate key is blank,
58  * or if the event store cannot be found.
59  */
60 template <class T>
61 inline
62 UpdateHandle<T>::UpdateHandle (const UpdateHandleKey<T>& key)
63  : VarHandleBase (key, nullptr)
64 {
65 }
66 
67 
68 /**
69  * @brief Constructor from an UpdateHandleKey and an explicit event context.
70  * @param key The key object holding the clid/key.
71  * @param ctx The event context.
72  *
73  * This will raise an exception if the StoreGate key is blank,
74  * or if the event store cannot be found.
75  *
76  * If the default event store has been requested, then the thread-specific
77  * store from the event context will be used.
78  */
79 template <class T>
80 UpdateHandle<T>::UpdateHandle (const UpdateHandleKey<T>& key,
81  const EventContext& ctx)
82  : VarHandleBase (key, &ctx)
83 {
84 }
85 
86 
87 /**
88  * @brief Copy constructor.
89  */
90 template <class T>
91 inline
92 UpdateHandle<T>::UpdateHandle(const UpdateHandle& h)
93  : VarHandleBase(h)
94 {
95 }
96 
97 /**
98  * @brief Move constructor.
99  */
100 template <class T>
101 inline
102 UpdateHandle<T>::UpdateHandle(UpdateHandle&& h)
103  : VarHandleBase(std::move(h))
104 {
105 }
106 
107 
108 /**
109  * @brief Assignment operator.
110  */
111 template <class T>
112 inline
113 UpdateHandle<T>&
114 UpdateHandle<T>::UpdateHandle::operator= (const UpdateHandle& h)
115 {
116  if (this != &h) {
117  this->VarHandleBase::operator=(h);
118  }
119  return *this;
120 }
121 
122 
123 /**
124  * @brief Move operator.
125  */
126 template <class T>
127 inline
128 UpdateHandle<T>&
129 UpdateHandle<T>::UpdateHandle::operator= (UpdateHandle&& h)
130 {
131  if (this != &h) {
132  this->VarHandleBase::operator=(std::move(h));
133  }
134  return *this;
135 }
136 
137 
138 //************************************************************************
139 // Dereference.
140 //
141 
142 
143 /**
144  * @brief Dereference the pointer.
145  * Throws ExcNullReadHandle on failure.
146  *
147  * This will inform Hive that the object has been modified.
148  */
149 template <class T>
150 inline
151 typename UpdateHandle<T>::pointer_type
152 UpdateHandle<T>::operator->()
153 {
154  return checkedPtr();
155 }
156 
157 
158 /**
159  * @brief Dereference the pointer.
160  * Throws ExcNullReadHandle on failure.
161  *
162  * This will inform Hive that the object has been modified.
163  */
164 template <class T>
165 inline
166 typename UpdateHandle<T>::reference_type UpdateHandle<T>::operator*()
167 {
168  return *checkedPtr();
169 }
170 
171 
172 /**
173  * @brief Dereference the pointer.
174  * Returns nullptr on failure.
175  *
176  * This will _not_ inform Hive that the object has been modified.
177  */
178 template <class T>
179 inline
180 typename UpdateHandle<T>::const_pointer_type UpdateHandle<T>::cptr()
181 {
182  return reinterpret_cast<const_pointer_type>(this->typeless_cptr());
183 }
184 
185 
186 /**
187  * @brief Dereference the pointer.
188  * Returns nullptr on failure.
189  *
190  * This will inform Hive that the object has been modified.
191  */
192 template <class T>
193 typename UpdateHandle<T>::pointer_type
194 UpdateHandle<T>::ptr()
195 {
196  pointer_type ptr = reinterpret_cast<pointer_type>(this->typeless_ptr());
197  return ptr;
198 }
199 
200 
201 /**
202  * @brief Return the cached pointer directly; no lookup.
203  */
204 template <class T>
205 inline
206 typename UpdateHandle<T>::pointer_type
207 UpdateHandle<T>::cachedPtr()
208 {
209  return reinterpret_cast<pointer_type>(this->m_ptr);
210 }
211 
212 
213 /**
214  * @brief Can the handle be successfully dereferenced?
215  */
216 template <class T>
217 inline
218 bool UpdateHandle<T>::isValid()
219 {
220  const bool QUIET=true;
221  if (0 != this->typeless_dataPointer(QUIET))
222  return !isConst();
223  return false;
224 }
225 
226 
227 /**
228  * @brief Reset this handle.
229  * @param hard If true, anything depending on the event store is cleared.
230  *
231  * Call reset() from the base class.
232  */
233 template <class T>
234 void UpdateHandle<T>::reset (bool hard)
235 {
236  VarHandleBase::reset (hard);
237 }
238 
239 
240 /**
241  * @brief Helper: dereference the pointer.
242  * Throws ExcNullUpdateHandle on failure.
243  */
244 template <class T>
245 inline
246 typename UpdateHandle<T>::pointer_type
247 UpdateHandle<T>::checkedPtr()
248 {
249  pointer_type p = this->ptr();
250  if (!p)
251  throwExcNullUpdateHandle (clid(), key(), store());
252  return p;
253 }
254 
255 
256 /**
257  * @brief Return an @c UpdateHandle referencing @c key.
258  * @param key The key object holding the clid/key/store.
259  *
260  * This will raise an exception if the StoreGate key is blank,
261  * or if the event store cannot be found.
262  */
263 template <class T>
264 UpdateHandle<T> makeHandle (const UpdateHandleKey<T>& key)
265 {
266  return UpdateHandle<T> (key);
267 }
268 
269 
270 /**
271  * @brief Return an @c UpdateHandle referencing @c key for an explicit context.
272  * @param key The key object holding the clid/key/store.
273  * @param ctx The event context.
274  *
275  * This will raise an exception if the StoreGate key is blank,
276  * or if the event store cannot be found.
277  *
278  * If the default event store has been requested, then the thread-specific
279  * store from the event context will be used.
280  */
281 template <class T>
282 UpdateHandle<T> makeHandle (const UpdateHandleKey<T>& key,
283  const EventContext& ctx)
284 {
285  return UpdateHandle<T> (key, ctx);
286 }
287 
288 
289 } /* namespace SG */
290 
291 
292 #endif //> !STOREGATE_SG_UPDATEHANDLE_ICC