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 }
61 
62 
63 /**
64  * @brief Fetch the variable for one element, as a non-const reference.
65  * @param e The element for which to fetch the variable.
66  */
67 template <class T, class ALLOC>
68 template <class ELT>
69 ATH_REQUIRES( 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 <class ELT>
105 ATH_REQUIRES( IsAuxElement<ELT> )
106 inline
107 void Accessor<T, ALLOC>::set (ELT& e, const element_type& x) const
108 {
109  (*this)(e) = x;
110 }
111 
112 
113 /**
114  * @brief Get a pointer to the start of the auxiliary data array.
115  * @param container The container from which to fetch the variable.
116  */
117 template <class T, class ALLOC>
118 inline
119 typename Accessor<T, ALLOC>::container_pointer_type
120 Accessor<T, ALLOC>::getDataArray (AuxVectorData& container) const
121 {
122  return reinterpret_cast<container_pointer_type>
123  (container.getDataArray (this->m_auxid));
124 }
125 
126 
127 /**
128  * @brief Get a span over the auxilary data array.
129  * @param container The container from which to fetch the variable.
130  */
131 template <class T, class ALLOC>
132 inline
133 typename Accessor<T, ALLOC>::span
134 Accessor<T, ALLOC>::getDataSpan (AuxVectorData& container) const
135 {
136  auto beg = reinterpret_cast<container_pointer_type>
137  (container.getDataArray (this->m_auxid));
138  return span (beg, container.size_v());
139 }
140 
141 
142 /**
143  * @brief Test to see if this variable exists in the store and is writable.
144  * @param e An element of the container which to test the variable.
145  */
146 template <class T, class ALLOC>
147 template <class ELT>
148 ATH_REQUIRES( IsAuxElement<ELT> )
149 inline
150 bool
151 Accessor<T, ALLOC>::isAvailableWritable (ELT& e) const
152 {
153  return e.container() && e.container()->isAvailableWritable (this->m_auxid);
154 }
155 
156 
157 } // namespace SG