Foundation
Loading...
Searching...
No Matches
Enums.hpp
Go to the documentation of this file.
1#pragma once
2#include <cstddef>
3#include <bit>
4namespace Foundation::Core
5{
11 template <typename T, typename Ty> struct BitmaskEnumWrapper {
16 BitmaskEnumWrapper operator=(auto v) { value = static_cast<Ty>(v); return *this; }
17 BitmaskEnumWrapper operator|(auto v) const { return { value | static_cast<Ty>(v) }; }
18 BitmaskEnumWrapper operator&(auto v) const { return { value & static_cast<Ty>(v) }; }
19 BitmaskEnumWrapper operator^(auto v) const { return { value ^ static_cast<Ty>(v) }; }
20 BitmaskEnumWrapper operator~() const { return { ~value }; }
21 BitmaskEnumWrapper& operator|=(auto v) { value |= static_cast<Ty>(v); return *this; }
22 BitmaskEnumWrapper& operator&=(auto v) { value &= static_cast<Ty>(v); return *this; }
23 BitmaskEnumWrapper& operator^=(auto v) { value ^= static_cast<Ty>(v); return *this; }
24 constexpr bool operator ==(auto v) const { return value == static_cast<Ty>(v); }
25 constexpr operator Ty() const { return static_cast<Ty>(value); }
26 constexpr operator T() const { return static_cast<T>(value); }
27 constexpr operator bool() const { return value != 0; }
28 constexpr bool operator<(auto v) const { return value < static_cast<Ty>(v); }
29 [[nodiscard]] constexpr bool is_pow2() const { return (value & (value - 1)) == 0; }
30 [[nodiscard]] constexpr bool is_bitmask() const { return is_pow2(); }
31 [[nodiscard]] constexpr int bit() const {
32 constexpr size_t bits = sizeof(Ty) * 8;
33 return std::countr_zero(value) & (bits - 1);
34 }
35 };
36} // namespace Foundation::Core
47#define BITMASK_ENUM_BEGIN(T,INT_T) \
48enum class T##Bits : INT_T; \
49inline constexpr INT_T to_integer(T##Bits e) { return static_cast<INT_T>(e); } \
50using T = Foundation::Core::BitmaskEnumWrapper<T##Bits, INT_T>; \
51inline T##Bits operator & (T##Bits x, T##Bits y) { return static_cast<T##Bits>(static_cast<INT_T>(x) & static_cast<INT_T>(y)); }; \
52inline T##Bits operator | (T##Bits x, T##Bits y) { return static_cast<T##Bits>(static_cast<INT_T>(x) | static_cast<INT_T>(y)); }; \
53inline T##Bits operator ^ (T##Bits x, T##Bits y) { return static_cast<T##Bits>(static_cast<INT_T>(x) ^ static_cast<INT_T>(y)); }; \
54inline T##Bits operator ~ (T##Bits x) { return static_cast<T##Bits>(~static_cast<INT_T>(x)); }; \
55inline T##Bits& operator &= (T##Bits& x, T##Bits y) { x = static_cast<T##Bits>(x & y); return x; }; \
56inline T##Bits& operator |= (T##Bits& x, T##Bits y) { x = static_cast<T##Bits>(x | y); return x; }; \
57inline T##Bits& operator ^= (T##Bits& x, T##Bits y) { x = static_cast<T##Bits>(x ^ y); return x; }; \
58enum class T##Bits : INT_T {
59
60#define BITMASK_ENUM_END() };
61
82#define ENUM_NAME_CONV_BEGIN(T) \
83inline constexpr const char* format_as(T elem) { \
84 using enum T; \
85 switch (elem) {
86
87#define ENUM_NAME_CONV_END() \
88 default: \
89 return "Unknown"; \
90 } \
91}
92// Shorthand for @ref ENUM_NAME_CONV_BEGIN case statements
93#define ENUM_NAME(E) case E: return #E;
Lock-free atomic primitives and implementations of data structures.
Definition Allocator.hpp:5
T * Construct(Allocator *resource, Args &&...args)
Convenience placement new with object of type T using a Foundation::Core::Allocator.
Definition Allocator.hpp:149
Wrapper for bitmask enum types that provides bitwise operators.
Definition Enums.hpp:11
constexpr bool operator==(auto v) const
Definition Enums.hpp:24
BitmaskEnumWrapper operator~() const
Definition Enums.hpp:20
BitmaskEnumWrapper & operator|=(auto v)
Definition Enums.hpp:21
constexpr bool is_bitmask() const
Definition Enums.hpp:30
BitmaskEnumWrapper(T v)
Definition Enums.hpp:14
BitmaskEnumWrapper()
Definition Enums.hpp:13
BitmaskEnumWrapper operator|(auto v) const
Definition Enums.hpp:17
constexpr int bit() const
Definition Enums.hpp:31
BitmaskEnumWrapper(Ty v)
Definition Enums.hpp:15
BitmaskEnumWrapper & operator&=(auto v)
Definition Enums.hpp:22
BitmaskEnumWrapper operator&(auto v) const
Definition Enums.hpp:18
BitmaskEnumWrapper & operator^=(auto v)
Definition Enums.hpp:23
constexpr bool is_pow2() const
Definition Enums.hpp:29
constexpr bool operator<(auto v) const
Definition Enums.hpp:28
BitmaskEnumWrapper operator=(auto v)
Definition Enums.hpp:16
Ty value
Definition Enums.hpp:12
BitmaskEnumWrapper operator^(auto v) const
Definition Enums.hpp:19