OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_SAFE_CONVERSIONS_IMPL_H_ | 5 #ifndef BASE_SAFE_CONVERSIONS_IMPL_H_ |
6 #define BASE_SAFE_CONVERSIONS_IMPL_H_ | 6 #define BASE_SAFE_CONVERSIONS_IMPL_H_ |
7 | 7 |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "../macros.h" | 10 #include "../macros.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 // We implement this as template specializations rather than simple static | 35 // We implement this as template specializations rather than simple static |
36 // comparisons to ensure type correctness in our comparisons. | 36 // comparisons to ensure type correctness in our comparisons. |
37 enum NumericRangeRepresentation { | 37 enum NumericRangeRepresentation { |
38 NUMERIC_RANGE_NOT_CONTAINED, | 38 NUMERIC_RANGE_NOT_CONTAINED, |
39 NUMERIC_RANGE_CONTAINED | 39 NUMERIC_RANGE_CONTAINED |
40 }; | 40 }; |
41 | 41 |
42 // Helper templates to statically determine if our destination type can contain | 42 // Helper templates to statically determine if our destination type can contain |
43 // maximum and minimum values represented by the source type. | 43 // maximum and minimum values represented by the source type. |
44 | 44 |
45 template < | 45 template <typename Dst, |
46 typename Dst, | 46 typename Src, |
47 typename Src, | 47 IntegerRepresentation DstSign = std::numeric_limits<Dst>::is_signed |
48 IntegerRepresentation DstSign = std::numeric_limits<Dst>::is_signed | 48 ? INTEGER_REPRESENTATION_SIGNED |
49 ? INTEGER_REPRESENTATION_SIGNED | 49 : INTEGER_REPRESENTATION_UNSIGNED, |
50 : INTEGER_REPRESENTATION_UNSIGNED, | 50 IntegerRepresentation SrcSign = |
51 IntegerRepresentation SrcSign = | 51 std::numeric_limits<Src>::is_signed |
52 std::numeric_limits<Src>::is_signed | 52 ? INTEGER_REPRESENTATION_SIGNED |
53 ? INTEGER_REPRESENTATION_SIGNED | 53 : INTEGER_REPRESENTATION_UNSIGNED > |
54 : INTEGER_REPRESENTATION_UNSIGNED > | 54 struct StaticDstRangeRelationToSrcRange; |
55 struct StaticDstRangeRelationToSrcRange; | |
56 | 55 |
57 // Same sign: Dst is guaranteed to contain Src only if its range is equal or | 56 // Same sign: Dst is guaranteed to contain Src only if its range is equal or |
58 // larger. | 57 // larger. |
59 template <typename Dst, typename Src, IntegerRepresentation Sign> | 58 template <typename Dst, typename Src, IntegerRepresentation Sign> |
60 struct StaticDstRangeRelationToSrcRange<Dst, Src, Sign, Sign> { | 59 struct StaticDstRangeRelationToSrcRange<Dst, Src, Sign, Sign> { |
61 static const NumericRangeRepresentation value = | 60 static const NumericRangeRepresentation value = |
62 MaxExponent<Dst>::value >= MaxExponent<Src>::value | 61 MaxExponent<Dst>::value >= MaxExponent<Src>::value |
63 ? NUMERIC_RANGE_CONTAINED | 62 ? NUMERIC_RANGE_CONTAINED |
64 : NUMERIC_RANGE_NOT_CONTAINED; | 63 : NUMERIC_RANGE_NOT_CONTAINED; |
65 }; | 64 }; |
(...skipping 14 matching lines...) Expand all Loading... |
80 // Signed to unsigned: Dst cannot be statically determined to contain Src. | 79 // Signed to unsigned: Dst cannot be statically determined to contain Src. |
81 template <typename Dst, typename Src> | 80 template <typename Dst, typename Src> |
82 struct StaticDstRangeRelationToSrcRange<Dst, | 81 struct StaticDstRangeRelationToSrcRange<Dst, |
83 Src, | 82 Src, |
84 INTEGER_REPRESENTATION_UNSIGNED, | 83 INTEGER_REPRESENTATION_UNSIGNED, |
85 INTEGER_REPRESENTATION_SIGNED> { | 84 INTEGER_REPRESENTATION_SIGNED> { |
86 static const NumericRangeRepresentation value = NUMERIC_RANGE_NOT_CONTAINED; | 85 static const NumericRangeRepresentation value = NUMERIC_RANGE_NOT_CONTAINED; |
87 }; | 86 }; |
88 | 87 |
89 enum RangeConstraint { | 88 enum RangeConstraint { |
90 RANGE_VALID = 0x0, // Value can be represented by the destination type. | 89 RANGE_VALID = 0x0, // Value can be represented by the destination type. |
91 RANGE_UNDERFLOW = 0x1, // Value would overflow. | 90 RANGE_UNDERFLOW = 0x1, // Value would overflow. |
92 RANGE_OVERFLOW = 0x2, // Value would underflow. | 91 RANGE_OVERFLOW = 0x2, // Value would underflow. |
93 RANGE_INVALID = RANGE_UNDERFLOW | RANGE_OVERFLOW // Invalid (i.e. NaN). | 92 RANGE_INVALID = RANGE_UNDERFLOW | RANGE_OVERFLOW // Invalid (i.e. NaN). |
94 }; | 93 }; |
95 | 94 |
96 // Helper function for coercing an int back to a RangeContraint. | 95 // Helper function for coercing an int back to a RangeContraint. |
97 inline RangeConstraint GetRangeConstraint(int integer_range_constraint) { | 96 inline RangeConstraint GetRangeConstraint(int integer_range_constraint) { |
98 assert(integer_range_constraint >= RANGE_VALID && | 97 assert(integer_range_constraint >= RANGE_VALID && |
99 integer_range_constraint <= RANGE_INVALID); | 98 integer_range_constraint <= RANGE_INVALID); |
100 return static_cast<RangeConstraint>(integer_range_constraint); | 99 return static_cast<RangeConstraint>(integer_range_constraint); |
101 } | 100 } |
102 | 101 |
103 // This function creates a RangeConstraint from an upper and lower bound | 102 // This function creates a RangeConstraint from an upper and lower bound |
104 // check by taking advantage of the fact that only NaN can be out of range in | 103 // check by taking advantage of the fact that only NaN can be out of range in |
105 // both directions at once. | 104 // both directions at once. |
106 inline RangeConstraint GetRangeConstraint(bool is_in_upper_bound, | 105 inline RangeConstraint GetRangeConstraint(bool is_in_upper_bound, |
107 bool is_in_lower_bound) { | 106 bool is_in_lower_bound) { |
108 return GetRangeConstraint((is_in_upper_bound ? 0 : RANGE_OVERFLOW) | | 107 return GetRangeConstraint((is_in_upper_bound ? 0 : RANGE_OVERFLOW) | |
109 (is_in_lower_bound ? 0 : RANGE_UNDERFLOW)); | 108 (is_in_lower_bound ? 0 : RANGE_UNDERFLOW)); |
110 } | 109 } |
111 | 110 |
112 template < | 111 template <typename Dst, |
113 typename Dst, | 112 typename Src, |
114 typename Src, | 113 IntegerRepresentation DstSign = std::numeric_limits<Dst>::is_signed |
115 IntegerRepresentation DstSign = std::numeric_limits<Dst>::is_signed | 114 ? INTEGER_REPRESENTATION_SIGNED |
116 ? INTEGER_REPRESENTATION_SIGNED | 115 : INTEGER_REPRESENTATION_UNSIGNED, |
117 : INTEGER_REPRESENTATION_UNSIGNED, | 116 IntegerRepresentation |
118 IntegerRepresentation SrcSign = std::numeric_limits<Src>::is_signed | 117 SrcSign = std::numeric_limits<Src>::is_signed |
119 ? INTEGER_REPRESENTATION_SIGNED | 118 ? INTEGER_REPRESENTATION_SIGNED |
120 : INTEGER_REPRESENTATION_UNSIGNED, | 119 : INTEGER_REPRESENTATION_UNSIGNED, |
121 NumericRangeRepresentation DstRange = | 120 NumericRangeRepresentation DstRange = |
122 StaticDstRangeRelationToSrcRange<Dst, Src>::value > | 121 StaticDstRangeRelationToSrcRange<Dst, Src>::value > |
123 struct DstRangeRelationToSrcRangeImpl; | 122 struct DstRangeRelationToSrcRangeImpl; |
124 | 123 |
125 // The following templates are for ranges that must be verified at runtime. We | 124 // The following templates are for ranges that must be verified at runtime. We |
126 // split it into checks based on signedness to avoid confusing casts and | 125 // split it into checks based on signedness to avoid confusing casts and |
127 // compiler warnings on signed an unsigned comparisons. | 126 // compiler warnings on signed an unsigned comparisons. |
128 | 127 |
129 // Dst range is statically determined to contain Src: Nothing to check. | 128 // Dst range is statically determined to contain Src: Nothing to check. |
130 template <typename Dst, | 129 template <typename Dst, |
131 typename Src, | 130 typename Src, |
132 IntegerRepresentation DstSign, | 131 IntegerRepresentation DstSign, |
133 IntegerRepresentation SrcSign> | 132 IntegerRepresentation SrcSign> |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 argument_must_be_numeric); | 206 argument_must_be_numeric); |
208 COMPILE_ASSERT(std::numeric_limits<Dst>::is_specialized, | 207 COMPILE_ASSERT(std::numeric_limits<Dst>::is_specialized, |
209 result_must_be_numeric); | 208 result_must_be_numeric); |
210 return DstRangeRelationToSrcRangeImpl<Dst, Src>::Check(value); | 209 return DstRangeRelationToSrcRangeImpl<Dst, Src>::Check(value); |
211 } | 210 } |
212 | 211 |
213 } // namespace internal | 212 } // namespace internal |
214 } // namespace base | 213 } // namespace base |
215 | 214 |
216 #endif // BASE_SAFE_CONVERSIONS_IMPL_H_ | 215 #endif // BASE_SAFE_CONVERSIONS_IMPL_H_ |
217 | |
OLD | NEW |