13#ifndef ML_TYPE_TRAITS_H
14#define ML_TYPE_TRAITS_H
18#ifndef __mlRangeCasts_H
24#include <ThirdPartyWarningsDisable.h>
26#include <ThirdPartyWarningsRestore.h>
90#define _ML_DECLARE_SCALAR_TYPETRAIT_BODY(TYPE, FLAGS) \
91 template <> struct DataTypeSelector<TYPE##Type> { typedef TYPE Type; }; \
92 template <> struct TypeTraits<TYPE > { \
93 typedef TYPE ComponentType; \
94 typedef double IntermediateType; \
96 dataType = TYPE##Type, \
97 isBuiltInFloatType = (((FLAGS) & ML_FLOAT_TYPE) != 0), \
98 isBuiltInIntegerType = (((FLAGS) & ML_INTEGER_TYPE) != 0), \
99 isScalarType = (((FLAGS) & (ML_INTEGER_TYPE | ML_FLOAT_TYPE)) != 0), \
100 isStandardType = isScalarType, \
101 isBuiltInSignedType = (((FLAGS) & ML_SIGNED_TYPE) != 0), \
102 is8BitInteger = ((((FLAGS) & ML_INTEGER_TYPE) != 0) && (sizeof(TYPE)==sizeof(MLuint8)) ), \
103 is16BitInteger = ((((FLAGS) & ML_INTEGER_TYPE) != 0) && (sizeof(TYPE)==sizeof(MLuint16)) ), \
104 is32BitInteger = ((((FLAGS) & ML_INTEGER_TYPE) != 0) && (sizeof(TYPE)==sizeof(MLuint32)) ), \
105 is64BitInteger = ((((FLAGS) & ML_INTEGER_TYPE) != 0) && (sizeof(TYPE)==sizeof(MLuint64)) ), \
106 is8_16_32BitInteger = ((((FLAGS) & ML_INTEGER_TYPE) != 0) && \
107 ((sizeof(TYPE)==sizeof(MLuint8)) || \
108 (sizeof(TYPE)==sizeof(MLuint16)) || \
109 (sizeof(TYPE)==sizeof(MLuint32)))), \
113 static inline bool matches(MLDataType dt) { return dt == dataType; } \
114 static inline MLDataType getDataType() { return dataType; } \
118#define ML_DECLARE_SCALAR_TYPETRAIT(TYPE, FLAGS) \
119 _ML_DECLARE_SCALAR_TYPETRAIT_BODY(TYPE, FLAGS)
121#define _ML_DECLARE_EXTENDED_TYPETRAIT_BODY(TYPE, TYPE_ID, COMPONENT_TYPE) \
122 template <> struct DataTypeSelector<TYPE_ID> { typedef TYPE Type; }; \
123 template <> struct TypeTraits<TYPE > { \
124 typedef COMPONENT_TYPE ComponentType; \
125 typedef TYPE IntermediateType; \
127 dataType = TYPE_ID, \
128 isBuiltInFloatType = false, \
129 isBuiltInIntegerType = false, \
130 isScalarType = false, \
131 isStandardType = false, \
132 isBuiltInSignedType = false, \
133 is8BitInteger = false, \
134 is16BitInteger = false, \
135 is32BitInteger = false, \
136 is64BitInteger = false, \
137 is8_16_32BitInteger = false, \
141 static inline bool matches(MLDataType dt) { return dt == dataType; } \
142 static inline MLDataType getDataType() { return dataType; } \
146#define ML_DECLARE_EXTENDED_TYPETRAIT(TYPE, TYPE_ID, COMPONENT_TYPE) \
147 _ML_DECLARE_EXTENDED_TYPETRAIT_BODY(TYPE, TYPE_ID, COMPONENT_TYPE)
163#define ML_DECLARE_FLOAT_VECTOR_TYPETRAIT(TEMPLATE_TYPE, BASE_TYPE, TYPE_ID) \
164 ML_DECLARE_EXTENDED_TYPETRAIT(TEMPLATE_TYPE<BASE_TYPE>, TYPE_ID, BASE_TYPE)
171template <
typename DT>
class TQuaternion;
177template <
class DT>
class Tvec2;
181template <
class DT>
class Tvec3;
185template <
class DT>
class Tvec4;
189template <
class DT>
class Tvec5;
193template <
class DT>
class Tvec6;
197template <
class DT>
class Tvec7;
201template <
class DT>
class Tvec8;
205template <
class DT>
class Tvec9;
209template <
class DT>
class Tvec10;
213template <
class DT>
class Tvec16;
217template <
class DT>
class Tvec32;
221template <
class DT>
class Tvec64;
225#undef ML_DECLARE_FLOAT_VECTOR_TYPETRAIT
228#define ML_DECLARE_MATRIX_TYPETRAIT(TEMPLATE_TYPE, BASE_TYPE, TYPE_ID) \
229 ML_DECLARE_EXTENDED_TYPETRAIT(TEMPLATE_TYPE<BASE_TYPE>, TYPE_ID, BASE_TYPE)
232template <
class DT>
class Tmat2;
236template <
class DT>
class Tmat3;
240template <
class DT>
class Tmat4;
244template <
class DT>
class Tmat5;
248template <
class DT>
class Tmat6;
252#undef ML_DECLARE_MATRIX_TYPETRAIT
256template <
typename CompIntType,
int NumDim>
class TVectorNDBase;
258#define INT_VECTOR_EXPANDER(BASE_TYPE, DIM_NUM) TVectorNDBase<BASE_TYPE, DIM_NUM>
260#define ML_DECLARE_INT_VECTOR_TYPETRAIT(BASE_TYPE, DIM_NUM, TYPE_ID) \
261 _ML_DECLARE_EXTENDED_TYPETRAIT_BODY(INT_VECTOR_EXPANDER(BASE_TYPE, DIM_NUM), TYPE_ID, BASE_TYPE)
323#undef ML_DECLARE_INT_VECTOR_TYPETRAIT
324#undef INT_VECTOR_EXPANDER
356 return static_cast<typename ML_NAMESPACE::TypeTraits<T>::ComponentType
>(v);
366inline typename ML_NAMESPACE::TypeTraits<typename ML_NAMESPACE::TypeTraits<T>::IntermediateType>::ComponentType
374inline typename ML_NAMESPACE::TypeTraits<typename ML_NAMESPACE::TypeTraits<T>::IntermediateType>::ComponentType
392 return static_cast<typename ML_NAMESPACE::TypeTraits<T>::IntermediateType
>(v);
396template<
typename T,
typename S>
397inline typename ML_NAMESPACE::TypeTraits<T>::IntermediateType
400 return static_cast<typename ML_NAMESPACE::TypeTraits<T>::IntermediateType
>(v) *
405#if defined(__GNUC__) && (__GNUC__ >= 7)
406#pragma GCC diagnostic push
407#pragma GCC diagnostic warning "-Wfloat-conversion"
421 return static_cast<T
>(
floor(v+0.5));
425#if defined(__GNUC__) && (__GNUC__ >= 7)
426#pragma GCC diagnostic pop
433 return static_cast<T
>(v);
448 return static_cast<T
>(
floor(v+0.5));
455 return static_cast<T
>(v);
#define ML_INVALID_DATA_TYPE
UINT64 MLuint64
Introduce platform-independent 64-bit unsigned integer type.
INT64 MLint64
Include 64-bit integer support for Windows or Unix.
#define ML_DECLARE_INT_VECTOR_TYPETRAIT(BASE_TYPE, DIM_NUM, TYPE_ID)
ml::TypeTraits< T >::ComponentType ml_component_cast(double v)
Casts the argument to the component type of the template argument.
T ml_cast_from_intermediate_type(const typename ml::TypeTraits< T >::IntermediateType &v)
T _ml_cast_from_intermediate_type_without_clamping(const typename ml::TypeTraits< T >::IntermediateType &v, OverloadSelector::OnTrue)
#define ML_DECLARE_SCALAR_TYPETRAIT(TYPE, FLAGS)
Macro to declare custom type traits.
ml::TypeTraits< typenameml::TypeTraits< T >::IntermediateType >::ComponentType ml_scalar_factor_cast(double v)
#define ML_DECLARE_FLOAT_VECTOR_TYPETRAIT(TEMPLATE_TYPE, BASE_TYPE, TYPE_ID)
Declaration of standard integer type traits.
T ml_cast_from_intermediate_type_without_clamping(const typename ml::TypeTraits< T >::IntermediateType &v)
ml::TypeTraits< T >::IntermediateType ml_cast_to_intermediate_type(const T &v)
#define ML_DECLARE_MATRIX_TYPETRAIT(TEMPLATE_TYPE, BASE_TYPE, TYPE_ID)
T _ml_cast_from_intermediate_type(const typename ml::TypeTraits< T >::IntermediateType &v, OverloadSelector::OnTrue)
ml::TypeTraits< T >::IntermediateType ml_cast_to_intermediate_type_and_multiply(const T &v, S s)
Same as above, but will also multiply with the second argument.
T ml_cast_from_scalar(double v)
Casts the (scalar) argument to the type of the template argument.
SwitchType< true > OnTrue
SwitchType< ml::TypeTraits< T >::isBuiltInIntegerType > isBuiltInIntegerType()
SwitchType< false > OnFalse
SwitchType< ml::TypeTraits< T >::isStandardType > isScalarType()
Helper types to switch between implementations of functions by overloading.
Defines a template to get the minimum and maximum values for each basic integer type.
static MLDataType getDataType()
Returns the data type.
static bool matches(MLDataType)
@ is8_16_32BitInteger
Flag that is true for ML[u]int(8|16|32) types.
@ isBuiltInFloatType
Flag that is true if the type is a built-in floating point type (float, double).
@ isBuiltInIntegerType
Flag that is true if the type is a built-in integer type (ML[u]int[8|16|32|64).
@ is8BitInteger
Flag that is true for ML[u]int8 types.
@ isUnknown
Flag that is false for all known ML types and true for unknown types (e.g., TypeTraits<Module>::isUnk...
@ dataType
Gives the data type ID for the given type, e.g., TypeTrait<MLuint8>::dataType == MLuint8Type,...
@ is32BitInteger
Flag that is true for ML[u]int32 types.
@ is64BitInteger
Flag that is true for ML[u]int64 types.
@ isBuiltInSignedType
Flag that is true if the type supports negative values (only for built-in types, use isSignedType() m...
@ isScalarType
Flag that is true for all built-in scalar types (and false for extended types).
@ is16BitInteger
Flag that is true for ML[u]int16 types.