2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-12-21 21:26:25 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <initializer_list>
|
|
|
|
#include <type_traits>
|
|
|
|
|
2023-01-21 12:06:20 +00:00
|
|
|
// NOLINTBEGIN(bugprone-macro-parentheses, cppcoreguidelines-macro-usage)
|
|
|
|
|
2022-12-21 21:26:25 +00:00
|
|
|
#define DEFINE_FLAGS_OPERATORS(FlagType) \
|
|
|
|
constexpr inline Flags<FlagType> operator|(FlagType lhs, FlagType rhs) noexcept \
|
|
|
|
{ \
|
|
|
|
return { lhs, rhs }; \
|
|
|
|
}
|
|
|
|
|
2023-01-21 12:06:20 +00:00
|
|
|
// NOLINTEND(bugprone-macro-parentheses, cppcoreguidelines-macro-usage)
|
|
|
|
|
2022-12-21 21:26:25 +00:00
|
|
|
template<typename T>
|
|
|
|
class Flags
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using FlagType = T;
|
2023-02-03 16:12:48 +00:00
|
|
|
using ValueType = std::make_unsigned_t<std::underlying_type_t<FlagType>>;
|
2022-12-21 21:26:25 +00:00
|
|
|
|
|
|
|
static_assert(std::is_enum_v<FlagType> && !std::is_convertible_v<FlagType, ValueType>);
|
|
|
|
|
|
|
|
public:
|
|
|
|
constexpr Flags() noexcept = default;
|
|
|
|
|
2023-02-03 16:12:48 +00:00
|
|
|
// NOLINTNEXTLINE(hicpp-explicit-conversions)
|
2022-12-21 21:26:25 +00:00
|
|
|
constexpr Flags(FlagType flag) noexcept
|
|
|
|
{
|
|
|
|
set(flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Flags(std::initializer_list<FlagType> flags) noexcept
|
|
|
|
{
|
|
|
|
for (auto const flag : flags)
|
|
|
|
{
|
|
|
|
set(flag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-21 12:06:20 +00:00
|
|
|
[[nodiscard]] constexpr bool none() const noexcept
|
2022-12-21 21:26:25 +00:00
|
|
|
{
|
|
|
|
return value_ == 0;
|
|
|
|
}
|
|
|
|
|
2023-01-21 12:06:20 +00:00
|
|
|
[[nodiscard]] constexpr bool any() const noexcept
|
2022-12-21 21:26:25 +00:00
|
|
|
{
|
|
|
|
return !none();
|
|
|
|
}
|
|
|
|
|
2023-01-21 12:06:20 +00:00
|
|
|
[[nodiscard]] constexpr bool test(FlagType flag) const noexcept
|
2022-12-21 21:26:25 +00:00
|
|
|
{
|
|
|
|
return (value_ & get_mask(flag)) != 0;
|
|
|
|
}
|
|
|
|
|
2023-01-21 12:06:20 +00:00
|
|
|
[[nodiscard]] constexpr bool test(Flags rhs) const noexcept
|
2022-12-21 21:26:25 +00:00
|
|
|
{
|
|
|
|
return (value_ & rhs.value_) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void set(FlagType flag) noexcept
|
|
|
|
{
|
|
|
|
value_ |= get_mask(flag);
|
|
|
|
}
|
|
|
|
|
2023-01-30 23:25:23 +00:00
|
|
|
[[nodiscard]] constexpr Flags operator|(Flags rhs) const noexcept
|
2022-12-21 21:26:25 +00:00
|
|
|
{
|
|
|
|
return Flags(value_ | rhs.value_);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Flags& operator|=(Flags rhs) noexcept
|
|
|
|
{
|
|
|
|
value_ |= rhs.value_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-01-21 12:06:20 +00:00
|
|
|
[[nodiscard]] constexpr Flags operator~() const noexcept
|
2022-12-21 21:26:25 +00:00
|
|
|
{
|
|
|
|
return Flags(~value_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
constexpr explicit Flags(ValueType value) noexcept
|
|
|
|
: value_(value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-21 12:06:20 +00:00
|
|
|
[[nodiscard]] static constexpr ValueType get_mask(FlagType flag) noexcept
|
2022-12-21 21:26:25 +00:00
|
|
|
{
|
|
|
|
return ValueType{ 1 } << static_cast<ValueType>(flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ValueType value_ = {};
|
|
|
|
};
|