diff options
author | Chris Robinson <[email protected]> | 2019-06-09 02:20:30 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2019-06-09 02:20:30 -0700 |
commit | 90d25e5187ca50a6e978603fabb6395035ad0db5 (patch) | |
tree | b44cddd07f7f17227e60b049659bd999a11e78fb /common/albyte.h | |
parent | 2e154069c6aa3bfc9d00d420e2f508a4127dd649 (diff) |
Make sure the bitfield indices are constants
Diffstat (limited to 'common/albyte.h')
-rw-r--r-- | common/albyte.h | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/common/albyte.h b/common/albyte.h index 8257560e..f95b92d7 100644 --- a/common/albyte.h +++ b/common/albyte.h @@ -73,18 +73,30 @@ class bitfield { 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<size_t b> + inline void set() noexcept + { + static_assert(b < N, "Bit index out of range"); + vals[b/bits_per_byte] |= 1 << (b%bits_per_byte); + } + template<size_t b> + inline void unset() noexcept + { + static_assert(b < N, "Bit index out of range"); + vals[b/bits_per_byte] &= ~(1 << (b%bits_per_byte)); + } + template<size_t b> + inline bool get() const noexcept + { + static_assert(b < N, "Bit index out of range"); + 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 + template<size_t b, size_t ...args, REQUIRES(sizeof...(args) > 0)> + void set() noexcept { - set(b); - /* Trick for calling set() on each element of the parameter pack. */ - using CharArray = char[sizeof...(Args)]; - (void)(CharArray{ (set(args),'\0')... }); + set<b>(); + set<args...>(); } }; |