2 * Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration.
5 * @file AthContainers/PackedLinkConstAccessor.icc
6 * @author scott snyder <snyder@bnl.gov>
8 * @brief Helper class to provide constant type-safe access to aux data,
9 * specialized for @c PackedLink.
13 #include "AthContainers/AuxElement.h"
14 #include "AthContainers/AuxTypeRegistry.h"
15 #include "AthContainers/exceptions.h"
23 * @param name Name of this aux variable.
25 * The name -> auxid lookup is done here.
27 template <class CONT, class ALLOC>
29 ConstAccessor<PackedLink<CONT>, ALLOC>::ConstAccessor (const std::string& name)
30 : ConstAccessor (name, "", AuxVarFlags::None)
37 * @param name Name of this aux variable.
38 * @param clsname The name of its associated class. May be blank.
40 * The name -> auxid lookup is done here.
42 template <class CONT, class ALLOC>
44 ConstAccessor<PackedLink<CONT>, ALLOC>::ConstAccessor
45 (const std::string& name, const std::string& clsname)
46 : ConstAccessor (name, clsname, AuxVarFlags::None)
48 // cppcheck-suppress missingReturn; false positive
53 * @brief Constructor taking an auxid directly.
54 * @param auxid ID for this auxiliary variable.
56 * Will throw @c SG::ExcAuxTypeMismatch if the types don't match.
58 template <class CONT, class ALLOC>
60 ConstAccessor<PackedLink<CONT>, ALLOC>::ConstAccessor (const SG::auxid_t auxid)
62 AuxTypeRegistry& r = AuxTypeRegistry::instance();
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
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 AuxTypeRegsitry.
79 * The name -> auxid lookup is done here.
81 template <class CONT, class ALLOC>
83 ConstAccessor<PackedLink<CONT>, ALLOC>::ConstAccessor
84 (const std::string& name,
85 const std::string& clsname,
86 const AuxVarFlags flags)
88 AuxTypeRegistry& r = AuxTypeRegistry::instance();
89 m_linkedAuxid = r.getAuxID<DLink_t, DLinkAlloc_t> (AuxTypeRegistry::linkedName (name),
91 flags | AuxVarFlags::Linked);
92 m_auxid = r.getAuxID<PLink_t, ALLOC> (name, clsname, flags, m_linkedAuxid);
97 * @brief Fetch the variable for one element.
98 * @param e The element for which to fetch the variable.
100 template <class CONT, class ALLOC>
101 template <IsConstAuxElement ELT>
104 ConstAccessor<PackedLink<CONT>, ALLOC>::operator()
105 (const ELT& e) const -> const Link_t
107 assert (e.container() != nullptr);
108 return resolveLink (*e.container(), e.index());
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.
118 * This allows retrieving aux data by container / index.
120 template <class CONT, class ALLOC>
123 ConstAccessor<PackedLink<CONT>, ALLOC>::operator()
124 (const AuxVectorData& container, size_t index) const -> const Link_t
126 return resolveLink (container, index);
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.
134 template <class CONT, class ALLOC>
137 ConstAccessor<PackedLink<CONT>, ALLOC>::getPackedLinkArray (const AuxVectorData& container) const
140 return reinterpret_cast<const PLink_t*>
141 (container.getDataArray (m_auxid));
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.
149 template <class CONT, class ALLOC>
152 ConstAccessor<PackedLink<CONT>, ALLOC>::getDataLinkArray (const AuxVectorData& container) const
155 return reinterpret_cast<const DLink_t*>
156 (container.getDataArray (m_linkedAuxid));
161 * @brief Get a span over the array of @c PackedLinks.
162 * @param container The container from which to fetch the variable.
164 template <class CONT, class ALLOC>
167 ConstAccessor<PackedLink<CONT>, ALLOC>::getPackedLinkSpan (const AuxVectorData& container) const
168 -> const_PackedLink_span
170 auto beg = reinterpret_cast<const PLink_t*>(container.getDataArray (m_auxid));
171 return const_PackedLink_span (beg, container.size_v());
176 * @brief Get a span over the array of @c DataLinks.
177 * @param container The container from which to fetch the variable.
179 template <class CONT, class ALLOC>
182 ConstAccessor<PackedLink<CONT>, ALLOC>::getDataLinkSpan (const AuxVectorData& container) const
183 -> const_DataLink_span
185 const SG::AuxDataSpanBase* sp = container.getDataSpan (m_linkedAuxid);
186 return const_DataLink_span (reinterpret_cast<const DLink_t*>(sp->beg),
192 * @brief Get a span of @c ElementLinks.
193 * @param container The container from which to fetch the variable.
195 template <class CONT, class ALLOC>
198 ConstAccessor<PackedLink<CONT>, ALLOC>::getDataSpan (const AuxVectorData& container) const
201 const_PackedLink_span pspan = getPackedLinkSpan(container);
202 typename ConstConverter_t::const_DataLink_span dlinks
203 (*container.getDataSpan (m_linkedAuxid));
204 return const_span (pspan, dlinks);
209 * @brief Helper to resolve a @c PackedLink to an @c ElementLink.
210 * @param container The container from which to fetch the variable.
211 * @param index The index of the desired element.
213 template <class CONT, class ALLOC>
216 ConstAccessor<PackedLink<CONT>, ALLOC>::resolveLink (const AuxVectorData& container,
220 const auto& l = container.template getData<PLink_t> (m_auxid, index);
221 return ConstConverter_t (*container.getDataSpan (m_linkedAuxid)) (l);
225 //************************************************************************
228 // To make the declarations a bit more readable.
229 #define CONSTACCESSOR ConstAccessor<std::vector<PackedLink<CONT>, VALLOC>, ALLOC>
233 * @brief Constructor.
234 * @param name Name of this aux variable.
236 * The name -> auxid lookup is done here.
238 template <class CONT, class ALLOC, class VALLOC>
240 CONSTACCESSOR::ConstAccessor (const std::string& name)
241 : ConstAccessor (name, "", AuxVarFlags::None)
247 * @brief Constructor.
248 * @param name Name of this aux variable.
249 * @param clsname The name of its associated class. May be blank.
251 * The name -> auxid lookup is done here.
253 template <class CONT, class ALLOC, class VALLOC>
255 CONSTACCESSOR::ConstAccessor
256 (const std::string& name, const std::string& clsname)
257 : ConstAccessor (name, clsname, AuxVarFlags::None)
263 * @brief Constructor taking an auxid directly.
264 * @param auxid ID for this auxiliary variable.
266 * Will throw @c SG::ExcAuxTypeMismatch if the types don't match.
268 template <class CONT, class ALLOC, class VALLOC>
270 CONSTACCESSOR::ConstAccessor (const SG::auxid_t auxid)
272 AuxTypeRegistry& r = AuxTypeRegistry::instance();
274 r.checkAuxID<VElt_t, ALLOC> (m_auxid);
275 m_linkedAuxid = r.linkedVariable (m_auxid);
276 if (m_linkedAuxid == static_cast<uint32_t>(null_auxid)) {
277 throw SG::ExcNoLinkedVar (auxid, typeid (CONT));
283 * @brief Constructor.
284 * @param name Name of this aux variable.
285 * @param clsname The name of its associated class. May be blank.
286 * @param flags Optional flags qualifying the type. See AuxTypeRegsitry.
288 * The name -> auxid lookup is done here.
290 template <class CONT, class ALLOC, class VALLOC>
292 CONSTACCESSOR::ConstAccessor
293 (const std::string& name,
294 const std::string& clsname,
295 const AuxVarFlags flags)
297 AuxTypeRegistry& r = AuxTypeRegistry::instance();
298 m_linkedAuxid = r.getAuxID<DLink_t, DLinkAlloc_t> (AuxTypeRegistry::linkedName (name),
300 flags | AuxVarFlags::Linked);
301 m_auxid = r.getAuxID<VElt_t, ALLOC> (name, clsname, flags, m_linkedAuxid);
306 * @brief Fetch the variable for one element.
307 * @param e The element for which to fetch the variable.
309 * This will return a range of @c ElementLinks.
311 template <class CONT, class ALLOC, class VALLOC>
312 template <IsConstAuxElement ELT>
315 CONSTACCESSOR::operator() (const ELT& e) const -> const_elt_span
317 const_PackedLink_span pspan = getPackedLinkSpan(e);
318 typename ConstVectorTransform_t::const_DataLink_span xform
319 (*e.container()->getDataSpan (m_linkedAuxid));
320 return const_elt_span (pspan, xform);
324 * @brief Fetch the variable for one element.
325 * @param container The container from which to fetch the variable.
326 * @param index The index of the desired element.
328 * This allows retrieving aux data by container / index.
330 * This will return a range of @c ElementLinks.
332 template <class CONT, class ALLOC, class VALLOC>
335 CONSTACCESSOR::operator()
336 (const AuxVectorData& container, size_t index) const -> const_elt_span
338 const_PackedLink_span pspan = getPackedLinkSpan(container, index);
339 typename ConstVectorTransform_t::const_DataLink_span xform
340 (*container.getDataSpan (m_linkedAuxid));
341 return const_elt_span (pspan, xform);
346 * @brief Get a pointer to the start of the array of vectors of @c PackedLinks.
347 * @param container The container from which to fetch the variable.
349 template <class CONT, class ALLOC, class VALLOC>
352 CONSTACCESSOR::getPackedLinkVectorArray (const AuxVectorData& container) const
355 return reinterpret_cast<const VElt_t*>
356 (container.getDataArray (m_auxid));
361 * @brief Get a pointer to the start of the linked array of @c DataLinks.
362 * @param container The container from which to fetch the variable.
364 template <class CONT, class ALLOC, class VALLOC>
367 CONSTACCESSOR::getDataLinkArray (const AuxVectorData& container) const
370 return reinterpret_cast<const DLink_t*>
371 (container.getDataArray (this->m_linkedAuxid));
376 * @brief Get a span over the vector of @c PackedLinks for a given element.
377 * @param e The element for which to fetch the variable.
379 template <class CONT, class ALLOC, class VALLOC>
380 template <IsConstAuxElement ELT>
382 auto CONSTACCESSOR::getPackedLinkSpan (const ELT& e) const
383 -> const_PackedLink_span
385 auto elt = reinterpret_cast<const VElt_t*>
386 (e.container()->getDataArray (m_auxid)) + e.index();
387 return const_PackedLink_span (elt->data(), elt->size());
392 * @brief Get a span over the vector of @c PackedLinks for a given element.
393 * @param container The container from which to fetch the variable.
394 * @param index The index of the desired element.
396 template <class CONT, class ALLOC, class VALLOC>
399 CONSTACCESSOR::getPackedLinkSpan (const AuxVectorData& container,
401 -> const_PackedLink_span
403 auto elt = reinterpret_cast<const VElt_t*>
404 (container.getDataArray (this->m_auxid)) + index;
405 return const_PackedLink_span (elt->data(), elt->size());
410 * @brief Get a span over the vectors of @c PackedLinks.
411 * @param container The container from which to fetch the variable.
413 template <class CONT, class ALLOC, class VALLOC>
416 CONSTACCESSOR::getPackedLinkVectorSpan (const AuxVectorData& container) const
417 -> const_PackedLinkVector_span
419 auto beg = reinterpret_cast<const VElt_t*>
420 (container.getDataArray (m_auxid));
421 return const_PackedLinkVector_span (beg, container.size_v());
426 * @brief Get a span over the array of @c DataLinks.
427 * @param container The container from which to fetch the variable.
429 template <class CONT, class ALLOC, class VALLOC>
431 CONSTACCESSOR::getDataLinkSpan (const AuxVectorData& container) const
432 -> const_DataLink_span
434 const SG::AuxDataSpanBase* sp = container.getDataSpan (m_linkedAuxid);
435 return const_DataLink_span (reinterpret_cast<const DLink_t*>(sp->beg),
441 * @brief Get a span over spans of @c ElementLinks.
442 * @param container The container from which to fetch the variable.
444 template <class CONT, class ALLOC, class VALLOC>
447 CONSTACCESSOR::getDataSpan (const AuxVectorData& container) const
450 const_PackedLinkVector_span pspan = getPackedLinkVectorSpan(container);
451 typename ConstVectorTransform_t::const_DataLink_span xform
452 (*container.getDataSpan (m_linkedAuxid));
453 return const_span (pspan, xform);