Skip to content

Commit

Permalink
feat: add FlagsEnum::isEmpty for checking if a FlagsEnum is empty (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada authored Aug 18, 2024
1 parent f3cae76 commit 66c3bc2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/common/FlagsEnum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ class FlagsEnum
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_;
Expand Down
17 changes: 17 additions & 0 deletions tests/src/FlagsEnum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>{}; };

Expand Down

0 comments on commit 66c3bc2

Please sign in to comment.