2 * Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration.
5 * @file AthContainers/ConstAccessor.icc
6 * @author scott snyder <snyder@bnl.gov>
8 * @brief Helper class to provide constant type-safe access to aux data.
12#include "AthContainers/AuxTypeRegistry.h"
13#include "AthContainers/exceptions.h"
21 * @param name Name of this aux variable.
23 * The name -> auxid lookup is done here.
25template <class T, class ALLOC>
27ConstAccessor<T, ALLOC>::ConstAccessor (const std::string& name)
28 : ConstAccessor (name, "", SG::AuxVarFlags::None)
30 // cppcheck-suppress missingReturn
36 * @param name Name of this aux variable.
37 * @param clsname The name of its associated class. May be blank.
39 * The name -> auxid lookup is done here.
41template <class T, class ALLOC>
43ConstAccessor<T, ALLOC>::ConstAccessor
44 (const std::string& name, const std::string& clsname)
45 : ConstAccessor (name, clsname, SG::AuxVarFlags::None)
51 * @brief Constructor taking an auxid directly.
52 * @param auxid ID for this auxiliary variable.
54 * Will throw @c SG::ExcAuxTypeMismatch if the types don't match.
56template <class T, class ALLOC>
58ConstAccessor<T, ALLOC>::ConstAccessor (const SG::auxid_t auxid)
59 : ConstAccessor (auxid, SG::AuxVarFlags::None)
61 // cppcheck-suppress missingReturn; false positive
67 * @param name Name of this aux variable.
68 * @param clsname The name of its associated class. May be blank.
69 * @param flags Optional flags qualifying the type. See AuxTypeRegistry.
71 * The name -> auxid lookup is done here.
73template <class T, class ALLOC>
75ConstAccessor<T, ALLOC>::ConstAccessor
76 (const std::string& name,
77 const std::string& clsname,
78 const SG::AuxVarFlags flags)
79 : m_auxid (SG::AuxTypeRegistry::instance().getAuxID<T, ALLOC> (name, clsname, flags))
85 * @brief Constructor taking an auxid directly.
86 * @param auxid ID for this auxiliary variable.
88 * Will throw @c SG::ExcAuxTypeMismatch if the types don't match.
90template <class T, class ALLOC>
92ConstAccessor<T, ALLOC>::ConstAccessor (const SG::auxid_t auxid,
93 const SG::AuxVarFlags flags)
96 //cppcheck-suppress missingReturn
97 SG::AuxTypeRegistry::instance().checkAuxID<T, ALLOC> (auxid, flags);
102 * @brief Fetch the variable for one element, as a const reference.
103 * @param e The element for which to fetch the variable.
105template <class T, class ALLOC>
106template <IsConstAuxElement ELT>
108typename ConstAccessor<T, ALLOC>::const_reference_type
109ConstAccessor<T, ALLOC>::operator() (const ELT& e) const
111 assert (e.container() != 0);
112 return e.container()->template getData<T> (m_auxid, e.index());
117 * @brief Fetch the variable for one element, as a const reference.
118 * @param container The container from which to fetch the variable.
119 * @param index The index of the desired element.
121 * This allows retrieving aux data by container / index.
122 * Looping over the index via this method will be faster then
123 * looping over the elements of the container.
125template <class T, class ALLOC>
127typename ConstAccessor<T, ALLOC>::const_reference_type
128ConstAccessor<T, ALLOC>::operator()
129 (const AuxVectorData& container,
132 return container.template getData<T> (m_auxid, index);
137 * @brief Fetch the variable for one element, as a const reference,
139 * @param e The element for which to fetch the variable.
140 * @param deflt Default value.
142 * If this variable is not available, then return @c deflt instead.
144template <class T, class ALLOC>
145template <IsConstAuxElement ELT>
147typename ConstAccessor<T, ALLOC>::const_reference_type
148ConstAccessor<T, ALLOC>::withDefault (const ELT& e, const T& deflt) const
150 if (!e.container() || !e.container()->isAvailable (m_auxid)) return deflt;
151 return e.container()->template getData<T> (m_auxid, e.index());
156 * @brief Fetch the variable for one element, as a const reference.
157 * @param container The container from which to fetch the variable.
158 * @param index The index of the desired element.
159 * @param deflt Default value.
161 * This allows retrieving aux data by container / index.
162 * Looping over the index via this method will be faster then
163 * looping over the elements of the container.
164 * If this variable is not available, then return @c deflt instead.
166template <class T, class ALLOC>
168typename ConstAccessor<T, ALLOC>::const_reference_type
169ConstAccessor<T, ALLOC>::withDefault
170 (const AuxVectorData& container,
172 const T& deflt) const
174 if (!container.isAvailable (m_auxid)) return deflt;
175 return container.template getData<T> (m_auxid, index);
180 * @brief Get a pointer to the start of the auxiliary data array.
181 * @param container The container from which to fetch the variable.
183template <class T, class ALLOC>
185typename ConstAccessor<T, ALLOC>::const_container_pointer_type
186ConstAccessor<T, ALLOC>::getDataArray (const AuxVectorData& container) const
188 return reinterpret_cast<const_container_pointer_type>
189 (container.getDataArray (m_auxid));
194 * @brief Get a span over the auxilary data array.
195 * @param container The container from which to fetch the variable.
197template <class T, class ALLOC>
199typename ConstAccessor<T, ALLOC>::const_span
200ConstAccessor<T, ALLOC>::getDataSpan (const AuxVectorData& container) const
202 auto beg = reinterpret_cast<const_container_pointer_type>
203 (container.getDataArray (m_auxid));
204 return const_span (beg, container.size_v());
209 * @brief Test to see if this variable exists in the store.
210 * @param e An element of the container in which to test the variable.
212template <class T, class ALLOC>
213template <IsConstAuxElement ELT>
216ConstAccessor<T, ALLOC>::isAvailable (const ELT& e) const
218 return e.container() && e.container()->isAvailable (m_auxid);
223 * @brief Test to see if this variable exists in the store.
224 * @param c The container in which to test the variable.
226template <class T, class ALLOC>
229ConstAccessor<T, ALLOC>::isAvailable (const AuxVectorData& c) const
231 return c.isAvailable (m_auxid);
236 * @brief Return the aux id for this variable.
238template <class T, class ALLOC>
241ConstAccessor<T, ALLOC>::auxid() const