Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ConstAccessor.icc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration.
3  */
4 /**
5  * @file AthContainers/ConstAccessor.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  */
10 
11 
12 #include "AthContainers/AuxTypeRegistry.h"
13 #include "AthContainers/exceptions.h"
14 
15 
16 namespace SG {
17 
18 
19 /**
20  * @brief Constructor.
21  * @param name Name of this aux variable.
22  *
23  * The name -> auxid lookup is done here.
24  */
25 template <class T, class ALLOC>
26 inline
27 ConstAccessor<T, ALLOC>::ConstAccessor (const std::string& name)
28  : ConstAccessor (name, "", SG::AuxVarFlags::None)
29 {
30  // cppcheck-suppress missingReturn
31 }
32 
33 
34 /**
35  * @brief Constructor.
36  * @param name Name of this aux variable.
37  * @param clsname The name of its associated class. May be blank.
38  *
39  * The name -> auxid lookup is done here.
40  */
41 template <class T, class ALLOC>
42 inline
43 ConstAccessor<T, ALLOC>::ConstAccessor
44  (const std::string& name, const std::string& clsname)
45  : ConstAccessor (name, clsname, SG::AuxVarFlags::None)
46 {
47 }
48 
49 
50 /**
51  * @brief Constructor taking an auxid directly.
52  * @param auxid ID for this auxiliary variable.
53  *
54  * Will throw @c SG::ExcAuxTypeMismatch if the types don't match.
55  */
56 template <class T, class ALLOC>
57 inline
58 ConstAccessor<T, ALLOC>::ConstAccessor (const SG::auxid_t auxid)
59  : ConstAccessor (auxid, SG::AuxVarFlags::None)
60 {
61  // cppcheck-suppress missingReturn; false positive
62 }
63 
64 
65 /**
66  * @brief Constructor.
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.
70  *
71  * The name -> auxid lookup is done here.
72  */
73 template <class T, class ALLOC>
74 inline
75 ConstAccessor<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))
80 {
81 }
82 
83 
84 /**
85  * @brief Constructor taking an auxid directly.
86  * @param auxid ID for this auxiliary variable.
87  *
88  * Will throw @c SG::ExcAuxTypeMismatch if the types don't match.
89  */
90 template <class T, class ALLOC>
91 inline
92 ConstAccessor<T, ALLOC>::ConstAccessor (const SG::auxid_t auxid,
93  const SG::AuxVarFlags flags)
94  : m_auxid (auxid)
95 {
96  //cppcheck-suppress missingReturn
97  SG::AuxTypeRegistry::instance().checkAuxID<T, ALLOC> (auxid, flags);
98 }
99 
100 
101 /**
102  * @brief Fetch the variable for one element, as a const reference.
103  * @param e The element for which to fetch the variable.
104  */
105 template <class T, class ALLOC>
106 template <IsConstAuxElement ELT>
107 inline
108 typename ConstAccessor<T, ALLOC>::const_reference_type
109 ConstAccessor<T, ALLOC>::operator() (const ELT& e) const
110 {
111  assert (e.container() != 0);
112  return e.container()->template getData<T> (m_auxid, e.index());
113 }
114 
115 
116 /**
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.
120  *
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.
124  */
125 template <class T, class ALLOC>
126 inline
127 typename ConstAccessor<T, ALLOC>::const_reference_type
128 ConstAccessor<T, ALLOC>::operator()
129  (const AuxVectorData& container,
130  size_t index) const
131 {
132  return container.template getData<T> (m_auxid, index);
133 }
134 
135 
136 /**
137  * @brief Fetch the variable for one element, as a const reference,
138  * with a default.
139  * @param e The element for which to fetch the variable.
140  * @param deflt Default value.
141  *
142  * If this variable is not available, then return @c deflt instead.
143  */
144 template <class T, class ALLOC>
145 template <IsConstAuxElement ELT>
146 inline
147 typename ConstAccessor<T, ALLOC>::const_reference_type
148 ConstAccessor<T, ALLOC>::withDefault (const ELT& e, const T& deflt) const
149 {
150  if (!e.container() || !e.container()->isAvailable (m_auxid)) return deflt;
151  return e.container()->template getData<T> (m_auxid, e.index());
152 }
153 
154 
155 /**
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.
160  *
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.
165  */
166 template <class T, class ALLOC>
167 inline
168 typename ConstAccessor<T, ALLOC>::const_reference_type
169 ConstAccessor<T, ALLOC>::withDefault
170  (const AuxVectorData& container,
171  size_t index,
172  const T& deflt) const
173 {
174  if (!container.isAvailable (m_auxid)) return deflt;
175  return container.template getData<T> (m_auxid, index);
176 }
177 
178 
179 /**
180  * @brief Get a pointer to the start of the auxiliary data array.
181  * @param container The container from which to fetch the variable.
182  */
183 template <class T, class ALLOC>
184 inline
185 typename ConstAccessor<T, ALLOC>::const_container_pointer_type
186 ConstAccessor<T, ALLOC>::getDataArray (const AuxVectorData& container) const
187 {
188  return reinterpret_cast<const_container_pointer_type>
189  (container.getDataArray (m_auxid));
190 }
191 
192 
193 /**
194  * @brief Get a span over the auxilary data array.
195  * @param container The container from which to fetch the variable.
196  */
197 template <class T, class ALLOC>
198 inline
199 typename ConstAccessor<T, ALLOC>::const_span
200 ConstAccessor<T, ALLOC>::getDataSpan (const AuxVectorData& container) const
201 {
202  auto beg = reinterpret_cast<const_container_pointer_type>
203  (container.getDataArray (m_auxid));
204  return const_span (beg, container.size_v());
205 }
206 
207 
208 /**
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.
211  */
212 template <class T, class ALLOC>
213 template <IsConstAuxElement ELT>
214 inline
215 bool
216 ConstAccessor<T, ALLOC>::isAvailable (const ELT& e) const
217 {
218  return e.container() && e.container()->isAvailable (m_auxid);
219 }
220 
221 
222 /**
223  * @brief Test to see if this variable exists in the store.
224  * @param c The container in which to test the variable.
225  */
226 template <class T, class ALLOC>
227 inline
228 bool
229 ConstAccessor<T, ALLOC>::isAvailable (const AuxVectorData& c) const
230 {
231  return c.isAvailable (m_auxid);
232 }
233 
234 
235 /**
236  * @brief Return the aux id for this variable.
237  */
238 template <class T, class ALLOC>
239 inline
240 SG::auxid_t
241 ConstAccessor<T, ALLOC>::auxid() const
242 {
243  return m_auxid;
244 }
245 
246 
247 } // namespace SG