Compile time bitmask string length check

This commit is contained in:
2021-04-16 22:49:41 +02:00
parent 8fc4c449d3
commit 8b19b2519d
2 changed files with 32 additions and 31 deletions
+13 -12
View File
@@ -9,23 +9,22 @@
class TstCppBitmask;
namespace cpputils {
template<typename T>
class basic_bit_pattern
template<std::size_t BITS_COUNT>
class bit_pattern
{
friend class ::TstCppBitmask;
T expected{};
T mask{};
public:
template<std::size_t Size>
constexpr basic_bit_pattern(const char (&input)[Size])
using value_t = std::uint32_t; // TODO make dependant to BITS_COUNT
constexpr bit_pattern(const char (&input)[BITS_COUNT+1])
{
T cur_bit = (1 << (Size - 2));
value_t cur_bit = (1 << (BITS_COUNT - 1));
for (const char val : input)
{
switch (val)
{
case 0:
case '\0':
return;
case '1':
expected |= cur_bit;
@@ -44,17 +43,19 @@ public:
}
}
constexpr bool match(const T value) const
constexpr bool match(const value_t value) const
{
return (value & mask) == expected;
}
constexpr friend bool operator==(const basic_bit_pattern &l, const basic_bit_pattern &r)
constexpr friend bool operator==(const bit_pattern &l, const bit_pattern &r)
{
return l.expected == r.expected &&
l.mask == r.mask;
}
};
using bit_pattern = basic_bit_pattern<std::uint32_t>;
private:
value_t expected{};
value_t mask{};
};
} // namespace cpputils