ATLAS Offline Software
Loading...
Searching...
No Matches
PackedLinkConstAccessor.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/PackedLinkConstAccessor.icc
6 * @author scott snyder <snyder@bnl.gov>
7 * @date Oct, 2023
8 * @brief Helper class to provide constant type-safe access to aux data,
9 * specialized for @c PackedLink.
10 */
11
12
13#include "AthContainers/AuxElement.h"
14#include "AthContainers/AuxTypeRegistry.h"
15#include "AthContainers/exceptions.h"
16
17
18namespace SG {
19
20
21/**
22 * @brief Constructor.
23 * @param name Name of this aux variable.
24 *
25 * The name -> auxid lookup is done here.
26 */
27template <class CONT, class ALLOC>
28inline
29ConstAccessor<PackedLink<CONT>, ALLOC>::ConstAccessor (const std::string& name)
30 : ConstAccessor (name, "", AuxVarFlags::None)
31{
32}
33
34
35/**
36 * @brief Constructor.
37 * @param name Name of this aux variable.
38 * @param clsname The name of its associated class. May be blank.
39 *
40 * The name -> auxid lookup is done here.
41 */
42template <class CONT, class ALLOC>
43inline
44ConstAccessor<PackedLink<CONT>, ALLOC>::ConstAccessor
45 (const std::string& name, const std::string& clsname)
46 : ConstAccessor (name, clsname, AuxVarFlags::None)
47{
48 // cppcheck-suppress missingReturn; false positive
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 CONT, class ALLOC>
59inline
60ConstAccessor<PackedLink<CONT>, ALLOC>::ConstAccessor (const SG::auxid_t auxid)
61{
62 AuxTypeRegistry& r = AuxTypeRegistry::instance();
63 m_auxid = auxid;
64 r.checkAuxID<PLink_t, ALLOC> (m_auxid);
65 m_linkedAuxid = r.linkedVariable (m_auxid);
66 if (m_linkedAuxid == static_cast<uint32_t>(null_auxid)) {
67 throw SG::ExcNoLinkedVar (auxid, typeid (CONT));
68 // cppcheck-suppress missingReturn; false positive
69 }
70}
71
72
73/**
74 * @brief Constructor.
75 * @param name Name of this aux variable.
76 * @param clsname The name of its associated class. May be blank.
77 * @param flags Optional flags qualifying the type. See AuxTypeRegistry.
78 *
79 * The name -> auxid lookup is done here.
80 */
81template <class CONT, class ALLOC>
82inline
83ConstAccessor<PackedLink<CONT>, ALLOC>::ConstAccessor
84 (const std::string& name,
85 const std::string& clsname,
86 const AuxVarFlags flags)
87{
88 AuxTypeRegistry& r = AuxTypeRegistry::instance();
89 m_linkedAuxid = r.getAuxID<DLink_t, DLinkAlloc_t> (AuxTypeRegistry::linkedName (name),
90 clsname,
91 flags | AuxVarFlags::Linked);
92 m_auxid = r.getAuxID<PLink_t, ALLOC> (name, clsname, flags, m_linkedAuxid);
93}
94
95
96/**
97 * @brief Fetch the variable for one element.
98 * @param e The element for which to fetch the variable.
99 */
100template <class CONT, class ALLOC>
101template <IsConstAuxElement ELT>
102inline
103auto
104ConstAccessor<PackedLink<CONT>, ALLOC>::operator()
105 (const ELT& e) const -> const Link_t
106{
107 assert (e.container() != nullptr);
108 return resolveLink (*e.container(), e.index());
109
110}
111
112
113/**
114 * @brief Fetch the variable for one element.
115 * @param container The container from which to fetch the variable.
116 * @param index The index of the desired element.
117 *
118 * This allows retrieving aux data by container / index.
119 */
120template <class CONT, class ALLOC>
121inline
122auto
123ConstAccessor<PackedLink<CONT>, ALLOC>::operator()
124 (const AuxVectorData& container, size_t index) const -> const Link_t
125{
126 return resolveLink (container, index);
127}
128
129
130/**
131 * @brief Get a pointer to the start of the array of @c PackedLinks
132 * @param container The container from which to fetch the variable.
133 */
134template <class CONT, class ALLOC>
135inline
136auto
137ConstAccessor<PackedLink<CONT>, ALLOC>::getPackedLinkArray (const AuxVectorData& container) const
138 -> const PLink_t*
139{
140 return reinterpret_cast<const PLink_t*>
141 (container.getDataArray (m_auxid));
142}
143
144
145/**
146 * @brief Get a pointer to the start of the linked array of @c DataLinks.
147 * @param container The container from which to fetch the variable.
148 */
149template <class CONT, class ALLOC>
150inline
151auto
152ConstAccessor<PackedLink<CONT>, ALLOC>::getDataLinkArray (const AuxVectorData& container) const
153 -> const DLink_t*
154{
155 return reinterpret_cast<const DLink_t*>
156 (container.getDataArray (m_linkedAuxid));
157}
158
159
160/**
161 * @brief Get a span over the array of @c PackedLinks.
162 * @param container The container from which to fetch the variable.
163 */
164template <class CONT, class ALLOC>
165inline
166auto
167ConstAccessor<PackedLink<CONT>, ALLOC>::getPackedLinkSpan (const AuxVectorData& container) const
168 -> const_PackedLink_span
169{
170 const SG::AuxDataSpanBase sp = *container.getDataSpan (m_auxid);
171 return const_PackedLink_span (reinterpret_cast<const PLink_t*>(sp.beg),
172 sp.size);
173}
174
175
176/**
177 * @brief Get a span over the array of @c DataLinks.
178 * @param container The container from which to fetch the variable.
179 */
180template <class CONT, class ALLOC>
181inline
182auto
183ConstAccessor<PackedLink<CONT>, ALLOC>::getDataLinkSpan (const AuxVectorData& container) const
184 -> const_DataLink_span
185{
186 const SG::AuxDataSpanBase sp = *container.getDataSpan (m_linkedAuxid);
187 return const_DataLink_span (reinterpret_cast<const DLink_t*>(sp.beg),
188 sp.size);
189}
190
191
192/**
193 * @brief Get a span of @c ElementLinks.
194 * @param container The container from which to fetch the variable.
195 */
196template <class CONT, class ALLOC>
197inline
198auto
199ConstAccessor<PackedLink<CONT>, ALLOC>::getDataSpan (const AuxVectorData& container) const
200 -> const_span
201{
202 const_PackedLink_span pspan = getPackedLinkSpan(container);
203 typename ConstConverter_t::const_DataLink_span dlinks
204 (*container.getDataSpan (m_linkedAuxid));
205 return const_span (pspan, dlinks);
206}
207
208
209/**
210 * @brief Helper to resolve a @c PackedLink to an @c ElementLink.
211 * @param container The container from which to fetch the variable.
212 * @param index The index of the desired element.
213 */
214template <class CONT, class ALLOC>
215inline
216auto
217ConstAccessor<PackedLink<CONT>, ALLOC>::resolveLink (const AuxVectorData& container,
218 size_t index) const
219 -> Link_t
220{
221 const auto& l = container.template getData<PLink_t> (m_auxid, index);
222 return ConstConverter_t (*container.getDataSpan (m_linkedAuxid)) (l);
223}
224
225
226//************************************************************************
227
228
229// To make the declarations a bit more readable.
230#define CONSTACCESSOR ConstAccessor<std::vector<PackedLink<CONT>, VALLOC>, ALLOC>
231
232
233/**
234 * @brief Constructor.
235 * @param name Name of this aux variable.
236 *
237 * The name -> auxid lookup is done here.
238 */
239template <class CONT, class ALLOC, class VALLOC>
240inline
241CONSTACCESSOR::ConstAccessor (const std::string& name)
242 : ConstAccessor (name, "", AuxVarFlags::None)
243{
244}
245
246
247/**
248 * @brief Constructor.
249 * @param name Name of this aux variable.
250 * @param clsname The name of its associated class. May be blank.
251 *
252 * The name -> auxid lookup is done here.
253 */
254template <class CONT, class ALLOC, class VALLOC>
255inline
256CONSTACCESSOR::ConstAccessor
257 (const std::string& name, const std::string& clsname)
258 : ConstAccessor (name, clsname, AuxVarFlags::None)
259{
260}
261
262
263/**
264 * @brief Constructor taking an auxid directly.
265 * @param auxid ID for this auxiliary variable.
266 *
267 * Will throw @c SG::ExcAuxTypeMismatch if the types don't match.
268 */
269template <class CONT, class ALLOC, class VALLOC>
270inline
271CONSTACCESSOR::ConstAccessor (const SG::auxid_t auxid)
272{
273 AuxTypeRegistry& r = AuxTypeRegistry::instance();
274 m_auxid = auxid;
275 r.checkAuxID<VElt_t, ALLOC> (m_auxid);
276 m_linkedAuxid = r.linkedVariable (m_auxid);
277 if (m_linkedAuxid == static_cast<uint32_t>(null_auxid)) {
278 throw SG::ExcNoLinkedVar (auxid, typeid (CONT));
279 }
280}
281
282
283/**
284 * @brief Constructor.
285 * @param name Name of this aux variable.
286 * @param clsname The name of its associated class. May be blank.
287 * @param flags Optional flags qualifying the type. See AuxTypeRegistry.
288 *
289 * The name -> auxid lookup is done here.
290 */
291template <class CONT, class ALLOC, class VALLOC>
292inline
293CONSTACCESSOR::ConstAccessor
294 (const std::string& name,
295 const std::string& clsname,
296 const AuxVarFlags flags)
297{
298 AuxTypeRegistry& r = AuxTypeRegistry::instance();
299 m_linkedAuxid = r.getAuxID<DLink_t, DLinkAlloc_t> (AuxTypeRegistry::linkedName (name),
300 clsname,
301 flags | AuxVarFlags::Linked);
302 m_auxid = r.getAuxID<VElt_t, ALLOC> (name, clsname, flags, m_linkedAuxid);
303}
304
305
306/**
307 * @brief Fetch the variable for one element.
308 * @param e The element for which to fetch the variable.
309 *
310 * This will return a range of @c ElementLinks.
311 */
312template <class CONT, class ALLOC, class VALLOC>
313template <IsConstAuxElement ELT>
314inline
315auto
316CONSTACCESSOR::operator() (const ELT& e) const -> const_elt_span
317{
318 const_PackedLink_span pspan = getPackedLinkSpan(e);
319 typename ConstVectorTransform_t::const_DataLink_span xform
320 (*e.container()->getDataSpan (m_linkedAuxid));
321 return const_elt_span (pspan, xform);
322}
323
324/**
325 * @brief Fetch the variable for one element.
326 * @param container The container from which to fetch the variable.
327 * @param index The index of the desired element.
328 *
329 * This allows retrieving aux data by container / index.
330 *
331 * This will return a range of @c ElementLinks.
332 */
333template <class CONT, class ALLOC, class VALLOC>
334inline
335auto
336CONSTACCESSOR::operator()
337 (const AuxVectorData& container, size_t index) const -> const_elt_span
338{
339 const_PackedLink_span pspan = getPackedLinkSpan(container, index);
340 typename ConstVectorTransform_t::const_DataLink_span xform
341 (*container.getDataSpan (m_linkedAuxid));
342 return const_elt_span (pspan, xform);
343}
344
345
346/**
347 * @brief Get a pointer to the start of the array of vectors of @c PackedLinks.
348 * @param container The container from which to fetch the variable.
349 */
350template <class CONT, class ALLOC, class VALLOC>
351inline
352auto
353CONSTACCESSOR::getPackedLinkVectorArray (const AuxVectorData& container) const
354 -> const VElt_t*
355{
356 return reinterpret_cast<const VElt_t*>
357 (container.getDataArray (m_auxid));
358}
359
360
361/**
362 * @brief Get a pointer to the start of the linked array of @c DataLinks.
363 * @param container The container from which to fetch the variable.
364 */
365template <class CONT, class ALLOC, class VALLOC>
366inline
367auto
368CONSTACCESSOR::getDataLinkArray (const AuxVectorData& container) const
369 -> const DLink_t*
370{
371 return reinterpret_cast<const DLink_t*>
372 (container.getDataArray (this->m_linkedAuxid));
373}
374
375
376/**
377 * @brief Get a span over the vector of @c PackedLinks for a given element.
378 * @param e The element for which to fetch the variable.
379 */
380template <class CONT, class ALLOC, class VALLOC>
381template <IsConstAuxElement ELT>
382inline
383auto CONSTACCESSOR::getPackedLinkSpan (const ELT& e) const
384 -> const_PackedLink_span
385{
386 auto elt = reinterpret_cast<const VElt_t*>
387 (e.container()->getDataArray (m_auxid)) + e.index();
388 return const_PackedLink_span (elt->data(), elt->size());
389}
390
391
392/**
393 * @brief Get a span over the vector of @c PackedLinks for a given element.
394 * @param container The container from which to fetch the variable.
395 * @param index The index of the desired element.
396 */
397template <class CONT, class ALLOC, class VALLOC>
398inline
399auto
400CONSTACCESSOR::getPackedLinkSpan (const AuxVectorData& container,
401 size_t index) const
402 -> const_PackedLink_span
403{
404 auto elt = reinterpret_cast<const VElt_t*>
405 (container.getDataArray (this->m_auxid)) + index;
406 return const_PackedLink_span (elt->data(), elt->size());
407}
408
409
410/**
411 * @brief Get a span over the vectors of @c PackedLinks.
412 * @param container The container from which to fetch the variable.
413 */
414template <class CONT, class ALLOC, class VALLOC>
415inline
416auto
417CONSTACCESSOR::getPackedLinkVectorSpan (const AuxVectorData& container) const
418 -> const_PackedLinkVector_span
419{
420 auto beg = reinterpret_cast<const VElt_t*>
421 (container.getDataArray (m_auxid));
422 return const_PackedLinkVector_span (beg, container.size_v());
423}
424
425
426/**
427 * @brief Get a span over the array of @c DataLinks.
428 * @param container The container from which to fetch the variable.
429 */
430template <class CONT, class ALLOC, class VALLOC>
431auto
432CONSTACCESSOR::getDataLinkSpan (const AuxVectorData& container) const
433 -> const_DataLink_span
434{
435 const SG::AuxDataSpanBase* sp = container.getDataSpan (m_linkedAuxid);
436 return const_DataLink_span (reinterpret_cast<const DLink_t*>(sp->beg),
437 sp->size);
438}
439
440
441/**
442 * @brief Get a span over spans of @c ElementLinks.
443 * @param container The container from which to fetch the variable.
444 */
445template <class CONT, class ALLOC, class VALLOC>
446inline
447auto
448CONSTACCESSOR::getDataSpan (const AuxVectorData& container) const
449 -> const_span
450{
451 const_PackedLinkVector_span pspan = getPackedLinkVectorSpan(container);
452 typename ConstVectorTransform_t::const_DataLink_span xform
453 (*container.getDataSpan (m_linkedAuxid));
454 return const_span (pspan, xform);
455}
456
457
458#undef CONSTACCESSOR
459
460
461} // namespace SG