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