2 * Copyright (C) 2002-2024 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.
25 template <class T, class ALLOC>
27 ConstAccessor<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.
41 template <class T, class ALLOC>
43 ConstAccessor<T, ALLOC>::ConstAccessor
44 (const std::string& name, const std::string& clsname)
45 : ConstAccessor (name, clsname, SG::AuxVarFlags::None)
47 // cppcheck-suppress missingReturn; false positive
52 * @brief Constructor taking an auxid directly.
53 * @param auxid ID for this auxiliary variable.
55 * Will throw @c SG::ExcAuxTypeMismatch if the types don't match.
57 template <class T, class ALLOC>
59 ConstAccessor<T, ALLOC>::ConstAccessor (const SG::auxid_t auxid)
60 : ConstAccessor (auxid, SG::AuxVarFlags::None)
62 // cppcheck-suppress missingReturn; false positive
68 * @param name Name of this aux variable.
69 * @param clsname The name of its associated class. May be blank.
70 * @param flags Optional flags qualifying the type. See AuxTypeRegistry.
72 * The name -> auxid lookup is done here.
74 template <class T, class ALLOC>
76 ConstAccessor<T, ALLOC>::ConstAccessor
77 (const std::string& name,
78 const std::string& clsname,
79 const SG::AuxVarFlags flags)
80 : m_auxid (SG::AuxTypeRegistry::instance().getAuxID<T, ALLOC> (name, clsname, flags))
86 * @brief Constructor taking an auxid directly.
87 * @param auxid ID for this auxiliary variable.
89 * Will throw @c SG::ExcAuxTypeMismatch if the types don't match.
91 template <class T, class ALLOC>
93 ConstAccessor<T, ALLOC>::ConstAccessor (const SG::auxid_t auxid,
94 const SG::AuxVarFlags flags)
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.
105 template <class T, class ALLOC>
107 ATH_REQUIRES( IsConstAuxElement<ELT> )
109 typename ConstAccessor<T, ALLOC>::const_reference_type
110 ConstAccessor<T, ALLOC>::operator() (const ELT& e) const
112 assert (e.container() != 0);
113 return e.container()->template getData<T> (m_auxid, e.index());
118 * @brief Fetch the variable for one element, as a const reference.
119 * @param container The container from which to fetch the variable.
120 * @param index The index of the desired element.
122 * This allows retrieving aux data by container / index.
123 * Looping over the index via this method will be faster then
124 * looping over the elements of the container.
126 template <class T, class ALLOC>
128 typename ConstAccessor<T, ALLOC>::const_reference_type
129 ConstAccessor<T, ALLOC>::operator()
130 (const AuxVectorData& container,
133 return container.template getData<T> (m_auxid, index);
138 * @brief Fetch the variable for one element, as a const reference,
140 * @param e The element for which to fetch the variable.
141 * @param deflt Default value.
143 * If this variable is not available, then return @c deflt instead.
145 template <class T, class ALLOC>
147 ATH_REQUIRES( IsConstAuxElement<ELT> )
149 typename ConstAccessor<T, ALLOC>::const_reference_type
150 ConstAccessor<T, ALLOC>::withDefault (const ELT& e, const T& deflt) const
152 if (!e.container() || !e.container()->isAvailable (m_auxid)) return deflt;
153 return e.container()->template getData<T> (m_auxid, e.index());
158 * @brief Fetch the variable for one element, as a const reference.
159 * @param container The container from which to fetch the variable.
160 * @param index The index of the desired element.
161 * @param deflt Default value.
163 * This allows retrieving aux data by container / index.
164 * Looping over the index via this method will be faster then
165 * looping over the elements of the container.
166 * If this variable is not available, then return @c deflt instead.
168 template <class T, class ALLOC>
170 typename ConstAccessor<T, ALLOC>::const_reference_type
171 ConstAccessor<T, ALLOC>::withDefault
172 (const AuxVectorData& container,
174 const T& deflt) const
176 if (!container.isAvailable (m_auxid)) return deflt;
177 return container.template getData<T> (m_auxid, index);
182 * @brief Get a pointer to the start of the auxiliary data array.
183 * @param container The container from which to fetch the variable.
185 template <class T, class ALLOC>
187 typename ConstAccessor<T, ALLOC>::const_container_pointer_type
188 ConstAccessor<T, ALLOC>::getDataArray (const AuxVectorData& container) const
190 return reinterpret_cast<const_container_pointer_type>
191 (container.getDataArray (m_auxid));
196 * @brief Get a span over the auxilary data array.
197 * @param container The container from which to fetch the variable.
199 template <class T, class ALLOC>
201 typename ConstAccessor<T, ALLOC>::const_span
202 ConstAccessor<T, ALLOC>::getDataSpan (const AuxVectorData& container) const
204 auto beg = reinterpret_cast<const_container_pointer_type>
205 (container.getDataArray (m_auxid));
206 return const_span (beg, container.size_v());
211 * @brief Test to see if this variable exists in the store.
212 * @param e An element of the container in which to test the variable.
214 template <class T, class ALLOC>
216 ATH_REQUIRES( IsConstAuxElement<ELT> )
219 ConstAccessor<T, ALLOC>::isAvailable (const ELT& e) const
221 return e.container() && e.container()->isAvailable (m_auxid);
226 * @brief Test to see if this variable exists in the store.
227 * @param c The container in which to test the variable.
229 template <class T, class ALLOC>
232 ConstAccessor<T, ALLOC>::isAvailable (const AuxVectorData& c) const
234 return c.isAvailable (m_auxid);
239 * @brief Return the aux id for this variable.
241 template <class T, class ALLOC>
244 ConstAccessor<T, ALLOC>::auxid() const