MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlRangeCasts.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2011, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_RANGE_CASTS_H
14#define ML_RANGE_CASTS_H
15
17
18#include "mlTypeDefs.h"
19#include "mlErrorMacros.h"
20#include "mlErrorOutput.h"
21
22#include <ThirdPartyWarningsDisable.h>
23#include <climits>
24#include <ThirdPartyWarningsRestore.h>
25
27// #define _ML_COMPILE_RANGE_CASTS_ONLY_AS_CASTS
28
31#define _ML_COMPILE_RANGE_CASTS_WITH_CHECKS
32
34#define _ML_RANGE_ERROR_TYPE ML_PRINT_FATAL_ERROR
35
37#define _ML_OUTPUT_RANGE_ERROR \
38 _ML_RANGE_ERROR_TYPE("_MLRangeCheck::checked_cast", ML_OUT_OF_RANGE, "Invalid numeric cast (range check failed).");
39
40//= _ml_numeric_limits ==========================================================================
41
43// The minimums are always returned with the equivalent signed type, while the
44// maximums are returned with the equivalent unsigned type. This is needed so
45// that comparisons of minimums or maximums of types with the same size but different
46// signed-ness do not fail (the compiler would cast to one of the types).
47template <typename T> struct _ml_numeric_limits;
48
49template <> struct _ml_numeric_limits<unsigned char>
50{
51 static const signed char MinValue = 0;
52 static const unsigned char MaxValue = UCHAR_MAX;
53 static const bool isFloat = false;
54};
55
56template <> struct _ml_numeric_limits<signed char>
57{
58 static const signed char MinValue = SCHAR_MIN;
59 static const unsigned char MaxValue = SCHAR_MAX;
60 static const bool isFloat = false;
61};
62
63template <> struct _ml_numeric_limits<char>
64{
65 static const signed char MinValue = CHAR_MIN;
66 static const unsigned char MaxValue = CHAR_MAX;
67 static const bool isFloat = false;
68};
69
71{
72 static const signed short MinValue = 0;
73 static const unsigned short MaxValue = USHRT_MAX;
74 static const bool isFloat = false;
75};
76
77template <> struct _ml_numeric_limits<short>
78{
79 static const short MinValue = SHRT_MIN;
80 static const unsigned short MaxValue = SHRT_MAX;
81 static const bool isFloat = false;
82};
83
84template <> struct _ml_numeric_limits<unsigned int>
85{
86 static const int MinValue = 0;
87 static const unsigned int MaxValue = UINT_MAX;
88 static const bool isFloat = false;
89};
90
91template <> struct _ml_numeric_limits<int>
92{
93 static const int MinValue = INT_MIN;
94 static const unsigned int MaxValue = INT_MAX;
95 static const bool isFloat = false;
96};
97
98template <> struct _ml_numeric_limits<unsigned long>
99{
100 static const long MinValue = 0;
101 static const unsigned long MaxValue = ULONG_MAX;
102 static const bool isFloat = false;
103};
104
105template <> struct _ml_numeric_limits<long>
106{
107 static const long MinValue = LONG_MIN;
108 static const unsigned long MaxValue = LONG_MAX;
109 static const bool isFloat = false;
110};
111
113{
114 static const long long MinValue = 0;
115 static const unsigned long long MaxValue = ULLONG_MAX;
116 static const bool isFloat = false;
117};
118
119template <> struct _ml_numeric_limits<long long>
120{
121 static const long long MinValue = LLONG_MIN;
122 static const unsigned long long MaxValue = LLONG_MAX;
123 static const bool isFloat = false;
124};
125
126template <> struct _ml_numeric_limits<float>
127{
128 // float values cannot be compile-time constants:
129 // static const double MinValue = -FLT_MAX;
130 // static const double MaxValue = FLT_MAX;
131 static const bool isFloat = true;
132};
133
134template <> struct _ml_numeric_limits<double>
135{
136 // float values cannot be compile-time constants:
137 // static const double MinValue = -DBL_MAX;
138 // static const double MaxValue = DBL_MAX;
139 static const bool isFloat = true;
140};
141
142
144#if defined(_ML_COMPILE_RANGE_CASTS_WITH_CHECKS)
145
146//= _MLIntegerRangeCheck ========================================================================
147
150template <bool CheckLowerBounds, bool CheckUpperBounds, typename Target, typename Source> struct _MLIntegerRangeCheck;
151
152template <typename Target, typename Source> struct _MLIntegerRangeCheck<true, true, Target, Source>
153{
154 static inline Target checked_cast(Source srcVal)
155 {
156 if ((srcVal < static_cast<Source>(_ml_numeric_limits<Target>::MinValue)) ||
157 (srcVal > static_cast<Source>(_ml_numeric_limits<Target>::MaxValue)))
158 {
160 }
161 return static_cast<Target>(srcVal);
162 }
163};
164
165template <typename Target, typename Source> struct _MLIntegerRangeCheck<false, true, Target, Source>
166{
167 static inline Target checked_cast(Source srcVal)
168 {
169 if (srcVal > static_cast<Source>(_ml_numeric_limits<Target>::MaxValue)) {
171 }
172 return static_cast<Target>(srcVal);
173 }
174};
175
176template <typename Target, typename Source> struct _MLIntegerRangeCheck<true, false, Target, Source>
177{
178 static inline Target checked_cast(Source srcVal)
179 {
180 if (srcVal < static_cast<Source>(_ml_numeric_limits<Target>::MinValue)) {
182 }
183 return static_cast<Target>(srcVal);
184 }
185};
186
187template <typename Target, typename Source> struct _MLIntegerRangeCheck<false, false, Target, Source>
188{
189 static inline Target checked_cast(Source srcVal)
190 {
191 return static_cast<Target>(srcVal);
192 }
193};
194
195//= _MLFloatRangeCheck ==========================================================================
196
197// The default float range check does nothing.
198template <typename Target, typename Source> struct _MLFloatRangeCheck
199{
200 static inline Target checked_cast(Source arg)
201 {
202 return static_cast<Target>(arg);
203 }
204};
205
206// Only if casting from double to float, a range check is applied.
207template <> struct _MLFloatRangeCheck<float, double>
208{
209 static inline float checked_cast(double arg)
210 {
211 if ((arg < static_cast<double>(-FLT_MAX)) || (arg > static_cast<double>(FLT_MAX))) {
213 }
214 return static_cast<float>(arg);
215 }
216};
217
218//= _MLRangeCheck ===============================================================================
219
222template <bool isTargetFloat, bool isSourceFloat, typename Target, typename Source> struct _MLRangeCheck;
223
224// Casting integer to integer performs (only the necessary) range checks.
225template <typename Target, typename Source> struct _MLRangeCheck<false, false, Target, Source>
226{
233};
234
235// Casting integer to float performs no range checks.
236template <typename Target, typename Source> struct _MLRangeCheck<true, false, Target, Source>
237{
238 static inline Target checked_cast(Source arg)
239 {
240 return static_cast<Target>(arg);
241 }
242};
243
244// Casting float to integer performs range checks.
245template <typename Target, typename Source> struct _MLRangeCheck<false, true, Target, Source>
246{
247 static inline Target checked_cast(Source arg)
248 {
249 arg = floor(arg + 0.5f);
250 if ((arg < static_cast<Source>(_ml_numeric_limits<Target>::MinValue)) ||
251 (arg > static_cast<Source>(_ml_numeric_limits<Target>::MaxValue)))
252 {
254 }
255 return static_cast<Target>(arg);
256 }
257};
258
259template <typename Target, typename Source> struct _MLRangeCheck<true, true, Target, Source>
260{
261 static inline Target checked_cast(Source arg)
262 {
264 }
265};
266
267#endif
268
269//===============================================================================================
270
271//----------------------------------------------
273//----------------------------------------------
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."
276#else
277#if !defined(_ML_COMPILE_RANGE_CASTS_ONLY_AS_CASTS) && !defined(_ML_COMPILE_RANGE_CASTS_WITH_CHECKS)
278// Compilation is disabled - so just insert original statements instead of function calls.
279
280template <Target, Source> inline Target mlrange_cast(Source arg)
281{
282 return arg;
283}
284
285#else
286
287#if defined(_ML_COMPILE_RANGE_CASTS_ONLY_AS_CASTS)
288// Compilation is enabled, but checks are disabled for maximum performance. Just insert casts then.
289
290template <Target, Source> inline Target mlrange_cast(Source arg)
291{
292 return static_cast<Target>(arg);
293}
294
295#else
296// Compilation is enabled with checks. Insert check function calls.
297
305template <typename Target, typename Source> inline Target mlrange_cast(Source arg)
306{
308 Source>::checked_cast(arg);
309}
310
311#endif
312#endif
313#endif
314#endif
#define _ML_OUTPUT_RANGE_ERROR
To avoid duplication...
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
double floor(T value)
static float checked_cast(double arg)
static Target checked_cast(Source arg)
Compile-check functions only if needed.
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.