Belle II Software development
FixedSizeNamedFloatTuple.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#pragma once
9
10#include <tracking/trackFindingCDC/varsets/NamedFloatTuple.h>
11
12#include <string>
13#include <cassert>
14
15namespace Belle2 {
20 namespace TrackFindingCDC {
21
29 constexpr
30 bool strequal(char const* s1, char const* s2)
31 {
32 return (not * s1 and not * s2) or (*s1 and * s2 and * s1 == *s2 and strequal(s1 + 1, s2 + 1));
33 }
34
45 template <size_t nVars>
46 constexpr int index(const char* const(&names)[nVars], const char* name, size_t iName = 0)
47 {
48 return ((nVars == iName) ?
49 iName :
50 strequal(names[iName], name) ? iName : index(names, name, iName + 1));
51 }
52
63 template<size_t nVars>
64 constexpr
65 int index(const char* (getName(int)),
66 const char* const name,
67 const size_t iName = 0)
68 {
69 return ((nVars == iName) ?
70 iName :
71 strequal(getName(iName), name) ? iName : index<nVars>(getName, name, iName + 1));
72 }
73
74 // *INDENT-OFF*
82 template <class T, size_t N>
83 constexpr size_t size(T (&array)[N] __attribute__((unused)))
84 {
85 return N;
86 }
87 // *INDENT-ON*
88
97 template<class ANames>
99
100 private:
102 static const size_t nVars = ANames::nVars;
103
104 protected:
115 constexpr static int named(const char* name)
116 {
117 return index<nVars>(ANames::getName, name);
118 }
119
120 public:
122 size_t size() const final
123 {
124 return ANames::nVars;
125 }
126
135 int getNameIndex(const char* name) const final
136 {
137 return named(name);
138 }
139
141 std::string getName(int iValue) const final
142 {
143 assert(iValue < (int)nVars);
144 assert(iValue >= 0);
145 return ANames::getName(iValue);
146 }
147
148 public:
150 template<int I>
151 void set(Float_t value)
152 {
153 static_assert(nVars != I, "Requested name not found in names.");
154 assert(I < (int)nVars);
155 assert(I >= 0);
156 m_values[I] = value;
157 }
158
160 void set(int iValue, Float_t value) final
161 {
162 assert(iValue < (int)nVars);
163 assert(iValue >= 0);
164 m_values[iValue] = value;
165 }
166
168 void set(const char* name, Float_t value)
169 {
170 set(named(name), value);
171 }
172
174 template<int I>
175 Float_t get() const
176 {
177 static_assert(nVars != I, "Requested name not found in names.");
178 assert(I < (int)nVars);
179 assert(I >= 0);
180 return m_values[I];
181 }
182
184 Float_t get(int iValue) const final
185 {
186 assert(iValue < (int)nVars);
187 assert(iValue >= 0);
188 return m_values[iValue];
189 }
190
192 Float_t get(const char* name) const
193 {
194 return get(named(name));
195 }
196
198 template<int I>
199 Float_t& var()
200 {
201 static_assert(nVars != I, "Requested name not found in names.");
202 assert(I < (int)nVars);
203 assert(I >= 0);
204 return m_values[I];
205 }
206
208 Float_t& operator[](int iValue) final
209 {
210 assert(iValue < (int)nVars);
211 assert(iValue >= 0);
212 return m_values[iValue];
213 }
214
216 Float_t& operator[](const char* name)
217 {
218 return this->var(named(name));
219 }
220
221 public:
223 Float_t m_values[nVars] = {};
224
225 };
226 }
228}
Generic class that contains a fixed number of named float values.
std::string getName(int iValue) const final
Getter for the ith name.
Float_t & operator[](int iValue) final
Reference getter for the ith value.
static constexpr int named(const char *name)
Static getter for the index from the name.
Float_t & var()
Reference getter for the ith value. Static index version.
Float_t & operator[](const char *name)
Reference getter for the value with the given name.
Float_t get(const char *name) const
Getter for the value with the given name.
Float_t m_values[nVars]
Memory for nVars floating point values.
static const size_t nVars
Number of floating point values represented by this class.
Float_t get() const
Getter for the ith value. Static index version.
Float_t get(int iValue) const final
Getter for the ith value.
void set(Float_t value)
Setter for the ith values. Static index version.
void set(int iValue, Float_t value) final
Setter for the ith value.
size_t size() const final
Getter for number of floating point values represented by this class.
int getNameIndex(const char *name) const final
Getter for the index from a name.
void set(const char *name, Float_t value)
Setter for the value with the given name.
An abstract tuple of float value where each value has an associated name.
TString getName(const TObject *obj)
human-readable name (e.g.
Definition: ObjectInfo.cc:45
Abstract base class for different kinds of events.