ATLAS Offline Software
Loading...
Searching...
No Matches
JaggedVecAccessor.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/JaggedVecAccessor.h
6 * @author scott snyder <snyder@bnl.gov>
7 * @date Apr, 2024
8 * @brief Helper class to provide type-safe access to aux data,
9 * specialized for @c JaggedVecElt.
10 */
11
12
13#include "AthContainers/AuxElement.h"
14#include "AthContainers/AuxTypeRegistry.h"
15#include "AthContainers/tools/AuxVectorInterface.h"
16#include "AthContainers/exceptions.h"
17#include "CxxUtils/checker_macros.h"
18
19
20namespace SG {
21
22
23/**
24 * @brief Constructor.
25 * @param name Name of this aux variable.
26 *
27 * The name -> auxid lookup is done here.
28 */
29template <class PAYLOAD_T, class ALLOC>
30inline
31Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::Accessor (const std::string& name)
32 : Base (name)
33{
34}
35
36
37/**
38 * @brief Constructor.
39 * @param name Name of this aux variable.
40 * @param clsname The name of its associated class. May be blank.
41 *
42 * The name -> auxid lookup is done here.
43 */
44template <class PAYLOAD_T, class ALLOC>
45inline
46Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::Accessor (const std::string& name,
47 const std::string& clsname)
48 : Base (name, clsname)
49{
50}
51
52
53/**
54 * @brief Constructor taking an auxid directly.
55 * @param auxid ID for this auxiliary variable.
56 *
57 * Will throw @c SG::ExcAuxTypeMismatch if the types don't match.
58 */
59template <class PAYLOAD_T, class ALLOC>
60inline
61Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::Accessor (const SG::auxid_t auxid)
62 : Base (auxid)
63{
64 // cppcheck-suppress missingReturn; false positive
65}
66
67
68/**
69 * @brief Fetch the variable for one element, as a non-const reference.
70 * @param e The element for which to fetch the variable.
71 *
72 * Will return a proxy object, which will allow treating this
73 * jagged vector element as a vector.
74 */
75template <class PAYLOAD_T, class ALLOC>
76template <IsAuxElement ELT>
77inline
78auto
79Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::operator() (ELT& e) const
80 -> reference_type
81{
82 assert (e.container() != 0);
83 return (*this) (*e.container(), e.index());
84}
85
86
87/**
88 * @brief Fetch the variable for one element, as a non-const reference.
89 * @param container The container from which to fetch the variable.
90 * @param index The index of the desired element.
91 *
92 * This allows retrieving aux data by container / index.
93 *
94 * Will return a proxy object, which will allow treating this
95 * jagged vector element as a vector.
96 */
97#ifndef __CPPCHECK__ // cppcheck gets parse errors on this
98template <class PAYLOAD_T, class ALLOC>
99inline
100auto
101Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::operator() (AuxVectorData& container,
102 size_t index) const
103 -> reference_type
104{
105 (void)this->getEltArray (container); // const/locking checks
106 return reference_type (index,
107 *container.getDataSpan (this->m_linkedAuxid),
108 *container.getDataSpan (this->m_auxid),
109 container,
110 this->m_auxid);
111}
112#endif // not __CPPCHECK__
113
114
115/**
116 * @brief Set the variable for one element.
117 * @param e The element for which to fetch the variable.
118 * @param x The variable value to set.
119 */
120template <class PAYLOAD_T, class ALLOC>
121template <CxxUtils::InputRangeOverT<PAYLOAD_T> RANGE>
122inline
123void Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::set (AuxElement& e,
124 const RANGE& x) const
125{
126 (*this) (*e.container(), e.index()) = x;
127}
128
129
130/**
131 * @brief Set the variable for one element.
132 * @param container The container from which to fetch the variable.
133 * @param index The index of the desired element.
134 * @param x The variable value to set.
135 */
136template <class PAYLOAD_T, class ALLOC>
137template <CxxUtils::InputRangeOverT<PAYLOAD_T> RANGE>
138void Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::set (AuxVectorData& container,
139 size_t index,
140 const RANGE& x) const
141{
142 (*this) (container, index) = x;
143}
144
145
146/**
147 * @brief Get a pointer to the start of the array of @c JaggedVecElt objects.
148 * @param container The container from which to fetch the variable.
149 */
150template <class PAYLOAD_T, class ALLOC>
151inline
152auto
153Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::getEltArray (AuxVectorData& container) const
154 -> Elt_t*
155{
156 return reinterpret_cast<Elt_t*>
157 (container.getDataArray (this->m_auxid));
158}
159
160
161/**
162 * @brief Get a pointer to the start of the payload array.
163 * @param container The container from which to fetch the variable.
164 */
165template <class PAYLOAD_T, class ALLOC>
166inline
167auto
168Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::getPayloadArray (AuxVectorData& container) const
169 -> Payload_t*
170{
171 return reinterpret_cast<Payload_t*>
172 (container.getDataArray (this->m_linkedAuxid));
173}
174
175
176/**
177 * @brief Get a span over the array of @c JaggedVecElt objects.
178 * @param container The container from which to fetch the variable.
179 */
180template <class PAYLOAD_T, class ALLOC>
181inline
182auto
183Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::getEltSpan (AuxVectorData& container) const
184 -> Elt_span
185{
186 // nb. AuxVectorData::getDataSpan is suitable only for const;
187 // getDataArray may throw if the container is empty.
188 size_t sz = container.size_v();
189 Elt_t* beg = nullptr;
190 if (sz) {
191 beg = reinterpret_cast<Elt_t*> (container.getDataArray (this->m_auxid));
192 }
193 return Elt_span (beg, sz);
194}
195
196
197/**
198 * @brief Get a span over the payload vector.
199 * @param container The container from which to fetch the variable.
200 */
201template <class PAYLOAD_T, class ALLOC>
202inline
203auto
204Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::getPayloadSpan (AuxVectorData& container) const
205 -> Payload_span
206{
207 const AuxDataSpanBase sp = *container.getDataSpan (this->m_linkedAuxid);
208 if (sp.size > 0) {
209 // Accessibility check. ??? Can we get rid of this?
210 (void)container.getDataArray (this->m_linkedAuxid);
211 }
212 return Payload_span (reinterpret_cast<Payload_t*>(sp.beg), sp.size);
213}
214
215
216/**
217 * @brief Get a span over spans representing the jagged vector.
218 * @param container The container from which to fetch the variable.
219 */
220template <class PAYLOAD_T, class ALLOC>
221inline
222auto
223Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::getDataSpan (AuxVectorData& container) const
224 -> span
225{
226 // Need to make sure getEltSpan() gets called before creating the
227 // Converter_t --- that's what will create the variable if needed.
228 Elt_span eltSpan = getEltSpan(container);
229 return span (eltSpan,
230 Converter_t (container, this->auxid(), this->linkedAuxid()));
231}
232
233
234/**
235 * @brief Test to see if this variable exists in the store and is writable.
236 * @param e An element of the container in which to test the variable.
237 */
238template <class PAYLOAD_T, class ALLOC>
239inline
240bool
241Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::isAvailableWritable (AuxElement& e) const
242{
243 return e.container() &&
244 e.container()->isAvailableWritable (this->m_auxid) &&
245 e.container()->isAvailableWritable (this->m_linkedAuxid);
246}
247
248
249/**
250 * @brief Test to see if this variable exists in the store and is writable.
251 * @param c The container in which to test the variable.
252 */
253template <class PAYLOAD_T, class ALLOC>
254inline
255bool
256Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::isAvailableWritable (AuxVectorData& c) const
257{
258 return c.isAvailableWritable (this->m_auxid) &&
259 c.isAvailableWritable (this->m_linkedAuxid);
260}
261
262
263} // namespace SG