diff options
author | Chris Robinson <[email protected]> | 2019-06-08 23:33:59 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2019-06-08 23:33:59 -0700 |
commit | c9ba7ba19353c9cf55dec668b2b059c267d1b0ea (patch) | |
tree | a4aaccb39a969c1272f29f9fffc793655fd341c4 | |
parent | b6ce793f849c25cb9ddf992e2b647d3babcaf6cb (diff) |
Add a bitfield class for indexed, auto-sized flags
-rw-r--r-- | common/albyte.h | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/common/albyte.h b/common/albyte.h index d1459e96..8257560e 100644 --- a/common/albyte.h +++ b/common/albyte.h @@ -1,6 +1,8 @@ #ifndef AL_BYTE_H #define AL_BYTE_H +#include <cstddef> +#include <limits> #include <type_traits> namespace al { @@ -63,6 +65,29 @@ AL_DECL_OP(^) #undef AL_DECL_OP +template<size_t N> +class bitfield { + static constexpr size_t bits_per_byte{std::numeric_limits<unsigned char>::digits}; + static constexpr size_t NumElems{(N+bits_per_byte-1) / bits_per_byte}; + + byte vals[NumElems]{}; + +public: + void set(size_t b) noexcept { vals[b/bits_per_byte] |= 1 << (b%bits_per_byte); } + void unset(size_t b) noexcept { vals[b/bits_per_byte] &= ~(1 << (b%bits_per_byte)); } + bool get(size_t b) const noexcept + { return (vals[b/bits_per_byte] & (1 << (b%bits_per_byte))) != byte{}; } + + template<typename ...Args, REQUIRES(sizeof...(Args) > 0)> + void set(size_t b, Args ...args) noexcept + { + set(b); + /* Trick for calling set() on each element of the parameter pack. */ + using CharArray = char[sizeof...(Args)]; + (void)(CharArray{ (set(args),'\0')... }); + } +}; + #undef REQUIRES } // namespace al |