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