Fix assignment when wrapped type is a c-style array

This commit is contained in:
King_DuckZ 2021-03-18 16:07:49 +01:00
parent c1bf2f2c3c
commit 7d5d4c2e05
2 changed files with 54 additions and 1 deletions

View file

@ -20,6 +20,21 @@ namespace VWR_OUTER_NAMESPACE {
namespace vwr {
namespace implem {
template <typename T>
struct AssignWrapped {
static void assign (T& dst, const T& src) {
dst = src;
}
};
template <typename T, unsigned int N>
struct AssignWrapped<T[N]> {
static void assign (T (&dst)[N], const T (&src)[N]) {
for (unsigned int z = 0; z < N; ++z) {
dst[z] = src[z];
}
}
};
template <typename T>
typename VectorWrapperInfo<T>::scalar_type& VecGetter<T, true>::get_at (T& parVec, size_type parIndex) {
assert(parIndex < VectorWrapperInfo<T>::dimensions);
@ -50,7 +65,7 @@ namespace vwr {
template <typename V>
Vec<V>& assign_same_type (Vec<V>& parLeft, const Vec<V>& parRight) {
parLeft.m_wrapped = parRight.m_wrapped;
implem::AssignWrapped<V>::assign(parLeft.m_wrapped, parRight.m_wrapped);
return parLeft;
}

View file

@ -18,6 +18,12 @@
#include <gtest/gtest.h>
#include <cstddef>
namespace {
struct Point {
unsigned short int x, y, z;
};
} //unnamed namespace
namespace vwr {
template <> struct VectorWrapperInfo<unsigned short int[3]> {
enum { dimensions = 3 };
@ -43,11 +49,23 @@ template <> struct VectorWrapperInfo<unsigned short int[4]> {
offset_w = sizeof(scalar_type) * 3
};
};
template <> struct VectorWrapperInfo<Point> {
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<unsigned short int[3]> vec3;
typedef vwr::Vec<unsigned short int[4]> vec4;
typedef vwr::Vec<Point> 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());
}