| 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_MATH_H_ | 5 #ifndef BASE_NUMERICS_SAFE_MATH_H_ |
| 6 #define BASE_NUMERICS_SAFE_MATH_H_ | 6 #define BASE_NUMERICS_SAFE_MATH_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 | 124 |
| 125 // ValueOrDie() - The primary accessor for the underlying value. If the | 125 // ValueOrDie() - The primary accessor for the underlying value. If the |
| 126 // current state is not valid it will CHECK and crash. | 126 // current state is not valid it will CHECK and crash. |
| 127 // A range checked destination type can be supplied using the Dst template | 127 // A range checked destination type can be supplied using the Dst template |
| 128 // parameter, which will trigger a CHECK if the value is not in bounds for | 128 // parameter, which will trigger a CHECK if the value is not in bounds for |
| 129 // the destination. | 129 // the destination. |
| 130 // The CHECK behavior can be overridden by supplying a handler as a | 130 // The CHECK behavior can be overridden by supplying a handler as a |
| 131 // template parameter, for test code, etc. However, the handler cannot access | 131 // template parameter, for test code, etc. However, the handler cannot access |
| 132 // the underlying value, and it is not available through other means. | 132 // the underlying value, and it is not available through other means. |
| 133 template <typename Dst = T, class CheckHandler = CheckOnFailure> | 133 template <typename Dst = T, class CheckHandler = CheckOnFailure> |
| 134 constexpr Dst ValueOrDie() const { | 134 constexpr StrictNumeric<Dst> ValueOrDie() const { |
| 135 return IsValid<Dst>() ? state_.value() | 135 return IsValid<Dst>() ? static_cast<Dst>(state_.value()) |
| 136 : CheckHandler::template HandleFailure<Dst>(); | 136 : CheckHandler::template HandleFailure<Dst>(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 // ValueOrDefault(T default_value) - A convenience method that returns the | 139 // ValueOrDefault(T default_value) - A convenience method that returns the |
| 140 // current value if the state is valid, and the supplied default_value for | 140 // current value if the state is valid, and the supplied default_value for |
| 141 // any other state. | 141 // any other state. |
| 142 // A range checked destination type can be supplied using the Dst template | 142 // A range checked destination type can be supplied using the Dst template |
| 143 // parameter. WARNING: This function may fail to compile or CHECK at runtime | 143 // parameter. WARNING: This function may fail to compile or CHECK at runtime |
| 144 // if the supplied default_value is not within range of the destination type. | 144 // if the supplied default_value is not within range of the destination type. |
| 145 template <typename Dst = T, typename Src> | 145 template <typename Dst = T, typename Src> |
| 146 constexpr Dst ValueOrDefault(const Src default_value) const { | 146 constexpr StrictNumeric<Dst> ValueOrDefault(const Src default_value) const { |
| 147 return IsValid<Dst>() ? state_.value() : checked_cast<Dst>(default_value); | 147 return IsValid<Dst>() ? static_cast<Dst>(state_.value()) |
| 148 : checked_cast<Dst>(default_value); |
| 148 } | 149 } |
| 149 | 150 |
| 150 // ValueFloating() - Since floating point values include their validity state, | 151 // ValueFloating() - Since floating point values include their validity state, |
| 151 // we provide an easy method for extracting them directly, without a risk of | 152 // we provide an easy method for extracting them directly, without a risk of |
| 152 // crashing on a CHECK. | 153 // crashing on a CHECK. |
| 153 // A range checked destination type can be supplied using the Dst template | 154 // A range checked destination type can be supplied using the Dst template |
| 154 // parameter. | 155 // parameter. |
| 155 template <typename Dst = T> | 156 template <typename Dst = T> |
| 156 constexpr Dst ValueFloating() const { | 157 constexpr StrictNumeric<Dst> ValueFloating() const { |
| 157 static_assert(std::numeric_limits<T>::is_iec559, | 158 static_assert(std::numeric_limits<T>::is_iec559, |
| 158 "Type must be floating point."); | 159 "Type must be floating point."); |
| 159 static_assert(std::numeric_limits<Dst>::is_iec559, | 160 static_assert(std::numeric_limits<Dst>::is_iec559, |
| 160 "Type must be floating point."); | 161 "Type must be floating point."); |
| 161 return static_cast<Dst>(state_.value()); | 162 return static_cast<Dst>(state_.value()); |
| 162 } | 163 } |
| 163 | 164 |
| 164 // Returns a checked numeric of the specified type, cast from the current | 165 // Returns a checked numeric of the specified type, cast from the current |
| 165 // CheckedNumeric. If the current state is invalid or the destination cannot | 166 // CheckedNumeric. If the current state is invalid or the destination cannot |
| 166 // represent the result then the returned CheckedNumeric will be invalid. | 167 // represent the result then the returned CheckedNumeric will be invalid. |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 using internal::CheckMod; | 406 using internal::CheckMod; |
| 406 using internal::CheckLsh; | 407 using internal::CheckLsh; |
| 407 using internal::CheckRsh; | 408 using internal::CheckRsh; |
| 408 using internal::CheckAnd; | 409 using internal::CheckAnd; |
| 409 using internal::CheckOr; | 410 using internal::CheckOr; |
| 410 using internal::CheckXor; | 411 using internal::CheckXor; |
| 411 | 412 |
| 412 } // namespace base | 413 } // namespace base |
| 413 | 414 |
| 414 #endif // BASE_NUMERICS_SAFE_MATH_H_ | 415 #endif // BASE_NUMERICS_SAFE_MATH_H_ |
| OLD | NEW |