ATLAS Offline Software
Loading...
Searching...
No Matches
Decorator.icc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2002-2026 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
16namespace 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 */
25template <class T, class ALLOC>
26inline
27Decorator<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 */
41template <class T, class ALLOC>
42inline
43Decorator<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 */
56template <class T, class ALLOC>
57inline
58Decorator<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 AuxTypeRegistry.
70 *
71 * The name -> auxid lookup is done here.
72 */
73template <class T, class ALLOC>
74inline
75Decorator<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 */
91template <class T, class ALLOC>
92inline
93Decorator<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 */
109template <class T, class ALLOC>
110template <IsConstAuxElement ELT>
111inline
112typename Decorator<T, ALLOC>::reference_type
113Decorator<T, ALLOC>::operator() (const ELT& e) const
114{
115 assert (e.container() != 0);
116 return e.container()->template getDecoration<T> (m_auxid, e.index());
117}
118
119/**
120 * @brief Fetch the variable for one element, as a non-const reference.
121 * @param container The container from which to fetch the variable.
122 * @param index The index of the desired element.
123 *
124 * This allows retrieving aux data by container / index.
125 * Looping over the index via this method will be faster then
126 * looping over the elements of the container.
127 *
128 * If the container is locked, this will allow fetching only variables
129 * that do not yet exist (in which case they will be marked as decorations)
130 * or variables already marked as decorations.
131 */
132template <class T, class ALLOC>
133inline
134typename Decorator<T, ALLOC>::reference_type
135Decorator<T, ALLOC>::operator() (const AuxVectorData& container,
136 size_t index) const
137{
138 return container.template getDecoration<T> (m_auxid, index);
139}
140
141
142/**
143 * @brief Set the variable for one element.
144 * @param e The element for which to fetch the variable.
145 * @param x The variable value to set.
146 */
147template <class T, class ALLOC>
148template <IsConstAuxElement ELT>
149inline
150void Decorator<T, ALLOC>::set (const ELT& e,
151 const element_type& x) const
152{
153 (*this)(e) = x;
154}
155
156
157/**
158 * @brief Get a pointer to the start of the auxiliary data array.
159 * @param container The container from which to fetch the variable.
160 */
161template <class T, class ALLOC>
162inline
163typename Decorator<T, ALLOC>::const_container_pointer_type
164Decorator<T, ALLOC>::getDataArray (const AuxVectorData& container) const
165{
166 return reinterpret_cast<const_container_pointer_type>
167 (container.getDataArray (m_auxid));
168}
169
170
171/**
172 * @brief Get a pointer to the start of the auxiliary data array.
173 * @param container The container from which to fetch the variable.
174 *
175 * If the container is locked, this will allow fetching only variables
176 * that do not yet exist (in which case they will be marked as decorations)
177 * or variables already marked as decorations.
178 */
179template <class T, class ALLOC>
180inline
181typename Decorator<T, ALLOC>::container_pointer_type
182Decorator<T, ALLOC>::getDecorationArray (const AuxVectorData& container) const
183{
184 return reinterpret_cast<container_pointer_type>
185 (container.getDecorationArray (m_auxid));
186}
187
188
189/**
190 * @brief Get a span over the auxiliary data array.
191 * @param container The container from which to fetch the variable.
192 */
193template <class T, class ALLOC>
194inline
195typename Decorator<T, ALLOC>::const_span
196Decorator<T, ALLOC>::getDataSpan (const AuxVectorData& container) const
197{
198 const AuxDataSpanBase sp = *container.getDataSpan (this->m_auxid);
199 return const_span (reinterpret_cast<const_container_pointer_type> (sp.beg),
200 sp.size);
201}
202
203
204/**
205 * @brief Get a span over the auxiliary data array.
206 * @param container The container from which to fetch the variable.
207 *
208 * If the container is locked, this will allow fetching only variables
209 * that do not yet exist (in which case they will be marked as decorations)
210 * or variables already marked as decorations.
211 */
212template <class T, class ALLOC>
213inline
214typename Decorator<T, ALLOC>::span
215Decorator<T, ALLOC>::getDecorationSpan (const AuxVectorData& container) const
216{
217 // nb. AuxVectorData::getDataSpan is suitable only for const;
218 // getDecorationArray may throw if the container is empty.
219 size_t sz = container.size_v();
220 container_pointer_type beg = nullptr;
221 if (sz) {
222 beg = reinterpret_cast<container_pointer_type>
223 (container.getDecorationArray (this->m_auxid));
224 }
225 return span (beg, sz);
226}
227
228
229/**
230 * @brief Test to see if this variable exists in the store.
231 * @param e An element of the container in which to test the variable.
232 */
233template <class T, class ALLOC>
234template <IsConstAuxElement ELT>
235inline
236bool
237Decorator<T, ALLOC>::isAvailable (const ELT& e) const
238{
239 return e.container() && e.container()->isAvailable (m_auxid);
240}
241
242/**
243 * @brief Test to see if this variable exists in the store and is writable.
244 * @param e An element of the container in which to test the variable.
245 */
246template <class T, class ALLOC>
247template <IsConstAuxElement ELT>
248inline
249bool
250Decorator<T, ALLOC>::isAvailableWritable (const ELT& e) const
251{
252 return e.container() && e.container()->isAvailableWritableAsDecoration (m_auxid);
253}
254
255
256/**
257 * @brief Test to see if this variable exists in the store.
258 * @param c The container in which to test the variable.
259 */
260template <class T, class ALLOC>
261inline
262bool
263Decorator<T, ALLOC>::isAvailable (const AuxVectorData& c) const
264{
265 return c.isAvailable (m_auxid);
266}
267
268/**
269 * @brief Test to see if this variable exists in the store and is writable.
270 * @param c The container in which to test the variable.
271 */
272template <class T, class ALLOC>
273inline
274bool
275Decorator<T, ALLOC>::isAvailableWritable (const AuxVectorData& c) const
276{
277 return c.isAvailableWritableAsDecoration (m_auxid);
278}
279
280
281/**
282 * @brief Return the aux id for this variable.
283 */
284template <class T, class ALLOC>
285inline
286SG::auxid_t
287Decorator<T, ALLOC>::auxid() const
288{
289 return m_auxid;
290}
291
292
293} // namespace SG