ATLAS Offline Software
Decorator.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 AthContainers/Decorator.icc
6  * @author scott snyder <snyder@bnl.gov>
7  * @date Oct, 2023
8  * @brief Helper class to provide type-safe access to aux data.
9  */
10 
11 
12 #include "AthContainers/AuxTypeRegistry.h"
13 #include "AthContainers/exceptions.h"
14 
15 
16 namespace SG {
17 
18 
19 /**
20  * @brief Constructor.
21  * @param name Name of this aux variable.
22  *
23  * The name -> auxid lookup is done here.
24  */
25 template <class T, class ALLOC>
26 inline
27 Decorator<T, ALLOC>::Decorator (const std::string& name)
28  : Decorator (name, "", SG::AuxVarFlags::None)
29 {
30  // cppcheck-suppress missingReturn
31 }
32 
33 
34 /**
35  * @brief Constructor.
36  * @param name Name of this aux variable.
37  * @param clsname The name of its associated class. May be blank.
38  *
39  * The name -> auxid lookup is done here.
40  */
41 template <class T, class ALLOC>
42 inline
43 Decorator<T, ALLOC>::Decorator (const std::string& name,
44  const std::string& clsname)
45  : Decorator (name, clsname, SG::AuxVarFlags::None)
46 {
47 }
48 
49 
50 /**
51  * @brief Constructor taking an auxid directly.
52  * @param auxid ID for this auxiliary variable.
53  *
54  * Will throw @c SG::ExcAuxTypeMismatch if the types don't match.
55  */
56 template <class T, class ALLOC>
57 inline
58 Decorator<T, ALLOC>::Decorator (const SG::auxid_t auxid)
59  : Decorator (auxid, SG::AuxVarFlags::None)
60 {
61  // cppcheck-suppress missingReturn; false positive
62 }
63 
64 
65 /**
66  * @brief Constructor.
67  * @param name Name of this aux variable.
68  * @param clsname The name of its associated class. May be blank.
69  * @param flags Optional flags qualifying the type. See AuxTypeRegsitry.
70  *
71  * The name -> auxid lookup is done here.
72  */
73 template <class T, class ALLOC>
74 inline
75 Decorator<T, ALLOC>::Decorator
76  (const std::string& name,
77  const std::string& clsname,
78  const SG::AuxVarFlags flags)
79  : m_auxid (SG::AuxTypeRegistry::instance().getAuxID<T, ALLOC> (name, clsname, flags))
80 {
81 }
82 
83 
84 /**
85  * @brief Constructor taking an auxid directly.
86  * @param auxid ID for this auxiliary variable.
87  * @param flags Optional flags qualifying the type. See AuxTypeRegistry.
88  *
89  * Will throw @c SG::ExcAuxTypeMismatch if the types don't match.
90  */
91 template <class T, class ALLOC>
92 inline
93 Decorator<T, ALLOC>::Decorator (const SG::auxid_t auxid,
94  const SG::AuxVarFlags flags)
95  : m_auxid (auxid)
96 {
97  SG::AuxTypeRegistry::instance().checkAuxID<T, ALLOC> (auxid, flags);
98 }
99 
100 
101 /**
102  * @brief Fetch the variable for one element, as a non-const reference.
103  * @param e The element for which to fetch the variable.
104  *
105  * If the container is locked, this will allow fetching only variables
106  * that do not yet exist (in which case they will be marked as decorations)
107  * or variables already marked as decorations.
108  */
109 template <class T, class ALLOC>
110 template <class ELT>
111 ATH_REQUIRES( IsConstAuxElement<ELT> )
112 inline
113 typename Decorator<T, ALLOC>::reference_type
114 Decorator<T, ALLOC>::operator() (const ELT& e) const
115 {
116  assert (e.container() != 0);
117  return e.container()->template getDecoration<T> (m_auxid, e.index());
118 }
119 
120 /**
121  * @brief Fetch the variable for one element, as a non-const reference.
122  * @param container The container from which to fetch the variable.
123  * @param index The index of the desired element.
124  *
125  * This allows retrieving aux data by container / index.
126  * Looping over the index via this method will be faster then
127  * looping over the elements of the container.
128  *
129  * If the container is locked, this will allow fetching only variables
130  * that do not yet exist (in which case they will be marked as decorations)
131  * or variables already marked as decorations.
132  */
133 template <class T, class ALLOC>
134 inline
135 typename Decorator<T, ALLOC>::reference_type
136 Decorator<T, ALLOC>::operator() (const AuxVectorData& container,
137  size_t index) const
138 {
139  return container.template getDecoration<T> (m_auxid, index);
140 }
141 
142 
143 /**
144  * @brief Set the variable for one element.
145  * @param e The element for which to fetch the variable.
146  * @param x The variable value to set.
147  */
148 template <class T, class ALLOC>
149 template <class ELT>
150 ATH_REQUIRES( IsConstAuxElement<ELT> )
151 inline
152 void Decorator<T, ALLOC>::set (const ELT& e,
153  const element_type& x) const
154 {
155  (*this)(e) = x;
156 }
157 
158 
159 /**
160  * @brief Get a pointer to the start of the auxiliary data array.
161  * @param container The container from which to fetch the variable.
162  */
163 template <class T, class ALLOC>
164 inline
165 typename Decorator<T, ALLOC>::const_container_pointer_type
166 Decorator<T, ALLOC>::getDataArray (const AuxVectorData& container) const
167 {
168  return reinterpret_cast<const_container_pointer_type>
169  (container.getDataArray (m_auxid));
170 }
171 
172 
173 /**
174  * @brief Get a pointer to the start of the auxiliary data array.
175  * @param container The container from which to fetch the variable.
176  *
177  * If the container is locked, this will allow fetching only variables
178  * that do not yet exist (in which case they will be marked as decorations)
179  * or variables already marked as decorations.
180  */
181 template <class T, class ALLOC>
182 inline
183 typename Decorator<T, ALLOC>::container_pointer_type
184 Decorator<T, ALLOC>::getDecorationArray (const AuxVectorData& container) const
185 {
186  return reinterpret_cast<container_pointer_type>
187  (container.getDecorationArray (m_auxid));
188 }
189 
190 
191 /**
192  * @brief Get a span over the auxilary data array.
193  * @param container The container from which to fetch the variable.
194  */
195 template <class T, class ALLOC>
196 inline
197 typename Decorator<T, ALLOC>::const_span
198 Decorator<T, ALLOC>::getDataSpan (const AuxVectorData& container) const
199 {
200  auto beg = reinterpret_cast<const_container_pointer_type>
201  (container.getDataArray (m_auxid));
202  return const_span (beg, container.size_v());
203 }
204 
205 
206 /**
207  * @brief Get a span over the auxilary data array.
208  * @param container The container from which to fetch the variable.
209  *
210  * If the container is locked, this will allow fetching only variables
211  * that do not yet exist (in which case they will be marked as decorations)
212  * or variables already marked as decorations.
213  */
214 template <class T, class ALLOC>
215 inline
216 typename Decorator<T, ALLOC>::span
217 Decorator<T, ALLOC>::getDecorationSpan (const AuxVectorData& container) const
218 {
219  auto beg = reinterpret_cast<container_pointer_type>
220  (container.getDecorationArray (m_auxid));
221  return span (beg, container.size_v());
222 }
223 
224 
225 /**
226  * @brief Test to see if this variable exists in the store.
227  * @param e An element of the container in which to test the variable.
228  */
229 template <class T, class ALLOC>
230 template <class ELT>
231 ATH_REQUIRES( IsConstAuxElement<ELT> )
232 inline
233 bool
234 Decorator<T, ALLOC>::isAvailable (const ELT& e) const
235 {
236  return e.container() && e.container()->isAvailable (m_auxid);
237 }
238 
239 /**
240  * @brief Test to see if this variable exists in the store and is writable.
241  * @param e An element of the container in which to test the variable.
242  */
243 template <class T, class ALLOC>
244 template <class ELT>
245 ATH_REQUIRES( IsConstAuxElement<ELT> )
246 inline
247 bool
248 Decorator<T, ALLOC>::isAvailableWritable (const ELT& e) const
249 {
250  return e.container() && e.container()->isAvailableWritableAsDecoration (m_auxid);
251 }
252 
253 
254 /**
255  * @brief Test to see if this variable exists in the store.
256  * @param c The container in which to test the variable.
257  */
258 template <class T, class ALLOC>
259 inline
260 bool
261 Decorator<T, ALLOC>::isAvailable (const AuxVectorData& c) const
262 {
263  return c.isAvailable (m_auxid);
264 }
265 
266 /**
267  * @brief Test to see if this variable exists in the store and is writable.
268  * @param c The container in which to test the variable.
269  */
270 template <class T, class ALLOC>
271 inline
272 bool
273 Decorator<T, ALLOC>::isAvailableWritable (const AuxVectorData& c) const
274 {
275  return c.isAvailableWritableAsDecoration (m_auxid);
276 }
277 
278 
279 /**
280  * @brief Return the aux id for this variable.
281  */
282 template <class T, class ALLOC>
283 inline
284 SG::auxid_t
285 Decorator<T, ALLOC>::auxid() const
286 {
287  return m_auxid;
288 }
289 
290 
291 } // namespace SG