CppELib 1.7.0
Loading...
Searching...
No Matches
BitPattern.h
Go to the documentation of this file.
1#ifndef CONTAINER_BIT_PATTERN_H_INCLUDED
2#define CONTAINER_BIT_PATTERN_H_INCLUDED
3
4#include <cstddef>
6
7namespace Container {
8
16template <typename T>
18private:
19 T m_data;
20
21public:
22 BitPattern() : m_data(0U) {}
23 BitPattern(T pattern) : m_data(pattern) {} // cppcheck-suppress noExplicitConstructor
24
25 std::size_t size() const
26 {
27 return sizeof m_data * 8U;
28 }
29
31 {
32 m_data = static_cast<T>(~static_cast<T>(0U));
33 return *this;
34 }
35
36 BitPattern& set(std::size_t pos)
37 {
38 DEBUG_ASSERT(pos < (sizeof m_data * 8U));
39 m_data |= static_cast<T>(static_cast<T>(1U) << pos);
40 return *this;
41 }
42
44 {
45 m_data = 0U;
46 return *this;
47 }
48
49 BitPattern& reset(std::size_t pos)
50 {
51 DEBUG_ASSERT(pos < (sizeof m_data * 8U));
52 m_data &= static_cast<T>(~(static_cast<T>(1U) << pos));
53 return *this;
54 }
55
57 {
58 m_data = static_cast<T>(~m_data);
59 return *this;
60 }
61
62 BitPattern& flip(std::size_t pos)
63 {
64 DEBUG_ASSERT(pos < (sizeof m_data * 8U));
65 m_data ^= static_cast<T>(static_cast<T>(1U) << pos);
66 return *this;
67 }
68
69 operator const T&() const
70 {
71 return m_data;
72 }
73
74 operator T&()
75 {
76 return m_data;
77 }
78
79 bool test(std::size_t pos) const
80 {
81 DEBUG_ASSERT(pos < (sizeof m_data * 8U));
82 return (m_data & static_cast<T>(static_cast<T>(1U) << pos)) != 0U;
83 }
84
85 bool all() const
86 {
87 return m_data == static_cast<T>(~static_cast<T>(0U));
88 }
89
90 bool any() const
91 {
92 return m_data != 0U;
93 }
94
95 bool none() const
96 {
97 return m_data == 0U;
98 }
99
100};
101
102}
103
104#endif // CONTAINER_BIT_PATTERN_H_INCLUDED
#define DEBUG_ASSERT(x)
The same as CHECK_ASSERT() macro.
Definition Assertion.h:39
The container with bitwise operation like std::bitset.
Definition BitPattern.h:17
bool any() const
Definition BitPattern.h:90
bool none() const
Definition BitPattern.h:95
BitPattern & set()
Definition BitPattern.h:30
BitPattern & flip(std::size_t pos)
Definition BitPattern.h:62
BitPattern & flip()
Definition BitPattern.h:56
BitPattern(T pattern)
Definition BitPattern.h:23
BitPattern & reset(std::size_t pos)
Definition BitPattern.h:49
BitPattern & set(std::size_t pos)
Definition BitPattern.h:36
BitPattern & reset()
Definition BitPattern.h:43
bool all() const
Definition BitPattern.h:85
std::size_t size() const
Definition BitPattern.h:25
bool test(std::size_t pos) const
Definition BitPattern.h:79
BitPattern()
Definition BitPattern.h:22
Definition Array.h:10