ATLAS Offline Software
ConstAccessor.icc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2002-2024 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  // cppcheck-suppress missingReturn; false positive
48 }
49 
50 
51 /**
52  * @brief Constructor taking an auxid directly.
53  * @param auxid ID for this auxiliary variable.
54  *
55  * Will throw @c SG::ExcAuxTypeMismatch if the types don't match.
56  */
57 template <class T, class ALLOC>
58 inline
59 ConstAccessor<T, ALLOC>::ConstAccessor (const SG::auxid_t auxid)
60  : ConstAccessor (auxid, SG::AuxVarFlags::None)
61 {
62  // cppcheck-suppress missingReturn; false positive
63 }
64 
65 
66 /**
67  * @brief Constructor.
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.
71  *
72  * The name -> auxid lookup is done here.
73  */
74 template <class T, class ALLOC>
75 inline
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))
81 {
82 }
83 
84 
85 /**
86  * @brief Constructor taking an auxid directly.
87  * @param auxid ID for this auxiliary variable.
88  *
89  * Will throw @c SG::ExcAuxTypeMismatch if the types don't match.
90  */
91 template <class T, class ALLOC>
92 inline
93 ConstAccessor<T, ALLOC>::ConstAccessor (const SG::auxid_t auxid,
94  const SG::AuxVarFlags flags)
95  : m_auxid (auxid)
96 {
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 <class ELT>
107 ATH_REQUIRES( IsConstAuxElement<ELT> )
108 inline
109 typename ConstAccessor<T, ALLOC>::const_reference_type
110 ConstAccessor<T, ALLOC>::operator() (const ELT& e) const
111 {
112  assert (e.container() != 0);
113  return e.container()->template getData<T> (m_auxid, e.index());
114 }
115 
116 
117 /**
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.
121  *
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.
125  */
126 template <class T, class ALLOC>
127 inline
128 typename ConstAccessor<T, ALLOC>::const_reference_type
129 ConstAccessor<T, ALLOC>::operator()
130  (const AuxVectorData& container,
131  size_t index) const
132 {
133  return container.template getData<T> (m_auxid, index);
134 }
135 
136 
137 /**
138  * @brief Fetch the variable for one element, as a const reference,
139  * with a default.
140  * @param e The element for which to fetch the variable.
141  * @param deflt Default value.
142  *
143  * If this variable is not available, then return @c deflt instead.
144  */
145 template <class T, class ALLOC>
146 template <class ELT>
147 ATH_REQUIRES( IsConstAuxElement<ELT> )
148 inline
149 typename ConstAccessor<T, ALLOC>::const_reference_type
150 ConstAccessor<T, ALLOC>::withDefault (const ELT& e, const T& deflt) const
151 {
152  if (!e.container() || !e.container()->isAvailable (m_auxid)) return deflt;
153  return e.container()->template getData<T> (m_auxid, e.index());
154 }
155 
156 
157 /**
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.
162  *
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.
167  */
168 template <class T, class ALLOC>
169 inline
170 typename ConstAccessor<T, ALLOC>::const_reference_type
171 ConstAccessor<T, ALLOC>::withDefault
172  (const AuxVectorData& container,
173  size_t index,
174  const T& deflt) const
175 {
176  if (!container.isAvailable (m_auxid)) return deflt;
177  return container.template getData<T> (m_auxid, index);
178 }
179 
180 
181 /**
182  * @brief Get a pointer to the start of the auxiliary data array.
183  * @param container The container from which to fetch the variable.
184  */
185 template <class T, class ALLOC>
186 inline
187 typename ConstAccessor<T, ALLOC>::const_container_pointer_type
188 ConstAccessor<T, ALLOC>::getDataArray (const AuxVectorData& container) const
189 {
190  return reinterpret_cast<const_container_pointer_type>
191  (container.getDataArray (m_auxid));
192 }
193 
194 
195 /**
196  * @brief Get a span over the auxilary data array.
197  * @param container The container from which to fetch the variable.
198  */
199 template <class T, class ALLOC>
200 inline
201 typename ConstAccessor<T, ALLOC>::const_span
202 ConstAccessor<T, ALLOC>::getDataSpan (const AuxVectorData& container) const
203 {
204  auto beg = reinterpret_cast<const_container_pointer_type>
205  (container.getDataArray (m_auxid));
206  return const_span (beg, container.size_v());
207 }
208 
209 
210 /**
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.
213  */
214 template <class T, class ALLOC>
215 template <class ELT>
216 ATH_REQUIRES( IsConstAuxElement<ELT> )
217 inline
218 bool
219 ConstAccessor<T, ALLOC>::isAvailable (const ELT& e) const
220 {
221  return e.container() && e.container()->isAvailable (m_auxid);
222 }
223 
224 
225 /**
226  * @brief Test to see if this variable exists in the store.
227  * @param c The container in which to test the variable.
228  */
229 template <class T, class ALLOC>
230 inline
231 bool
232 ConstAccessor<T, ALLOC>::isAvailable (const AuxVectorData& c) const
233 {
234  return c.isAvailable (m_auxid);
235 }
236 
237 
238 /**
239  * @brief Return the aux id for this variable.
240  */
241 template <class T, class ALLOC>
242 inline
243 SG::auxid_t
244 ConstAccessor<T, ALLOC>::auxid() const
245 {
246  return m_auxid;
247 }
248 
249 
250 } // namespace SG