Foundation
Loading...
Searching...
No Matches
Classes | Namespaces | Macros
Enums.hpp File Reference
#include <cstddef>
#include <bit>

Go to the source code of this file.

Classes

struct  Foundation::Bits::BitmaskEnumWrapper< T, Ty >
 Wrapper for bitmask enum types that provides bitwise operators. More...
 

Namespaces

namespace  Foundation
 
namespace  Foundation::Bits
 Header-only convenience utilities.
 

Macros

#define BITMASK_ENUM_BEGIN(T, INT_T)
 Defines a bitmask enum type {T}Bits with underlying integer type INT_T whilst defining a wrapper class of type T that provides bitwise operators.
 
#define BITMASK_ENUM_END()   };
 
#define ENUM_NAME_CONV_BEGIN(T)
 Defines convince to_string() method and format_as() [fmt] for the respective enum class Example usage:
 
#define ENUM_NAME_CONV_END()
 
#define ENUM_NAME(E)   case E: return #E;
 

Macro Definition Documentation

◆ BITMASK_ENUM_BEGIN

#define BITMASK_ENUM_BEGIN (   T,
  INT_T 
)
Value:
enum class T##Bits : INT_T; \
inline constexpr INT_T to_integer(T##Bits e) { return static_cast<INT_T>(e); } \
inline T##Bits operator & (T##Bits x, T##Bits y) { return static_cast<T##Bits>(static_cast<INT_T>(x) & static_cast<INT_T>(y)); }; \
inline T##Bits operator | (T##Bits x, T##Bits y) { return static_cast<T##Bits>(static_cast<INT_T>(x) | static_cast<INT_T>(y)); }; \
inline T##Bits operator ^ (T##Bits x, T##Bits y) { return static_cast<T##Bits>(static_cast<INT_T>(x) ^ static_cast<INT_T>(y)); }; \
inline T##Bits operator ~ (T##Bits x) { return static_cast<T##Bits>(~static_cast<INT_T>(x)); }; \
inline T##Bits& operator &= (T##Bits& x, T##Bits y) { x = static_cast<T##Bits>(x & y); return x; }; \
inline T##Bits& operator |= (T##Bits& x, T##Bits y) { x = static_cast<T##Bits>(x | y); return x; }; \
inline T##Bits& operator ^= (T##Bits& x, T##Bits y) { x = static_cast<T##Bits>(x ^ y); return x; }; \
enum class T##Bits : INT_T {
std::unique_ptr< T, StlDeleter< T > > UniquePtr
std::unique_ptr with custom deleter that uses a Foundation::Core::Allocator to deallocate memory.
Definition Allocator.hpp:161

Defines a bitmask enum type {T}Bits with underlying integer type INT_T whilst defining a wrapper class of type T that provides bitwise operators.

Parameters
TEnum name
INT_TUnderlying integer type (e.g. uint32_t, uint64_t)
Note
The actual enum values must be defined between BITMASK_ENUM_BEGIN and BITMASK_ENUM_END.
The enum values should be powers of two, or combinations thereof.
This also defines the to_integer() function for converting the enum to its underlying integer type.
This also defines the bitwise operators for the enum type.

◆ BITMASK_ENUM_END

#define BITMASK_ENUM_END ( )    };

◆ ENUM_NAME

#define ENUM_NAME (   E)    case E: return #E;

◆ ENUM_NAME_CONV_BEGIN

#define ENUM_NAME_CONV_BEGIN (   T)
Value:
inline constexpr const char* format_as(T elem) { \
using enum T; \
switch (elem) {

Defines convince to_string() method and format_as() [fmt] for the respective enum class Example usage:

ENUM_NAME(Red)
ENUM_NAME(Green)
ENUM_NAME(Blue)
#define ENUM_NAME_CONV_BEGIN(T)
Defines convince to_string() method and format_as() [fmt] for the respective enum class Example usage...
Definition Enums.hpp:84
#define ENUM_NAME_CONV_END()
Definition Enums.hpp:89
#define ENUM_NAME(E)
Definition Enums.hpp:95

The enum can be subsequently used as:

Color c = Color::Red;
std::cout << to_string(c); // prints "Red"
fmt::print("Color is {}\n", c); // prints "Color is Red"
Parameters
TEnum type
Note
The actual enum values must be defined between ENUM_NAME_CONV_BEGIN and ENUM_NAME_CONV_END.
If the enum value is not recognized (not defined), "Unknown" is returned.

◆ ENUM_NAME_CONV_END

#define ENUM_NAME_CONV_END ( )
Value:
default: \
return "Unknown"; \
} \
}