 Chromium Code Reviews
 Chromium Code Reviews Issue 2931323002:
  Split out code to be shared between CheckedNumeric and ClampedNumeric  (Closed)
    
  
    Issue 2931323002:
  Split out code to be shared between CheckedNumeric and ClampedNumeric  (Closed) 
  | Index: base/numerics/checked_math.h | 
| diff --git a/base/numerics/safe_math.h b/base/numerics/checked_math.h | 
| similarity index 83% | 
| copy from base/numerics/safe_math.h | 
| copy to base/numerics/checked_math.h | 
| index f5007db39c42e450646013b9d92273eec3bbdcc0..00a88e3aad50e3741538453b3c69a2ce8bf7cd18 100644 | 
| --- a/base/numerics/safe_math.h | 
| +++ b/base/numerics/checked_math.h | 
| @@ -1,16 +1,16 @@ | 
| -// Copyright 2014 The Chromium Authors. All rights reserved. | 
| +// Copyright 2017 The Chromium Authors. All rights reserved. | 
| // Use of this source code is governed by a BSD-style license that can be | 
| // found in the LICENSE file. | 
| -#ifndef BASE_NUMERICS_SAFE_MATH_H_ | 
| -#define BASE_NUMERICS_SAFE_MATH_H_ | 
| +#ifndef BASE_NUMERICS_CHECKED_MATH_H_ | 
| +#define BASE_NUMERICS_CHECKED_MATH_H_ | 
| #include <stddef.h> | 
| #include <limits> | 
| #include <type_traits> | 
| -#include "base/numerics/safe_math_impl.h" | 
| +#include "base/numerics/checked_math_impl.h" | 
| namespace base { | 
| namespace internal { | 
| @@ -299,7 +299,7 @@ class CheckedNumeric { | 
| Wrapper<L>::is_valid(lhs) && Wrapper<R>::is_valid(rhs) && | 
| Math::Do(Wrapper<L>::value(lhs), Wrapper<R>::value(rhs), &result); | 
| return CheckedNumeric<T>(result, is_valid); | 
| - }; | 
| 
brucedawson
2017/06/12 21:09:22
Who *wrote* the previous version? Must have been a
 
jschuh
2017/06/12 21:19:21
Let's not dwell on that, and instead just be happy
 | 
| + } | 
| // Assignment arithmetic operations. | 
| template <template <typename, typename, typename> class M, typename R> | 
| @@ -310,7 +310,7 @@ class CheckedNumeric { | 
| Math::Do(state_.value(), Wrapper<R>::value(rhs), &result); | 
| *this = CheckedNumeric<T>(result, is_valid); | 
| return *this; | 
| - }; | 
| + } | 
| private: | 
| CheckedNumericState<T> state_; | 
| @@ -365,30 +365,6 @@ constexpr StrictNumeric<Dst> ValueOrDefaultForType( | 
| return value.template ValueOrDefault<Dst>(default_value); | 
| } | 
| -// These variadic templates work out the return types. | 
| -// TODO(jschuh): Rip all this out once we have C++14 non-trailing auto support. | 
| -template <template <typename, typename, typename> class M, | 
| - typename L, | 
| - typename R, | 
| - typename... Args> | 
| -struct ResultType; | 
| - | 
| -template <template <typename, typename, typename> class M, | 
| - typename L, | 
| - typename R> | 
| -struct ResultType<M, L, R> { | 
| - using type = typename MathWrapper<M, L, R>::type; | 
| -}; | 
| - | 
| -template <template <typename, typename, typename> class M, | 
| - typename L, | 
| - typename R, | 
| - typename... Args> | 
| -struct ResultType { | 
| - using type = | 
| - typename ResultType<M, typename ResultType<M, L, R>::type, Args...>::type; | 
| -}; | 
| - | 
| // Convience wrapper to return a new CheckedNumeric from the provided arithmetic | 
| // or CheckedNumericType. | 
| template <typename T> | 
| @@ -418,51 +394,20 @@ ChkMathOp(const L lhs, const R rhs, const Args... args) { | 
| auto tmp = ChkMathOp<M>(lhs, rhs); | 
| return tmp.IsValid() ? ChkMathOp<M>(tmp, args...) | 
| : decltype(ChkMathOp<M>(tmp, args...))(tmp); | 
| -}; | 
| - | 
| -// The following macros are just boilerplate for the standard arithmetic | 
| -// operator overloads and variadic function templates. A macro isn't the nicest | 
| -// solution, but it beats rewriting these over and over again. | 
| -#define BASE_NUMERIC_ARITHMETIC_VARIADIC(NAME) \ | 
| - template <typename L, typename R, typename... Args> \ | 
| - CheckedNumeric<typename ResultType<Checked##NAME##Op, L, R, Args...>::type> \ | 
| - Check##NAME(const L lhs, const R rhs, const Args... args) { \ | 
| - return ChkMathOp<Checked##NAME##Op, L, R, Args...>(lhs, rhs, args...); \ | 
| - } | 
| +} | 
| -#define BASE_NUMERIC_ARITHMETIC_OPERATORS(NAME, OP, COMPOUND_OP) \ | 
| - /* Binary arithmetic operator for all CheckedNumeric operations. */ \ | 
| - template <typename L, typename R, \ | 
| - typename std::enable_if<IsCheckedOp<L, R>::value>::type* = \ | 
| - nullptr> \ | 
| - CheckedNumeric<typename MathWrapper<Checked##NAME##Op, L, R>::type> \ | 
| - operator OP(const L lhs, const R rhs) { \ | 
| - return decltype(lhs OP rhs)::template MathOp<Checked##NAME##Op>(lhs, rhs); \ | 
| - } \ | 
| - /* Assignment arithmetic operator implementation from CheckedNumeric. */ \ | 
| - template <typename L> \ | 
| - template <typename R> \ | 
| - CheckedNumeric<L>& CheckedNumeric<L>::operator COMPOUND_OP(const R rhs) { \ | 
| - return MathOp<Checked##NAME##Op>(rhs); \ | 
| - } \ | 
| - /* Variadic arithmetic functions that return CheckedNumeric. */ \ | 
| - BASE_NUMERIC_ARITHMETIC_VARIADIC(NAME) | 
| - | 
| -BASE_NUMERIC_ARITHMETIC_OPERATORS(Add, +, +=) | 
| -BASE_NUMERIC_ARITHMETIC_OPERATORS(Sub, -, -=) | 
| -BASE_NUMERIC_ARITHMETIC_OPERATORS(Mul, *, *=) | 
| -BASE_NUMERIC_ARITHMETIC_OPERATORS(Div, /, /=) | 
| -BASE_NUMERIC_ARITHMETIC_OPERATORS(Mod, %, %=) | 
| -BASE_NUMERIC_ARITHMETIC_OPERATORS(Lsh, <<, <<=) | 
| -BASE_NUMERIC_ARITHMETIC_OPERATORS(Rsh, >>, >>=) | 
| -BASE_NUMERIC_ARITHMETIC_OPERATORS(And, &, &=) | 
| -BASE_NUMERIC_ARITHMETIC_OPERATORS(Or, |, |=) | 
| -BASE_NUMERIC_ARITHMETIC_OPERATORS(Xor, ^, ^=) | 
| -BASE_NUMERIC_ARITHMETIC_VARIADIC(Max) | 
| -BASE_NUMERIC_ARITHMETIC_VARIADIC(Min) | 
| - | 
| -#undef BASE_NUMERIC_ARITHMETIC_VARIADIC | 
| -#undef BASE_NUMERIC_ARITHMETIC_OPERATORS | 
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Checked, Check, Add, +, +=) | 
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Checked, Check, Sub, -, -=) | 
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Checked, Check, Mul, *, *=) | 
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Checked, Check, Div, /, /=) | 
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Checked, Check, Mod, %, %=) | 
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Checked, Check, Lsh, <<, <<=) | 
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Checked, Check, Rsh, >>, >>=) | 
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Checked, Check, And, &, &=) | 
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Checked, Check, Or, |, |=) | 
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Checked, Check, Xor, ^, ^=) | 
| +BASE_NUMERIC_ARITHMETIC_VARIADIC(Checked, Check, Max) | 
| +BASE_NUMERIC_ARITHMETIC_VARIADIC(Checked, Check, Min) | 
| // These are some extra StrictNumeric operators to support simple pointer | 
| // arithmetic with our result types. Since wrapping on a pointer is always | 
| @@ -505,4 +450,4 @@ using internal::CheckXor; | 
| } // namespace base | 
| -#endif // BASE_NUMERICS_SAFE_MATH_H_ | 
| +#endif // BASE_NUMERICS_CHECKED_MATH_H_ |