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 template <class PAYLOAD_T, class ALLOC>
97 inline
98 auto
99 Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::operator() (AuxVectorData& container,
100  size_t index) const
101  -> reference_type
102 {
103  (void)this->getEltArray (container); // const/locking checks
104  return reference_type (index,
105  *container.getDataSpan (this->m_linkedAuxid),
106  *container.getDataSpan (this->m_auxid),
107  container,
108  this->m_auxid);
109 }
110 
111 
112 /**
113  * @brief Set the variable for one element.
114  * @param e The element for which to fetch the variable.
115  * @param x The variable value to set.
116  */
117 template <class PAYLOAD_T, class ALLOC>
118 template <CxxUtils::InputRangeOverT<PAYLOAD_T> RANGE>
119 inline
120 void Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::set (AuxElement& e,
121  const RANGE& x) const
122 {
123  (*this) (*e.container(), e.index()) = x;
124 }
125 
126 
127 /**
128  * @brief Set the variable for one element.
129  * @param container The container from which to fetch the variable.
130  * @param index The index of the desired element.
131  * @param x The variable value to set.
132  */
133 template <class PAYLOAD_T, class ALLOC>
134 template <CxxUtils::InputRangeOverT<PAYLOAD_T> RANGE>
135 void Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::set (AuxVectorData& container,
136  size_t index,
137  const RANGE& x) const
138 {
139  (*this) (container, index) = x;
140 }
141 
142 
143 /**
144  * @brief Get a pointer to the start of the array of @c JaggedVecElt objects.
145  * @param container The container from which to fetch the variable.
146  */
147 template <class PAYLOAD_T, class ALLOC>
148 inline
149 auto
150 Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::getEltArray (AuxVectorData& container) const
151  -> Elt_t*
152 {
153  return reinterpret_cast<Elt_t*>
154  (container.getDataArray (this->m_auxid));
155 }
156 
157 
158 /**
159  * @brief Get a pointer to the start of the payload array.
160  * @param container The container from which to fetch the variable.
161  */
162 template <class PAYLOAD_T, class ALLOC>
163 inline
164 auto
165 Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::getPayloadArray (AuxVectorData& container) const
166  -> Payload_t*
167 {
168  return reinterpret_cast<Payload_t*>
169  (container.getDataArray (this->m_linkedAuxid));
170 }
171 
172 
173 /**
174  * @brief Get a span over the array of @c JaggedVecElt objects.
175  * @param container The container from which to fetch the variable.
176  */
177 template <class PAYLOAD_T, class ALLOC>
178 inline
179 auto
180 Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::getEltSpan (AuxVectorData& container) const
181  -> Elt_span
182 {
183  auto beg = reinterpret_cast<Elt_t*> (container.getDataArray (this->m_auxid));
184  return Elt_span (beg, container.size_v());
185 }
186 
187 
188 /**
189  * @brief Get a span over the payload vector.
190  * @param container The container from which to fetch the variable.
191  */
192 template <class PAYLOAD_T, class ALLOC>
193 inline
194 auto
195 Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::getPayloadSpan (AuxVectorData& container) const
196  -> Payload_span
197 {
198  const AuxDataSpanBase* sp = container.getDataSpan (this->m_linkedAuxid);
199  return Payload_span (reinterpret_cast<Payload_t*>(sp->beg), sp->size);
200 }
201 
202 
203 /**
204  * @brief Get a span over spans representing the jagged vector.
205  * @param container The container from which to fetch the variable.
206  */
207 template <class PAYLOAD_T, class ALLOC>
208 inline
209 auto
210 Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::getDataSpan (AuxVectorData& container) const
211  -> span
212 {
213  return span (getEltSpan(container),
214  Converter_t (container, this->auxid(), this->linkedAuxid()));
215 }
216 
217 
218 /**
219  * @brief Test to see if this variable exists in the store and is writable.
220  * @param e An element of the container in which to test the variable.
221  */
222 template <class PAYLOAD_T, class ALLOC>
223 inline
224 bool
225 Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::isAvailableWritable (AuxElement& e) const
226 {
227  return e.container() &&
228  e.container()->isAvailableWritable (this->m_auxid) &&
229  e.container()->isAvailableWritable (this->m_linkedAuxid);
230 }
231 
232 
233 /**
234  * @brief Test to see if this variable exists in the store and is writable.
235  * @param c The container in which to test the variable.
236  */
237 template <class PAYLOAD_T, class ALLOC>
238 inline
239 bool
240 Accessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::isAvailableWritable (AuxVectorData& c) const
241 {
242  return c.isAvailableWritable (this->m_auxid) &&
243  c.isAvailableWritable (this->m_linkedAuxid);
244 }
245 
246 
247 } // namespace SG