ATLAS Offline Software
JaggedVecAccessor.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/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 
19 namespace 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  */
28 template <class PAYLOAD_T, class ALLOC>
29 inline
30 Accessor<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  */
43 template <class PAYLOAD_T, class ALLOC>
44 inline
45 Accessor<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  */
58 template <class PAYLOAD_T, class ALLOC>
59 inline
60 Accessor<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  */
74 template <class PAYLOAD_T, class ALLOC>
75 template <IsAuxElement ELT>
76 inline
77 auto
78 Accessor<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
97 template <class PAYLOAD_T, class ALLOC>
98 inline
99 auto
100 Accessor<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  */
119 template <class PAYLOAD_T, class ALLOC>
120 template <CxxUtils::InputRangeOverT<PAYLOAD_T> RANGE>
121 inline
122 void 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  */
135 template <class PAYLOAD_T, class ALLOC>
136 template <CxxUtils::InputRangeOverT<PAYLOAD_T> RANGE>
137 void 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  */
149 template <class PAYLOAD_T, class ALLOC>
150 inline
151 auto
152 Accessor<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  */
164 template <class PAYLOAD_T, class ALLOC>
165 inline
166 auto
167 Accessor<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  */
179 template <class PAYLOAD_T, class ALLOC>
180 inline
181 auto
182 Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::getEltSpan (AuxVectorData& container) const
183  -> Elt_span
184 {
185  auto beg = reinterpret_cast<Elt_t*> (container.getDataArray (this->m_auxid));
186  return Elt_span (beg, container.size_v());
187 }
188 
189 
190 /**
191  * @brief Get a span over the payload vector.
192  * @param container The container from which to fetch the variable.
193  */
194 template <class PAYLOAD_T, class ALLOC>
195 inline
196 auto
197 Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::getPayloadSpan (AuxVectorData& container) const
198  -> Payload_span
199 {
200  const AuxDataSpanBase* sp = container.getDataSpan (this->m_linkedAuxid);
201  return Payload_span (reinterpret_cast<Payload_t*>(sp->beg), sp->size);
202 }
203 
204 
205 /**
206  * @brief Get a span over spans representing the jagged vector.
207  * @param container The container from which to fetch the variable.
208  */
209 template <class PAYLOAD_T, class ALLOC>
210 inline
211 auto
212 Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::getDataSpan (AuxVectorData& container) const
213  -> span
214 {
215  return span (getEltSpan(container),
216  Converter_t (container, this->auxid(), this->linkedAuxid()));
217 }
218 
219 
220 /**
221  * @brief Test to see if this variable exists in the store and is writable.
222  * @param e An element of the container in which to test the variable.
223  */
224 template <class PAYLOAD_T, class ALLOC>
225 inline
226 bool
227 Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::isAvailableWritable (AuxElement& e) const
228 {
229  return e.container() &&
230  e.container()->isAvailableWritable (this->m_auxid) &&
231  e.container()->isAvailableWritable (this->m_linkedAuxid);
232 }
233 
234 
235 /**
236  * @brief Test to see if this variable exists in the store and is writable.
237  * @param c The container in which to test the variable.
238  */
239 template <class PAYLOAD_T, class ALLOC>
240 inline
241 bool
242 Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::isAvailableWritable (AuxVectorData& c) const
243 {
244  return c.isAvailableWritable (this->m_auxid) &&
245  c.isAvailableWritable (this->m_linkedAuxid);
246 }
247 
248 
249 } // namespace SG