Browse Source

Added NumericType and FloatingPointType to Math::MathTypeTraits.

NumericType is corresponding numeric type with size at least the same as
int. It is used in debug operators for Matrix and Vector to prevent
printing chars as characters.

If the type isn't already floating-point, FloatingPointType is
corresponding larger type with sufficient size for normalization of
given integral type.

Also updated type traits for long types, they are now subclassed either
from int or long long based on sizeof(long).
pull/279/head
Vladimír Vondruš 14 years ago
parent
commit
57d6ded237
  1. 81
      src/Math/MathTypeTraits.h
  2. 2
      src/Math/Matrix.h
  3. 2
      src/Math/Vector.h

81
src/Math/MathTypeTraits.h

@ -51,6 +51,22 @@ support given feature, thus forcing the compilation stop with an error.
*/
template<class T> struct MathTypeTraits {
#ifdef DOXYGEN_GENERATING_OUTPUT
/**
* @brief Corresponding numeric type large at least as `int`
*
* Usable e.g. to prevent conversion of `char` to characters when printing
* numeric types to output.
*/
typedef U NumericType;
/**
* @brief Corresponding floating-point type for normalization
*
* If the type is not already floating-point, defines smallest larger
* floating-point type.
*/
typedef U FloatingPointType;
/**
* @brief Epsilon value for fuzzy compare
*
@ -95,29 +111,70 @@ template<class T> struct MathTypeTraitsFloatingPoint {
}
};
template<size_t> struct MathTypeTraitsLong {};
template<> struct MathTypeTraitsLong<8> {
typedef unsigned int UnsignedType;
typedef int Type;
};
template<> struct MathTypeTraitsLong<16> {
typedef unsigned long long UnsignedType;
typedef long long Type;
};
}
template<> struct MathTypeTraits<unsigned char>: public Implementation::MathTypeTraitsIntegral<unsigned char> {};
template<> struct MathTypeTraits<char>: public Implementation::MathTypeTraitsIntegral<char> {};
template<> struct MathTypeTraits<unsigned char>: public Implementation::MathTypeTraitsIntegral<unsigned char> {
typedef unsigned int NumericType;
typedef float FloatingPointType;
};
template<> struct MathTypeTraits<char>: public Implementation::MathTypeTraitsIntegral<char> {
typedef int NumericType;
typedef float FloatingPointType;
};
template<> struct MathTypeTraits<unsigned short>: public Implementation::MathTypeTraitsIntegral<unsigned short> {};
template<> struct MathTypeTraits<short>: public Implementation::MathTypeTraitsIntegral<short> {};
template<> struct MathTypeTraits<unsigned short>: public Implementation::MathTypeTraitsIntegral<unsigned short> {
typedef unsigned int NumericType;
typedef float FloatingPointType;
};
template<> struct MathTypeTraits<short>: public Implementation::MathTypeTraitsIntegral<short> {
typedef int NumericType;
typedef float FloatingPointType;
};
template<> struct MathTypeTraits<unsigned int>: public Implementation::MathTypeTraitsIntegral<unsigned int> {};
template<> struct MathTypeTraits<int>: public Implementation::MathTypeTraitsIntegral<int> {};
template<> struct MathTypeTraits<unsigned int>: public Implementation::MathTypeTraitsIntegral<unsigned int> {
typedef unsigned int NumericType;
typedef double FloatingPointType;
};
template<> struct MathTypeTraits<int>: public Implementation::MathTypeTraitsIntegral<int> {
typedef int NumericType;
typedef double FloatingPointType;
};
/* long is 32 bits somewhere and 64 bits elsewhere, so it cannot be mapped to
any of them */
template<> struct MathTypeTraits<long unsigned int>: public Implementation::MathTypeTraitsIntegral<long unsigned int> {};
template<> struct MathTypeTraits<long int>: public Implementation::MathTypeTraitsIntegral<long int> {};
template<> struct MathTypeTraits<unsigned long long>: public Implementation::MathTypeTraitsIntegral<unsigned long long> {
typedef unsigned long long NumericType;
typedef long double FloatingPointType;
};
template<> struct MathTypeTraits<long long>: public Implementation::MathTypeTraitsIntegral<long long> {
typedef long long NumericType;
typedef long double FloatingPointType;
};
template<> struct MathTypeTraits<unsigned long long>: public Implementation::MathTypeTraitsIntegral<unsigned long long> {};
template<> struct MathTypeTraits<long long>: public Implementation::MathTypeTraitsIntegral<long long> {};
/* long is 32 bits somewhere and 64 bits elsewhere */
template<> struct MathTypeTraits<long unsigned int>: public Implementation::MathTypeTraitsIntegral<typename Implementation::MathTypeTraitsLong<sizeof(long unsigned int)>::Type> {};
template<> struct MathTypeTraits<long int>: public Implementation::MathTypeTraitsIntegral<typename Implementation::MathTypeTraitsLong<sizeof(long int)>::Type> {};
template<> struct MathTypeTraits<float>: public Implementation::MathTypeTraitsFloatingPoint<float> {
typedef float NumericType;
typedef float FloatingPointType;
inline constexpr static float epsilon() { return FLOAT_EQUALITY_PRECISION; }
};
template<> struct MathTypeTraits<double>: public Implementation::MathTypeTraitsFloatingPoint<double> {
typedef float NumericType;
typedef double FloatingPointType;
inline constexpr static double epsilon() { return DOUBLE_EQUALITY_PRECISION; }
};
#endif

2
src/Math/Matrix.h

@ -260,7 +260,7 @@ template<class T, size_t size> Corrade::Utility::Debug operator<<(Corrade::Utili
if(row != 0) debug << ",\n ";
for(size_t col = 0; col != size; ++col) {
if(col != 0) debug << ", ";
debug << value[col][row];
debug << typename MathTypeTraits<T>::NumericType(value[col][row]);
}
}
debug << ')';

2
src/Math/Vector.h

@ -324,7 +324,7 @@ template<class T, size_t size> Corrade::Utility::Debug operator<<(Corrade::Utili
debug.setFlag(Corrade::Utility::Debug::SpaceAfterEachValue, false);
for(size_t i = 0; i != size; ++i) {
if(i != 0) debug << ", ";
debug << value[i];
debug << typename MathTypeTraits<T>::NumericType(value[i]);
}
debug << ')';
debug.setFlag(Corrade::Utility::Debug::SpaceAfterEachValue, true);

Loading…
Cancel
Save