Euphoria
bitfield.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <type_traits>
4 
5 namespace eu::core
6 {
7 
8 
9  template<typename T, typename I = std::underlying_type_t<T>>
10  struct Bitfield
11  {
12  T value;
13 
15  : value(static_cast<T>(0))
16  {
17  }
18 
19  void
20  set(T bit, bool new_value)
21  {
22  if(new_value)
23  {
24  value = static_cast<T>(static_cast<I>(value) | static_cast<I>(bit));
25  }
26  else
27  {
28  value = static_cast<T>(static_cast<I>(value) & ~static_cast<I>(bit));
29  }
30  }
31 
32  void
33  toggle(T bit)
34  {
35  value = static_cast<T>(static_cast<I>(value) ^ static_cast<I>(bit));
36  }
37 
38  void
40  {
41  value = static_cast<T>(0);
42  }
43 
44  [[nodiscard]] bool
45  get(T bit) const
46  {
47  const I i = static_cast<I>(value) & static_cast<I>(bit);
48  return bit == static_cast<T>(i);
49  }
50 
51  [[nodiscard]] bool
53  {
54  return value != static_cast<T>(0);
55  }
56  };
57 
58 }
bool has_any_values() const
Definition: bitfield.h:52
bool get(T bit) const
Definition: bitfield.h:45
void toggle(T bit)
Definition: bitfield.h:33
void set(T bit, bool new_value)
Definition: bitfield.h:20