|
| 1 | +// Copyright (c) 2024 Sapphire's Suite. All Rights Reserved. |
| 2 | + |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#ifndef SAPPHIRE_SUPPORT_FLAGS_GUARD |
| 6 | +#define SAPPHIRE_SUPPORT_FLAGS_GUARD |
| 7 | + |
| 8 | +#include <cstdint> |
| 9 | + |
| 10 | +/** |
| 11 | +* \file Flags.hpp |
| 12 | +* |
| 13 | +* \brief \b Flags support file. |
| 14 | +* |
| 15 | +* Define preprocessor for enum class to flag support. |
| 16 | +* |
| 17 | +* \ingroup Support |
| 18 | +* \{ |
| 19 | +*/ |
| 20 | + |
| 21 | + |
| 22 | +namespace SA |
| 23 | +{ |
| 24 | + /// \cond Internal |
| 25 | + |
| 26 | + namespace Intl |
| 27 | + { |
| 28 | + template <uint32_t size> |
| 29 | + struct IntSizeT; |
| 30 | + |
| 31 | + template <> |
| 32 | + struct IntSizeT<sizeof(int8_t)> |
| 33 | + { |
| 34 | + using intT = int8_t; |
| 35 | + using uintT = uint8_t; |
| 36 | + }; |
| 37 | + |
| 38 | + template <> |
| 39 | + struct IntSizeT<sizeof(int16_t)> |
| 40 | + { |
| 41 | + using intT = int16_t; |
| 42 | + using uintT = uint16_t; |
| 43 | + }; |
| 44 | + |
| 45 | + template <> |
| 46 | + struct IntSizeT<sizeof(int32_t)> |
| 47 | + { |
| 48 | + using intT = int32_t; |
| 49 | + using uintT = uint32_t; |
| 50 | + }; |
| 51 | + |
| 52 | + template <> |
| 53 | + struct IntSizeT<sizeof(int64_t)> |
| 54 | + { |
| 55 | + using intT = int64_t; |
| 56 | + using uintT = uint64_t; |
| 57 | + }; |
| 58 | + |
| 59 | + #define __SA_EINT(EnumType) SA::Intl::IntSizeT<sizeof(EnumType)>::intT |
| 60 | + #define __SA_EUINT(EnumType) SA::Intl::IntSizeT<sizeof(EnumType)>::uintT |
| 61 | + } |
| 62 | + |
| 63 | + #define __SA_DEFINE_ENUM_CLASS_FLAGS_OP(EnumType, op)\ |
| 64 | + inline constexpr __SA_EUINT(EnumType) operator op(EnumType _e1, EnumType _e2) {\ |
| 65 | + return static_cast<__SA_EUINT(EnumType)>(_e1) op static_cast<__SA_EUINT(EnumType)>(_e2);\ |
| 66 | + }\ |
| 67 | + inline constexpr __SA_EUINT(EnumType) operator op(__SA_EUINT(EnumType) _flags, EnumType _e) {\ |
| 68 | + return _flags op static_cast<__SA_EUINT(EnumType)>(_e);\ |
| 69 | + }\ |
| 70 | + inline constexpr __SA_EUINT(EnumType) operator op(EnumType _e, __SA_EUINT(EnumType) _flags) {\ |
| 71 | + return static_cast<__SA_EUINT(EnumType)>(_e) op _flags;\ |
| 72 | + }\ |
| 73 | + inline __SA_EUINT(EnumType)& operator op##=(__SA_EUINT(EnumType)& _flags, EnumType _e) {\ |
| 74 | + return _flags op##= static_cast<__SA_EUINT(EnumType)>(_e);\ |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + /// \endcond |
| 79 | + |
| 80 | + #define SA_DEFINE_ENUM_CLASS_FLAGS(EnumType)\ |
| 81 | + __SA_DEFINE_ENUM_CLASS_FLAGS_OP(EnumType, &)\ |
| 82 | + __SA_DEFINE_ENUM_CLASS_FLAGS_OP(EnumType, |)\ |
| 83 | + __SA_DEFINE_ENUM_CLASS_FLAGS_OP(EnumType, ^)\ |
| 84 | + inline constexpr __SA_EUINT(EnumType) operator ~(EnumType _e){\ |
| 85 | + return ~static_cast<__SA_EUINT(EnumType)>(_e);\ |
| 86 | + }\ |
| 87 | + inline constexpr __SA_EUINT(EnumType) operator +(EnumType _e){\ |
| 88 | + return static_cast<__SA_EUINT(EnumType)>(_e);\ |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +/** \} */ |
| 94 | + |
| 95 | +#endif // SAPPHIRE_SUPPORT_FLAGS_GUARD |
0 commit comments