Browse Source

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.
pull/183/head^2
Vladimír Vondruš 10 years ago
parent
commit
52f2d297ca
  1. 11
      doc/namespaces.dox
  2. 5
      doc/types.dox
  3. 2
      src/Magnum/Audio/Test/SourceTest.cpp
  4. 27
      src/Magnum/Magnum.h
  5. 18
      src/Magnum/Math/Angle.h
  6. 2
      src/Magnum/Math/Test/AngleTest.cpp
  7. 2
      src/Magnum/Math/Test/DualQuaternionTest.cpp
  8. 2
      src/Magnum/Math/Test/DualTest.cpp
  9. 2
      src/Magnum/Trade/CameraData.h

11
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

5
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

2
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;

27
src/Magnum/Magnum.h

@ -48,12 +48,16 @@ namespace Math {
#ifndef DOXYGEN_GENERATING_OUTPUT
template<class> struct Constants;
#ifndef MAGNUM_TARGET_GLES
constexpr Rad<Double> operator "" _rad(long double);
constexpr Deg<Double> operator "" _deg(long double);
#endif
constexpr Rad<Float> operator "" _radf(long double);
constexpr Deg<Float> operator "" _degf(long double);
#ifdef MAGNUM_BUILD_DEPRECATED
namespace Literals {
#ifndef MAGNUM_TARGET_GLES
constexpr Rad<Double> operator "" _rad(long double);
constexpr Deg<Double> operator "" _deg(long double);
#endif
constexpr Rad<Float> operator "" _radf(long double);
constexpr Deg<Float> operator "" _degf(long double);
#endif
}
#endif
}
@ -434,13 +438,14 @@ typedef Math::Range3D<Double> 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 */

18
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<Float>
auto radians = 1.047_rad; // type is Rad<Double>
@endcode
@ -151,8 +153,10 @@ template<class T> class Deg: public Unit<Deg, T> {
constexpr /*implicit*/ Deg(Unit<Rad, T> 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<Double> operator "" _deg(long double value) { return Deg<Double>(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<Float> operator "" _degf(long double value) { return Deg<Float>(Float(value)); }
}
/**
@brief Angle in radians
@ -214,8 +220,10 @@ template<class T> class Rad: public Unit<Rad, T> {
constexpr /*implicit*/ Rad(Unit<Deg, T> 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<Double> operator "" _rad(long double value) { return Rad<Double>(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<Float> operator "" _radf(long double value) { return Rad<Float>(Float(value)); }
}
template<class T> constexpr Deg<T>::Deg(Unit<Rad, T> value): Unit<Math::Deg, T>(T(180)*T(value)/Math::Constants<T>::pi()) {}
template<class T> constexpr Rad<T>::Rad(Unit<Deg, T> value): Unit<Math::Rad, T>(T(value)*Math::Constants<T>::pi()/T(180)) {}

2
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<decltype(a), const Degd>::value));

2
src/Magnum/Math/Test/DualQuaternionTest.cpp

@ -101,6 +101,8 @@ typedef Math::DualQuaternion<Float> DualQuaternion;
typedef Math::Quaternion<Float> Quaternion;
typedef Math::Vector3<Float> Vector3;
using namespace Literals;
DualQuaternionTest::DualQuaternionTest() {
addTests({&DualQuaternionTest::construct,
&DualQuaternionTest::constructVectorScalar,

2
src/Magnum/Math/Test/DualTest.cpp

@ -67,6 +67,8 @@ typedef Math::Deg<Float> Deg;
typedef Math::Rad<Float> Rad;
typedef Math::Constants<Float> Constants;
using namespace Literals;
DualTest::DualTest() {
addTests({&DualTest::construct,
&DualTest::constructDefault,

2
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} {}

Loading…
Cancel
Save