Browse Source

Math: expand UnderlyingTypeOf to support vectors and matrices.

This allows getting underlying type of those without needing to include
them. Yay.
pull/368/head
Vladimír Vondruš 7 years ago
parent
commit
4bc84f6815
  1. 7
      src/Magnum/Math/Test/TypeTraitsTest.cpp
  2. 32
      src/Magnum/Math/TypeTraits.h

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

@ -247,8 +247,15 @@ void TypeTraitsTest::isUnitless() {
void TypeTraitsTest::underlyingTypeOf() { void TypeTraitsTest::underlyingTypeOf() {
CORRADE_VERIFY((std::is_same<UnderlyingTypeOf<Int>, Int>::value)); CORRADE_VERIFY((std::is_same<UnderlyingTypeOf<Int>, Int>::value));
CORRADE_VERIFY((std::is_same<UnderlyingTypeOf<Deg<Float>>, Float>::value)); CORRADE_VERIFY((std::is_same<UnderlyingTypeOf<Deg<Float>>, Float>::value));
CORRADE_VERIFY((std::is_same<UnderlyingTypeOf<Unit<Rad, Double>>, Double>::value)); CORRADE_VERIFY((std::is_same<UnderlyingTypeOf<Unit<Rad, Double>>, Double>::value));
CORRADE_VERIFY((std::is_same<UnderlyingTypeOf<Vector2<UnsignedByte>>, UnsignedByte>::value));
CORRADE_VERIFY((std::is_same<UnderlyingTypeOf<Color3<Float>>, Float>::value));
CORRADE_VERIFY((std::is_same<UnderlyingTypeOf<Matrix2x3<Double>>, Double>::value));
CORRADE_VERIFY((std::is_same<UnderlyingTypeOf<Matrix4<Float>>, Float>::value));
} }
template<class T> void TypeTraitsTest::equalsIntegral() { template<class T> void TypeTraitsTest::equalsIntegral() {

32
src/Magnum/Math/TypeTraits.h

@ -250,20 +250,36 @@ namespace Implementation {
}; };
template<class T> struct UnderlyingType<Deg<T>> { typedef T Type; }; template<class T> struct UnderlyingType<Deg<T>> { typedef T Type; };
template<class T> struct UnderlyingType<Rad<T>> { typedef T Type; }; template<class T> struct UnderlyingType<Rad<T>> { typedef T Type; };
template<std::size_t size, class T> struct UnderlyingType<Vector<size, T>> {
typedef T Type;
};
template<class T> struct UnderlyingType<Vector2<T>> { typedef T Type; };
template<class T> struct UnderlyingType<Vector3<T>> { typedef T Type; };
template<class T> struct UnderlyingType<Vector4<T>> { typedef T Type; };
template<class T> struct UnderlyingType<Color3<T>> { typedef T Type; };
template<class T> struct UnderlyingType<Color4<T>> { typedef T Type; };
template<std::size_t cols, std::size_t rows, class T> struct UnderlyingType<RectangularMatrix<cols, rows, T>> {
typedef T Type;
};
template<std::size_t size, class T> struct UnderlyingType<Matrix<size, T>> {
typedef T Type;
};
template<class T> struct UnderlyingType<Matrix3<T>> { typedef T Type; };
template<class T> struct UnderlyingType<Matrix4<T>> { typedef T Type; };
} }
/** /**
@brief Underlying builtin type for a scalar type @brief Underlying type of a math type
For builtin types returns the type itself, for wrapped types like @ref Deg or For builtin scalar types returns the type itself, for wrapped types like
@ref Rad returns the underlying builtin type. It's guaranteed that the input @ref Deg or @ref Rad returns the underlying builtin type, for vector and matrix
type is always explicitly convertible to the output type and the output type types the type of their components.
is usable with standard APIs such as @ref std::isinf().
Passed types are required to satisfy @ref IsScalar. All non-scalar Magnum math For scalar types it's guaranteed that the input type is always explicitly
types have a member @cpp typedef @ce ``Type`` containing the underlying type. convertible to the output type and the output type is usable with standard APIs
such as @ref std::isinf().
*/ */
template<class T> using UnderlyingTypeOf = typename Implementation::UnderlyingType<T>::Type; template<class T> using UnderlyingTypeOf = typename Implementation::UnderlyingType<T>::Type;
namespace Implementation { namespace Implementation {
template<class T> struct TypeTraitsDefault { template<class T> struct TypeTraitsDefault {

Loading…
Cancel
Save