wut/include/wut_types.h
GaryOderNichts 48b6c3b32a
wut_types: Implement more operators for WUT_ENUM_BITMASK_TYPE (#369)
* wut_types: Implement more operators for `WUT_ENUM_BITMASK_TYPE`

* wut_types: Use `std::underlying_type_t`

* wut_types: Use an anonymous namespace
2024-06-09 12:56:37 +02:00

47 lines
1.8 KiB
C++

#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdalign.h>
typedef int32_t BOOL;
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#if defined(__cplusplus) && (__cplusplus >= 201402L)
#include <type_traits>
#define WUT_ENUM_BITMASK_TYPE(_type) \
extern "C++" { namespace { \
constexpr inline _type operator~(_type lhs) { \
return static_cast<_type>(~static_cast<std::underlying_type_t<_type>>(lhs)); \
} \
constexpr inline _type operator&(_type lhs, _type rhs) { \
return static_cast<_type>(static_cast<std::underlying_type_t<_type>>(lhs) & static_cast<std::underlying_type_t<_type>>(rhs)); \
} \
constexpr inline _type operator|(_type lhs, _type rhs) { \
return static_cast<_type>(static_cast<std::underlying_type_t<_type>>(lhs) | static_cast<std::underlying_type_t<_type>>(rhs)); \
} \
constexpr inline _type operator^(_type lhs, _type rhs) { \
return static_cast<_type>(static_cast<std::underlying_type_t<_type>>(lhs) ^ static_cast<std::underlying_type_t<_type>>(rhs)); \
} \
inline _type& operator&=(_type &lhs, _type rhs) { \
return reinterpret_cast<_type&>(reinterpret_cast<std::underlying_type_t<_type>&>(lhs) &= static_cast<std::underlying_type_t<_type>>(rhs)); \
} \
inline _type& operator|=(_type &lhs, _type rhs) { \
return reinterpret_cast<_type&>(reinterpret_cast<std::underlying_type_t<_type>&>(lhs) |= static_cast<std::underlying_type_t<_type>>(rhs)); \
} \
inline _type& operator^=(_type &lhs, _type rhs) { \
return reinterpret_cast<_type&>(reinterpret_cast<std::underlying_type_t<_type>&>(lhs) ^= static_cast<std::underlying_type_t<_type>>(rhs)); \
} \
} }
#else
#define WUT_ENUM_BITMASK_TYPE(_type)
#endif