Belle II Software  release-06-02-00
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 
15 namespace 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  assert(iValue < (int)nVars);
162  assert(iValue >= 0);
163  m_values[iValue] = value;
164  }
165 
167  void set(const char* name, Float_t value)
168  {
169  set(named(name), value);
170  }
171 
173  template<int I>
174  Float_t get() const
175  {
176  static_assert(nVars != I, "Requested name not found in names.");
177  assert(I < (int)nVars);
178  assert(I >= 0);
179  return m_values[I];
180  }
181 
183  Float_t get(int iValue) const final
184  {
185  assert(iValue < (int)nVars);
186  assert(iValue >= 0);
187  return m_values[iValue];
188  }
189 
191  Float_t get(const char* name) const
192  {
193  return get(named(name));
194  }
195 
197  template<int I>
198  Float_t& var()
199  {
200  static_assert(nVars != I, "Requested name not found in names.");
201  assert(I < (int)nVars);
202  assert(I >= 0);
203  return m_values[I];
204  }
205 
207  Float_t& operator[](int iValue) final {
208  assert(iValue < (int)nVars);
209  assert(iValue >= 0);
210  return m_values[iValue];
211  }
212 
214  Float_t& operator[](const char* name)
215  {
216  return this->var(named(name));
217  }
218 
219  public:
221  Float_t m_values[nVars] = {};
222 
223  };
224  }
226 }
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 get(const char *name) const
Getter for the value with the given name.
constexpr static int named(const char *name)
Static getter for the index from the 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.
Float_t & operator[](const char *name)
Reference getter for the value with the given name.
void set(Float_t value)
Setter for the ith values. Static index version.
Float_t & var()
Reference getter for the ith value. Static index version.
Float_t & operator[](int iValue) final
Reference getter for the ith value.
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.