diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-11-14 15:13:11 -0500 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-11-14 15:13:11 -0500 |
| commit | 6a30d21c5175c27aeccc8a6ae956c274f3ef9621 (patch) | |
| tree | 3a51b6124cbb4c5d5c415410559e3705c17fe117 | |
| parent | 6317bcab647e852ec97f2f2b71d656ae0e725905 (diff) | |
add bitwise | and ^ to TypeSafeInt and rework parameters
| -rw-r--r-- | src/macros.hpp | 89 |
1 files changed, 72 insertions, 17 deletions
diff --git a/src/macros.hpp b/src/macros.hpp index 176f7b1..30f1c77 100644 --- a/src/macros.hpp +++ b/src/macros.hpp @@ -12,37 +12,43 @@ #define IS_DESTRUCTIBLE(T) constexpr(std::is_destructible<T>::value && !std::is_integral<T>::value && !std::is_floating_point<T>::value) #define TypeSafeInt2_H(TypeName, Type, Max, TypeName_T, TypeName_MAX, TypeName_T_MAX)\ - using TypeName_T = Type; \ - enum class TypeName : TypeName_T; \ - constexpr TypeName_T TypeName_T_MAX = TypeName_T{Max}; \ - constexpr TypeName TypeName_MAX = TypeName{TypeName_T_MAX}; \ - TypeName operator+(TypeName a, TypeName_T i); \ - TypeName operator-(TypeName a, const TypeName_T i); \ - TypeName operator+(TypeName a, const TypeName &b); \ - TypeName operator-(TypeName a, const TypeName &b); \ - TypeName operator&(TypeName a, const TypeName &b); \ - TypeName operator|(TypeName a, const TypeName &b); \ + using TypeName_T = Type; \ + enum class TypeName : TypeName_T; \ + constexpr TypeName_T TypeName_T_MAX = TypeName_T{Max}; \ + constexpr TypeName TypeName_MAX = TypeName{TypeName_T_MAX}; \ + TypeName operator+(const TypeName &a, const TypeName_T &i); \ + TypeName operator-(const TypeName &a, const TypeName_T &i); \ + TypeName operator+(const TypeName &a, const TypeName &b); \ + TypeName operator-(const TypeName &a, const TypeName &b); \ + TypeName operator&(const TypeName &a, const TypeName &b); \ + TypeName operator|(const TypeName &a, const TypeName &b); \ + TypeName operator^(const TypeName &a, const TypeName &b); \ TypeName &operator++(TypeName &a); \ - TypeName &operator--(TypeName &a); + TypeName &operator--(TypeName &a); \ + TypeName operator<<(const TypeName &a, const TypeName &b); \ + TypeName operator>>(const TypeName &a, const TypeName &b); #define TypeSafeInt2_B(TypeName, TypeName_T) \ - inline TypeName operator+(TypeName a, TypeName_T i) { \ + inline TypeName operator+(const TypeName &a, const TypeName_T &i) { \ return TypeName(static_cast<TypeName_T>(a) + i); \ } \ - inline TypeName operator-(TypeName a, const TypeName_T i) { \ + inline TypeName operator-(const TypeName &a, const TypeName_T &i) { \ return TypeName(static_cast<TypeName_T>(a) - i); \ } \ - inline TypeName operator+(TypeName a, const TypeName &b) { \ + inline TypeName operator+(const TypeName &a, const TypeName &b) { \ return TypeName(static_cast<TypeName_T>(a) + static_cast<TypeName_T>(b)); \ } \ - inline TypeName operator-(TypeName a, const TypeName &b) { \ + inline TypeName operator-(const TypeName &a, const TypeName &b) { \ return TypeName(static_cast<TypeName_T>(a) - static_cast<TypeName_T>(b)); \ } \ - inline TypeName operator&(TypeName a, const TypeName &b) { \ + inline TypeName operator&(const TypeName &a, const TypeName &b) { \ return TypeName(static_cast<TypeName_T>(a) & static_cast<TypeName_T>(b)); \ } \ - inline TypeName operator|(TypeName a, const TypeName &b) { \ + inline TypeName operator|(const TypeName &a, const TypeName &b) { \ return TypeName(static_cast<TypeName_T>(a) | static_cast<TypeName_T>(b)); \ } \ + inline TypeName operator^(const TypeName &a, const TypeName &b) { \ + return TypeName(static_cast<TypeName_T>(a) ^ static_cast<TypeName_T>(b)); \ + } \ inline TypeName &operator++(TypeName &a) { \ a = a + 1; \ return a; \ @@ -50,11 +56,60 @@ inline TypeName &operator--(TypeName &a) { \ a = a - 1; \ return a; \ + }; \ + inline TypeName operator<<(const TypeName &a, const TypeName &b) { \ + return TypeName(static_cast<TypeName_T>(a) << static_cast<TypeName_T>(b)); \ + }; \ + inline TypeName operator>>(const TypeName &a, const TypeName &b) { \ + return TypeName(static_cast<TypeName_T>(a) >> static_cast<TypeName_T>(b)); \ }; #define TypeSafeInt_H(TypeName, Type, Max) \ TypeSafeInt2_H(TypeName, Type, Max, CONCAT(TypeName, _T), CONCAT(TypeName, _MAX), CONCAT(TypeName, _T_MAX)) #define TypeSafeInt_B(TypeName) \ TypeSafeInt2_B(TypeName, CONCAT(TypeName, _T)) +#define TypeSafeInt2_H_constexpr(TypeName, Type, Max, TypeName_T, TypeName_MAX, TypeName_T_MAX)\ + using TypeName_T = Type; \ + enum class TypeName : TypeName_T; \ + constexpr TypeName_T TypeName_T_MAX = TypeName_T{Max}; \ + constexpr TypeName TypeName_MAX = TypeName{TypeName_T_MAX}; \ + constexpr TypeName operator+(const TypeName &a, const TypeName_T &b) { \ + return TypeName(static_cast<TypeName_T>(a) + static_cast<TypeName_T>(b)); \ + } \ + constexpr TypeName operator-(const TypeName &a, const TypeName_T &b) { \ + return TypeName(static_cast<TypeName_T>(a) - static_cast<TypeName_T>(b)); \ + } \ + constexpr TypeName operator+(const TypeName &a, const TypeName &b) { \ + return TypeName(static_cast<TypeName_T>(a) + static_cast<TypeName_T>(b)); \ + } \ + constexpr TypeName operator-(const TypeName &a, const TypeName &b) { \ + return TypeName(static_cast<TypeName_T>(a) - static_cast<TypeName_T>(b)); \ + } \ + constexpr TypeName operator&(const TypeName &a, const TypeName &b) { \ + return TypeName(static_cast<TypeName_T>(a) & static_cast<TypeName_T>(b)); \ + } \ + constexpr TypeName operator|(const TypeName &a, const TypeName &b) { \ + return TypeName(static_cast<TypeName_T>(a) | static_cast<TypeName_T>(b)); \ + } \ + constexpr TypeName operator^(const TypeName &a, const TypeName &b) { \ + return TypeName(static_cast<TypeName_T>(a) ^ static_cast<TypeName_T>(b)); \ + } \ + constexpr TypeName &operator++(TypeName &a) { \ + a = a + TypeName(1); \ + return a; \ + } \ + constexpr TypeName &operator--(TypeName &a) { \ + a = a - TypeName{1}; \ + return a; \ + }; \ + constexpr TypeName operator<<(const TypeName &a, const TypeName &b) { \ + return TypeName(static_cast<TypeName_T>(a) << static_cast<TypeName_T>(b)); \ + }; \ + constexpr TypeName operator>>(const TypeName &a, const TypeName &b) { \ + return TypeName(static_cast<TypeName_T>(a) >> static_cast<TypeName_T>(b)); \ + }; +#define TypeSafeInt_Const_Expr(TypeName, Type, Max) \ + TypeSafeInt2_H_constexpr(TypeName, Type, Max, CONCAT(TypeName, _T), CONCAT(TypeName, _MAX), CONCAT(TypeName, _T_MAX)) + #endif /* PKE_MACROS_HPP */ |
