Chromium Code Reviews| 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 24 matching lines...) Expand all Loading... | |
| 35 // The unary negation, increment, and decrement operators are supported, along | 35 // The unary negation, increment, and decrement operators are supported, along |
| 36 // with the following unary arithmetic methods, which return a new | 36 // with the following unary arithmetic methods, which return a new |
| 37 // CheckedNumeric as a result of the operation: | 37 // CheckedNumeric as a result of the operation: |
| 38 // Abs() - Absolute value. | 38 // Abs() - Absolute value. |
| 39 // UnsignedAbs() - Absolute value as an equival-width unsigned underlying type | 39 // UnsignedAbs() - Absolute value as an equival-width unsigned underlying type |
| 40 // (valid for only integral types). | 40 // (valid for only integral types). |
| 41 // | 41 // |
| 42 // The following methods convert from CheckedNumeric to standard numeric values: | 42 // The following methods convert from CheckedNumeric to standard numeric values: |
| 43 // IsValid() - Returns true if the underlying numeric value is valid (i.e. has | 43 // IsValid() - Returns true if the underlying numeric value is valid (i.e. has |
| 44 // has not wrapped and is not the result of an invalid conversion). | 44 // has not wrapped and is not the result of an invalid conversion). |
| 45 // AssignIfValid() - Assigns the underlying value to the supplied destination | |
| 46 // pointer if the value is currently valid and within the range | |
|
scottmg
2016/11/25 23:47:51
indent
| |
| 47 // supported by the destination type. Returns true on success. | |
| 45 // ValueOrDie() - Returns the underlying value. If the state is not valid this | 48 // ValueOrDie() - Returns the underlying value. If the state is not valid this |
| 46 // call will crash on a CHECK. | 49 // call will crash on a CHECK. |
| 47 // ValueOrDefault() - Returns the current value, or the supplied default if the | 50 // ValueOrDefault() - Returns the current value, or the supplied default if the |
| 48 // state is not valid (will not trigger a CHECK). | 51 // state is not valid (will not trigger a CHECK). |
| 49 // ValueFloating() - Returns the underlying floating point value (valid only | 52 // ValueFloating() - Returns the underlying floating point value (valid only |
| 50 // for floating point CheckedNumeric types; will not cause a CHECK). | 53 // for floating point CheckedNumeric types; will not cause a CHECK). |
| 51 // | 54 // |
| 52 // The following are general utility methods that are useful for converting | 55 // The following are general utility methods that are useful for converting |
| 53 // between arithmetic types and CheckedNumeric types: | 56 // between arithmetic types and CheckedNumeric types: |
| 54 // CheckedNumeric::Cast<Dst>() - Instance method returning a CheckedNumeric | 57 // CheckedNumeric::Cast<Dst>() - Instance method returning a CheckedNumeric |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 "Argument must be numeric."); | 95 "Argument must be numeric."); |
| 93 } | 96 } |
| 94 | 97 |
| 95 // This is not an explicit constructor because we want a seamless conversion | 98 // This is not an explicit constructor because we want a seamless conversion |
| 96 // from StrictNumeric types. | 99 // from StrictNumeric types. |
| 97 template <typename Src> | 100 template <typename Src> |
| 98 constexpr CheckedNumeric( | 101 constexpr CheckedNumeric( |
| 99 StrictNumeric<Src> value) // NOLINT(runtime/explicit) | 102 StrictNumeric<Src> value) // NOLINT(runtime/explicit) |
| 100 : state_(static_cast<Src>(value)) {} | 103 : state_(static_cast<Src>(value)) {} |
| 101 | 104 |
| 102 // IsValid() is the public API to test if a CheckedNumeric is currently valid. | 105 // IsValid() - The public API to test if a CheckedNumeric is currently valid. |
| 103 // A range checked destination type can be supplied using the Dst template | 106 // A range checked destination type can be supplied using the Dst template |
| 104 // parameter, which will trigger a CHECK if the value is not in bounds for | 107 // parameter. |
| 105 // the destination. | |
| 106 template <typename Dst = T> | 108 template <typename Dst = T> |
| 107 constexpr bool IsValid() const { | 109 constexpr bool IsValid() const { |
| 108 return state_.is_valid() && | 110 return state_.is_valid() && |
| 109 IsValueInRangeForNumericType<Dst>(state_.value()); | 111 IsValueInRangeForNumericType<Dst>(state_.value()); |
| 110 } | 112 } |
| 111 | 113 |
| 112 // ValueOrDie() The primary accessor for the underlying value. If the current | 114 // AssignIfValid(Dst) - Assigns the underlying value if it is currently valid |
| 113 // state is not valid it will CHECK and crash. | 115 // and is within the range supported by the destination type. Returns true if |
| 116 // successful and false otherwise. | |
| 117 template <typename Dst> | |
| 118 constexpr bool AssignIfValid(Dst* result) const { | |
| 119 return IsValid<Dst>() ? ((*result = static_cast<Dst>(state_.value())), true) | |
| 120 : false; | |
| 121 } | |
| 122 | |
| 123 // ValueOrDie() - The primary accessor for the underlying value. If the | |
| 124 // current state is not valid it will CHECK and crash. | |
| 114 // A range checked destination type can be supplied using the Dst template | 125 // A range checked destination type can be supplied using the Dst template |
| 115 // parameter, which will trigger a CHECK if the value is not in bounds for | 126 // parameter, which will trigger a CHECK if the value is not in bounds for |
| 116 // the destination. | 127 // the destination. |
| 117 // The CHECK behavior can be overridden by supplying a handler as a | 128 // The CHECK behavior can be overridden by supplying a handler as a |
| 118 // template parameter, for test code, etc. However, the handler cannot access | 129 // template parameter, for test code, etc. However, the handler cannot access |
| 119 // the underlying value, and it is not available through other means. | 130 // the underlying value, and it is not available through other means. |
| 120 template <typename Dst = T, class CheckHandler = CheckOnFailure> | 131 template <typename Dst = T, class CheckHandler = CheckOnFailure> |
| 121 constexpr Dst ValueOrDie() const { | 132 constexpr Dst ValueOrDie() const { |
| 122 return IsValid<Dst>() ? state_.value() | 133 return IsValid<Dst>() ? state_.value() |
| 123 : CheckHandler::template HandleFailure<Dst>(); | 134 : CheckHandler::template HandleFailure<Dst>(); |
| 124 } | 135 } |
| 125 | 136 |
| 126 // ValueOrDefault(T default_value) A convenience method that returns the | 137 // ValueOrDefault(T default_value) - A convenience method that returns the |
| 127 // current value if the state is valid, and the supplied default_value for | 138 // current value if the state is valid, and the supplied default_value for |
| 128 // any other state. | 139 // any other state. |
| 129 // A range checked destination type can be supplied using the Dst template | 140 // A range checked destination type can be supplied using the Dst template |
| 130 // parameter. WARNING: This function may fail to compile or CHECK at runtime | 141 // parameter. WARNING: This function may fail to compile or CHECK at runtime |
| 131 // if the supplied default_value is not within range of the destination type. | 142 // if the supplied default_value is not within range of the destination type. |
| 132 template <typename Dst = T, typename Src> | 143 template <typename Dst = T, typename Src> |
| 133 constexpr Dst ValueOrDefault(const Src default_value) const { | 144 constexpr Dst ValueOrDefault(const Src default_value) const { |
| 134 return IsValid<Dst>() ? state_.value() : checked_cast<Dst>(default_value); | 145 return IsValid<Dst>() ? state_.value() : checked_cast<Dst>(default_value); |
| 135 } | 146 } |
| 136 | 147 |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 392 using internal::CheckMod; | 403 using internal::CheckMod; |
| 393 using internal::CheckLsh; | 404 using internal::CheckLsh; |
| 394 using internal::CheckRsh; | 405 using internal::CheckRsh; |
| 395 using internal::CheckAnd; | 406 using internal::CheckAnd; |
| 396 using internal::CheckOr; | 407 using internal::CheckOr; |
| 397 using internal::CheckXor; | 408 using internal::CheckXor; |
| 398 | 409 |
| 399 } // namespace base | 410 } // namespace base |
| 400 | 411 |
| 401 #endif // BASE_NUMERICS_SAFE_MATH_H_ | 412 #endif // BASE_NUMERICS_SAFE_MATH_H_ |
| OLD | NEW |