| 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_CONVERSIONS_H_ | 5 #ifndef BASE_NUMERICS_SAFE_CONVERSIONS_H_ |
| 6 #define BASE_NUMERICS_SAFE_CONVERSIONS_H_ | 6 #define BASE_NUMERICS_SAFE_CONVERSIONS_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 // strict_cast<> - Analogous to static_cast<> for numeric types, except that | 29 // strict_cast<> - Analogous to static_cast<> for numeric types, except that |
| 30 // it will cause a compile failure if the destination type is not large | 30 // it will cause a compile failure if the destination type is not large |
| 31 // enough to contain any value in the source type. It performs no runtime | 31 // enough to contain any value in the source type. It performs no runtime |
| 32 // checking and thus introduces no runtime overhead. | 32 // checking and thus introduces no runtime overhead. |
| 33 // IsValueInRangeForNumericType<>() - A convenience function that returns true | 33 // IsValueInRangeForNumericType<>() - A convenience function that returns true |
| 34 // if the type supplied to the template parameter can represent the value | 34 // if the type supplied to the template parameter can represent the value |
| 35 // passed as an argument to the function. | 35 // passed as an argument to the function. |
| 36 // IsValueNegative<>() - A convenience function that will accept any arithmetic | 36 // IsValueNegative<>() - A convenience function that will accept any arithmetic |
| 37 // type as an argument and will return whether the value is less than zero. | 37 // type as an argument and will return whether the value is less than zero. |
| 38 // Unsigned types always return false. | 38 // Unsigned types always return false. |
| 39 // SafeUnsignedAbs() - Returns the absolute value of the supplied integer |
| 40 // parameter as an unsigned result (thus avoiding an overflow if the value |
| 41 // is the signed, two's complement minimum). |
| 39 // StrictNumeric<> - A wrapper type that performs assignments and copies via | 42 // StrictNumeric<> - A wrapper type that performs assignments and copies via |
| 40 // the strict_cast<> template, and can perform valid arithmetic comparisons | 43 // the strict_cast<> template, and can perform valid arithmetic comparisons |
| 41 // across any range of arithmetic types. StrictNumeric is the return type | 44 // across any range of arithmetic types. StrictNumeric is the return type |
| 42 // for values extracted from a CheckedNumeric class instance. The raw | 45 // for values extracted from a CheckedNumeric class instance. The raw |
| 43 // arithmetic value is extracted via static_cast to the underlying type. | 46 // arithmetic value is extracted via static_cast to the underlying type. |
| 44 // MakeStrictNum() - Creates a new StrictNumeric from the underlying type of | 47 // MakeStrictNum() - Creates a new StrictNumeric from the underlying type of |
| 45 // the supplied arithmetic or StrictNumeric type. | 48 // the supplied arithmetic or StrictNumeric type. |
| 46 | 49 |
| 47 // Convenience function that returns true if the supplied value is in range | 50 // Convenience function that returns true if the supplied value is in range |
| 48 // for the destination type. | 51 // for the destination type. |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 STRICT_COMPARISON_OP(IsGreater, >); | 275 STRICT_COMPARISON_OP(IsGreater, >); |
| 273 STRICT_COMPARISON_OP(IsGreaterOrEqual, >=); | 276 STRICT_COMPARISON_OP(IsGreaterOrEqual, >=); |
| 274 STRICT_COMPARISON_OP(IsEqual, ==); | 277 STRICT_COMPARISON_OP(IsEqual, ==); |
| 275 STRICT_COMPARISON_OP(IsNotEqual, !=); | 278 STRICT_COMPARISON_OP(IsNotEqual, !=); |
| 276 | 279 |
| 277 #undef STRICT_COMPARISON_OP | 280 #undef STRICT_COMPARISON_OP |
| 278 }; | 281 }; |
| 279 | 282 |
| 280 using internal::strict_cast; | 283 using internal::strict_cast; |
| 281 using internal::saturated_cast; | 284 using internal::saturated_cast; |
| 285 using internal::SafeUnsignedAbs; |
| 282 using internal::StrictNumeric; | 286 using internal::StrictNumeric; |
| 283 using internal::MakeStrictNum; | 287 using internal::MakeStrictNum; |
| 284 | 288 |
| 285 // Explicitly make a shorter size_t alias for convenience. | 289 // Explicitly make a shorter size_t alias for convenience. |
| 286 using SizeT = StrictNumeric<size_t>; | 290 using SizeT = StrictNumeric<size_t>; |
| 287 | 291 |
| 288 } // namespace base | 292 } // namespace base |
| 289 | 293 |
| 290 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_H_ | 294 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_H_ |
| OLD | NEW |