| 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_NUMERICS_SAFE_CONVERSIONS_H_ | 5 #ifndef BASE_NUMERICS_SAFE_CONVERSIONS_H_ |
| 6 #define BASE_NUMERICS_SAFE_CONVERSIONS_H_ | 6 #define BASE_NUMERICS_SAFE_CONVERSIONS_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 // across any range of arithmetic types. StrictNumeric is the return type | 44 // across any range of arithmetic types. StrictNumeric is the return type |
| 45 // for values extracted from a CheckedNumeric class instance. The raw | 45 // for values extracted from a CheckedNumeric class instance. The raw |
| 46 // arithmetic value is extracted via static_cast to the underlying type. | 46 // arithmetic value is extracted via static_cast to the underlying type. |
| 47 // MakeStrictNum() - Creates a new StrictNumeric from the underlying type of | 47 // MakeStrictNum() - Creates a new StrictNumeric from the underlying type of |
| 48 // the supplied arithmetic or StrictNumeric type. | 48 // the supplied arithmetic or StrictNumeric type. |
| 49 | 49 |
| 50 // Convenience function that returns true if the supplied value is in range | 50 // Convenience function that returns true if the supplied value is in range |
| 51 // for the destination type. | 51 // for the destination type. |
| 52 template <typename Dst, typename Src> | 52 template <typename Dst, typename Src> |
| 53 constexpr bool IsValueInRangeForNumericType(Src value) { | 53 constexpr bool IsValueInRangeForNumericType(Src value) { |
| 54 return internal::DstRangeRelationToSrcRange<Dst>(value) == | 54 return internal::DstRangeRelationToSrcRange<Dst>(value).IsValid(); |
| 55 internal::RANGE_VALID; | |
| 56 } | 55 } |
| 57 | 56 |
| 58 // Forces a crash, like a CHECK(false). Used for numeric boundary errors. | 57 // Forces a crash, like a CHECK(false). Used for numeric boundary errors. |
| 59 struct CheckOnFailure { | 58 struct CheckOnFailure { |
| 60 template <typename T> | 59 template <typename T> |
| 61 static T HandleFailure() { | 60 static T HandleFailure() { |
| 62 #if defined(__GNUC__) || defined(__clang__) | 61 #if defined(__GNUC__) || defined(__clang__) |
| 63 __builtin_trap(); | 62 __builtin_trap(); |
| 64 #else | 63 #else |
| 65 ((void)(*(volatile char*)0 = 0)); | 64 ((void)(*(volatile char*)0 = 0)); |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 using internal::StrictNumeric; | 269 using internal::StrictNumeric; |
| 271 using internal::MakeStrictNum; | 270 using internal::MakeStrictNum; |
| 272 using internal::IsValueNegative; | 271 using internal::IsValueNegative; |
| 273 | 272 |
| 274 // Explicitly make a shorter size_t alias for convenience. | 273 // Explicitly make a shorter size_t alias for convenience. |
| 275 using SizeT = StrictNumeric<size_t>; | 274 using SizeT = StrictNumeric<size_t>; |
| 276 | 275 |
| 277 } // namespace base | 276 } // namespace base |
| 278 | 277 |
| 279 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_H_ | 278 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_H_ |
| OLD | NEW |