ATLAS Offline Software
JaggedVecConstAccessor.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/JaggedVecConstAccessor.icc
6  * @author scott snyder <snyder@bnl.gov>
7  * @date Apr, 2024
8  * @brief Helper class to provide constant type-safe access to aux data.
9  */
10 
11 
12 #include "AthContainers/AuxElement.h"
13 #include "AthContainers/AuxTypeRegistry.h"
14 #include "AthContainers/exceptions.h"
15 
16 
17 namespace SG {
18 
19 
20 /**
21  * @brief Constructor.
22  * @param name Name of this aux variable.
23  *
24  * The name -> auxid lookup is done here.
25  */
26 template <class PAYLOAD_T, class ALLOC>
27 inline
28 ConstAccessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::ConstAccessor (const std::string& name)
29  : ConstAccessor (name, "", AuxVarFlags::None)
30 {
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 PAYLOAD_T, class ALLOC>
42 inline
43 ConstAccessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::ConstAccessor
44  (const std::string& name, const std::string& clsname)
45  : ConstAccessor (name, clsname, 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 PAYLOAD_T, class ALLOC>
58 inline
59 ConstAccessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::ConstAccessor (const SG::auxid_t auxid)
60 {
61  AuxTypeRegistry& r = AuxTypeRegistry::instance();
62  this->m_auxid = auxid;
63  r.checkAuxID<Elt_t, ALLOC> (this->m_auxid);
64  this->m_linkedAuxid = r.linkedVariable (this->m_auxid);
65  if (this->m_linkedAuxid == static_cast<uint32_t>(null_auxid)) {
66  throw SG::ExcNoLinkedVar (auxid, typeid (Payload_t));
67  }
68 }
69 
70 
71 /**
72  * @brief Constructor.
73  * @param name Name of this aux variable.
74  * @param clsname The name of its associated class. May be blank.
75  * @param flags Optional flags qualifying the type. See AuxTypeRegsitry.
76  *
77  * The name -> auxid lookup is done here.
78  */
79 template <class PAYLOAD_T, class ALLOC>
80 inline
81 ConstAccessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::ConstAccessor
82  (const std::string& name,
83  const std::string& clsname,
84  const AuxVarFlags flags)
85 {
86  AuxTypeRegistry& r = AuxTypeRegistry::instance();
87  m_linkedAuxid = r.getAuxID<Payload_t, PayloadAlloc_t> (AuxTypeRegistry::linkedName (name),
88  clsname,
89  flags | AuxVarFlags::Linked);
90  m_auxid = r.getAuxID<Elt_t, ALLOC> (name, clsname, flags, m_linkedAuxid);
91 }
92 
93 
94 /**
95  * @brief Constructor.
96  * @param b Another accessor from which to copy the auxids.
97  */
98 template <class PAYLOAD_T, class ALLOC>
99 inline
100 ConstAccessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::ConstAccessor (const detail::LinkedVarAccessorBase& b)
101  : detail::LinkedVarAccessorBase (b)
102 {
103  // cppcheck-suppress missingReturn; false positive
104 }
105 
106 
107 /**
108  * @brief Fetch the variable for one element, as a const reference.
109  * @param e The element for which to fetch the variable.
110  */
111 template <class PAYLOAD_T, class ALLOC>
112 template <IsConstAuxElement ELT>
113 inline
114 auto
115 ConstAccessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::operator() (const ELT& e) const -> const element_type
116 {
117  const AuxVectorData* container = e.container();
118  assert (container != nullptr);
119  return (*this)(*container, e.index());
120 }
121 
122 
123 /**
124  * @brief Fetch the variable for one element, as a const reference.
125  * @param container The container from which to fetch the variable.
126  * @param index The index of the desired element.
127  *
128  * This allows retrieving aux data by container / index.
129  */
130 template <class PAYLOAD_T, class ALLOC>
131 inline
132 auto
133 ConstAccessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::operator()
134  (const AuxVectorData& container, size_t index) const -> const element_type
135 {
136  const Elt_t& jelt = container.template getData<Elt_t> (m_auxid, index);
137  if (jelt.end() == 0) return element_type();
138  const Payload_t* payload = this->getPayloadArray (container);
139  return element_type (payload+jelt.begin (index), payload+jelt.end());
140 }
141 
142 
143 /**
144  * @brief Get a pointer to the start of the array of @c JaggedVecElt objects.
145  * @param container The container from which to fetch the variable.
146  */
147 template <class PAYLOAD_T, class ALLOC>
148 inline
149 auto
150 ConstAccessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::getEltArray (const AuxVectorData& container) const
151  -> const Elt_t*
152 {
153  return reinterpret_cast<const Elt_t*> (container.getDataArray (m_auxid));
154 }
155 
156 
157 /**
158  * @brief Get a pointer to the start of the payload array.
159  * @param container The container from which to fetch the variable.
160  */
161 template <class PAYLOAD_T, class ALLOC>
162 inline
163 auto
164 ConstAccessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::getPayloadArray (const AuxVectorData& container) const
165  -> const Payload_t*
166 {
167  return reinterpret_cast<const Payload_t*>
168  (container.getDataArray (m_linkedAuxid));
169 }
170 
171 
172 /**
173  * @brief Get a span over the array of @c JaggedVecElt objects.
174  * @param container The container from which to fetch the variable.
175  */
176 template <class PAYLOAD_T, class ALLOC>
177 inline
178 auto
179 ConstAccessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::getEltSpan (const AuxVectorData& container) const
180  -> const_Elt_span
181 {
182  auto beg = reinterpret_cast<const Elt_t*> (container.getDataArray (m_auxid));
183  return const_Elt_span (beg, container.size_v());
184 }
185 
186 
187 /**
188  * @brief Get a span over the payload vector.
189  * @param container The container from which to fetch the variable.
190  */
191 template <class PAYLOAD_T, class ALLOC>
192 inline
193 auto
194 ConstAccessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::getPayloadSpan (const AuxVectorData& container) const
195  -> const_Payload_span
196 {
197  const SG::AuxDataSpanBase* sp = container.getDataSpan (m_linkedAuxid);
198  return const_Payload_span (reinterpret_cast<const Payload_t*>(sp->beg),
199  sp->size);
200 }
201 
202 
203 /**
204  * @brief Get a span over spans representing the jagged vector.
205  * @param container The container from which to fetch the variable.
206  */
207 template <class PAYLOAD_T, class ALLOC>
208 inline
209 auto
210 ConstAccessor<JaggedVecElt<PAYLOAD_T>, ALLOC>::getDataSpan (const AuxVectorData& container) const
211  -> const_span
212 {
213  const_Elt_span elt_span = getEltSpan (container);
214  return const_span (elt_span,
215  ConstConverter_t (elt_span.data(),
216  *container.getDataSpan (m_linkedAuxid)));
217 }
218 
219 
220 } // namespace SG