OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 * | |
10 */ | |
11 | |
12 // Borrowed from Chromium's src/base/numerics/safe_conversions.h. | |
13 // - Modified to work in WebRTC (paths, namespace, use of webrtc/base). | |
14 // Based on 'chromium_revision': 'ee311243eae6aef9c907543663754ff38f1f4f40'. | |
15 | |
16 #ifndef WEBRTC_BASE_NUMERICS_SAFE_CONVERSIONS_H_ | |
17 #define WEBRTC_BASE_NUMERICS_SAFE_CONVERSIONS_H_ | |
18 | |
19 #include <stddef.h> | |
20 | |
21 #include <limits> | |
22 #include <type_traits> | |
23 | |
24 #include "webrtc/base/numerics/safe_conversions_impl.h" | |
25 | |
26 namespace rtc { | |
27 | |
28 // Convenience function that returns true if the supplied value is in range | |
29 // for the destination type. | |
30 template <typename Dst, typename Src> | |
31 inline bool IsValueInRangeForNumericType(Src value) { | |
32 return internal::DstRangeRelationToSrcRange<Dst>(value) == | |
33 internal::RANGE_VALID; | |
34 } | |
35 | |
36 // Convenience function for determining if a numeric value is negative without | |
37 // throwing compiler warnings on: unsigned(value) < 0. | |
38 template <typename T> | |
39 typename std::enable_if<std::numeric_limits<T>::is_signed, bool>::type | |
40 IsValueNegative(T value) { | |
41 static_assert(std::numeric_limits<T>::is_specialized, | |
42 "Argument must be numeric."); | |
43 return value < 0; | |
44 } | |
45 | |
46 template <typename T> | |
47 typename std::enable_if<!std::numeric_limits<T>::is_signed, bool>::type | |
48 IsValueNegative(T) { | |
49 static_assert(std::numeric_limits<T>::is_specialized, | |
50 "Argument must be numeric."); | |
51 return false; | |
52 } | |
53 | |
54 // checked_cast<> is analogous to static_cast<> for numeric types, | |
55 // except that it CHECKs that the specified numeric conversion will not | |
56 // overflow or underflow. NaN source will always trigger a CHECK. | |
57 template <typename Dst, typename Src> | |
58 inline Dst checked_cast(Src value) { | |
59 RTC_CHECK(IsValueInRangeForNumericType<Dst>(value)); | |
60 return static_cast<Dst>(value); | |
61 } | |
62 | |
63 // HandleNaN will cause this class to RTC_CHECK(false). | |
64 struct SaturatedCastNaNBehaviorCheck { | |
65 template <typename T> | |
66 static T HandleNaN() { | |
67 RTC_CHECK(false); | |
68 return T(); | |
69 } | |
70 }; | |
71 | |
72 // HandleNaN will return 0 in this case. | |
73 struct SaturatedCastNaNBehaviorReturnZero { | |
74 template <typename T> | |
75 static T HandleNaN() { | |
76 return T(); | |
77 } | |
78 }; | |
79 | |
80 // saturated_cast<> is analogous to static_cast<> for numeric types, except | |
81 // that the specified numeric conversion will saturate rather than overflow or | |
82 // underflow. NaN assignment to an integral will defer the behavior to a | |
83 // specified class. By default, it will return 0. | |
84 template <typename Dst, | |
85 class NaNHandler = SaturatedCastNaNBehaviorReturnZero, | |
86 typename Src> | |
87 inline Dst saturated_cast(Src value) { | |
88 // Optimization for floating point values, which already saturate. | |
89 if (std::numeric_limits<Dst>::is_iec559) | |
90 return static_cast<Dst>(value); | |
91 | |
92 switch (internal::DstRangeRelationToSrcRange<Dst>(value)) { | |
93 case internal::RANGE_VALID: | |
94 return static_cast<Dst>(value); | |
95 | |
96 case internal::RANGE_UNDERFLOW: | |
97 return std::numeric_limits<Dst>::min(); | |
98 | |
99 case internal::RANGE_OVERFLOW: | |
100 return std::numeric_limits<Dst>::max(); | |
101 | |
102 // Should fail only on attempting to assign NaN to a saturated integer. | |
103 case internal::RANGE_INVALID: | |
104 return NaNHandler::template HandleNaN<Dst>(); | |
105 } | |
106 | |
107 RTC_NOTREACHED(); | |
108 return static_cast<Dst>(value); | |
109 } | |
110 | |
111 // strict_cast<> is analogous to static_cast<> for numeric types, except that | |
112 // it will cause a compile failure if the destination type is not large enough | |
113 // to contain any value in the source type. It performs no runtime checking. | |
114 template <typename Dst, typename Src> | |
115 inline Dst strict_cast(Src value) { | |
116 static_assert(std::numeric_limits<Src>::is_specialized, | |
117 "Argument must be numeric."); | |
118 static_assert(std::numeric_limits<Dst>::is_specialized, | |
119 "Result must be numeric."); | |
120 static_assert((internal::StaticDstRangeRelationToSrcRange<Dst, Src>::value == | |
121 internal::NUMERIC_RANGE_CONTAINED), | |
122 "The numeric conversion is out of range for this type. You " | |
123 "should probably use one of the following conversion " | |
124 "mechanisms on the value you want to pass:\n" | |
125 "- rtc::checked_cast\n" | |
126 "- rtc::saturated_cast\n" | |
127 "- rtc::CheckedNumeric"); | |
128 | |
129 return static_cast<Dst>(value); | |
130 } | |
131 | |
132 // StrictNumeric implements compile time range checking between numeric types by | |
133 // wrapping assignment operations in a strict_cast. This class is intended to be | |
134 // used for function arguments and return types, to ensure the destination type | |
135 // can always contain the source type. This is essentially the same as enforcing | |
136 // -Wconversion in gcc and C4302 warnings on MSVC, but it can be applied | |
137 // incrementally at API boundaries, making it easier to convert code so that it | |
138 // compiles cleanly with truncation warnings enabled. | |
139 // This template should introduce no runtime overhead, but it also provides no | |
140 // runtime checking of any of the associated mathematical operations. Use | |
141 // CheckedNumeric for runtime range checks of tha actual value being assigned. | |
142 template <typename T> | |
143 class StrictNumeric { | |
144 public: | |
145 typedef T type; | |
146 | |
147 StrictNumeric() : value_(0) {} | |
148 | |
149 // Copy constructor. | |
150 template <typename Src> | |
151 StrictNumeric(const StrictNumeric<Src>& rhs) | |
152 : value_(strict_cast<T>(rhs.value_)) {} | |
153 | |
154 // This is not an explicit constructor because we implicitly upgrade regular | |
155 // numerics to StrictNumerics to make them easier to use. | |
156 template <typename Src> | |
157 StrictNumeric(Src value) | |
158 : value_(strict_cast<T>(value)) {} | |
159 | |
160 // The numeric cast operator basically handles all the magic. | |
161 template <typename Dst> | |
162 operator Dst() const { | |
163 return strict_cast<Dst>(value_); | |
164 } | |
165 | |
166 private: | |
167 T value_; | |
168 }; | |
169 | |
170 // Explicitly make a shorter size_t typedef for convenience. | |
171 typedef StrictNumeric<size_t> SizeT; | |
172 | |
173 } // namespace rtc | |
174 | |
175 #endif // WEBRTC_BASE_NUMERICS_SAFE_CONVERSIONS_H_ | |
OLD | NEW |