diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-01-23 13:55:37 -0500 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-01-23 13:55:37 -0500 |
| commit | e780ba321897169ace90baa4b1d85190bc526065 (patch) | |
| tree | f7a428767f6f0fb5daa76198ed7d4b0e6f2357b0 | |
| parent | 4db94de7c5cd21e176a0b52374e28f7ee6451be3 (diff) | |
pkmacros: add TypeSafeInt operators: *,/
| -rw-r--r-- | pkmacros.h | 32 | ||||
| -rw-r--r-- | test/pkmacros.cpp | 18 |
2 files changed, 48 insertions, 2 deletions
@@ -62,6 +62,8 @@ constexpr TypeName TypeName_MAX = TypeName{TypeName_T_MAX}; \ 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|(const TypeName& a, const TypeName& b); \ TypeName operator^(const TypeName& a, const TypeName& b); \ @@ -73,6 +75,8 @@ TypeName operator>>(const 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); \ + 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); \ @@ -84,6 +88,12 @@ 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/(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)); \ } \ @@ -123,6 +133,14 @@ a = TypeName{a - b}; \ return a; \ }; \ + inline TypeName operator*=(TypeName& a, const TypeName& b) { \ + a = TypeName{a * b}; \ + return a; \ + }; \ + inline TypeName operator/=(TypeName& a, const TypeName& b) { \ + a = TypeName{a / b}; \ + return a; \ + }; \ inline TypeName operator&=(TypeName& a, const TypeName& b) { \ a = TypeName{a & b}; \ return a; \ @@ -155,6 +173,12 @@ 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)); \ } \ @@ -194,6 +218,14 @@ a = TypeName{a - b}; \ return a; \ }; \ + constexpr TypeName operator*=(TypeName& a, const TypeName& b) { \ + a = TypeName{a * b}; \ + return a; \ + }; \ + constexpr TypeName operator/=(TypeName& a, const TypeName& b) { \ + a = TypeName{a / b}; \ + return a; \ + }; \ constexpr TypeName operator&=(TypeName& a, const TypeName& b) { \ a = TypeName{a & b}; \ return a; \ diff --git a/test/pkmacros.cpp b/test/pkmacros.cpp index 8edcbfd..bbba6f0 100644 --- a/test/pkmacros.cpp +++ b/test/pkmacros.cpp @@ -25,6 +25,8 @@ template<typename T, typename T_T> bool tsi_test_operators() { + T mtsi01 {0x01}; + T mtsi10 {0x10}; T mtsi0F {0x0F}; T mtsiF0 {0xF0}; T r; @@ -38,6 +40,10 @@ tsi_test_operators() if (r == T{0}) return false; r = mtsi0F - mtsiF0; if (r == T{0}) return false; + r = mtsi0F * mtsiF0; + if (r == T{0}) return false; + r = mtsi10 / mtsi01; + if (r != T{0x10}) return false; r = mtsi0F & mtsiF0; if (r != T{0}) return false; r = mtsi0F | mtsiF0; @@ -60,6 +66,10 @@ tsi_test_operators() if (r == T{0}) return false; r = mtsi0F -= mtsiF0; if (r == T{0}) return false; + r = mtsi0F *= mtsi01; + if (r == T{0}) return false; + r = mtsi10 /= mtsi01; + if (r != T{0x10}) return false; r = mtsi0F &= mtsiF0; if (r != T{0}) return false; r = mtsi0F |= mtsiF0; @@ -84,9 +94,13 @@ tsi_test_operators_constexpr() constexpr T r02 = mtsi0F - T_T{static_cast<T_T>(mtsiF0)}; if constexpr (r02 == mtsi00) return false; */ - constexpr T r03 = mtsi0F + mtsiF0; + constexpr T r01 = mtsi0F + mtsiF0; + if constexpr (r01 == mtsi00) return false; + constexpr T r02 = mtsi0F - mtsiF0; + if constexpr (r02 == mtsi00) return false; + constexpr T r03 = mtsi0F * mtsiF0; if constexpr (r03 == mtsi00) return false; - constexpr T r04 = mtsi0F - mtsiF0; + constexpr T r04 = mtsiF0 / mtsi0F; if constexpr (r04 == mtsi00) return false; constexpr T r05 = mtsi0F & mtsiF0; if constexpr (r05 != mtsi00) return false; |
