From 347c721a7f364103403bf993839a63dd23b62ee1 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sun, 21 Feb 2021 00:50:38 +0700 Subject: [PATCH] basic types non-arithmetic T support --- README.md | 45 +++++++++---- headers/vmath.hpp/vmath_mat.hpp | 109 ++++++++++++++++---------------- headers/vmath.hpp/vmath_qua.hpp | 27 ++++---- headers/vmath.hpp/vmath_vec.hpp | 12 +++- 4 files changed, 111 insertions(+), 82 deletions(-) diff --git a/README.md b/README.md index c172d60..e4c5385 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,8 @@ class vec_base; template < typename T > class vec_base { public: - T x{}, y{}; + T x = T{0}; + T y = T{0}; constexpr vec_base() = default; constexpr explicit vec_base(T v); @@ -97,7 +98,9 @@ public: template < typename T > class vec_base { public: - T x{}, y{}, z{}; + T x = T{0}; + T y = T{0}; + T z = T{0}; constexpr vec_base() = default; constexpr explicit vec_base(T v); @@ -110,7 +113,10 @@ public: template < typename T > class vec_base { public: - T x{}, y{}, z{}, w{}; + T x = T{0}; + T y = T{0}; + T z = T{0}; + T w = T{0}; constexpr vec_base() = default; constexpr explicit vec_base(T v); @@ -142,6 +148,10 @@ public: static constexpr size_t size = Size; + constexpr vec() = default; + constexpr vec(const vec&) = default; + constexpr vec& operator=(const vec&) = default; + void swap(vec& other) noexcept(is_nothrow_swappable_v); iterator begin() noexcept; @@ -210,8 +220,8 @@ public: using row_type = vec; row_type rows[2] = { - {1, 0}, - {0, 1}}; + row_type{T{1}, T{0}}, + row_type{T{0}, T{1}}}; constexpr mat_base() = default; constexpr explicit mat_base(T d); @@ -235,9 +245,9 @@ public: using row_type = vec; row_type rows[3] = { - {1, 0, 0}, - {0, 1, 0}, - {0, 0, 1}}; + row_type{T{1}, T{0}, T{0}}, + row_type{T{0}, T{1}, T{0}}, + row_type{T{0}, T{0}, T{1}}}; constexpr mat_base() = default; constexpr explicit mat_base(T d); @@ -267,10 +277,10 @@ public: using row_type = vec; row_type rows[4] = { - {1, 0, 0, 0}, - {0, 1, 0, 0}, - {0, 0, 1, 0}, - {0, 0, 0, 1}}; + row_type{T{1}, T{0}, T{0}, T{0}}, + row_type{T{0}, T{1}, T{0}, T{0}}, + row_type{T{0}, T{0}, T{1}, T{0}}, + row_type{T{0}, T{0}, T{0}, T{1}}}; constexpr mat_base() = default; constexpr explicit mat_base(T d); @@ -315,6 +325,10 @@ public: static constexpr size_t size = Size; + constexpr mat() = default; + constexpr mat(const mat&) = default; + constexpr mat& operator=(const mat&) = default; + void swap(mat& other) noexcept(is_nothrow_swappable_v); iterator begin() noexcept; @@ -377,8 +391,11 @@ using ptrdiff4x4_t = mat; template < typename T > class qua final { public: - vec v{0}; - T s{1}; + using imag_type = vec; + using real_type = T; + + imag_type v = imag_type{T{0}}; + real_type s = real_type{T{1}}; public: using self_type = qua; using component_type = T; diff --git a/headers/vmath.hpp/vmath_mat.hpp b/headers/vmath.hpp/vmath_mat.hpp index c176340..d6c377a 100644 --- a/headers/vmath.hpp/vmath_mat.hpp +++ b/headers/vmath.hpp/vmath_mat.hpp @@ -20,21 +20,22 @@ namespace vmath_hpp::detail class mat_base { public: using row_type = vec; + row_type rows[2] = { - {1, 0}, - {0, 1}}; + row_type{T{1}, T{0}}, + row_type{T{0}, T{1}}}; public: constexpr mat_base() = default; constexpr explicit mat_base(T d) : rows{ - row_type{d, 0}, - row_type{0, d}} {} + row_type{d, T{0}}, + row_type{T{0}, d}} {} constexpr explicit mat_base(const row_type& d) : rows{ - row_type{d[0], 0}, - row_type{0, d[1]}} {} + row_type{d[0], T{0}}, + row_type{T{0}, d[1]}} {} constexpr mat_base( T m11, T m12, @@ -65,24 +66,25 @@ namespace vmath_hpp::detail class mat_base { public: using row_type = vec; + row_type rows[3] = { - {1, 0, 0}, - {0, 1, 0}, - {0, 0, 1}}; + row_type{T{1}, T{0}, T{0}}, + row_type{T{0}, T{1}, T{0}}, + row_type{T{0}, T{0}, T{1}}}; public: constexpr mat_base() = default; constexpr explicit mat_base(T d) : rows{ - row_type{d, 0, 0}, - row_type{0, d, 0}, - row_type{0, 0, d}} {} + row_type{d, T{0}, T{0}}, + row_type{T{0}, d, T{0}}, + row_type{T{0}, T{0}, d}} {} constexpr explicit mat_base(const row_type& d) : rows{ - row_type{d[0], 0, 0}, - row_type{0, d[1], 0}, - row_type{0, 0, d[2]}} {} + row_type{d[0], T{0}, T{0}}, + row_type{T{0}, d[1], T{0}}, + row_type{T{0}, T{0}, d[2]}} {} constexpr mat_base( T m11, T m12, T m13, @@ -103,16 +105,16 @@ namespace vmath_hpp::detail const mat_base& m, const vec_base& v) : rows{ - row_type{m.rows[0], 0}, - row_type{m.rows[1], 0}, - row_type{v, 1}} {} + row_type{m.rows[0], T{0}}, + row_type{m.rows[1], T{0}}, + row_type{v, T{1}}} {} constexpr explicit mat_base( const mat_base& other) : rows{ - row_type{other.rows[0], 0}, - row_type{other.rows[1], 0}, - row_type{0, 0, 1}} {} + row_type{other.rows[0], T{0}}, + row_type{other.rows[1], T{0}}, + row_type{T{0}, T{0}, T{1}}} {} constexpr explicit mat_base( const mat_base& other) @@ -126,27 +128,28 @@ namespace vmath_hpp::detail class mat_base { public: using row_type = vec; + row_type rows[4] = { - {1, 0, 0, 0}, - {0, 1, 0, 0}, - {0, 0, 1, 0}, - {0, 0, 0, 1}}; + row_type{T{1}, T{0}, T{0}, T{0}}, + row_type{T{0}, T{1}, T{0}, T{0}}, + row_type{T{0}, T{0}, T{1}, T{0}}, + row_type{T{0}, T{0}, T{0}, T{1}}}; public: constexpr mat_base() = default; constexpr explicit mat_base(T d) : rows{ - row_type{d, 0, 0, 0}, - row_type{0, d, 0, 0}, - row_type{0, 0, d, 0}, - row_type{0, 0, 0, d}} {} + row_type{d, T{0}, T{0}, T{0}}, + row_type{T{0}, d, T{0}, T{0}}, + row_type{T{0}, T{0}, d, T{0}}, + row_type{T{0}, T{0}, T{0}, d}} {} constexpr explicit mat_base(const row_type& d) : rows{ - row_type{d[0], 0, 0, 0}, - row_type{0, d[1], 0, 0}, - row_type{0, 0, d[2], 0}, - row_type{0, 0, 0, d[3]}} {} + row_type{d[0], T{0}, T{0}, T{0}}, + row_type{T{0}, d[1], T{0}, T{0}}, + row_type{T{0}, T{0}, d[2], T{0}}, + row_type{T{0}, T{0}, T{0}, d[3]}} {} constexpr mat_base( T m11, T m12, T m13, T m14, @@ -170,26 +173,26 @@ namespace vmath_hpp::detail const mat_base& m, const vec_base& v) : rows{ - row_type{m.rows[0], 0}, - row_type{m.rows[1], 0}, - row_type{m.rows[2], 0}, - row_type{v, 1}} {} + row_type{m.rows[0], T{0}}, + row_type{m.rows[1], T{0}}, + row_type{m.rows[2], T{0}}, + row_type{v, T{1}}} {} constexpr explicit mat_base( const mat_base& other) : rows{ - row_type{other.rows[0], 0, 0}, - row_type{other.rows[1], 0, 0}, - row_type{0, 0, 1, 0}, - row_type{0, 0, 0, 1}} {} + row_type{other.rows[0], T{0}, T{0}}, + row_type{other.rows[1], T{0}, T{0}}, + row_type{T{0}, T{0}, T{1}, T{0}}, + row_type{T{0}, T{0}, T{0}, T{1}}} {} constexpr explicit mat_base( const mat_base& other) : rows{ - row_type{other.rows[0], 0}, - row_type{other.rows[1], 0}, - row_type{other.rows[2], 0}, - row_type{0, 0, 0, 1}} {} + row_type{other.rows[0], T{0}}, + row_type{other.rows[1], T{0}}, + row_type{other.rows[2], T{0}}, + row_type{T{0}, T{0}, T{0}, T{1}}} {} }; } @@ -253,14 +256,6 @@ namespace vmath_hpp return &rows[0]; } - [[nodiscard]] constexpr reference operator[](std::size_t index) noexcept { - return rows[index]; - } - - [[nodiscard]] constexpr const_reference operator[](std::size_t index) const noexcept { - return rows[index]; - } - [[nodiscard]] constexpr reference at(std::size_t index) { VMATH_HPP_THROW_IF(index >= size, std::out_of_range("mat::at")); return rows[index]; @@ -270,6 +265,14 @@ namespace vmath_hpp VMATH_HPP_THROW_IF(index >= size, std::out_of_range("mat::at")); return rows[index]; } + + [[nodiscard]] constexpr reference operator[](std::size_t index) noexcept { + return rows[index]; + } + + [[nodiscard]] constexpr const_reference operator[](std::size_t index) const noexcept { + return rows[index]; + } }; } diff --git a/headers/vmath.hpp/vmath_qua.hpp b/headers/vmath.hpp/vmath_qua.hpp index 7cced4b..9d955b8 100644 --- a/headers/vmath.hpp/vmath_qua.hpp +++ b/headers/vmath.hpp/vmath_qua.hpp @@ -16,8 +16,11 @@ namespace vmath_hpp template < typename T > class qua final { public: - vec v{0}; - T s{1}; + using imag_type = vec; + using real_type = T; + + imag_type v = imag_type{T{0}}; + real_type s = real_type{T{1}}; public: using self_type = qua; using component_type = T; @@ -85,6 +88,16 @@ namespace vmath_hpp return &(*this)[0]; } + [[nodiscard]] constexpr reference at(std::size_t index) { + VMATH_HPP_THROW_IF(index >= size, std::out_of_range("qua::at")); + return (*this)[index]; + } + + [[nodiscard]] constexpr const_reference at(std::size_t index) const { + VMATH_HPP_THROW_IF(index >= size, std::out_of_range("qua::at")); + return (*this)[index]; + } + [[nodiscard]] constexpr reference operator[](std::size_t index) noexcept { switch ( index ) { default: @@ -104,16 +117,6 @@ namespace vmath_hpp case 3: return s; } } - - [[nodiscard]] constexpr reference at(std::size_t index) { - VMATH_HPP_THROW_IF(index >= size, std::out_of_range("qua::at")); - return (*this)[index]; - } - - [[nodiscard]] constexpr const_reference at(std::size_t index) const { - VMATH_HPP_THROW_IF(index >= size, std::out_of_range("qua::at")); - return (*this)[index]; - } }; } diff --git a/headers/vmath.hpp/vmath_vec.hpp b/headers/vmath.hpp/vmath_vec.hpp index ccad622..36a6f13 100644 --- a/headers/vmath.hpp/vmath_vec.hpp +++ b/headers/vmath.hpp/vmath_vec.hpp @@ -16,7 +16,8 @@ namespace vmath_hpp::detail template < typename T > class vec_base { public: - T x{}, y{}; + T x = T{0}; + T y = T{0}; public: constexpr vec_base() = default; @@ -52,7 +53,9 @@ namespace vmath_hpp::detail template < typename T > class vec_base { public: - T x{}, y{}, z{}; + T x = T{0}; + T y = T{0}; + T z = T{0}; public: constexpr vec_base() = default; @@ -93,7 +96,10 @@ namespace vmath_hpp::detail template < typename T > class vec_base { public: - T x{}, y{}, z{}, w{}; + T x = T{0}; + T y = T{0}; + T z = T{0}; + T w = T{0}; public: constexpr vec_base() = default;