From 52f2d297ca9e29920f8fab4cedb33c2e7648e628 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 24 Apr 2016 00:17:26 +0200 Subject: [PATCH] Math: put angle literals into dedicated Literals namespace. Similarly as it is done in STL for C++14 literals, the user has to explicitly put them to scope with `using` keyword to avoid accidental collisions. If MAGNUM_BUILD_DEPRECATED is set, they are still brought to the root namespace, but that will be removed in a future release. --- doc/namespaces.dox | 11 ++++++++- doc/types.dox | 5 +++- src/Magnum/Audio/Test/SourceTest.cpp | 2 ++ src/Magnum/Magnum.h | 27 ++++++++++++--------- src/Magnum/Math/Angle.h | 18 +++++++++++--- src/Magnum/Math/Test/AngleTest.cpp | 2 ++ src/Magnum/Math/Test/DualQuaternionTest.cpp | 2 ++ src/Magnum/Math/Test/DualTest.cpp | 2 ++ src/Magnum/Trade/CameraData.h | 2 +- 9 files changed, 53 insertions(+), 18 deletions(-) diff --git a/doc/namespaces.dox b/doc/namespaces.dox index b8e993080..0964b018d 100644 --- a/doc/namespaces.dox +++ b/doc/namespaces.dox @@ -54,7 +54,7 @@ information. */ /** @dir Magnum/Math - * @brief Namespace @ref Magnum::Math + * @brief Namespace @ref Magnum::Math, @ref Magnum::Math::Literals */ /** @namespace Magnum::Math @brief Math library @@ -65,6 +65,15 @@ This library is built as part of Magnum by default. To use it, you need to find `Magnum` package and link to `Magnum::Magnum` target. See @ref building, @ref cmake, @ref matrix-vector and @ref transformations for more information. */ +/** @namespace Magnum::Math::Literals +@brief Math literals + +Literals for easy construction of angle and color values. + +This library is built as part of Magnum by default. To use it, you need to +find `Magnum` package and link to `Magnum::Magnum` target. See @ref building +and @ref cmake for more information. +*/ /** @dir Magnum/Math/Algorithms * @brief Namespace @ref Magnum::Math::Algorithms diff --git a/doc/types.dox b/doc/types.dox index 71d0df520..c510c8b2c 100644 --- a/doc/types.dox +++ b/doc/types.dox @@ -117,8 +117,11 @@ usability impact in practice. These classes are *not* implicitly constructible or convertible from/to @ref Float or @ref Double, you have to either construct/convert them explicitly -or use custom `_degf`/`_deg` and `_radf`/`_rad` literals: +or use custom `_degf`/`_deg` and `_radf`/`_rad` literals that are provided in +the @ref Math::Literals namespace: @code +using namespace Math::Literals; + //Deg a = 60.0f // error, no implicit conversion from Float Deg a = 60.0_degf; // okay diff --git a/src/Magnum/Audio/Test/SourceTest.cpp b/src/Magnum/Audio/Test/SourceTest.cpp index a6a2e6316..563d66c52 100644 --- a/src/Magnum/Audio/Test/SourceTest.cpp +++ b/src/Magnum/Audio/Test/SourceTest.cpp @@ -144,6 +144,8 @@ void SourceTest::minGain() { } void SourceTest::coneAnglesAndGain() { + using namespace Math::Literals; + Source source; constexpr auto outerAngle = 12.0_degf; constexpr auto innerAngle = 21.0_degf; diff --git a/src/Magnum/Magnum.h b/src/Magnum/Magnum.h index 45e1dd5df..ad4cc9ed4 100644 --- a/src/Magnum/Magnum.h +++ b/src/Magnum/Magnum.h @@ -48,12 +48,16 @@ namespace Math { #ifndef DOXYGEN_GENERATING_OUTPUT template struct Constants; - #ifndef MAGNUM_TARGET_GLES - constexpr Rad operator "" _rad(long double); - constexpr Deg operator "" _deg(long double); - #endif - constexpr Rad operator "" _radf(long double); - constexpr Deg operator "" _degf(long double); + #ifdef MAGNUM_BUILD_DEPRECATED + namespace Literals { + #ifndef MAGNUM_TARGET_GLES + constexpr Rad operator "" _rad(long double); + constexpr Deg operator "" _deg(long double); + #endif + constexpr Rad operator "" _radf(long double); + constexpr Deg operator "" _degf(long double); + #endif + } #endif } @@ -434,13 +438,14 @@ typedef Math::Range3D Range3Dd; /*@}*/ #endif -/* Using angle literals from Math namespace */ +#ifdef MAGNUM_BUILD_DEPRECATED #ifndef MAGNUM_TARGET_GLES -using Math::operator "" _deg; -using Math::operator "" _rad; +using Math::Literals::operator "" _deg; +using Math::Literals::operator "" _rad; +#endif +using Math::Literals::operator "" _degf; +using Math::Literals::operator "" _radf; #endif -using Math::operator "" _degf; -using Math::operator "" _radf; /* Forward declarations for all types in root namespace */ diff --git a/src/Magnum/Math/Angle.h b/src/Magnum/Math/Angle.h index b6a0e3a8d..c5d40cdf4 100644 --- a/src/Magnum/Math/Angle.h +++ b/src/Magnum/Math/Angle.h @@ -49,6 +49,8 @@ conversion less error-prone. You can enter the value either by using literal: @code +using namespace Literals; + auto degrees = 60.0_degf; // type is Deg auto radians = 1.047_rad; // type is Rad @endcode @@ -151,8 +153,10 @@ template class Deg: public Unit { constexpr /*implicit*/ Deg(Unit value); }; +namespace Literals { + #ifndef MAGNUM_TARGET_GLES -/** @relatesalso Deg +/** @relatesalso Magnum::Math::Deg @brief Double-precision degree value literal Example usage: @@ -166,7 +170,7 @@ Double cosine = Math::cos(1.047_rad); // cosine = 0.5 constexpr Deg operator "" _deg(long double value) { return Deg(Double(value)); } #endif -/** @relatesalso Deg +/** @relatesalso Magnum::Math::Deg @brief Single-precision degree value literal Example usage: @@ -178,6 +182,8 @@ Float tangent = Math::tan(1.047_radf); // tangent = 1.732f */ constexpr Deg operator "" _degf(long double value) { return Deg(Float(value)); } +} + /** @brief Angle in radians @@ -214,8 +220,10 @@ template class Rad: public Unit { constexpr /*implicit*/ Rad(Unit value); }; +namespace Literals { + #ifndef MAGNUM_TARGET_GLES -/** @relatesalso Rad +/** @relatesalso Magnum::Math::Rad @brief Double-precision radian value literal See @link operator""_deg() @endlink for more information. @@ -225,7 +233,7 @@ See @link operator""_deg() @endlink for more information. constexpr Rad operator "" _rad(long double value) { return Rad(Double(value)); } #endif -/** @relatesalso Rad +/** @relatesalso Magnum::Math::Rad @brief Single-precision radian value literal See @link operator""_degf() @endlink for more information. @@ -233,6 +241,8 @@ See @link operator""_degf() @endlink for more information. */ constexpr Rad operator "" _radf(long double value) { return Rad(Float(value)); } +} + template constexpr Deg::Deg(Unit value): Unit(T(180)*T(value)/Math::Constants::pi()) {} template constexpr Rad::Rad(Unit value): Unit(T(value)*Math::Constants::pi()/T(180)) {} diff --git a/src/Magnum/Math/Test/AngleTest.cpp b/src/Magnum/Math/Test/AngleTest.cpp index 764ee8d3c..43843e70a 100644 --- a/src/Magnum/Math/Test/AngleTest.cpp +++ b/src/Magnum/Math/Test/AngleTest.cpp @@ -121,6 +121,8 @@ void AngleTest::constructNoInit() { } void AngleTest::literals() { + using namespace Literals; + #ifndef MAGNUM_TARGET_GLES constexpr auto a = 25.0_deg; CORRADE_VERIFY((std::is_same::value)); diff --git a/src/Magnum/Math/Test/DualQuaternionTest.cpp b/src/Magnum/Math/Test/DualQuaternionTest.cpp index a5106ba22..7d8884026 100644 --- a/src/Magnum/Math/Test/DualQuaternionTest.cpp +++ b/src/Magnum/Math/Test/DualQuaternionTest.cpp @@ -101,6 +101,8 @@ typedef Math::DualQuaternion DualQuaternion; typedef Math::Quaternion Quaternion; typedef Math::Vector3 Vector3; +using namespace Literals; + DualQuaternionTest::DualQuaternionTest() { addTests({&DualQuaternionTest::construct, &DualQuaternionTest::constructVectorScalar, diff --git a/src/Magnum/Math/Test/DualTest.cpp b/src/Magnum/Math/Test/DualTest.cpp index 8eeb4aae8..c99cebcbf 100644 --- a/src/Magnum/Math/Test/DualTest.cpp +++ b/src/Magnum/Math/Test/DualTest.cpp @@ -67,6 +67,8 @@ typedef Math::Deg Deg; typedef Math::Rad Rad; typedef Math::Constants Constants; +using namespace Literals; + DualTest::DualTest() { addTests({&DualTest::construct, &DualTest::constructDefault, diff --git a/src/Magnum/Trade/CameraData.h b/src/Magnum/Trade/CameraData.h index e56f45eaa..7daa4acd2 100644 --- a/src/Magnum/Trade/CameraData.h +++ b/src/Magnum/Trade/CameraData.h @@ -77,7 +77,7 @@ class CameraData { }; inline CameraData::CameraData(const Rad fov, const Float near, const Float far, const void* const importerState) noexcept: - _fov{fov != fov ? Rad{35.0_degf} : fov}, + _fov{fov != fov ? Rad{Deg{35.0f}} : fov}, _near{near != near ? 0.01f : near}, _far{far != far ? 100.0f : far}, _importerState{importerState} {}