From dc05e543a4f530e6ea851aa210fac04a9e88963d Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Thu, 3 Dec 2020 06:11:31 +0700 Subject: [PATCH] vector and matrix deduction guides --- headers/vmath.hpp/vmath_mat.hpp | 38 ++++++++++++++++++++++++++++ headers/vmath.hpp/vmath_vec.hpp | 44 +++++++++++++++++++++++++++++++++ untests/vmath_mat_tests.cpp | 14 +++++++++++ untests/vmath_vec_tests.cpp | 16 ++++++++++++ 4 files changed, 112 insertions(+) diff --git a/headers/vmath.hpp/vmath_mat.hpp b/headers/vmath.hpp/vmath_mat.hpp index bac8fc2..9fa1acb 100644 --- a/headers/vmath.hpp/vmath_mat.hpp +++ b/headers/vmath.hpp/vmath_mat.hpp @@ -230,6 +230,44 @@ namespace vmath_hpp return rows[index]; } }; +} + +namespace vmath_hpp +{ + // mat2 + + template < typename T > + mat(T, T, T, T) -> mat; + + template < typename T > + mat(const vec&, const vec&) -> mat; + + template < typename T > + mat(std::initializer_list, std::initializer_list) -> mat; + + // mat3 + + template < typename T > + mat(T, T, T, T, T, T, T, T, T) -> mat; + + template < typename T > + mat(const vec&, const vec&, const vec&) -> mat; + + template < typename T > + mat(std::initializer_list, std::initializer_list, std::initializer_list) -> mat; + + // mat4 + + template < typename T > + mat(T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T) -> mat; + + template < typename T > + mat(const vec&, const vec&, const vec&, const vec&) -> mat; + + template < typename T > + mat(std::initializer_list, std::initializer_list, std::initializer_list, std::initializer_list) -> mat; + + // swap template < typename T, std::size_t Size > void swap(mat& l, mat& r) noexcept(noexcept(l.swap(r))) { diff --git a/headers/vmath.hpp/vmath_vec.hpp b/headers/vmath.hpp/vmath_vec.hpp index 3f37281..bfd4346 100644 --- a/headers/vmath.hpp/vmath_vec.hpp +++ b/headers/vmath.hpp/vmath_vec.hpp @@ -189,6 +189,50 @@ namespace vmath_hpp return (*this)[index]; } }; +} + +namespace vmath_hpp +{ + // vec2 + + template < typename T > + vec(T, T) -> vec; + + // vec3 + + template < typename T > + vec(T, T, T) -> vec; + + template < typename T > + vec(const vec&, T) -> vec; + + template < typename T > + vec(T, const vec&) -> vec; + + // vec4 + + template < typename T > + vec(T, T, T, T) -> vec; + + template < typename T > + vec(const vec&, T, T) -> vec; + + template < typename T > + vec(T, const vec&, T) -> vec; + + template < typename T > + vec(T, T, const vec&) -> vec; + + template < typename T > + vec(const vec&, const vec&) -> vec; + + template < typename T > + vec(const vec&, T) -> vec; + + template < typename T > + vec(T, const vec&) -> vec; + + // swap template < typename T, std::size_t Size > void swap(vec& l, vec& r) noexcept(noexcept(l.swap(r))) { diff --git a/untests/vmath_mat_tests.cpp b/untests/vmath_mat_tests.cpp index e8830b1..0f2abf9 100644 --- a/untests/vmath_mat_tests.cpp +++ b/untests/vmath_mat_tests.cpp @@ -24,6 +24,20 @@ TEST_CASE("vmath/mat") { STATIC_REQUIRE(sizeof(int4x4{}) == sizeof(int) * 4 * 4); } + SUBCASE("guides") { + STATIC_REQUIRE(mat{1,2,3,4}.size == 2); + STATIC_REQUIRE(mat{{1,2},{3,4}}.size == 2); + STATIC_REQUIRE(mat{vec{1,2},vec{3,4}}.size == 2); + + STATIC_REQUIRE(mat{1,2,3,4,5,6,7,8,9}.size == 3); + STATIC_REQUIRE(mat{{1,2,3},{4,5,6},{7,8,9}}.size == 3); + STATIC_REQUIRE(mat{vec{1,2,3},vec{4,5,6},vec{7,8,9}}.size == 3); + + STATIC_REQUIRE(mat{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}.size == 4); + STATIC_REQUIRE(mat{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}.size == 4); + STATIC_REQUIRE(mat{vec{1,2,3,4},vec{5,6,7,8},vec{9,10,11,12},vec{13,14,15,16}}.size == 4); + } + SUBCASE("ctors") { { STATIC_REQUIRE(int2x2()[0] == int2(1,0)); diff --git a/untests/vmath_vec_tests.cpp b/untests/vmath_vec_tests.cpp index 4a77442..7c3d2da 100644 --- a/untests/vmath_vec_tests.cpp +++ b/untests/vmath_vec_tests.cpp @@ -24,6 +24,22 @@ TEST_CASE("vmath/vec") { STATIC_REQUIRE(sizeof(int4{}) == sizeof(int) * 4); } + SUBCASE("guides") { + STATIC_REQUIRE(vec{1,2}.size == 2); + + STATIC_REQUIRE(vec{1,2,3}.size == 3); + STATIC_REQUIRE(vec{{1,2},3}.size == 3); + STATIC_REQUIRE(vec{1,{2,3}}.size == 3); + + STATIC_REQUIRE(vec{1,2,3,4}.size == 4); + STATIC_REQUIRE(vec{vec{1,2},3,4}.size == 4); + STATIC_REQUIRE(vec{1,vec{2,3},4}.size == 4); + STATIC_REQUIRE(vec{1,2,vec{3,4}}.size == 4); + STATIC_REQUIRE(vec{vec{1,2},vec{3,4}}.size == 4); + STATIC_REQUIRE(vec{vec{1,2,3},4}.size == 4); + STATIC_REQUIRE(vec{1,vec{2,3,4}}.size == 4); + } + SUBCASE("ctors") { { STATIC_REQUIRE(int2().x == 0);