ATLAS Offline Software
Accessor.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 Accessor.icc
6  * @author scott snyder <snyder@bnl.gov>
7  * @date Oct, 2023
8  * @brief Helper class to provide 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 Accessor<T, ALLOC>::Accessor (const std::string& name)
28  : ConstAccessor<T, ALLOC> (name)
29 {
30 }
31 
32 
33 /**
34  * @brief Constructor.
35  * @param name Name of this aux variable.
36  * @param clsname The name of its associated class. May be blank.
37  *
38  * The name -> auxid lookup is done here.
39  */
40 template <class T, class ALLOC>
41 inline
42 Accessor<T, ALLOC>::Accessor (const std::string& name,
43  const std::string& clsname)
44  : ConstAccessor<T, ALLOC> (name, clsname)
45 {
46 }
47 
48 
49 /**
50  * @brief Constructor taking an auxid directly.
51  * @param auxid ID for this auxiliary variable.
52  *
53  * Will throw @c SG::ExcAuxTypeMismatch if the types don't match.
54  */
55 template <class T, class ALLOC>
56 inline
57 Accessor<T, ALLOC>::Accessor (const SG::auxid_t auxid)
58  : ConstAccessor<T, ALLOC> (auxid)
59 {
60  // cppcheck-suppress missingReturn
61 }
62 
63 
64 /**
65  * @brief Fetch the variable for one element, as a non-const reference.
66  * @param e The element for which to fetch the variable.
67  */
68 template <class T, class ALLOC>
69 template <class ELT>
70 ATH_REQUIRES( IsAuxElement<ELT> )
71 inline
72 typename Accessor<T, ALLOC>::reference_type
73 Accessor<T, ALLOC>::operator() (ELT& e) const
74 {
75  assert (e.container() != 0);
76  return e.container()->template getData<T> (this->m_auxid, e.index());
77 }
78 
79 
80 /**
81  * @brief Fetch the variable for one element, as a non-const reference.
82  * @param container The container from which to fetch the variable.
83  * @param index The index of the desired element.
84  *
85  * This allows retrieving aux data by container / index.
86  * Looping over the index via this method will be faster then
87  * looping over the elements of the container.
88  */
89 template <class T, class ALLOC>
90 inline
91 typename Accessor<T, ALLOC>::reference_type
92 Accessor<T, ALLOC>::operator() (AuxVectorData& container,
93  size_t index) const
94 {
95  return container.template getData<T> (this->m_auxid, index);
96 }
97 
98 
99 /**
100  * @brief Set the variable for one element.
101  * @param e The element for which to fetch the variable.
102  * @param x The variable value to set.
103  */
104 template <class T, class ALLOC>
105 template <class ELT>
106 ATH_REQUIRES( IsAuxElement<ELT> )
107 inline
108 void Accessor<T, ALLOC>::set (ELT& e, const element_type& x) const
109 {
110  (*this)(e) = x;
111 }
112 
113 
114 /**
115  * @brief Get a pointer to the start of the auxiliary data array.
116  * @param container The container from which to fetch the variable.
117  */
118 template <class T, class ALLOC>
119 inline
120 typename Accessor<T, ALLOC>::container_pointer_type
121 Accessor<T, ALLOC>::getDataArray (AuxVectorData& container) const
122 {
123  return reinterpret_cast<container_pointer_type>
124  (container.getDataArray (this->m_auxid));
125 }
126 
127 
128 /**
129  * @brief Get a span over the auxilary data array.
130  * @param container The container from which to fetch the variable.
131  */
132 template <class T, class ALLOC>
133 inline
134 typename Accessor<T, ALLOC>::span
135 Accessor<T, ALLOC>::getDataSpan (AuxVectorData& container) const
136 {
137  auto beg = reinterpret_cast<container_pointer_type>
138  (container.getDataArray (this->m_auxid));
139  return span (beg, container.size_v());
140 }
141 
142 
143 /**
144  * @brief Test to see if this variable exists in the store and is writable.
145  * @param e An element of the container in which to test the variable.
146  */
147 template <class T, class ALLOC>
148 template <class ELT>
149 ATH_REQUIRES( IsAuxElement<ELT> )
150 inline
151 bool
152 Accessor<T, ALLOC>::isAvailableWritable (ELT& e) const
153 {
154  return e.container() && e.container()->isAvailableWritable (this->m_auxid);
155 }
156 
157 
158 /**
159  * @brief Test to see if this variable exists in the store and is writable.
160  * @param c The container in which to test the variable.
161  */
162 template <class T, class ALLOC>
163 inline
164 bool
165 Accessor<T, ALLOC>::isAvailableWritable (AuxVectorData& c) const
166 {
167  return c.isAvailableWritable (this->m_auxid);
168 }
169 
170 
171 } // namespace SG