Refactored specialized templates to avoid duplicates

This commit is contained in:
trotFunky 2019-10-14 21:34:14 +02:00
parent bb674cdbb6
commit 5c90d15869

View file

@ -69,122 +69,68 @@ struct CoordinatesVector
template <typename T>
struct CoordinatesVector<T,2>
struct Vec2 : public CoordinatesVector<T,2>
{
T coordinates[2];
T& x = coordinates[0];
T& y = coordinates[1];
T& x = CoordinatesVector<T,2>::coordinates[0];
T& y = CoordinatesVector<T,2>::coordinates[1];
// Use SFINAE to declare the function if and only if the type is a scalar
template <typename Scalar>
typename std::enable_if<std::is_arithmetic<Scalar>::value,CoordinatesVector<T,2>>::type
operator*(const Scalar& scalar)
Vec2() = default;
// Cast-copy constructor in order to use the operators of the mother class.
Vec2(const CoordinatesVector<T,2>& origin)
{
CoordinatesVector result;
for (unsigned int i = 0;i<2;i++)
if (this != &origin)
{
result.coordinates[i] = coordinates[i] * scalar;
std::copy(std::begin(origin.coordinates),std::end(origin.coordinates),std::begin(CoordinatesVector<T,2>::coordinates));
}
}
CoordinatesVector operator+(const CoordinatesVector<T,2>& op)
Vec2& operator=(const Vec2<T>& origin)
{
CoordinatesVector result;
result.x = x + op.x;
result.y = y + op.y;
return result;
}
CoordinatesVector operator-(const CoordinatesVector<T,2>& op)
{
CoordinatesVector<T,2> result;
result.x = x - op.x;
result.y = y - op.y;
return result;
}
CoordinatesVector& operator=(const CoordinatesVector<T,2>& original)
{
if (this != &original)
if (this != &origin)
{
std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(coordinates));
std::copy(std::begin(origin.coordinates), std::end(origin.coordinates), std::begin(CoordinatesVector<T,2>::coordinates));
}
return *this;
}
friend std::fstream& operator>>(std::fstream& stream, CoordinatesVector<T,2>& vec2)
{
stream >> vec2.x >> vec2.y;
return stream;
}
};
template <typename T>
struct CoordinatesVector<T,3>
struct Vec3 : CoordinatesVector<T,3>
{
T coordinates[3];
T& x = coordinates[0];
T& y = coordinates[1];
T& z = coordinates[2];
T& x = CoordinatesVector<T,3>::coordinates[0];
T& y = CoordinatesVector<T,3>::coordinates[1];
T& z = CoordinatesVector<T,3>::coordinates[2];
// Use SFINAE to declare the function if and only if the type is a scalar
template <typename Scalar>
typename std::enable_if<std::is_arithmetic<Scalar>::value,CoordinatesVector<T,3>>::type
operator*(const Scalar& scalar)
Vec3() = default;
// Cast-copy constructor in order to use the operators of the mother class.
Vec3(const CoordinatesVector<T,3>& origin)
{
CoordinatesVector result;
for (unsigned int i = 0;i<3;i++)
if (this != &origin)
{
result.coordinates[i] = coordinates[i] * scalar;
std::copy(std::begin(origin.coordinates),std::end(origin.coordinates),std::begin(CoordinatesVector<T,2>::coordinates));
}
}
CoordinatesVector operator+(const CoordinatesVector<T,3>& op)
{
CoordinatesVector result;
result.x = x + op.x;
result.y = y + op.y;
result.z = z + op.z;
return result;
}
CoordinatesVector operator-(const CoordinatesVector<T,3>& op)
{
CoordinatesVector<T,3> result;
result.x = x - op.x;
result.y = y - op.y;
result.z = z - op.z;
return result;
}
CoordinatesVector& operator=(const CoordinatesVector<T,3>& original)
Vec3& operator=(const Vec3<T>& original)
{
if (this != &original)
{
std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(coordinates));
std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(CoordinatesVector<T,3>::coordinates));
}
return *this;
}
friend std::fstream& operator>>(std::fstream& stream, CoordinatesVector<T,3>& vec3)
{
stream >> vec3.x >> vec3.y >> vec3.z;
return stream;
}
};
// Define aliases for common types
typedef CoordinatesVector<int,2> Vec2i;
typedef CoordinatesVector<float,2> Vec2f;
typedef Vec2<int> Vec2i;
typedef Vec2<float> Vec2f;
typedef CoordinatesVector<int,3> Vec3i;
typedef CoordinatesVector<float,3> Vec3f;
typedef Vec3<int> Vec3i;
typedef Vec3<float> Vec3f;
#endif //TESTS_OPENGL_TYPES_H