2 * Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration.
5 * @file AthContainers/Decorator.icc
6 * @author scott snyder <snyder@bnl.gov>
8 * @brief Helper class to provide 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 Decorator<T, ALLOC>::Decorator (const std::string& name)
28 : Decorator (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 Decorator<T, ALLOC>::Decorator (const std::string& name,
44 const std::string& clsname)
45 : Decorator (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.
56 template <class T, class ALLOC>
58 Decorator<T, ALLOC>::Decorator (const SG::auxid_t auxid)
59 : Decorator (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 AuxTypeRegsitry.
71 * The name -> auxid lookup is done here.
73 template <class T, class ALLOC>
75 Decorator<T, ALLOC>::Decorator
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.
87 * @param flags Optional flags qualifying the type. See AuxTypeRegistry.
89 * Will throw @c SG::ExcAuxTypeMismatch if the types don't match.
91 template <class T, class ALLOC>
93 Decorator<T, ALLOC>::Decorator (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 non-const reference.
103 * @param e The element for which to fetch the variable.
105 * If the container is locked, this will allow fetching only variables
106 * that do not yet exist (in which case they will be marked as decorations)
107 * or variables already marked as decorations.
109 template <class T, class ALLOC>
110 template <IsConstAuxElement ELT>
112 typename Decorator<T, ALLOC>::reference_type
113 Decorator<T, ALLOC>::operator() (const ELT& e) const
115 assert (e.container() != 0);
116 return e.container()->template getDecoration<T> (m_auxid, e.index());
120 * @brief Fetch the variable for one element, as a non-const reference.
121 * @param container The container from which to fetch the variable.
122 * @param index The index of the desired element.
124 * This allows retrieving aux data by container / index.
125 * Looping over the index via this method will be faster then
126 * looping over the elements of the container.
128 * If the container is locked, this will allow fetching only variables
129 * that do not yet exist (in which case they will be marked as decorations)
130 * or variables already marked as decorations.
132 template <class T, class ALLOC>
134 typename Decorator<T, ALLOC>::reference_type
135 Decorator<T, ALLOC>::operator() (const AuxVectorData& container,
138 return container.template getDecoration<T> (m_auxid, index);
143 * @brief Set the variable for one element.
144 * @param e The element for which to fetch the variable.
145 * @param x The variable value to set.
147 template <class T, class ALLOC>
148 template <IsConstAuxElement ELT>
150 void Decorator<T, ALLOC>::set (const ELT& e,
151 const element_type& x) const
158 * @brief Get a pointer to the start of the auxiliary data array.
159 * @param container The container from which to fetch the variable.
161 template <class T, class ALLOC>
163 typename Decorator<T, ALLOC>::const_container_pointer_type
164 Decorator<T, ALLOC>::getDataArray (const AuxVectorData& container) const
166 return reinterpret_cast<const_container_pointer_type>
167 (container.getDataArray (m_auxid));
172 * @brief Get a pointer to the start of the auxiliary data array.
173 * @param container The container from which to fetch the variable.
175 * If the container is locked, this will allow fetching only variables
176 * that do not yet exist (in which case they will be marked as decorations)
177 * or variables already marked as decorations.
179 template <class T, class ALLOC>
181 typename Decorator<T, ALLOC>::container_pointer_type
182 Decorator<T, ALLOC>::getDecorationArray (const AuxVectorData& container) const
184 return reinterpret_cast<container_pointer_type>
185 (container.getDecorationArray (m_auxid));
190 * @brief Get a span over the auxilary data array.
191 * @param container The container from which to fetch the variable.
193 template <class T, class ALLOC>
195 typename Decorator<T, ALLOC>::const_span
196 Decorator<T, ALLOC>::getDataSpan (const AuxVectorData& container) const
198 auto beg = reinterpret_cast<const_container_pointer_type>
199 (container.getDataArray (m_auxid));
200 return const_span (beg, container.size_v());
205 * @brief Get a span over the auxilary data array.
206 * @param container The container from which to fetch the variable.
208 * If the container is locked, this will allow fetching only variables
209 * that do not yet exist (in which case they will be marked as decorations)
210 * or variables already marked as decorations.
212 template <class T, class ALLOC>
214 typename Decorator<T, ALLOC>::span
215 Decorator<T, ALLOC>::getDecorationSpan (const AuxVectorData& container) const
217 auto beg = reinterpret_cast<container_pointer_type>
218 (container.getDecorationArray (m_auxid));
219 return span (beg, container.size_v());
224 * @brief Test to see if this variable exists in the store.
225 * @param e An element of the container in which to test the variable.
227 template <class T, class ALLOC>
228 template <IsConstAuxElement ELT>
231 Decorator<T, ALLOC>::isAvailable (const ELT& e) const
233 return e.container() && e.container()->isAvailable (m_auxid);
237 * @brief Test to see if this variable exists in the store and is writable.
238 * @param e An element of the container in which to test the variable.
240 template <class T, class ALLOC>
241 template <IsConstAuxElement ELT>
244 Decorator<T, ALLOC>::isAvailableWritable (const ELT& e) const
246 return e.container() && e.container()->isAvailableWritableAsDecoration (m_auxid);
251 * @brief Test to see if this variable exists in the store.
252 * @param c The container in which to test the variable.
254 template <class T, class ALLOC>
257 Decorator<T, ALLOC>::isAvailable (const AuxVectorData& c) const
259 return c.isAvailable (m_auxid);
263 * @brief Test to see if this variable exists in the store and is writable.
264 * @param c The container in which to test the variable.
266 template <class T, class ALLOC>
269 Decorator<T, ALLOC>::isAvailableWritable (const AuxVectorData& c) const
271 return c.isAvailableWritableAsDecoration (m_auxid);
276 * @brief Return the aux id for this variable.
278 template <class T, class ALLOC>
281 Decorator<T, ALLOC>::auxid() const