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