13#ifndef ML_RANGE_CASTS_H
14#define ML_RANGE_CASTS_H
22#include <ThirdPartyWarningsDisable.h>
24#include <ThirdPartyWarningsRestore.h>
30 #define _ML_COMPILE_RANGE_CASTS_WITH_CHECKS
33 #define _ML_RANGE_ERROR_TYPE ML_PRINT_FATAL_ERROR
36 #define _ML_OUTPUT_RANGE_ERROR \
37 _ML_RANGE_ERROR_TYPE("_MLRangeCheck::checked_cast", ML_OUT_OF_RANGE, "Invalid numeric cast (range check failed).");
52 static const signed char MinValue = 0;
53 static const unsigned char MaxValue = UCHAR_MAX;
54 static const bool isFloat =
false;
60 static const signed char MinValue = SCHAR_MIN;
61 static const unsigned char MaxValue = SCHAR_MAX;
62 static const bool isFloat =
false;
68 static const signed char MinValue = CHAR_MIN;
69 static const unsigned char MaxValue = CHAR_MAX;
70 static const bool isFloat =
false;
76 static const signed short MinValue = 0;
77 static const unsigned short MaxValue = USHRT_MAX;
78 static const bool isFloat =
false;
84 static const short MinValue = SHRT_MIN;
85 static const unsigned short MaxValue = SHRT_MAX;
86 static const bool isFloat =
false;
92 static const int MinValue = 0;
93 static const unsigned int MaxValue = UINT_MAX;
94 static const bool isFloat =
false;
100 static const int MinValue = INT_MIN;
101 static const unsigned int MaxValue = INT_MAX;
102 static const bool isFloat =
false;
108 static const long MinValue = 0;
109 static const unsigned long MaxValue = ULONG_MAX;
110 static const bool isFloat =
false;
116 static const long MinValue = LONG_MIN;
117 static const unsigned long MaxValue = LONG_MAX;
118 static const bool isFloat =
false;
124 static const long long MinValue = 0;
125 static const unsigned long long MaxValue = ULLONG_MAX;
126 static const bool isFloat =
false;
132 static const long long MinValue = LLONG_MIN;
133 static const unsigned long long MaxValue = LLONG_MAX;
134 static const bool isFloat =
false;
143 static const bool isFloat =
true;
152 static const bool isFloat =
true;
157 #if defined(_ML_COMPILE_RANGE_CASTS_WITH_CHECKS)
163 template <
bool CheckLowerBounds,
bool CheckUpperBounds,
typename Target,
typename Source>
166 template <
typename Target,
typename Source>
176 return static_cast<Target
>(srcVal);
180 template <
typename Target,
typename Source>
189 return static_cast<Target
>(srcVal);
193 template <
typename Target,
typename Source>
202 return static_cast<Target
>(srcVal);
206 template <
typename Target,
typename Source>
211 return static_cast<Target
>(srcVal);
218 template <
typename Target,
typename Source>
223 return static_cast<Target
>(arg);
233 if ((arg <
static_cast<double>(-FLT_MAX)) ||
234 (arg >
static_cast<double>(FLT_MAX)))
238 return static_cast<float>(arg);
246 template <
bool isTargetFloat,
bool isSourceFloat,
typename Target,
typename Source>
250 template <
typename Target,
typename Source>
263 template <
typename Target,
typename Source>
268 return static_cast<Target
>(arg);
273 template <
typename Target,
typename Source>
278 arg = floor(arg + 0.5f);
284 return static_cast<Target
>(arg);
288 template <
typename Target,
typename Source>
304 #if defined(_ML_COMPILE_RANGE_CASTS_ONLY_AS_CASTS) && defined(_ML_COMPILE_RANGE_CASTS_WITH_CHECKS)
305 #error "_ML_COMPILE_RANGE_CASTS_ONLY_AS_CASTS and _ML_COMPILE_RANGE_CAST_FUNCTIONS_WITH_CHECKS must not be set both."
307 #if !defined(_ML_COMPILE_RANGE_CASTS_ONLY_AS_CASTS) && !defined(_ML_COMPILE_RANGE_CASTS_WITH_CHECKS)
310 template<Target, Source>
315 #if defined(_ML_COMPILE_RANGE_CASTS_ONLY_AS_CASTS)
318 template<Target, Source>
319 inline Target
mlrange_cast(Source arg) {
return static_cast<Target
>(arg); }
331 template<
typename Target,
typename Source>
#define _ML_OUTPUT_RANGE_ERROR
To avoid duplication...
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
static float checked_cast(double arg)
static Target checked_cast(Source arg)
static Target checked_cast(Source srcVal)
static Target checked_cast(Source srcVal)
static Target checked_cast(Source srcVal)
static Target checked_cast(Source srcVal)
Compile check functions only if needed.
static Target checked_cast(Source arg)
static Target checked_cast(Source arg)
static Target checked_cast(Source arg)
static Target checked_cast(Source arg)
Utility template that multiplexes between the different combinations of casting between integer and f...
Define a template to get the min and max values for each basic integer type.