Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: base/numerics/safe_math.h

Issue 2545303002: Rename CheckNum to MakeCheckedNum (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/numerics/safe_numerics_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 // disambiguator syntax when converting a destination type. 73 // disambiguator syntax when converting a destination type.
74 // IsValidForType<>() in place of: a.template IsValid<Dst>() 74 // IsValidForType<>() in place of: a.template IsValid<Dst>()
75 // ValueOrDieForType<>() in place of: a.template ValueOrDie() 75 // ValueOrDieForType<>() in place of: a.template ValueOrDie()
76 // ValueOrDefaultForType<>() in place of: a.template ValueOrDefault(default) 76 // ValueOrDefaultForType<>() in place of: a.template ValueOrDefault(default)
77 // 77 //
78 // The following are general utility methods that are useful for converting 78 // The following are general utility methods that are useful for converting
79 // between arithmetic types and CheckedNumeric types: 79 // between arithmetic types and CheckedNumeric types:
80 // CheckedNumeric::Cast<Dst>() - Instance method returning a CheckedNumeric 80 // CheckedNumeric::Cast<Dst>() - Instance method returning a CheckedNumeric
81 // derived from casting the current instance to a CheckedNumeric of 81 // derived from casting the current instance to a CheckedNumeric of
82 // the supplied destination type. 82 // the supplied destination type.
83 // CheckNum() - Creates a new CheckedNumeric from the underlying type of the 83 // MakeCheckedNum() - Creates a new CheckedNumeric from the underlying type of
84 // supplied arithmetic, CheckedNumeric, or StrictNumeric type. 84 // the supplied arithmetic, CheckedNumeric, or StrictNumeric type.
85 // 85 //
86 // Comparison operations are explicitly not supported because they could result 86 // Comparison operations are explicitly not supported because they could result
87 // in a crash on an unexpected CHECK condition. You should use patterns like the 87 // in a crash on an unexpected CHECK condition. You should use patterns like the
88 // following for comparisons: 88 // following for comparisons:
89 // CheckedNumeric<size_t> checked_size = untrusted_input_value; 89 // CheckedNumeric<size_t> checked_size = untrusted_input_value;
90 // checked_size += HEADER LENGTH; 90 // checked_size += HEADER LENGTH;
91 // if (checked_size.IsValid() && checked_size.ValueOrDie() < buffer_size) 91 // if (checked_size.IsValid() && checked_size.ValueOrDie() < buffer_size)
92 // Do stuff... 92 // Do stuff...
93 93
94 template <typename T> 94 template <typename T>
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 struct ResultType { 389 struct ResultType {
390 // The typedef was required here because MSVC fails to compile with "using". 390 // The typedef was required here because MSVC fails to compile with "using".
391 typedef 391 typedef
392 typename ResultType<M, typename ResultType<M, L, R>::type, Args...>::type 392 typename ResultType<M, typename ResultType<M, L, R>::type, Args...>::type
393 type; 393 type;
394 }; 394 };
395 395
396 // Convience wrapper to return a new CheckedNumeric from the provided arithmetic 396 // Convience wrapper to return a new CheckedNumeric from the provided arithmetic
397 // or CheckedNumericType. 397 // or CheckedNumericType.
398 template <typename T> 398 template <typename T>
399 constexpr CheckedNumeric<typename UnderlyingType<T>::type> CheckNum( 399 constexpr CheckedNumeric<typename UnderlyingType<T>::type> MakeCheckedNum(
400 const T value) { 400 const T value) {
401 return value; 401 return value;
402 } 402 }
403 403
404 // These implement the variadic wrapper for the math operations. 404 // These implement the variadic wrapper for the math operations.
405 template <template <typename, typename, typename> class M, 405 template <template <typename, typename, typename> class M,
406 typename L, 406 typename L,
407 typename R> 407 typename R>
408 CheckedNumeric<typename MathWrapper<M, L, R>::type> ChkMathOp(const L lhs, 408 CheckedNumeric<typename MathWrapper<M, L, R>::type> ChkMathOp(const L lhs,
409 const R rhs) { 409 const R rhs) {
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 .template ValueOrDie<uintptr_t>(); 486 .template ValueOrDie<uintptr_t>();
487 return reinterpret_cast<L*>(result); 487 return reinterpret_cast<L*>(result);
488 } 488 }
489 489
490 } // namespace internal 490 } // namespace internal
491 491
492 using internal::CheckedNumeric; 492 using internal::CheckedNumeric;
493 using internal::IsValidForType; 493 using internal::IsValidForType;
494 using internal::ValueOrDieForType; 494 using internal::ValueOrDieForType;
495 using internal::ValueOrDefaultForType; 495 using internal::ValueOrDefaultForType;
496 using internal::CheckNum; 496 using internal::MakeCheckedNum;
497 using internal::CheckMax; 497 using internal::CheckMax;
498 using internal::CheckMin; 498 using internal::CheckMin;
499 using internal::CheckAdd; 499 using internal::CheckAdd;
500 using internal::CheckSub; 500 using internal::CheckSub;
501 using internal::CheckMul; 501 using internal::CheckMul;
502 using internal::CheckDiv; 502 using internal::CheckDiv;
503 using internal::CheckMod; 503 using internal::CheckMod;
504 using internal::CheckLsh; 504 using internal::CheckLsh;
505 using internal::CheckRsh; 505 using internal::CheckRsh;
506 using internal::CheckAnd; 506 using internal::CheckAnd;
507 using internal::CheckOr; 507 using internal::CheckOr;
508 using internal::CheckXor; 508 using internal::CheckXor;
509 509
510 } // namespace base 510 } // namespace base
511 511
512 #endif // BASE_NUMERICS_SAFE_MATH_H_ 512 #endif // BASE_NUMERICS_SAFE_MATH_H_
OLDNEW
« no previous file with comments | « no previous file | base/numerics/safe_numerics_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698