Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Accessor.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 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 <IsAuxElement ELT>
70 inline
71 typename Accessor<T, ALLOC>::reference_type
72 Accessor<T, ALLOC>::operator() (ELT& e) const
73 {
74  assert (e.container() != 0);
75  return e.container()->template getData<T> (this->m_auxid, e.index());
76 }
77 
78 
79 /**
80  * @brief Fetch the variable for one element, as a non-const reference.
81  * @param container The container from which to fetch the variable.
82  * @param index The index of the desired element.
83  *
84  * This allows retrieving aux data by container / index.
85  * Looping over the index via this method will be faster then
86  * looping over the elements of the container.
87  */
88 template <class T, class ALLOC>
89 inline
90 typename Accessor<T, ALLOC>::reference_type
91 Accessor<T, ALLOC>::operator() (AuxVectorData& container,
92  size_t index) const
93 {
94  return container.template getData<T> (this->m_auxid, index);
95 }
96 
97 
98 /**
99  * @brief Set the variable for one element.
100  * @param e The element for which to fetch the variable.
101  * @param x The variable value to set.
102  */
103 template <class T, class ALLOC>
104 template <IsAuxElement ELT>
105 inline
106 void Accessor<T, ALLOC>::set (ELT& e, const element_type& x) const
107 {
108  (*this)(e) = x;
109 }
110 
111 
112 /**
113  * @brief Get a pointer to the start of the auxiliary data array.
114  * @param container The container from which to fetch the variable.
115  */
116 template <class T, class ALLOC>
117 inline
118 typename Accessor<T, ALLOC>::container_pointer_type
119 Accessor<T, ALLOC>::getDataArray (AuxVectorData& container) const
120 {
121  return reinterpret_cast<container_pointer_type>
122  (container.getDataArray (this->m_auxid));
123 }
124 
125 
126 /**
127  * @brief Get a span over the auxilary data array.
128  * @param container The container from which to fetch the variable.
129  */
130 template <class T, class ALLOC>
131 inline
132 typename Accessor<T, ALLOC>::span
133 Accessor<T, ALLOC>::getDataSpan (AuxVectorData& container) const
134 {
135  auto beg = reinterpret_cast<container_pointer_type>
136  (container.getDataArray (this->m_auxid));
137  return span (beg, container.size_v());
138 }
139 
140 
141 /**
142  * @brief Test to see if this variable exists in the store and is writable.
143  * @param e An element of the container in which to test the variable.
144  */
145 template <class T, class ALLOC>
146 template <IsAuxElement ELT>
147 inline
148 bool
149 Accessor<T, ALLOC>::isAvailableWritable (ELT& e) const
150 {
151  return e.container() && e.container()->isAvailableWritable (this->m_auxid);
152 }
153 
154 
155 /**
156  * @brief Test to see if this variable exists in the store and is writable.
157  * @param c The container in which to test the variable.
158  */
159 template <class T, class ALLOC>
160 inline
161 bool
162 Accessor<T, ALLOC>::isAvailableWritable (AuxVectorData& c) const
163 {
164  return c.isAvailableWritable (this->m_auxid);
165 }
166 
167 
168 } // namespace SG