13#ifndef ML_RANGE_CASTS_H
14#define ML_RANGE_CASTS_H
22#include <ThirdPartyWarningsDisable.h>
24#include <ThirdPartyWarningsRestore.h>
31#define _ML_COMPILE_RANGE_CASTS_WITH_CHECKS
34#define _ML_RANGE_ERROR_TYPE ML_PRINT_FATAL_ERROR
37#define _ML_OUTPUT_RANGE_ERROR \
38 _ML_RANGE_ERROR_TYPE("_MLRangeCheck::checked_cast", ML_OUT_OF_RANGE, "Invalid numeric cast (range check failed).");
51 static const signed char MinValue = 0;
53 static const bool isFloat =
false;
60 static const bool isFloat =
false;
66 static const unsigned char MaxValue =
CHAR_MAX;
67 static const bool isFloat =
false;
72 static const signed short MinValue = 0;
74 static const bool isFloat =
false;
80 static const unsigned short MaxValue =
SHRT_MAX;
81 static const bool isFloat =
false;
86 static const int MinValue = 0;
88 static const bool isFloat =
false;
94 static const unsigned int MaxValue =
INT_MAX;
95 static const bool isFloat =
false;
100 static const long MinValue = 0;
102 static const bool isFloat =
false;
109 static const bool isFloat =
false;
114 static const long long MinValue = 0;
116 static const bool isFloat =
false;
123 static const bool isFloat =
false;
131 static const bool isFloat =
true;
139 static const bool isFloat =
true;
144#if defined(_ML_COMPILE_RANGE_CASTS_WITH_CHECKS)
150template <
bool CheckLowerBounds,
bool CheckUpperBounds,
typename Target,
typename Source>
struct _MLIntegerRangeCheck;
161 return static_cast<Target
>(
srcVal);
172 return static_cast<Target
>(
srcVal);
183 return static_cast<Target
>(
srcVal);
191 return static_cast<Target
>(
srcVal);
202 return static_cast<Target
>(arg);
211 if ((arg <
static_cast<double>(-
FLT_MAX)) || (arg >
static_cast<double>(
FLT_MAX))) {
214 return static_cast<float>(arg);
222template <
bool isTargetFloat,
bool isSourceFloat,
typename Target,
typename Source>
struct _MLRangeCheck;
231 Source>::checked_cast(arg);
240 return static_cast<Target
>(arg);
249 arg =
floor(arg + 0.5f);
255 return static_cast<Target
>(arg);
274#if defined(_ML_COMPILE_RANGE_CASTS_ONLY_AS_CASTS) && defined(_ML_COMPILE_RANGE_CASTS_WITH_CHECKS)
275#error "_ML_COMPILE_RANGE_CASTS_ONLY_AS_CASTS and _ML_COMPILE_RANGE_CAST_FUNCTIONS_WITH_CHECKS must not be set both."
277#if !defined(_ML_COMPILE_RANGE_CASTS_ONLY_AS_CASTS) && !defined(_ML_COMPILE_RANGE_CASTS_WITH_CHECKS)
280template <Target, Source>
inline Target
mlrange_cast(Source arg)
287#if defined(_ML_COMPILE_RANGE_CASTS_ONLY_AS_CASTS)
290template <Target, Source>
inline Target
mlrange_cast(Source arg)
292 return static_cast<Target
>(arg);
305template <
typename Target,
typename Source>
inline Target
mlrange_cast(Source arg)
308 Source>::checked_cast(arg);
#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...
Defines a template to get the minimum and maximum values for each basic integer type.