Chromium Code Reviews| Index: base/numerics/safe_math.h |
| diff --git a/base/numerics/safe_math.h b/base/numerics/safe_math.h |
| index 598bd3e71847daf7fb46db9a07efe9c06452ede4..0e2482629476a7fe2811147ad0e65392a7baa0c7 100644 |
| --- a/base/numerics/safe_math.h |
| +++ b/base/numerics/safe_math.h |
| @@ -106,27 +106,54 @@ class CheckedNumeric { |
| : state_(static_cast<Src>(value)) {} |
| // IsValid() is the public API to test if a CheckedNumeric is currently valid. |
| - constexpr bool IsValid() const { return state_.is_valid(); } |
| + // A range checked destination type can be supplied using the Dst template |
| + // parameter, which will trigger a CHECK if the value is not in bounds for |
| + // the destination. |
| + template <typename Dst = T> |
| + constexpr bool IsValid() const { |
| + return state_.is_valid() && |
| + IsValueInRangeForNumericType<Dst>(state_.value()); |
| + } |
| // ValueOrDie() The primary accessor for the underlying value. If the current |
| // state is not valid it will CHECK and crash. |
| - constexpr T ValueOrDie() const { |
| - return IsValid() ? state_.value() : CheckOnFailure::HandleFailure<T>(); |
| + // A range checked destination type can be supplied using the Dst template |
| + // parameter, which will trigger a CHECK if the value is not in bounds for |
|
scottmg
2016/11/24 20:23:37
This comment is misleading since there's an overri
jschuh
2016/11/25 14:56:39
I've tweaked the comment, and I'm okay with the ov
|
| + // the destination. |
| + // The CHECK behavior can be overridden by supplying a handler as a |
| + // template parameter, however the value cannot be extracted by the handler. |
| + template <typename Dst = T, class CheckHandler = CheckOnFailure> |
| + constexpr Dst ValueOrDie() const { |
| + return IsValid<Dst>() ? state_.value() |
| + : CheckHandler::template HandleFailure<Dst>(); |
| } |
| // ValueOrDefault(T default_value) A convenience method that returns the |
| // current value if the state is valid, and the supplied default_value for |
| // any other state. |
| - constexpr T ValueOrDefault(T default_value) const { |
| - return IsValid() ? state_.value() : default_value; |
| + // A range checked destination type can be supplied using the Dst template |
| + // parameter, which will trigger a CHECK if the value is not in bounds for |
| + // the destination. |
| + template <typename Dst = T, typename Src> |
| + constexpr Dst ValueOrDefault(const Src default_value) const { |
|
scottmg
2016/11/24 20:23:37
Similar for the other ones, ValueOrDefault() and V
jschuh
2016/11/25 14:56:39
This comment was actually wrong, so I fixed it.
|
| + return IsValueInRangeForNumericType<Dst>(default_value) && IsValid<Dst>() |
| + ? state_.value() |
| + : static_cast<Dst>(default_value); |
| } |
| // ValueFloating() - Since floating point values include their validity state, |
| // we provide an easy method for extracting them directly, without a risk of |
| // crashing on a CHECK. |
| - constexpr T ValueFloating() const { |
| - static_assert(std::numeric_limits<T>::is_iec559, "Argument must be float."); |
| - return state_.value(); |
| + // A range checked destination type can be supplied using the Dst template |
| + // parameter, which will trigger a CHECK if the value is not in bounds for |
| + // the destination. |
| + template <typename Dst = T> |
| + constexpr Dst ValueFloating() const { |
| + static_assert(std::numeric_limits<T>::is_iec559, |
| + "Type must be floating point."); |
| + static_assert(std::numeric_limits<Dst>::is_iec559, |
| + "Type must be floating point."); |
| + return static_cast<Dst>(state_.value()); |
| } |
| // Returns a checked numeric of the specified type, cast from the current |