LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
hexfloat.h
Go to the documentation of this file.
1 
16 #ifndef LARDATA_RECOBASEART_DUMPERS_HEXFLOAT_H
17 #define LARDATA_RECOBASEART_DUMPERS_HEXFLOAT_H 1
18 
19 // C/C++ standard libraries
20 #include <cstdio> // std::snprintf()
21 #include <iosfwd> // std::ostream
22 #include <type_traits> // std::is_same<>
23 #include <utility> // std::forward()
24 
25 namespace lar {
26 
27  namespace details {
28 
29  template <typename T>
31  public:
32  using real_t = T;
33 
34  // sprintf() does not support floats;
35  // it will probably do some bad thing with them
36  static_assert(!std::is_same<std::decay_t<T>, float>::value,
37  "OptionalHexFloatFormatter does not support float values");
38 
39  OptionalHexFloatFormatter(real_t v, bool start_active = true) : active(start_active), value(v)
40  {}
41 
43  template <typename Stream>
44  Stream& operator()(Stream&& os) const
45  {
46  write(std::forward<Stream>(os), value);
47  return os;
48  }
49 
51  template <typename Stream>
52  void write(Stream&& os, real_t v) const
53  {
54  if (active)
55  write_hexfloat(std::forward<Stream>(os), v);
56  else
57  write_standard(std::forward<Stream>(os), v);
58  }
59 
61  template <typename Stream>
62  static void write_hexfloat(Stream&& os, real_t v)
63  {
64  constexpr auto buf_size = 8 * sizeof(real_t) + 1;
65  char buf[buf_size];
66  std::snprintf(buf, buf_size, "%+24.14a", v);
67  os << buf;
68  }
69 
71  template <typename Stream>
72  static void write_standard(Stream&& os, real_t v)
73  {
74  os << v;
75  }
76 
77  private:
78  bool active;
80 
81  }; // OptionalHexFloatFormatter
82 
83  template <typename T>
84  std::ostream& operator<<(std::ostream& os, details::OptionalHexFloatFormatter<T> fmt)
85  {
86  return fmt(os);
87  }
88 
89  } // namespace details
90 
110 
111  public:
113  OptionalHexFloat(bool start_active = true) : active(start_active) {}
114 
116  bool enabled() const { return active; }
117 
119  void enable(bool enable = true) { active = enable; }
120 
122  void disable() { active = false; }
123 
125  template <typename T>
126  auto operator()(T value) const
127  {
128  return formatter_t<T>(value, active);
129  }
130 
132  template <typename T>
133  auto operator()(bool this_active, T value) const
134  {
135  return formatter_t<T>(this_active, value);
136  }
137 
138  private:
139  template <typename T>
141 
142  bool active;
143 
144  }; // OptionalHexFloat
145 
146 } // namespace lar
147 
148 #endif // LARDATA_RECOBASEART_DUMPERS_HEXFLOAT_H
static void write_standard(Stream &&os, real_t v)
Prints the specified value into the specified stream.
Definition: hexfloat.h:72
auto operator()(bool this_active, T value) const
Returns an object that knows what to do with an output stream.
Definition: hexfloat.h:133
OptionalHexFloat(bool start_active=true)
Constructor: if start_active is true, it will print floats in base 16.
Definition: hexfloat.h:113
Stream & operator()(Stream &&os) const
Prints the value set at construction.
Definition: hexfloat.h:44
static void write_hexfloat(Stream &&os, real_t v)
Prints the specified value into the specified stream.
Definition: hexfloat.h:62
void write(Stream &&os, real_t v) const
Prints the specified value into the specified stream.
Definition: hexfloat.h:52
real_t value
the value to be printed
Definition: hexfloat.h:79
void disable()
Disables base 16 printing.
Definition: hexfloat.h:122
auto operator()(T value) const
Returns an object that knows what to do with an output stream.
Definition: hexfloat.h:126
Helper for formatting floats in base 16.
Definition: hexfloat.h:109
void enable(bool enable=true)
Enables base 16 printing (or disables it if enable is false)
Definition: hexfloat.h:119
bool active
whether we are writing in base 16
Definition: hexfloat.h:142
LArSoft-specific namespace.
OptionalHexFloatFormatter(real_t v, bool start_active=true)
Definition: hexfloat.h:39
bool active
whether we are writing in base 16
Definition: hexfloat.h:78
bool enabled() const
Returns whether base 16 printing is enabled.
Definition: hexfloat.h:116