feat: add FlagsEnum::isEmpty for checking if a FlagsEnum is empty (#5550)

This commit is contained in:
pajlada 2024-08-18 12:22:41 +02:00 committed by GitHub
parent f3cae76abf
commit 66c3bc2112
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 0 deletions

View file

@ -64,6 +64,7 @@
- Dev: Cleanly exit on shutdown. (#5537)
- Dev: Renamed threads created by Chatterino on Linux and Windows. (#5538, #5539, #5544)
- Dev: Refactored a few `#define`s into `const(expr)` and cleaned includes. (#5527)
- Dev: Added `FlagsEnum::isEmpty`. (#5550)
- Dev: Prepared for Qt 6.8 by addressing some deprecations. (#5529)
## 2.5.1

View file

@ -132,6 +132,12 @@ public:
return this->hasNone(FlagsEnum{flags...});
}
/// Returns true if the enum has no flag set (i.e. its underlying value is 0)
constexpr bool isEmpty() const noexcept
{
return static_cast<Int>(this->value_) == 0;
}
constexpr T value() const noexcept
{
return this->value_;

View file

@ -326,6 +326,23 @@ TEST(FlagsEnum, hasNone)
testHasNone<BasicUnscoped>();
}
template <typename E>
consteval void testIsEmpty()
{
using FE = FlagsEnum<E>;
static_assert(FE{}.isEmpty());
static_assert(!FE{E::Foo}.isEmpty());
static_assert(FE{E::None}.isEmpty());
static_assert(!FE{E::Foo, E::Waldo}.isEmpty());
}
TEST(FlagsEnum, isEmpty)
{
testIsEmpty<BasicScoped>();
testIsEmpty<BasicUnscoped>();
}
template <typename T>
constexpr inline auto CONSTRUCTION_VALID = requires() { FlagsEnum<T>{}; };