ATLAS Offline Software
Loading...
Searching...
No Matches
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
16namespace 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 */
25template <class T, class ALLOC>
26inline
27Accessor<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 */
40template <class T, class ALLOC>
41inline
42Accessor<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 */
55template <class T, class ALLOC>
56inline
57Accessor<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 */
68template <class T, class ALLOC>
69template <IsAuxElement ELT>
70inline
71typename Accessor<T, ALLOC>::reference_type
72Accessor<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 */
88template <class T, class ALLOC>
89inline
90typename Accessor<T, ALLOC>::reference_type
91Accessor<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 */
103template <class T, class ALLOC>
104template <IsAuxElement ELT>
105inline
106void 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 */
116template <class T, class ALLOC>
117inline
118typename Accessor<T, ALLOC>::container_pointer_type
119Accessor<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 */
130template <class T, class ALLOC>
131inline
132typename Accessor<T, ALLOC>::span
133Accessor<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 */
145template <class T, class ALLOC>
146template <IsAuxElement ELT>
147inline
148bool
149Accessor<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 */
159template <class T, class ALLOC>
160inline
161bool
162Accessor<T, ALLOC>::isAvailableWritable (AuxVectorData& c) const
163{
164 return c.isAvailableWritable (this->m_auxid);
165}
166
167
168} // namespace SG