From 66c3bc21126b7e961226dbc89d8ef58ac99898da Mon Sep 17 00:00:00 2001 From: pajlada Date: Sun, 18 Aug 2024 12:22:41 +0200 Subject: [PATCH] feat: add `FlagsEnum::isEmpty` for checking if a FlagsEnum is empty (#5550) --- CHANGELOG.md | 1 + src/common/FlagsEnum.hpp | 6 ++++++ tests/src/FlagsEnum.cpp | 17 +++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d4c7ffcc..3afc6f95a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/common/FlagsEnum.hpp b/src/common/FlagsEnum.hpp index e49c30aa6..d2ae4ebf7 100644 --- a/src/common/FlagsEnum.hpp +++ b/src/common/FlagsEnum.hpp @@ -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(this->value_) == 0; + } + constexpr T value() const noexcept { return this->value_; diff --git a/tests/src/FlagsEnum.cpp b/tests/src/FlagsEnum.cpp index 4b3e94ba5..0d165c9f0 100644 --- a/tests/src/FlagsEnum.cpp +++ b/tests/src/FlagsEnum.cpp @@ -326,6 +326,23 @@ TEST(FlagsEnum, hasNone) testHasNone(); } +template +consteval void testIsEmpty() +{ + using FE = FlagsEnum; + + 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(); + testIsEmpty(); +} + template constexpr inline auto CONSTRUCTION_VALID = requires() { FlagsEnum{}; };