From a3b754b88e89dd3b821bc65a1c28d7bd3625cde5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 16 Apr 2012 02:57:31 +0200 Subject: [PATCH] Possiblity for constructing Vector from one value, like in GLSL. Marked the constructor as explicit, because we don't want mistakes like this to happen: Matrix4::rotation(1.0f, deg(3.0f)); // oops, swapped axis and angle! Instead, when calling such constructor, the type must be said explicitly (initializer-list is forbidden, too): Matrix4::rotation(deg(3.0f), Vector3(1.0f)); // okay --- src/Math/Vector.h | 9 +++++++++ src/Math/Vector2.h | 4 ++-- src/Math/Vector3.h | 4 ++-- src/Math/Vector4.h | 3 +++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Math/Vector.h b/src/Math/Vector.h index 3e781883b..41c8e512b 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -72,6 +72,15 @@ template class Vector { template inline constexpr Vector(T first, U&&... next); #endif + /** + * @brief Constructor + * @param value Value for all fields + */ + inline explicit Vector(T value) { + for(size_t i = 0; i != size; ++i) + _data[i] = value; + } + /** @brief Copy constructor */ inline constexpr Vector(const Vector& other) = default; diff --git a/src/Math/Vector2.h b/src/Math/Vector2.h index c99c4083c..7dbba24f4 100644 --- a/src/Math/Vector2.h +++ b/src/Math/Vector2.h @@ -36,8 +36,8 @@ template class Vector2: public Vector { return *reinterpret_cast*>(data); } - /** @copydoc Vector::Vector */ - inline constexpr Vector2() {} + /** @copydoc Vector::Vector(T) */ + inline constexpr explicit Vector2(T value = T()): Vector(value, value) {} /** @copydoc Vector::Vector(const Vector&) */ inline constexpr Vector2(const Vector& other): Vector(other) {} diff --git a/src/Math/Vector3.h b/src/Math/Vector3.h index 61aa55770..8c92a5215 100644 --- a/src/Math/Vector3.h +++ b/src/Math/Vector3.h @@ -52,8 +52,8 @@ template class Vector3: public Vector { a[0]*b[1]-a[1]*b[0]); } - /** @copydoc Vector::Vector */ - inline constexpr Vector3() {} + /** @copydoc Vector::Vector(T) */ + inline constexpr explicit Vector3(T value = T()): Vector(value, value, value) {} /** @copydoc Vector::Vector(const Vector&) */ inline constexpr Vector3(const Vector& other): Vector(other) {} diff --git a/src/Math/Vector4.h b/src/Math/Vector4.h index 48491bcec..403e08332 100644 --- a/src/Math/Vector4.h +++ b/src/Math/Vector4.h @@ -43,6 +43,9 @@ template class Vector4: public Vector { */ inline constexpr Vector4(): Vector(T(0), T(0), T(0), T(1)) {} + /** @copydoc Vector::Vector(T) */ + inline constexpr explicit Vector4(T value): Vector(value, value, value, value) {} + /** @copydoc Vector::Vector(const Vector&) */ inline constexpr Vector4(const Vector& other): Vector(other) {}