William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | #include <utility> |
| 3 | |
| 4 | namespace stdplus |
| 5 | { |
| 6 | |
| 7 | template <typename Int, typename Flag = Int> |
| 8 | class BitFlags |
| 9 | { |
| 10 | public: |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 11 | inline explicit BitFlags(Int val = 0) noexcept : val(val) {} |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 12 | |
| 13 | inline BitFlags& set(Flag flag) & noexcept |
| 14 | { |
| 15 | val |= static_cast<Int>(flag); |
| 16 | return *this; |
| 17 | } |
| 18 | inline BitFlags&& set(Flag flag) && noexcept |
| 19 | { |
| 20 | val |= static_cast<Int>(flag); |
| 21 | return std::move(*this); |
| 22 | } |
| 23 | |
| 24 | inline BitFlags& unset(Flag flag) & noexcept |
| 25 | { |
| 26 | val &= ~static_cast<Int>(flag); |
| 27 | return *this; |
| 28 | } |
| 29 | inline BitFlags&& unset(Flag flag) && noexcept |
| 30 | { |
| 31 | val &= ~static_cast<Int>(flag); |
| 32 | return std::move(*this); |
| 33 | } |
| 34 | |
| 35 | explicit inline operator Int() const noexcept |
| 36 | { |
| 37 | return val; |
| 38 | } |
| 39 | |
| 40 | private: |
| 41 | Int val; |
| 42 | }; |
| 43 | |
| 44 | } // namespace stdplus |