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 "base/numerics/safe_math_impl.h" | 8 #include "base/numerics/safe_math_impl.h" |
9 | 9 |
10 namespace base { | 10 namespace base { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 | 59 |
60 // This is not an explicit constructor because we implicitly upgrade regular | 60 // This is not an explicit constructor because we implicitly upgrade regular |
61 // numerics to CheckedNumerics to make them easier to use. | 61 // numerics to CheckedNumerics to make them easier to use. |
62 template <typename Src> | 62 template <typename Src> |
63 CheckedNumeric(Src value) | 63 CheckedNumeric(Src value) |
64 : state_(value) { | 64 : state_(value) { |
65 static_assert(std::numeric_limits<Src>::is_specialized, | 65 static_assert(std::numeric_limits<Src>::is_specialized, |
66 "Argument must be numeric."); | 66 "Argument must be numeric."); |
67 } | 67 } |
68 | 68 |
| 69 // This is not an explicit constructor because we want a seamless conversion |
| 70 // from StrictNumeric types. |
| 71 template <typename Src> |
| 72 CheckedNumeric(StrictNumeric<Src> value) |
| 73 : state_(static_cast<Src>(value)) { |
| 74 } |
| 75 |
69 // IsValid() is the public API to test if a CheckedNumeric is currently valid. | 76 // IsValid() is the public API to test if a CheckedNumeric is currently valid. |
70 bool IsValid() const { return validity() == RANGE_VALID; } | 77 bool IsValid() const { return validity() == RANGE_VALID; } |
71 | 78 |
72 // ValueOrDie() The primary accessor for the underlying value. If the current | 79 // ValueOrDie() The primary accessor for the underlying value. If the current |
73 // state is not valid it will CHECK and crash. | 80 // state is not valid it will CHECK and crash. |
74 T ValueOrDie() const { | 81 T ValueOrDie() const { |
75 CHECK(IsValid()); | 82 CHECK(IsValid()); |
76 return state_.value(); | 83 return state_.value(); |
77 } | 84 } |
78 | 85 |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 | 270 |
264 #undef BASE_NUMERIC_ARITHMETIC_OPERATORS | 271 #undef BASE_NUMERIC_ARITHMETIC_OPERATORS |
265 | 272 |
266 } // namespace internal | 273 } // namespace internal |
267 | 274 |
268 using internal::CheckedNumeric; | 275 using internal::CheckedNumeric; |
269 | 276 |
270 } // namespace base | 277 } // namespace base |
271 | 278 |
272 #endif // BASE_NUMERICS_SAFE_MATH_H_ | 279 #endif // BASE_NUMERICS_SAFE_MATH_H_ |
OLD | NEW |