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;
54 static const bool isFloat =
false;
62 static const bool isFloat =
false;
69 static const unsigned char MaxValue =
CHAR_MAX;
70 static const bool isFloat =
false;
76 static const signed short MinValue = 0;
78 static const bool isFloat =
false;
85 static const unsigned short MaxValue =
SHRT_MAX;
86 static const bool isFloat =
false;
92 static const int MinValue = 0;
94 static const bool isFloat =
false;
102 static const bool isFloat =
false;
108 static const long MinValue = 0;
110 static const bool isFloat =
false;
118 static const bool isFloat =
false;
124 static const long long MinValue = 0;
126 static const bool isFloat =
false;
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.