2022-03-27 11:48:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
|
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-01-11 21:41:29 +00:00
|
|
|
#include <AK/Badge.h>
|
2022-03-27 11:48:12 +00:00
|
|
|
#include <AK/DistinctNumeric.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct ArbitrarySizedEnum : public T {
|
|
|
|
using T::T;
|
|
|
|
|
|
|
|
consteval ArbitrarySizedEnum(T v)
|
|
|
|
: T(v)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ArbitrarySizedEnum(T v, Badge<ArbitrarySizedEnum<T>>)
|
|
|
|
: T(v)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Integral X>
|
|
|
|
[[nodiscard]] consteval ArbitrarySizedEnum<T> operator<<(X other) const
|
|
|
|
{
|
|
|
|
return T(this->value() << other);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Integral X>
|
|
|
|
constexpr ArbitrarySizedEnum<T>& operator<<=(X other)
|
|
|
|
{
|
|
|
|
this->value() <<= other;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Integral X>
|
|
|
|
[[nodiscard]] consteval ArbitrarySizedEnum<T> operator>>(X other) const
|
|
|
|
{
|
|
|
|
return T(this->value() >> other);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Integral X>
|
|
|
|
constexpr ArbitrarySizedEnum<T>& operator>>=(X other)
|
|
|
|
{
|
|
|
|
this->value() >>= other;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Integral X>
|
|
|
|
[[nodiscard]] constexpr bool operator==(X other) const
|
|
|
|
{
|
|
|
|
return this->value() == T(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr bool operator==(ArbitrarySizedEnum<T> const& other) const
|
|
|
|
{
|
|
|
|
return this->value() == other.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: The following operators mirror AK_ENUM_BITWISE_OPERATORS.
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr ArbitrarySizedEnum<T> operator|(ArbitrarySizedEnum<T> const& other) const
|
|
|
|
{
|
2023-01-11 21:41:29 +00:00
|
|
|
return { T(this->value() | other.value()), Badge<ArbitrarySizedEnum<T>> {} };
|
2022-03-27 11:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr ArbitrarySizedEnum<T> operator&(ArbitrarySizedEnum<T> const& other) const
|
|
|
|
{
|
2023-01-11 21:41:29 +00:00
|
|
|
return { T(this->value() & other.value()), Badge<ArbitrarySizedEnum<T>> {} };
|
2022-03-27 11:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr ArbitrarySizedEnum<T> operator^(ArbitrarySizedEnum<T> const& other) const
|
|
|
|
{
|
2023-01-11 21:41:29 +00:00
|
|
|
return { T(this->value() ^ other.value()), Badge<ArbitrarySizedEnum<T>> {} };
|
2022-03-27 11:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr ArbitrarySizedEnum<T> operator~() const
|
|
|
|
{
|
2023-01-11 21:41:29 +00:00
|
|
|
return { T(~this->value()), Badge<ArbitrarySizedEnum<T>> {} };
|
2022-03-27 11:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ArbitrarySizedEnum<T>& operator|=(ArbitrarySizedEnum<T> const& other)
|
|
|
|
{
|
|
|
|
this->value() |= other.value();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ArbitrarySizedEnum<T>& operator&=(ArbitrarySizedEnum<T> const& other)
|
|
|
|
{
|
|
|
|
this->value() &= other.value();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ArbitrarySizedEnum<T>& operator^=(ArbitrarySizedEnum<T> const& other)
|
|
|
|
{
|
|
|
|
this->value() ^= other.value();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr bool has_flag(ArbitrarySizedEnum<T> const& mask) const
|
|
|
|
{
|
|
|
|
return (*this & mask) == mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr bool has_any_flag(ArbitrarySizedEnum<T> const& mask) const
|
|
|
|
{
|
|
|
|
return (*this & mask) != 0u;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
AK+Everywhere: Replace DistinctNumeric bool parameters with named ones
This means that rather than this:
```
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false,
false, true, FunctionAddress);
```
We now have this:
```
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, FunctionAddress, Arithmetic,
Comparison, Increment);
```
Which is a lot more readable. :^)
Co-authored-by: Ali Mohammad Pur <mpfard@serenityos.org>
2022-11-10 15:43:23 +00:00
|
|
|
#define AK_MAKE_ARBITRARY_SIZED_ENUM(EnumName, T, ...) \
|
|
|
|
namespace EnumName { \
|
|
|
|
using EnumName = ArbitrarySizedEnum<DistinctNumeric<T, struct __##EnumName##Tag, AK::DistinctNumericFeature::Comparison>>; \
|
|
|
|
using Type = EnumName; \
|
|
|
|
using UnderlyingType = T; \
|
|
|
|
inline constexpr static EnumName __VA_ARGS__; \
|
2022-03-27 11:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2022-03-27 11:48:12 +00:00
|
|
|
using AK::ArbitrarySizedEnum;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|