Browse Source

Math: add TypeTraits<T>::min() and max().

Need to use them in various places in the UI library so I might as well
make a generic API for those.
next
Vladimír Vondruš 1 day ago
parent
commit
774ffdbe01
  1. 2
      doc/changelog.dox
  2. 21
      src/Magnum/Math/Test/TypeTraitsTest.cpp
  3. 35
      src/Magnum/Math/TypeTraits.h

2
doc/changelog.dox

@ -337,6 +337,8 @@ See also:
@ref Math::scatter(), which is now marked with @ref CORRADE_NODISCARD to
better convey that it returns the modified value instead of mutating it
in-place
- Added @ref Math::TypeTraits::min() and @relativeref{Math::TypeTraits,max()}
returning minimal and maximal representable values of integer types.
@subsubsection changelog-latest-new-materialtools MaterialTools library

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

@ -24,6 +24,7 @@
DEALINGS IN THE SOFTWARE.
*/
#include <limits> /* for TypeTraits<T>::min() / max() verification */
#include <Corrade/Containers/ArrayView.h>
#include <Corrade/Containers/StringView.h>
#include <Corrade/TestSuite/Tester.h>
@ -50,6 +51,8 @@ struct TypeTraitsTest: TestSuite::Tester {
template<class T> void epsilonConsistentWithCorrade();
template<class T> void minMax();
template<class T> void equalsIntegral();
void equalsHalf();
template<class T> void equalsFloatingPoint0();
@ -117,6 +120,15 @@ TypeTraitsTest::TypeTraitsTest() {
&TypeTraitsTest::epsilonConsistentWithCorrade<Double>,
&TypeTraitsTest::epsilonConsistentWithCorrade<long double>,
&TypeTraitsTest::minMax<UnsignedByte>,
&TypeTraitsTest::minMax<Byte>,
&TypeTraitsTest::minMax<UnsignedShort>,
&TypeTraitsTest::minMax<Short>,
&TypeTraitsTest::minMax<UnsignedInt>,
&TypeTraitsTest::minMax<Int>,
&TypeTraitsTest::minMax<UnsignedLong>,
&TypeTraitsTest::minMax<Long>,
&TypeTraitsTest::equalsIntegral<UnsignedByte>,
&TypeTraitsTest::equalsIntegral<Byte>,
&TypeTraitsTest::equalsIntegral<UnsignedShort>,
@ -260,6 +272,15 @@ template<class T> void TypeTraitsTest::epsilonConsistentWithCorrade() {
CORRADE_VERIFY(TypeTraits<T>::epsilon() == Utility::Implementation::FloatPrecision<T>::epsilon());
}
template<class T> void TypeTraitsTest::minMax() {
setTestCaseTemplateName(TypeTraits<T>::name());
/* Using std::numeric_limits to verify the implementation uses the right
<cstdint> values for the right types */
CORRADE_COMPARE(TypeTraits<T>::min(), std::numeric_limits<T>::min());
CORRADE_COMPARE(TypeTraits<T>::max(), std::numeric_limits<T>::max());
}
template<class T> void TypeTraitsTest::equalsIntegral() {
setTestCaseTemplateName(TypeTraits<T>::name());

35
src/Magnum/Math/TypeTraits.h

@ -370,6 +370,25 @@ template<class T> struct TypeTraits: Implementation::TypeTraitsDefault<T> {
*/
constexpr static T epsilon();
/**
* @brief Minimal representable integer value
* @m_since_latest
*
* Returns @cpp 0 @ce for unsigned integer types and largest representable
* negative value for signed integer types. Not implemented for
* floating-point types.
*/
constexpr static T min();
/**
* @brief Maximal representable integer value
* @m_since_latest
*
* Returns largest representable positive value for integer types. Not
* implemented for floating-point types.
*/
constexpr static T max();
/**
* @brief Fuzzy compare
*
@ -459,27 +478,43 @@ namespace Implementation {
#ifndef DOXYGEN_GENERATING_OUTPUT
template<> struct TypeTraits<UnsignedByte>: Implementation::TypeTraitsIntegral<UnsignedByte> {
typedef Float FloatingPointType;
constexpr static UnsignedByte min() { return 0; }
constexpr static UnsignedByte max() { return UINT8_MAX; }
};
template<> struct TypeTraits<Byte>: Implementation::TypeTraitsIntegral<Byte> {
typedef Float FloatingPointType;
constexpr static Byte min() { return INT8_MIN; }
constexpr static Byte max() { return INT8_MAX; }
};
template<> struct TypeTraits<UnsignedShort>: Implementation::TypeTraitsIntegral<UnsignedShort> {
typedef Float FloatingPointType;
constexpr static UnsignedShort min() { return 0; }
constexpr static UnsignedShort max() { return UINT16_MAX; }
};
template<> struct TypeTraits<Short>: Implementation::TypeTraitsIntegral<Short> {
typedef Float FloatingPointType;
constexpr static Short min() { return INT16_MIN; }
constexpr static Short max() { return INT16_MAX; }
};
template<> struct TypeTraits<UnsignedInt>: Implementation::TypeTraitsIntegral<UnsignedInt> {
typedef Double FloatingPointType;
constexpr static UnsignedInt min() { return 0; }
constexpr static UnsignedInt max() { return UINT32_MAX; }
};
template<> struct TypeTraits<Int>: Implementation::TypeTraitsIntegral<Int> {
typedef Double FloatingPointType;
constexpr static Int min() { return INT32_MIN; }
constexpr static Int max() { return INT32_MAX; }
};
template<> struct TypeTraits<UnsignedLong>: Implementation::TypeTraitsIntegral<UnsignedLong> {
typedef long double FloatingPointType;
constexpr static UnsignedLong min() { return 0; }
constexpr static UnsignedLong max() { return UINT64_MAX; }
};
template<> struct TypeTraits<Long>: Implementation::TypeTraitsIntegral<Long> {
typedef long double FloatingPointType;
constexpr static Long min() { return INT64_MIN; }
constexpr static Long max() { return INT64_MAX; }
};
/* Floating-point scalar types */

Loading…
Cancel
Save