| 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 <limits> | 8 #include <limits> |
| 9 #include <type_traits> |
| 9 | 10 |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/numerics/safe_conversions_impl.h" | 12 #include "base/numerics/safe_conversions_impl.h" |
| 12 | 13 |
| 13 namespace base { | 14 namespace base { |
| 14 | 15 |
| 15 // Convenience function that returns true if the supplied value is in range | 16 // Convenience function that returns true if the supplied value is in range |
| 16 // for the destination type. | 17 // for the destination type. |
| 17 template <typename Dst, typename Src> | 18 template <typename Dst, typename Src> |
| 18 inline bool IsValueInRangeForNumericType(Src value) { | 19 inline bool IsValueInRangeForNumericType(Src value) { |
| 19 return internal::DstRangeRelationToSrcRange<Dst>(value) == | 20 return internal::DstRangeRelationToSrcRange<Dst>(value) == |
| 20 internal::RANGE_VALID; | 21 internal::RANGE_VALID; |
| 21 } | 22 } |
| 22 | 23 |
| 23 // Convenience function for determining if a numeric value is negative without | 24 // Convenience function for determining if a numeric value is negative without |
| 24 // throwing compiler warnings on: unsigned(value) < 0. | 25 // throwing compiler warnings on: unsigned(value) < 0. |
| 25 template <typename T> | 26 template <typename T> |
| 26 typename enable_if<std::numeric_limits<T>::is_signed, bool>::type | 27 typename std::enable_if<std::numeric_limits<T>::is_signed, bool>::type |
| 27 IsValueNegative(T value) { | 28 IsValueNegative(T value) { |
| 28 static_assert(std::numeric_limits<T>::is_specialized, | 29 static_assert(std::numeric_limits<T>::is_specialized, |
| 29 "Argument must be numeric."); | 30 "Argument must be numeric."); |
| 30 return value < 0; | 31 return value < 0; |
| 31 } | 32 } |
| 32 | 33 |
| 33 template <typename T> | 34 template <typename T> |
| 34 typename enable_if<!std::numeric_limits<T>::is_signed, bool>::type | 35 typename std::enable_if<!std::numeric_limits<T>::is_signed, bool>::type |
| 35 IsValueNegative(T) { | 36 IsValueNegative(T) { |
| 36 static_assert(std::numeric_limits<T>::is_specialized, | 37 static_assert(std::numeric_limits<T>::is_specialized, |
| 37 "Argument must be numeric."); | 38 "Argument must be numeric."); |
| 38 return false; | 39 return false; |
| 39 } | 40 } |
| 40 | 41 |
| 41 // checked_cast<> is analogous to static_cast<> for numeric types, | 42 // checked_cast<> is analogous to static_cast<> for numeric types, |
| 42 // except that it CHECKs that the specified numeric conversion will not | 43 // except that it CHECKs that the specified numeric conversion will not |
| 43 // overflow or underflow. NaN source will always trigger a CHECK. | 44 // overflow or underflow. NaN source will always trigger a CHECK. |
| 44 template <typename Dst, typename Src> | 45 template <typename Dst, typename Src> |
| 45 inline Dst checked_cast(Src value) { | 46 inline Dst checked_cast(Src value) { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 private: | 135 private: |
| 135 T value_; | 136 T value_; |
| 136 }; | 137 }; |
| 137 | 138 |
| 138 // Explicitly make a shorter size_t typedef for convenience. | 139 // Explicitly make a shorter size_t typedef for convenience. |
| 139 typedef StrictNumeric<size_t> SizeT; | 140 typedef StrictNumeric<size_t> SizeT; |
| 140 | 141 |
| 141 } // namespace base | 142 } // namespace base |
| 142 | 143 |
| 143 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_H_ | 144 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_H_ |
| OLD | NEW |