Extended MaximInterface
Dependents: mbed_DS28EC20_GPIO
Diff: Utilities/FlagSet.hpp
- Revision:
- 6:a8c83a2e6fa4
- Parent:
- 0:f77ad7f72d04
- Child:
- 7:471901a04573
--- a/Utilities/FlagSet.hpp Fri Jan 19 10:25:02 2018 -0600 +++ b/Utilities/FlagSet.hpp Wed Jan 23 13:11:04 2019 -0600 @@ -45,15 +45,20 @@ class reference { public: reference(FlagSet & flagSet, Flag flag) : flagSet(&flagSet), flag(flag) {} + reference & operator=(bool x) { flagSet->set(flag, x); return *this; } + reference & operator=(const reference & x) { return operator=(static_cast<bool>(x)); } + operator bool() const { return flagSet->test(flag); } + bool operator~() const { return reference(*this).flip(); } + reference & flip() { *this = !*this; return *this; @@ -65,6 +70,7 @@ }; FlagSet() : bits() {} + FlagSet(unsigned long val) : bits(val) {} template <typename CharT, typename Traits, typename Alloc> @@ -76,38 +82,53 @@ : bits(str, pos, n) {} bool operator==(const FlagSet & rhs) const { return bits == rhs.bits; } + bool operator!=(const FlagSet & rhs) const { return !operator==(rhs); } - // Element access + /// @name Element access + /// @{ bool operator[](Flag flag) const { return test(flag); } + reference operator[](Flag flag) { return reference(*this, flag); } + bool test(Flag flag) const { return (bits.to_ulong() & flag) == flag; } + bool any() const { return bits.any(); } + bool none() const { return bits.none(); } + size_t count() const { return bits.count(); } + /// @} - // Capacity + /// @name Capacity + /// @{ size_t size() const { return bits.size(); } + /// @} - // Modifiers + /// @name Modifiers + /// @{ FlagSet & operator&=(const FlagSet & other) { bits &= other.bits; return *this; } + FlagSet & operator|=(const FlagSet & other) { bits |= other.bits; return *this; } + FlagSet & operator^=(const FlagSet & other) { bits ^= other.bits; return *this; } + FlagSet operator~() const { return ~bits; } FlagSet & set() { bits.set(); return *this; } + FlagSet & set(Flag flag, bool value = true) { if (value) { bits |= flag; @@ -116,26 +137,34 @@ } return *this; } + FlagSet & reset() { bits.reset(); return *this; } + FlagSet & reset(Flag flag) { return set(flag, false); } + FlagSet & flip() { bits.flip(); return *this; } + FlagSet & flip(Flag flag) { bits ^= flag; return *this; } + /// @} - // Conversions + /// @name Conversions + /// @{ template <typename CharT, typename Traits, typename Allocator> std::basic_string<CharT, Traits, Allocator> to_string() const { return bits.template to_string<CharT, Traits, Allocator>(); } + unsigned long to_ulong() const { return bits.to_ulong(); } + /// @} private: std::bitset<flagBits> bits; @@ -185,4 +214,4 @@ } // namespace MaximInterface -#endif \ No newline at end of file +#endif