ATLAS Offline Software
TypelessConstAccessor.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/TypelessConstAccessor.icc
6  * @author scott snyder <snyder@bnl.gov>
7  * @date Sep, 2023
8  * @brief Helper class to provide const generic 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 inline
26 TypelessConstAccessor::TypelessConstAccessor (const std::string& name)
27 {
28  SG::AuxTypeRegistry& r = SG::AuxTypeRegistry::instance();
29  m_auxid = r.findAuxID(name);
30  if (m_auxid == null_auxid)
31  SG::throwExcUnknownAuxItem (name);
32  m_eltSize = r.getEltSize (m_auxid);
33 }
34 
35 
36 /**
37  * @brief Constructor.
38  * @param ti The type for this aux data item.
39  * @param name Name of this aux variable.
40  *
41  * The name -> auxid lookup is done here.
42  */
43 inline
44 TypelessConstAccessor::TypelessConstAccessor (const std::type_info& ti,
45  const std::string& name)
46 {
47  SG::AuxTypeRegistry& r = SG::AuxTypeRegistry::instance();
48  m_auxid = r.getAuxID (ti, name);
49  if (m_auxid == null_auxid)
50  SG::throwExcUnknownAuxItem (name, "", &ti);
51  m_eltSize = r.getEltSize (m_auxid);
52 }
53 
54 
55 /**
56  * @brief Constructor.
57  * @param name Name of this aux variable.
58  * @param clsname The name of its associated class. May be blank.
59  *
60  * The name -> auxid lookup is done here.
61  */
62 inline
63 TypelessConstAccessor::TypelessConstAccessor (const std::string& name,
64  const std::string& clsname)
65 {
66  SG::AuxTypeRegistry& r = SG::AuxTypeRegistry::instance();
67  m_auxid = r.findAuxID(name, clsname);
68  if (m_auxid == null_auxid)
69  SG::throwExcUnknownAuxItem (name, clsname);
70  m_eltSize = r.getEltSize (m_auxid);
71 }
72 
73 
74 /**
75  * @brief Constructor.
76  * @param ti The type for this aux data item.
77  * @param name Name of this aux variable.
78  * @param clsname The name of its associated class. May be blank.
79  *
80  * The name -> auxid lookup is done here.
81  */
82 inline
83 TypelessConstAccessor::TypelessConstAccessor (const std::type_info& ti,
84  const std::string& name,
85  const std::string& clsname)
86 {
87  SG::AuxTypeRegistry& r = SG::AuxTypeRegistry::instance();
88  m_auxid = r.getAuxID (ti, name, clsname);
89  if (m_auxid == null_auxid)
90  SG::throwExcUnknownAuxItem (name, clsname, &ti);
91  m_eltSize = r.getEltSize (m_auxid);
92 }
93 
94 
95 /**
96  * @brief Fetch the variable for one element, as a const pointer.
97  * @param e The element for which to fetch the variable.
98  */
99 template <class ELT>
100 ATH_REQUIRES( IsConstAuxElement<ELT> )
101 inline
102 const void*
103 TypelessConstAccessor::operator() (const ELT& e) const
104 {
105  assert (e.container() != 0);
106  return (*this) (*e.container(), e.index());
107 }
108 
109 
110 /**
111  * @brief Fetch the variable for one element, as a const pointer.
112  * @param container The container from which to fetch the variable.
113  * @param index The index of the desired element.
114  *
115  * This allows retrieving aux data by container / index.
116  * Looping over the index via this method will be faster then
117  * looping over the elements of the container.
118  */
119 inline
120 const void*
121 TypelessConstAccessor::operator() (const AuxVectorData& container,
122  size_t index) const
123 {
124  const char* ptr = reinterpret_cast<const char*> (getDataArray (container));
125  return ptr + index * m_eltSize;
126 }
127 
128 
129 /**
130  * @brief Get a pointer to the start of the auxiliary data array.
131  * @param container The container from which to fetch the variable.
132  */
133 inline
134 const void*
135 TypelessConstAccessor::getDataArray (const AuxVectorData& container) const
136 {
137  return container.getDataArray (m_auxid);
138 }
139 
140 
141 /**
142  * @brief Test to see if this variable exists in the store.
143  * @param e An element of the container which to test the variable.
144  */
145 template <class ELT>
146 ATH_REQUIRES( IsConstAuxElement<ELT> )
147 inline
148 bool
149 TypelessConstAccessor::isAvailable (const ELT& e) const
150 {
151  return e.container() && e.container()->isAvailable (m_auxid);
152 }
153 
154 
155 /**
156  * @brief Return the aux id for this variable.
157  */
158 inline
159 SG::auxid_t
160 TypelessConstAccessor::auxid() const
161 {
162  return m_auxid;
163 }
164 
165 
166 } // namespace SG