| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_NUMERICS_H_ | 5 #ifndef BASE_SAFE_NUMERICS_H_ |
| 6 #define BASE_SAFE_NUMERICS_H_ | 6 #define BASE_SAFE_NUMERICS_H_ |
| 7 | 7 |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 (sizeof(Dest) > sizeof(Source)), | 113 (sizeof(Dest) > sizeof(Source)), |
| 114 DestLimits::is_signed, | 114 DestLimits::is_signed, |
| 115 SourceLimits::is_signed>::Test( | 115 SourceLimits::is_signed>::Test( |
| 116 source, | 116 source, |
| 117 DestLimits::min(), | 117 DestLimits::min(), |
| 118 DestLimits::max()); | 118 DestLimits::max()); |
| 119 } | 119 } |
| 120 | 120 |
| 121 } // namespace internal | 121 } // namespace internal |
| 122 | 122 |
| 123 // numeric_cast<> is analogous to static_cast<> for numeric types, except that | 123 // checked_numeric_cast<> is analogous to static_cast<> for numeric types, |
| 124 // it CHECKs that the specified numeric conversion will not overflow or | 124 // except that it CHECKs that the specified numeric conversion will not |
| 125 // underflow. Floating point arguments are not currently allowed (this is | 125 // overflow or underflow. Floating point arguments are not currently allowed |
| 126 // COMPILE_ASSERTd), though this could be supported if necessary. | 126 // (this is COMPILE_ASSERTd), though this could be supported if necessary. |
| 127 template <class Dest, class Source> | 127 template <class Dest, class Source> |
| 128 inline Dest checked_numeric_cast(Source source) { | 128 inline Dest checked_numeric_cast(Source source) { |
| 129 CHECK(internal::IsValidNumericCast<Dest>(source)); | 129 CHECK(internal::IsValidNumericCast<Dest>(source)); |
| 130 return static_cast<Dest>(source); | 130 return static_cast<Dest>(source); |
| 131 } | 131 } |
| 132 | 132 |
| 133 } // namespace base | 133 } // namespace base |
| 134 | 134 |
| 135 #endif // BASE_SAFE_NUMERICS_H_ | 135 #endif // BASE_SAFE_NUMERICS_H_ |
| OLD | NEW |