mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-10-16 20:38:10 +00:00
Use compile-time flag setting
This commit is contained in:
121
dCommon/Flag.h
121
dCommon/Flag.h
@@ -1,81 +1,158 @@
|
||||
#include "GeneralUtils.h"
|
||||
|
||||
#include <bit>
|
||||
// TODO: Test bounds checking
|
||||
|
||||
/**
|
||||
* A bitset flag class capable of compile-time bounds-checking.
|
||||
* It should be possible to unify its methods with C++23, but until then
|
||||
* use the compile-time overloads preferentially.
|
||||
*/
|
||||
template <typename T>
|
||||
requires std::is_enum_v<T>
|
||||
class Flag {
|
||||
public:
|
||||
// Type aliases
|
||||
using type = T;
|
||||
using underlying_type = std::underlying_type_t<T>;
|
||||
static constexpr auto MAX_FLAG_VAL = std::bit_width(sizeof(underlying_type));
|
||||
|
||||
// Static constants
|
||||
static constexpr auto MAX_FLAG_VAL = sizeof(underlying_type) * CHAR_BIT;
|
||||
|
||||
// Constructors
|
||||
Flag() = default;
|
||||
|
||||
// Run-time constructor
|
||||
constexpr Flag(const T value) : m_Flags{ ConvertFlag(value) } {}
|
||||
|
||||
/**
|
||||
* Sets one or more flags
|
||||
* RUNTIME: Sets one or more flags
|
||||
* @param flag Flag(s) to set
|
||||
*/
|
||||
template <std::same_as<T>... varArg>
|
||||
constexpr void Set(varArg... flag) noexcept {
|
||||
constexpr void Set(const varArg... flag) noexcept {
|
||||
m_Flags |= (ConvertFlag(flag) | ...);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets ONLY have the specified flag(s), clearing all others
|
||||
* COMPILETIME: Sets one or more flags
|
||||
* @param flag Flag(s) to set
|
||||
*/
|
||||
template <T... flag>
|
||||
constexpr void Set() noexcept {
|
||||
m_Flags |= (ConvertFlag<flag>() | ...);
|
||||
}
|
||||
|
||||
/**
|
||||
* RUNTIME: Sets ONLY have the specified flag(s), clearing all others
|
||||
* @param flag Flag(s) to set exclusively
|
||||
*/
|
||||
template <std::same_as<T>... varArg>
|
||||
constexpr void Reset(varArg... flag) {
|
||||
constexpr void Reset(const varArg... flag) {
|
||||
m_Flags = (ConvertFlag(flag) | ...);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsets one or more flags
|
||||
* COMPILETIME: Sets ONLY have the specified flag(s), clearing all others
|
||||
* @param flag Flag(s) to set exclusively
|
||||
*/
|
||||
template <T... flag>
|
||||
constexpr void Reset() noexcept {
|
||||
m_Flags = (ConvertFlag<flag>() | ...);
|
||||
}
|
||||
|
||||
/**
|
||||
* RUNTIME: Unsets one or more flags
|
||||
* @param flag Flag(s) to unset
|
||||
*/
|
||||
template <std::same_as<T>... varArg>
|
||||
constexpr void Unset(varArg... flag) {
|
||||
constexpr void Unset(const varArg... flag) {
|
||||
m_Flags &= ~(ConvertFlag(flag) | ...);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true all the specified flag(s) are set
|
||||
* COMPILETIME: Unsets one or more flags
|
||||
* @param flag Flag(s) to unset
|
||||
*/
|
||||
template <T... flag>
|
||||
constexpr void Unset() noexcept {
|
||||
m_Flags &= ~(ConvertFlag<flag>() | ...);
|
||||
}
|
||||
|
||||
/**
|
||||
* RUNTIME: Returns true all the specified flag(s) are set
|
||||
* @param flag Flag(s) to check
|
||||
*/
|
||||
template <std::same_as<T>... varArg>
|
||||
constexpr bool Has(varArg... flag) const {
|
||||
[[nodiscard]]
|
||||
constexpr bool Has(const varArg... flag) const {
|
||||
return (m_Flags & (ConvertFlag(flag) | ...)) == (ConvertFlag(flag) | ...);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true ONLY the specified flag(s) are set
|
||||
* COMPILETIME: Returns true if all the specified flag(s) are set
|
||||
* @param flag Flag(s) to check
|
||||
*/
|
||||
template <T... flag>
|
||||
[[nodiscard]]
|
||||
constexpr bool Has() const noexcept {
|
||||
return (m_Flags & (ConvertFlag<flag>() | ...)) == (ConvertFlag<flag>() | ...);
|
||||
}
|
||||
|
||||
/**
|
||||
* RUNTIME: Returns true if ONLY the specified flag(s) are set
|
||||
* @param flag Flag(s) to check
|
||||
*/
|
||||
template <std::same_as<T>... varArg>
|
||||
constexpr bool HasOnly(varArg... flag) const {
|
||||
[[nodiscard]]
|
||||
constexpr bool HasOnly(const varArg... flag) const {
|
||||
return m_Flags == (ConvertFlag(flag) | ...);
|
||||
}
|
||||
|
||||
/**
|
||||
* COMPILETIME: Returns true if ONLY the specified flag(s) are set
|
||||
* @param flag Flag(s) to check
|
||||
*/
|
||||
template <T... flag>
|
||||
[[nodiscard]]
|
||||
constexpr bool HasOnly() const noexcept {
|
||||
return m_Flags == (ConvertFlag<flag>() | ...);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the raw value of the flags set
|
||||
*/
|
||||
[[nodiscard]]
|
||||
constexpr T Value() const noexcept {
|
||||
return static_cast<T>(m_Flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Operator overload to allow for '=' assignment
|
||||
*/
|
||||
constexpr Flag& operator=(const T value) {
|
||||
Reset(value);
|
||||
m_Flags = ConvertFlag(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
template <T flag>
|
||||
[[nodiscard]]
|
||||
static consteval underlying_type ConvertFlag() noexcept {
|
||||
constexpr auto flag_val = GeneralUtils::ToUnderlying(flag);
|
||||
static_assert(flag_val <= MAX_FLAG_VAL, "Flag value is too large to set!");
|
||||
|
||||
return flag_val != 0 ? 1 << flag_val : flag_val;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static constexpr underlying_type ConvertFlag(const T flag) {
|
||||
auto flag_val = GeneralUtils::ToUnderlying(flag);
|
||||
if (flag_val != 0) {
|
||||
return 1 << flag_val;
|
||||
|
||||
// This is less-efficient than the compile-time check, but still works
|
||||
// We can probably unify this and the above functions with C++23 and 'if consteval'
|
||||
if (flag_val > MAX_FLAG_VAL) {
|
||||
throw std::runtime_error{ "Flag value is too large to set!" };
|
||||
}
|
||||
// This should theoeretically be possible to do at compile time, but idk how
|
||||
if (std::bit_width(flag_val) > MAX_FLAG_VAL) {
|
||||
throw std::runtime_error{ "Enum value too large to be a flag!" };
|
||||
}
|
||||
return flag_val;
|
||||
|
||||
return flag_val != 0 ? 1 << flag_val : flag_val;
|
||||
}
|
||||
|
||||
underlying_type m_Flags;
|
||||
|
Reference in New Issue
Block a user