From 1464d778e405f0882c996a90b93072b8a6d490e1 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Fri, 8 Nov 2024 15:40:24 +0200 Subject: [PATCH 1/2] Using lots of macros to define operators for both int24 and int types --- openVulkanoCpp/Math/Int24.hpp | 169 ++++++---------------------------- 1 file changed, 26 insertions(+), 143 deletions(-) diff --git a/openVulkanoCpp/Math/Int24.hpp b/openVulkanoCpp/Math/Int24.hpp index 5676084..e30510a 100644 --- a/openVulkanoCpp/Math/Int24.hpp +++ b/openVulkanoCpp/Math/Int24.hpp @@ -57,94 +57,25 @@ namespace OpenVulkano return *this; } - int24 operator +(const int24& val) const - { - return int24( (int)*this + (int)val ); - } +#define INT24_OPERATORS(op) \ + int24 operator op(const int24& val) const { return int24( (int)*this op (int)val ); } \ + int24 operator op(const int val) const { return int24( (int)*this op val ); } - int24 operator -(const int24& val) const - { - return int24( (int)*this - (int)val ); - } + INT24_OPERATORS(+) + INT24_OPERATORS(-) + INT24_OPERATORS(*) + INT24_OPERATORS(/) +#undef INT24_OPERATORS - int24 operator *(const int24& val) const - { - return int24( (int)*this * (int)val ); - } +#define INT24_OPERATORS(op) \ + int24& operator op##=(const int24& val ) { *this = *this op val; return *this; } \ + int24& operator op##=(const int val ) { *this = *this op val; return *this; } - int24 operator /(const int24& val) const - { - return int24( (int)*this / (int)val ); - } - - int24 operator +(const int val) const - { - return int24( (int)*this + val ); - } - - int24 operator -(const int val) const - { - return int24( (int)*this - val ); - } - - int24 operator *(const int val) const - { - return int24( (int)*this * val ); - } - - int24 operator /(const int val) const - { - return int24( (int)*this / val ); - } - - - int24& operator +=(const int24& val ) - { - *this = *this + val; - return *this; - } - - int24& operator -=(const int24& val ) - { - *this = *this - val; - return *this; - } - - int24& operator *=(const int24& val ) - { - *this = *this * val; - return *this; - } - - int24& operator /=(const int24& val ) - { - *this = *this / val; - return *this; - } - - int24& operator +=(const int val ) - { - *this = *this + val; - return *this; - } - - int24& operator -=(const int val ) - { - *this = *this - val; - return *this; - } - - int24& operator *=(const int val ) - { - *this = *this * val; - return *this; - } - - int24& operator /=(const int val ) - { - *this = *this / val; - return *this; - } + INT24_OPERATORS(+) + INT24_OPERATORS(-) + INT24_OPERATORS(*) + INT24_OPERATORS(/) +#undef INT24_OPERATORS int24 operator >>(const int val) const { @@ -183,64 +114,16 @@ namespace OpenVulkano return int24( -(int)*this ); } - bool operator ==(const int24& val) const - { - return (int)*this == (int)val; - } +#define INT24_OPERATORS(op) \ + bool operator op(const int24& val) const { return (int)*this op (int)val; } \ + bool operator op(const int val) const { return (int)*this op val; } - bool operator !=(const int24& val) const - { - return (int)*this != (int)val; - } - - bool operator >=(const int24& val) const - { - return (int)*this >= (int)val; - } - - bool operator <=(const int24& val) const - { - return (int)*this <= (int)val; - } - - bool operator >(const int24& val) const - { - return (int)*this > (int)val; - } - - bool operator <(const int24& val) const - { - return (int)*this < (int)val; - } - - bool operator ==(const int val) const - { - return (int)*this == val; - } - - bool operator !=(const int val) const - { - return (int)*this != val; - } - - bool operator >=(const int val) const - { - return (int)*this >= val; - } - - bool operator <=(const int val) const - { - return (int)*this <= val; - } - - bool operator >(const int val) const - { - return ((int)*this) > val; - } - - bool operator <(const int val) const - { - return (int)*this < val; - } + INT24_OPERATORS(==) + INT24_OPERATORS(!=) + INT24_OPERATORS(>=) + INT24_OPERATORS(<=) + INT24_OPERATORS(>) + INT24_OPERATORS(<) +#undef INT24_OPERATORS }; } \ No newline at end of file From 4926c44a0441a216f1afa1b54dc984c73211a4d2 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Fri, 8 Nov 2024 17:45:57 +0200 Subject: [PATCH 2/2] Defining almost all operators in one go, added templates to the ops, separate == and <=> operator --- openVulkanoCpp/Math/Int24.hpp | 37 ++++++++++++++--------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/openVulkanoCpp/Math/Int24.hpp b/openVulkanoCpp/Math/Int24.hpp index e30510a..111bd20 100644 --- a/openVulkanoCpp/Math/Int24.hpp +++ b/openVulkanoCpp/Math/Int24.hpp @@ -7,6 +7,7 @@ #pragma once #include +#include namespace OpenVulkano { @@ -58,18 +59,10 @@ namespace OpenVulkano } #define INT24_OPERATORS(op) \ - int24 operator op(const int24& val) const { return int24( (int)*this op (int)val ); } \ - int24 operator op(const int val) const { return int24( (int)*this op val ); } - - INT24_OPERATORS(+) - INT24_OPERATORS(-) - INT24_OPERATORS(*) - INT24_OPERATORS(/) -#undef INT24_OPERATORS - -#define INT24_OPERATORS(op) \ - int24& operator op##=(const int24& val ) { *this = *this op val; return *this; } \ - int24& operator op##=(const int val ) { *this = *this op val; return *this; } + template, bool> = true> \ + int24 operator op(const T& val) const { return int24( (int)*this op static_cast(val) ); } \ + template, bool> = true> \ + int24& operator op##=(const T& val ) { *this = *this op static_cast(val); return *this; } INT24_OPERATORS(+) INT24_OPERATORS(-) @@ -114,16 +107,16 @@ namespace OpenVulkano return int24( -(int)*this ); } -#define INT24_OPERATORS(op) \ - bool operator op(const int24& val) const { return (int)*this op (int)val; } \ - bool operator op(const int val) const { return (int)*this op val; } + template, bool> = true> + [[nodiscard]] bool operator==(const T& val) const + { + return static_cast(*this) == static_cast(val); + } - INT24_OPERATORS(==) - INT24_OPERATORS(!=) - INT24_OPERATORS(>=) - INT24_OPERATORS(<=) - INT24_OPERATORS(>) - INT24_OPERATORS(<) -#undef INT24_OPERATORS + template, bool> = true> + [[nodiscard]] constexpr auto operator<=>(const T& other) const + { + return operator int() <=> static_cast(other); + } }; } \ No newline at end of file