From 7d5d4c2e05d28d35eb5942df00cb6e5a6d8061b6 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 18 Mar 2021 16:07:49 +0100 Subject: [PATCH] Fix assignment when wrapped type is a c-style array --- include/vectorwrapper/vectorwrapper.inl | 17 ++++++++++- test/unit/test_c_array.cpp | 38 +++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/include/vectorwrapper/vectorwrapper.inl b/include/vectorwrapper/vectorwrapper.inl index 5bdb1b2..d2c3b5d 100644 --- a/include/vectorwrapper/vectorwrapper.inl +++ b/include/vectorwrapper/vectorwrapper.inl @@ -20,6 +20,21 @@ namespace VWR_OUTER_NAMESPACE { namespace vwr { namespace implem { + template + struct AssignWrapped { + static void assign (T& dst, const T& src) { + dst = src; + } + }; + template + struct AssignWrapped { + static void assign (T (&dst)[N], const T (&src)[N]) { + for (unsigned int z = 0; z < N; ++z) { + dst[z] = src[z]; + } + } + }; + template typename VectorWrapperInfo::scalar_type& VecGetter::get_at (T& parVec, size_type parIndex) { assert(parIndex < VectorWrapperInfo::dimensions); @@ -50,7 +65,7 @@ namespace vwr { template Vec& assign_same_type (Vec& parLeft, const Vec& parRight) { - parLeft.m_wrapped = parRight.m_wrapped; + implem::AssignWrapped::assign(parLeft.m_wrapped, parRight.m_wrapped); return parLeft; } diff --git a/test/unit/test_c_array.cpp b/test/unit/test_c_array.cpp index 00dd13c..fff1b74 100644 --- a/test/unit/test_c_array.cpp +++ b/test/unit/test_c_array.cpp @@ -18,6 +18,12 @@ #include #include +namespace { +struct Point { + unsigned short int x, y, z; +}; +} //unnamed namespace + namespace vwr { template <> struct VectorWrapperInfo { enum { dimensions = 3 }; @@ -43,11 +49,23 @@ template <> struct VectorWrapperInfo { offset_w = sizeof(scalar_type) * 3 }; }; + +template <> struct VectorWrapperInfo { + enum { dimensions = 3 }; + using scalar_type = unsigned short int; + using vector_type = Point; + enum { + offset_x = offsetof(Point, x), + offset_y = offsetof(Point, y), + offset_z = offsetof(Point, z) + }; +}; } //namespace vwr namespace { typedef vwr::Vec vec3; typedef vwr::Vec vec4; +typedef vwr::Vec struct_vec3; } //unnamed namespace TEST(vwr, c_array) { @@ -89,4 +107,24 @@ TEST(vwr, c_array) { EXPECT_EQ(4511U, myvec3.x() + myvec3.y() + myvec3.z()); EXPECT_NE(myvec3, myvec4.xyz()); + + myvec3 = vec3{800U, 900U, 1000U}; + EXPECT_EQ(800U, myvec3.x()); + EXPECT_EQ(900U, myvec3.y()); + EXPECT_EQ(1000U, myvec3.z()); +} + +TEST(vwr, c_array_interactions) { + vec3 myvec3 {7, 11, 13}; + struct_vec3 mystructvec3 { 17, 19, 23 }; + EXPECT_NE(myvec3, mystructvec3); + + myvec3 = mystructvec3; + EXPECT_EQ(myvec3, mystructvec3); + + mystructvec3 = vec3{29, 31, 37}; + EXPECT_NE(myvec3, mystructvec3); + EXPECT_EQ(29, mystructvec3.x()); + EXPECT_EQ(31, mystructvec3.y()); + EXPECT_EQ(37, mystructvec3.z()); }