Browse Source

Math: added IsUnitless type trait.

pull/342/head
Vladimír Vondruš 7 years ago
parent
commit
7941a769cd
  1. 6
      doc/changelog.dox
  2. 10
      src/Magnum/Math/Test/TypeTraitsTest.cpp
  3. 18
      src/Magnum/Math/TypeTraits.h

6
doc/changelog.dox

@ -123,9 +123,9 @@ See also:
@ref Corrade::Utility::Debug::color modifier
- Added convenience @ref BoolVector2, @ref BoolVector3 and @ref BoolVector4
typedefs to the root namespace
- New @ref Math::IsScalar, @ref Math::IsVector, @ref Math::IsIntegral and
@ref Math::IsFloatingPoint type traits and a @ref Math::UnderlyingTypeOf
utility
- New @ref Math::IsScalar, @ref Math::IsVector, @ref Math::IsIntegral,
@ref Math::IsFloatingPoint and @ref Math::IsUnitless type traits and a
@ref Math::UnderlyingTypeOf utility
@subsubsection changelog-latest-new-platform Platform libraries

10
src/Magnum/Math/Test/TypeTraitsTest.cpp

@ -43,6 +43,7 @@ struct TypeTraitsTest: Corrade::TestSuite::Tester {
void isVector();
void isIntegral();
void isFloatingPoint();
void isUnitless();
void underlyingTypeOf();
@ -107,6 +108,7 @@ TypeTraitsTest::TypeTraitsTest() {
&TypeTraitsTest::isVector,
&TypeTraitsTest::isIntegral,
&TypeTraitsTest::isFloatingPoint,
&TypeTraitsTest::isUnitless,
&TypeTraitsTest::underlyingTypeOf,
@ -231,6 +233,14 @@ void TypeTraitsTest::isFloatingPoint() {
CORRADE_VERIFY(!IsFloatingPoint<char*>::value);
}
void TypeTraitsTest::isUnitless() {
CORRADE_VERIFY(IsUnitless<Int>::value);
CORRADE_VERIFY(IsUnitless<Color4<Float>>::value);
CORRADE_VERIFY(!IsUnitless<Deg<Float>>::value);
CORRADE_VERIFY(!(IsUnitless<Unit<Rad, Double>>::value));
CORRADE_VERIFY(!IsUnitless<char*>::value);
}
void TypeTraitsTest::underlyingTypeOf() {
CORRADE_VERIFY((std::is_same<UnderlyingTypeOf<Int>, Int>::value));
CORRADE_VERIFY((std::is_same<UnderlyingTypeOf<Deg<Float>>, Float>::value));

18
src/Magnum/Math/TypeTraits.h

@ -222,6 +222,24 @@ template<class T> struct IsFloatingPoint<Deg<T>>: IsFloatingPoint<T> {};
template<class T> struct IsFloatingPoint<Rad<T>>: IsFloatingPoint<T> {};
#endif
/**
@brief Whether @p T is a unitless tpye
Equivalent to @ref std::true_type for scalar or vector types that have an
unitless underlying type (i.e., not @ref Deg or @ref Rad); @ref std::false_type
otherwise. Some math functions such as @ref sqrt() or @ref log() work only with
unitless types because the resulting unit couldn't be expressed otherwise.
@see @ref IsScalar, @ref IsVector
*/
template<class T> struct IsUnitless
#ifndef DOXYGEN_GENERATING_OUTPUT
: std::integral_constant<bool, IsScalar<T>::value || IsVector<T>::value>
#endif
{};
template<template<class> class Derived, class T> struct IsUnitless<Unit<Derived, T>>: std::false_type {};
template<class T> struct IsUnitless<Deg<T>>: std::false_type {};
template<class T> struct IsUnitless<Rad<T>>: std::false_type {};
namespace Implementation {
template<class T> struct UnderlyingType {
static_assert(IsScalar<T>::value, "type is not scalar");

Loading…
Cancel
Save