From e780ba321897169ace90baa4b1d85190bc526065 Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Thu, 23 Jan 2025 13:55:37 -0500 Subject: pkmacros: add TypeSafeInt operators: *,/ --- pkmacros.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'pkmacros.h') diff --git a/pkmacros.h b/pkmacros.h index eb8d6e7..583f6bc 100644 --- a/pkmacros.h +++ b/pkmacros.h @@ -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(a) - static_cast(b)); \ } \ + inline TypeName operator*(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) * static_cast(b)); \ + } \ + inline TypeName operator/(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) / static_cast(b)); \ + } \ inline TypeName operator&(const TypeName& a, const TypeName& b) { \ return TypeName(static_cast(a) & static_cast(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(a) - static_cast(b)); \ } \ + constexpr TypeName operator*(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) * static_cast(b)); \ + } \ + constexpr TypeName operator/(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) / static_cast(b)); \ + } \ constexpr TypeName operator&(const TypeName& a, const TypeName& b) { \ return TypeName(static_cast(a) & static_cast(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; \ -- cgit v1.2.3