From 4d46fd12cfcb7c985c4b2c83850a0241694beac3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 16 Sep 2012 18:22:33 +0200 Subject: [PATCH] Returning references from vector component accessors. Before it has to be done with overly verbose and cumbersome Java-style: Vector4 vec; vec.setX(vec.x() + 5); vec = Vector4(vec.xyz()*2, vec.w()); Now it can be done this way: vec.x() += 5; vec.xyz() *= 2; --- src/Color.h | 33 ++++++++++++++++----------------- src/Math/Vector2.h | 9 ++++----- src/Math/Vector3.h | 16 ++++++++-------- src/Math/Vector4.h | 23 ++++++++++++----------- src/Test/SwizzleTest.cpp | 3 ++- 5 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/Color.h b/src/Color.h index 304c5408e..30b5af324 100644 --- a/src/Color.h +++ b/src/Color.h @@ -224,13 +224,12 @@ template class Color3: public Math::Vector3 { */ inline constexpr Color3(T r, T g, T b): Math::Vector3(r, g, b) {} - inline constexpr T r() const { return Math::Vector3::x(); } /**< @brief R component */ - inline constexpr T g() const { return Math::Vector3::y(); } /**< @brief G component */ - inline constexpr T b() const { return Math::Vector3::z(); } /**< @brief B component */ - - inline void setR(T value) { Math::Vector3::setX(value); } /**< @brief Set R component */ - inline void setG(T value) { Math::Vector3::setY(value); } /**< @brief Set G component */ - inline void setB(T value) { Math::Vector3::setZ(value); } /**< @brief Set B component */ + inline T& r() { return Math::Vector3::x(); } /**< @brief R component */ + inline constexpr T r() const { return Math::Vector3::x(); } /**< @overload */ + inline T& g() { return Math::Vector3::y(); } /**< @brief G component */ + inline constexpr T g() const { return Math::Vector3::y(); } /**< @overload */ + inline T& b() { return Math::Vector3::z(); } /**< @brief B component */ + inline constexpr T b() const { return Math::Vector3::z(); } /**< @overload */ /** * @brief Convert to HSV @@ -362,15 +361,14 @@ template class Color4: public Math::Vector4 { is fairly common, nearly always with A set to 1 */ inline constexpr Color4(const Math::Vector<3, T>& rgb, T a = Implementation::defaultAlpha()): Math::Vector4(rgb[0], rgb[1], rgb[2], a) {} - inline constexpr T r() const { return Math::Vector4::x(); } /**< @brief R component */ - inline constexpr T g() const { return Math::Vector4::y(); } /**< @brief G component */ - inline constexpr T b() const { return Math::Vector4::z(); } /**< @brief B component */ - inline constexpr T a() const { return Math::Vector4::w(); } /**< @brief A component */ - - inline void setR(T value) { Math::Vector4::setX(value); } /**< @brief Set R component */ - inline void setG(T value) { Math::Vector4::setY(value); } /**< @brief Set G component */ - inline void setB(T value) { Math::Vector4::setZ(value); } /**< @brief Set B component */ - inline void setA(T value) { Math::Vector4::setW(value); } /**< @brief Set A component */ + inline T& r() { return Math::Vector4::x(); } /**< @brief R component */ + inline constexpr T r() const { return Math::Vector4::x(); } /**< @overload */ + inline T& g() { return Math::Vector4::y(); } /**< @brief G component */ + inline constexpr T g() const { return Math::Vector4::y(); } /**< @overload */ + inline T& b() { return Math::Vector4::z(); } /**< @brief B component */ + inline constexpr T b() const { return Math::Vector4::z(); } /**< @overload */ + inline T& a() { return Math::Vector4::w(); } /**< @brief A component */ + inline constexpr T a() const { return Math::Vector4::w(); } /**< @overload */ /** * @brief RGB part of the vector @@ -378,7 +376,8 @@ template class Color4: public Math::Vector4 { * * @see swizzle() */ - inline constexpr Color3 rgb() const { return Math::Vector4::xyz(); } + inline Color3& rgb() { return Math::Vector4::xyz(); } + inline constexpr Color3 rgb() const { return Math::Vector4::xyz(); } /**< @overload */ /** @copydoc Color3::toHSV() */ inline constexpr HSV toHSV() const { diff --git a/src/Math/Vector2.h b/src/Math/Vector2.h index d67e8b0c7..6c72d25f4 100644 --- a/src/Math/Vector2.h +++ b/src/Math/Vector2.h @@ -81,11 +81,10 @@ template class Vector2: public Vector<2, T> { */ inline constexpr Vector2(T x, T y): Vector<2, T>(x, y) {} - inline constexpr T x() const { return (*this)[0]; } /**< @brief X component */ - inline constexpr T y() const { return (*this)[1]; } /**< @brief Y component */ - - inline void setX(T value) { (*this)[0] = value; } /**< @brief Set X component */ - inline void setY(T value) { (*this)[1] = value; } /**< @brief Set Y component */ + inline T& x() { return (*this)[0]; } /**< @brief X component */ + inline constexpr T x() const { return (*this)[0]; } /**< @overload */ + inline T& y() { return (*this)[1]; } /**< @brief Y component */ + inline constexpr T y() const { return (*this)[1]; } /**< @overload */ MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector2, 2) MAGNUM_RECTANGULARMATRIX_SUBCLASS_OPERATOR_IMPLEMENTATION(1, 2, Vector2) diff --git a/src/Math/Vector3.h b/src/Math/Vector3.h index 623bb2c33..001d615a7 100644 --- a/src/Math/Vector3.h +++ b/src/Math/Vector3.h @@ -123,13 +123,12 @@ template class Vector3: public Vector<3, T> { */ inline constexpr Vector3(const Vector<2, T>& xy, T z): Vector<3, T>(xy[0], xy[1], z) {} - inline constexpr T x() const { return (*this)[0]; } /**< @brief X component */ - inline constexpr T y() const { return (*this)[1]; } /**< @brief Y component */ - inline constexpr T z() const { return (*this)[2]; } /**< @brief Z component */ - - inline void setX(T value) { (*this)[0] = value; } /**< @brief Set X component */ - inline void setY(T value) { (*this)[1] = value; } /**< @brief Set Y component */ - inline void setZ(T value) { (*this)[2] = value; } /**< @brief Set Z component */ + inline T& x() { return (*this)[0]; } /**< @brief X component */ + inline constexpr T x() const { return (*this)[0]; } /**< @overload */ + inline T& y() { return (*this)[1]; } /**< @brief Y component */ + inline constexpr T y() const { return (*this)[1]; } /**< @overload */ + inline T& z() { return (*this)[2]; } /**< @brief Z component */ + inline constexpr T z() const { return (*this)[2]; } /**< @overload */ /** * @brief XY part of the vector @@ -137,7 +136,8 @@ template class Vector3: public Vector<3, T> { * * @see swizzle() */ - inline constexpr Vector2 xy() const { return Vector2::from(Vector<3, T>::data()); } + inline Vector2& xy() { return Vector2::from(Vector<3, T>::data()); } + inline constexpr Vector2 xy() const { return Vector2::from(Vector<3, T>::data()); } /**< @overload */ MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector3, 3) MAGNUM_RECTANGULARMATRIX_SUBCLASS_OPERATOR_IMPLEMENTATION(1, 3, Vector3) diff --git a/src/Math/Vector4.h b/src/Math/Vector4.h index 5adfe60ee..e25c85fea 100644 --- a/src/Math/Vector4.h +++ b/src/Math/Vector4.h @@ -61,15 +61,14 @@ template class Vector4: public Vector<4, T> { is fairly common, nearly always with W set to 1 */ inline constexpr Vector4(const Vector<3, T>& xyz, T w = T(1)): Vector<4, T>(xyz[0], xyz[1], xyz[2], w) {} - inline constexpr T x() const { return (*this)[0]; } /**< @brief X component */ - inline constexpr T y() const { return (*this)[1]; } /**< @brief Y component */ - inline constexpr T z() const { return (*this)[2]; } /**< @brief Z component */ - inline constexpr T w() const { return (*this)[3]; } /**< @brief W component */ - - inline void setX(T value) { (*this)[0] = value; } /**< @brief Set X component */ - inline void setY(T value) { (*this)[1] = value; } /**< @brief Set Y component */ - inline void setZ(T value) { (*this)[2] = value; } /**< @brief Set Z component */ - inline void setW(T value) { (*this)[3] = value; } /**< @brief Set W component */ + inline T& x() { return (*this)[0]; } /**< @brief X component */ + inline constexpr T x() const { return (*this)[0]; } /**< @overload */ + inline T& y() { return (*this)[1]; } /**< @brief Y component */ + inline constexpr T y() const { return (*this)[1]; } /**< @overload */ + inline T& z() { return (*this)[2]; } /**< @brief Z component */ + inline constexpr T z() const { return (*this)[2]; } /**< @overload */ + inline T& w() { return (*this)[3]; } /**< @brief W component */ + inline constexpr T w() const { return (*this)[3]; } /**< @overload */ /** * @brief XYZ part of the vector @@ -77,7 +76,8 @@ template class Vector4: public Vector<4, T> { * * @see swizzle() */ - inline constexpr Vector3 xyz() const { return Vector3::from(Vector<4, T>::data()); } + inline Vector3& xyz() { return Vector3::from(Vector<4, T>::data()); } + inline constexpr Vector3 xyz() const { return Vector3::from(Vector<4, T>::data()); } /**< @overload */ /** * @brief XY part of the vector @@ -85,7 +85,8 @@ template class Vector4: public Vector<4, T> { * * @see swizzle() */ - inline constexpr Vector2 xy() const { return Vector2::from(Vector<4, T>::data()); } + inline Vector2& xy() { return Vector2::from(Vector<4, T>::data()); } + inline constexpr Vector2 xy() const { return Vector2::from(Vector<4, T>::data()); } /**< @overload */ MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector4, 4) MAGNUM_RECTANGULARMATRIX_SUBCLASS_OPERATOR_IMPLEMENTATION(1, 3, Vector4) diff --git a/src/Test/SwizzleTest.cpp b/src/Test/SwizzleTest.cpp index 00be3d7ea..561b53fcf 100644 --- a/src/Test/SwizzleTest.cpp +++ b/src/Test/SwizzleTest.cpp @@ -52,7 +52,8 @@ void SwizzleTest::rgba() { void SwizzleTest::fromSmall() { /* Force compile-time evaluation for both */ constexpr Vector2 orig(1, 2); - CORRADE_VERIFY((integral_constant::value)); + constexpr Vector3 swizzled(swizzle(orig, "gxr")); + CORRADE_VERIFY((integral_constant::value)); CORRADE_COMPARE((swizzle<'g', 'x', 'r'>(orig)), Vector3(2, 1, 1)); }