From 68b36c8c86dbc453c615b0e9555308cfcc406151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 25 Oct 2013 17:14:15 +0200 Subject: [PATCH] Deprecate swizzle() in favor of Math::swizzle(). All the functionality is moved to Math::swizzle() and the result is casted to given type only if its header is included. Thus it is possible to remove include dependency on Color. The original swizzle() is now just an alias marked as deprecated and will be removed in future release. --- src/CMakeLists.txt | 4 +- src/Color.h | 7 +++ src/Math/Swizzle.h | 27 +++++++----- src/Math/Test/Vector2Test.cpp | 8 ++++ src/Math/Test/Vector3Test.cpp | 7 +++ src/Math/Test/Vector4Test.cpp | 8 ++++ src/Math/Vector2.h | 5 +++ src/Math/Vector3.h | 4 ++ src/Math/Vector4.h | 4 ++ src/Swizzle.h | 48 ++++++-------------- src/Test/CMakeLists.txt | 1 - src/Test/ColorTest.cpp | 19 ++++++++ src/Test/SwizzleTest.cpp | 83 ----------------------------------- 13 files changed, 93 insertions(+), 132 deletions(-) delete mode 100644 src/Test/SwizzleTest.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9ccf64856..2e681f3e2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -129,7 +129,6 @@ set(Magnum_HEADERS ResourceManager.h Sampler.h Shader.h - Swizzle.h Texture.h TextureFormat.h Timeline.h @@ -140,7 +139,8 @@ set(Magnum_HEADERS # Deprecated headers if(BUILD_DEPRECATED) set(Magnum_HEADERS ${Magnum_HEADERS} - ImageFormat.h) + ImageFormat.h + Swizzle.h) endif() # Desktop-only headers and libraries diff --git a/src/Color.h b/src/Color.h index e8dc05537..2cc4e2ba1 100644 --- a/src/Color.h +++ b/src/Color.h @@ -367,6 +367,13 @@ template inline Debug operator<<(Debug debug, const BasicColor4& val return debug << static_cast&>(value); } +namespace Math { namespace Implementation { + template struct TypeForSize<3, BasicColor3> { typedef BasicColor3 Type; }; + template struct TypeForSize<3, BasicColor4> { typedef BasicColor3 Type; }; + template struct TypeForSize<4, BasicColor3> { typedef BasicColor4 Type; }; + template struct TypeForSize<4, BasicColor4> { typedef BasicColor4 Type; }; +}} + } namespace Corrade { namespace Utility { diff --git a/src/Math/Swizzle.h b/src/Math/Swizzle.h index 75046860d..0a8606ffa 100644 --- a/src/Math/Swizzle.h +++ b/src/Math/Swizzle.h @@ -54,6 +54,10 @@ namespace Implementation { template struct Component { template constexpr static T value(const Math::Vector&) { return T(1); } }; + + template struct TypeForSize { + typedef Math::Vector Type; + }; } /** @@ -61,23 +65,22 @@ namespace Implementation { Creates new vector from given components. Example: @code -Vector4 original(-1, 2, 3, 4); +Vector4i original(-1, 2, 3, 4); auto vec = swizzle<'w', '1', '0', 'x', 'y', 'z'>(original); // vec == { 4, 1, 0, -1, 2, 3 } @endcode -You can use letters `x`, `y`, `z`, `w` for addressing components or letters -`0` and `1` for zero and one. Count of elements is unlimited, but must be at -least one. - -See also Magnum::swizzle() which has some added convenience features not -present in this lightweight implementation for Math namespace. - -@see @ref matrix-vector-component-access, Vector4::xyz(), - Vector4::xy(), Vector3::xy() +You can use letters `x`, `y`, `z`, `w` and `r`, `g`, `b`, `a` for addressing +components or letters `0` and `1` for zero and one. Count of elements is +unlimited, but must be at least one. If the resulting vector is two, three or +four-component, corresponding @ref Math::Vector2, @ref Math::Vector3, +@ref Math::Vector4, @ref Color3 or @ref Color4 specialization is returned. + +@see @ref matrix-vector-component-access, @ref Vector4::xyz(), + @ref Vector4::xy(), @ref Vector3::xy(), @ref Color4::rgb() */ -template constexpr Vector swizzle(const Vector& vector) { - return {Implementation::Component::value(vector)...}; +template constexpr typename Implementation::TypeForSize::Type swizzle(const T& vector) { + return {Implementation::Component::value(vector)...}; } }} diff --git a/src/Math/Test/Vector2Test.cpp b/src/Math/Test/Vector2Test.cpp index b49c74aee..902bbf5fa 100644 --- a/src/Math/Test/Vector2Test.cpp +++ b/src/Math/Test/Vector2Test.cpp @@ -69,6 +69,7 @@ class Vector2Test: public Corrade::TestSuite::Tester { void perpendicular(); void aspectRatio(); + void swizzleType(); void debug(); void configuration(); }; @@ -93,6 +94,7 @@ Vector2Test::Vector2Test() { &Vector2Test::perpendicular, &Vector2Test::aspectRatio, + &Vector2Test::swizzleType, &Vector2Test::debug, &Vector2Test::configuration}); } @@ -203,6 +205,12 @@ void Vector2Test::aspectRatio() { CORRADE_COMPARE(a.aspectRatio(), 0.75f); } +void Vector2Test::swizzleType() { + constexpr Vector<4, Int> orig; + constexpr auto a = swizzle<'y', 'a'>(orig); + CORRADE_VERIFY((std::is_same::value)); +} + void Vector2Test::debug() { std::ostringstream o; Debug(&o) << Vector2(0.5f, 15.0f); diff --git a/src/Math/Test/Vector3Test.cpp b/src/Math/Test/Vector3Test.cpp index 7295c3ee5..6af45f4a5 100644 --- a/src/Math/Test/Vector3Test.cpp +++ b/src/Math/Test/Vector3Test.cpp @@ -69,6 +69,7 @@ class Vector3Test: public Corrade::TestSuite::Tester { void scales(); void twoComponent(); + void swizzleType(); void debug(); void configuration(); }; @@ -224,6 +225,12 @@ void Vector3Test::twoComponent() { CORRADE_COMPARE(d, 2.0f); } +void Vector3Test::swizzleType() { + constexpr Vector<4, Int> orig; + constexpr auto b = swizzle<'y', 'z', 'a'>(orig); + CORRADE_VERIFY((std::is_same::value)); +} + void Vector3Test::debug() { std::ostringstream o; Debug(&o) << Vector3(0.5f, 15.0f, 1.0f); diff --git a/src/Math/Test/Vector4Test.cpp b/src/Math/Test/Vector4Test.cpp index baa9c43c4..1d9176c77 100644 --- a/src/Math/Test/Vector4Test.cpp +++ b/src/Math/Test/Vector4Test.cpp @@ -67,6 +67,7 @@ class Vector4Test: public Corrade::TestSuite::Tester { void threeComponent(); void twoComponent(); + void swizzleType(); void debug(); void configuration(); }; @@ -90,6 +91,7 @@ Vector4Test::Vector4Test() { &Vector4Test::threeComponent, &Vector4Test::twoComponent, + &Vector4Test::swizzleType, &Vector4Test::debug, &Vector4Test::configuration}); } @@ -215,6 +217,12 @@ void Vector4Test::twoComponent() { CORRADE_COMPARE(d, 1.0f); } +void Vector4Test::swizzleType() { + constexpr Vector4i orig; + constexpr auto c = swizzle<'y', 'a', 'y', 'x'>(orig); + CORRADE_VERIFY((std::is_same::value)); +} + void Vector4Test::debug() { std::ostringstream o; Debug(&o) << Vector4(0.5f, 15.0f, 1.0f, 1.0f); diff --git a/src/Math/Vector2.h b/src/Math/Vector2.h index 2fb887fd8..5f7d31968 100644 --- a/src/Math/Vector2.h +++ b/src/Math/Vector2.h @@ -153,6 +153,11 @@ template inline Corrade::Utility::Debug operator<<(Corrade::Utility::De return debug << static_cast&>(value); } +namespace Implementation { + template struct TypeForSize; + template struct TypeForSize<2, T> { typedef Math::Vector2 Type; }; +} + }} namespace Corrade { namespace Utility { diff --git a/src/Math/Vector3.h b/src/Math/Vector3.h index 11a8aa8ea..4da38e208 100644 --- a/src/Math/Vector3.h +++ b/src/Math/Vector3.h @@ -213,6 +213,10 @@ template inline Corrade::Utility::Debug operator<<(Corrade::Utility::De return debug << static_cast&>(value); } +namespace Implementation { + template struct TypeForSize<3, T> { typedef Math::Vector3 Type; }; +} + }} namespace Corrade { namespace Utility { diff --git a/src/Math/Vector4.h b/src/Math/Vector4.h index 068af33b3..260d7e20f 100644 --- a/src/Math/Vector4.h +++ b/src/Math/Vector4.h @@ -177,6 +177,10 @@ template inline Corrade::Utility::Debug operator<<(Corrade::Utility::De return debug << static_cast&>(value); } +namespace Implementation { + template struct TypeForSize<4, T> { typedef Math::Vector4 Type; }; +} + }} namespace Corrade { namespace Utility { diff --git a/src/Swizzle.h b/src/Swizzle.h index 194bb8e39..ecf717e8a 100644 --- a/src/Swizzle.h +++ b/src/Swizzle.h @@ -25,50 +25,30 @@ */ /** @file /Swizzle.h - * @brief Function Magnum::swizzle() + * @brief Function @ref Magnum::swizzle() + * @deprecated Use @ref Math/Swizzle.h instead. */ #include "Math/Swizzle.h" + +#ifdef MAGNUM_BUILD_DEPRECATED #include "Color.h" namespace Magnum { -namespace Implementation { - template struct TypeForSize { - typedef Math::Vector Type; - }; - template struct TypeForSize<2, T> { typedef Math::Vector2 Type; }; - template struct TypeForSize<3, T> { typedef Math::Vector3 Type; }; - template struct TypeForSize<4, T> { typedef Math::Vector4 Type; }; - template struct TypeForSize<3, BasicColor3> { typedef BasicColor3 Type; }; - template struct TypeForSize<3, BasicColor4> { typedef BasicColor3 Type; }; - template struct TypeForSize<4, BasicColor3> { typedef BasicColor4 Type; }; - template struct TypeForSize<4, BasicColor4> { typedef BasicColor4 Type; }; -} - /** -@brief Swizzle Vector components - -Creates new vector from given components. Example: -@code -Vector4i original(-1, 2, 3, 4); - -auto vec = swizzle<'a', '1', '0', 'r', 'g', 'b'>(original); -// vec == { 4, 1, 0, -1, 2, 3 } -@endcode -You can use letters `x`, `y`, `z`, `w` and `r`, `g`, `b`, `a` for addressing -components or letters `0` and `1` for zero and one. Count of elements is -unlimited, but must be at least one. If the resulting vector is two, three or -four-component, corresponding Math::Vector2, Math::Vector3, Math::Vector4, -Color3 or Color4 specialization is returned. - -@see @ref matrix-vector-component-access, Math::swizzle(), Vector4::xyz(), - Color4::rgb(), Vector4::xy(), Vector3::xy() +@copybrief Math::swizzle() +@deprecated Use @ref Magnum::Math::swizzle() "Math::swizzle()" instead. */ -template constexpr typename Implementation::TypeForSize::Type swizzle(const T& vector) { - return {Math::Implementation::Component::value(vector)...}; -} +#ifdef DOXYGEN_GENERATING_OUTPUT +template constexpr typename Math::Implementation::TypeForSize::Type swizzle(const T& vector); +#else +using Math::swizzle; +#endif } +#else +#error +#endif #endif diff --git a/src/Test/CMakeLists.txt b/src/Test/CMakeLists.txt index 8f10d79ab..f3c741aed 100644 --- a/src/Test/CMakeLists.txt +++ b/src/Test/CMakeLists.txt @@ -33,7 +33,6 @@ corrade_add_test(MeshTest MeshTest.cpp LIBRARIES Magnum) corrade_add_test(RendererTest RendererTest.cpp LIBRARIES Magnum) corrade_add_test(ResourceManagerTest ResourceManagerTest.cpp LIBRARIES MagnumTestLib) corrade_add_test(SamplerTest SamplerTest.cpp LIBRARIES Magnum) -corrade_add_test(SwizzleTest SwizzleTest.cpp LIBRARIES MagnumMathTestLib) if(BUILD_GL_TESTS) corrade_add_test(BufferGLTest BufferGLTest.cpp LIBRARIES ${GL_TEST_LIBRARIES}) diff --git a/src/Test/ColorTest.cpp b/src/Test/ColorTest.cpp index d4493b9f3..657802f65 100644 --- a/src/Test/ColorTest.cpp +++ b/src/Test/ColorTest.cpp @@ -46,6 +46,7 @@ class ColorTest: public TestSuite::Tester { void hsvOverflow(); void hsvAlpha(); + void swizzleType(); void debug(); void configuration(); }; @@ -66,6 +67,7 @@ ColorTest::ColorTest() { &ColorTest::hsvOverflow, &ColorTest::hsvAlpha, + &ColorTest::swizzleType, &ColorTest::debug, &ColorTest::configuration}); } @@ -137,6 +139,23 @@ void ColorTest::hsvAlpha() { CORRADE_COMPARE(Color4ub::fromHSV(Deg(230.0f), 0.749f, 0.427f, 23), Color4ub(27, 40, 108, 23)); } +void ColorTest::swizzleType() { + constexpr Color3 origColor3; + constexpr BasicColor4 origColor4; + + constexpr auto a = Math::swizzle<'y', 'z', 'r'>(origColor3); + CORRADE_VERIFY((std::is_same::value)); + + constexpr auto b = Math::swizzle<'y', 'z', 'a'>(origColor4); + CORRADE_VERIFY((std::is_same>::value)); + + constexpr auto c = Math::swizzle<'y', 'z', 'y', 'x'>(origColor3); + CORRADE_VERIFY((std::is_same::value)); + + constexpr auto d = Math::swizzle<'y', 'a', 'y', 'x'>(origColor4); + CORRADE_VERIFY((std::is_same>::value)); +} + void ColorTest::debug() { std::ostringstream o; Debug(&o) << Color3(0.5f, 0.75f, 1.0f); diff --git a/src/Test/SwizzleTest.cpp b/src/Test/SwizzleTest.cpp deleted file mode 100644 index 2eeb9b37d..000000000 --- a/src/Test/SwizzleTest.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/* - This file is part of Magnum. - - Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. -*/ - -#include - -#include "Swizzle.h" - -namespace Magnum { namespace Test { - -class SwizzleTest: public TestSuite::Tester { - public: - SwizzleTest(); - - void type(); - void defaultType(); -}; - -SwizzleTest::SwizzleTest() { - addTests({&SwizzleTest::type, - &SwizzleTest::defaultType}); -} - -void SwizzleTest::type() { - constexpr Vector4i orig; - constexpr Color3 origColor3; - constexpr BasicColor4 origColor4; - - constexpr auto a = swizzle<'y', 'a'>(orig); - CORRADE_VERIFY((std::is_same::value)); - - constexpr auto b = swizzle<'y', 'z', 'a'>(orig); - CORRADE_VERIFY((std::is_same::value)); - - constexpr auto c = swizzle<'y', 'a', 'y', 'x'>(orig); - CORRADE_VERIFY((std::is_same::value)); - - constexpr auto d = swizzle<'y', 'z', 'r'>(origColor3); - CORRADE_VERIFY((std::is_same::value)); - - constexpr auto e = swizzle<'y', 'z', 'a'>(origColor4); - CORRADE_VERIFY((std::is_same>::value)); - - constexpr auto f = swizzle<'y', 'z', 'y', 'x'>(origColor3); - CORRADE_VERIFY((std::is_same::value)); - - constexpr auto g = swizzle<'y', 'a', 'y', 'x'>(origColor4); - CORRADE_VERIFY((std::is_same>::value)); -} - -void SwizzleTest::defaultType() { - constexpr Vector4i orig(1, 2, 3, 4); - - constexpr auto a = swizzle<'b'>(orig); - CORRADE_COMPARE(a, (Math::Vector<1, Int>(3))); - - constexpr auto b = swizzle<'b', 'r', 'a', 'g', 'z', 'y', 'x'>(orig); - CORRADE_COMPARE(b, (Math::Vector<7, Int>(3, 1, 4, 2, 3, 2, 1))); -} - -}} - -CORRADE_TEST_MAIN(Magnum::Test::SwizzleTest)