From 5f892dda81e3901dec839ce7ea8fef837a098156 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 4 Sep 2019 02:41:25 -0400 Subject: [PATCH 1/5] CVector2i: Remove use of union and unnamed struct Nothing uses the v data array, so we can remove it entirely. This removes the use of a compiler extension (anonymous struct), and simplifies the underlying data within the vector. --- include/zeus/CVector2i.hpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/include/zeus/CVector2i.hpp b/include/zeus/CVector2i.hpp index ecb8354..99fac02 100644 --- a/include/zeus/CVector2i.hpp +++ b/include/zeus/CVector2i.hpp @@ -1,23 +1,20 @@ #pragma once +#include #include "zeus/CVector2f.hpp" namespace zeus { class CVector2i { public: - union { - struct { - int x, y; - }; - int v[2]; - }; + int32_t x = 0; + int32_t y = 0; - constexpr CVector2i() : x(0), y(0) {} + constexpr CVector2i() = default; - constexpr CVector2i(int xin, int yin) : x(xin), y(yin) {} + constexpr CVector2i(int32_t xin, int32_t yin) : x(xin), y(yin) {} - CVector2i(const CVector2f& vec) : x(int(vec.x())), y(int(vec.y())) {} + CVector2i(const CVector2f& vec) : x(int32_t(vec.x())), y(int32_t(vec.y())) {} CVector2f toVec2f() const { return CVector2f(x, y); } @@ -33,6 +30,6 @@ public: bool operator!=(const CVector2i& other) const { return x != other.x || y != other.y; } - CVector2i operator*(int val) const { return CVector2i(x * val, y * val); } + CVector2i operator*(int32_t val) const { return CVector2i(x * val, y * val); } }; } // namespace zeus From 3083285c79b961cfbde702b5f837e55de8a7c26d Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 4 Sep 2019 02:42:55 -0400 Subject: [PATCH 2/5] CVector2i: Implement operator!= in terms of operator== Same behavior, but without duplicated inverted logic. --- include/zeus/CVector2i.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/zeus/CVector2i.hpp b/include/zeus/CVector2i.hpp index 99fac02..ce5db60 100644 --- a/include/zeus/CVector2i.hpp +++ b/include/zeus/CVector2i.hpp @@ -28,7 +28,7 @@ public: bool operator==(const CVector2i& other) const { return x == other.x && y == other.y; } - bool operator!=(const CVector2i& other) const { return x != other.x || y != other.y; } + bool operator!=(const CVector2i& other) const { return !operator==(other); } CVector2i operator*(int32_t val) const { return CVector2i(x * val, y * val); } }; From 056515b2d3740d2397a1fa9a624dab9ef86b70aa Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 4 Sep 2019 02:46:26 -0400 Subject: [PATCH 3/5] CVector2i: Make interface constexpr where applicable These are just manipulating two integral values, so they can be made constexpr. While we're at it, we can also apply the noexcept specifier. --- include/zeus/CVector2i.hpp | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/include/zeus/CVector2i.hpp b/include/zeus/CVector2i.hpp index ce5db60..b963567 100644 --- a/include/zeus/CVector2i.hpp +++ b/include/zeus/CVector2i.hpp @@ -10,26 +10,22 @@ public: int32_t x = 0; int32_t y = 0; - constexpr CVector2i() = default; + constexpr CVector2i() noexcept = default; - constexpr CVector2i(int32_t xin, int32_t yin) : x(xin), y(yin) {} + constexpr CVector2i(int32_t xin, int32_t yin) noexcept : x(xin), y(yin) {} - CVector2i(const CVector2f& vec) : x(int32_t(vec.x())), y(int32_t(vec.y())) {} + constexpr CVector2i(const CVector2f& vec) noexcept : x(int32_t(vec.x())), y(int32_t(vec.y())) {} - CVector2f toVec2f() const { return CVector2f(x, y); } + constexpr CVector2f toVec2f() const noexcept { return CVector2f(x, y); } - CVector2i operator+(const CVector2i& val) const { return CVector2i(x + val.x, y + val.y); } + constexpr CVector2i operator+(const CVector2i& val) const noexcept { return CVector2i(x + val.x, y + val.y); } + constexpr CVector2i operator-(const CVector2i& val) const noexcept { return CVector2i(x - val.x, y - val.y); } + constexpr CVector2i operator*(const CVector2i& val) const noexcept { return CVector2i(x * val.x, y * val.y); } + constexpr CVector2i operator/(const CVector2i& val) const noexcept { return CVector2i(x / val.x, y / val.y); } - CVector2i operator-(const CVector2i& val) const { return CVector2i(x - val.x, y - val.y); } + constexpr bool operator==(const CVector2i& other) const noexcept { return x == other.x && y == other.y; } + constexpr bool operator!=(const CVector2i& other) const noexcept { return !operator==(other); } - CVector2i operator*(const CVector2i& val) const { return CVector2i(x * val.x, y * val.y); } - - CVector2i operator/(const CVector2i& val) const { return CVector2i(x / val.x, y / val.y); } - - bool operator==(const CVector2i& other) const { return x == other.x && y == other.y; } - - bool operator!=(const CVector2i& other) const { return !operator==(other); } - - CVector2i operator*(int32_t val) const { return CVector2i(x * val, y * val); } + constexpr CVector2i operator*(int32_t val) const noexcept { return CVector2i(x * val, y * val); } }; } // namespace zeus From c39186d3ba55d18881e9d7539505a711dc7f7947 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 4 Sep 2019 02:51:24 -0400 Subject: [PATCH 4/5] CVector2i: Make conversions to float explicit --- include/zeus/CVector2i.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/zeus/CVector2i.hpp b/include/zeus/CVector2i.hpp index b963567..930f19f 100644 --- a/include/zeus/CVector2i.hpp +++ b/include/zeus/CVector2i.hpp @@ -16,7 +16,7 @@ public: constexpr CVector2i(const CVector2f& vec) noexcept : x(int32_t(vec.x())), y(int32_t(vec.y())) {} - constexpr CVector2f toVec2f() const noexcept { return CVector2f(x, y); } + constexpr CVector2f toVec2f() const noexcept { return CVector2f(float(x), float(y)); } constexpr CVector2i operator+(const CVector2i& val) const noexcept { return CVector2i(x + val.x, y + val.y); } constexpr CVector2i operator-(const CVector2i& val) const noexcept { return CVector2i(x - val.x, y - val.y); } From 890c1e28e06d4afc793253d3275834663c518443 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 4 Sep 2019 03:05:25 -0400 Subject: [PATCH 5/5] CVector2i: Add static assert for enforcing vector size Provides compile-time guarantees about struct layout. --- include/zeus/CVector2i.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/zeus/CVector2i.hpp b/include/zeus/CVector2i.hpp index 930f19f..9f08fe8 100644 --- a/include/zeus/CVector2i.hpp +++ b/include/zeus/CVector2i.hpp @@ -28,4 +28,6 @@ public: constexpr CVector2i operator*(int32_t val) const noexcept { return CVector2i(x * val, y * val); } }; +static_assert(sizeof(CVector2i) == sizeof(int32_t) * 2); + } // namespace zeus