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

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

Issue 2945433003: Add ClampedNumeric templates (Closed)
Patch Set: more docs and modulus Created 3 years, 5 months 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
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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_SHARED_IMPL_H_ 5 #ifndef BASE_NUMERICS_SAFE_MATH_SHARED_IMPL_H_
6 #define BASE_NUMERICS_SAFE_MATH_SHARED_IMPL_H_ 6 #define BASE_NUMERICS_SAFE_MATH_SHARED_IMPL_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 typename R, 107 typename R,
108 typename... Args> 108 typename... Args>
109 struct ResultType { 109 struct ResultType {
110 using type = 110 using type =
111 typename ResultType<M, typename ResultType<M, L, R>::type, Args...>::type; 111 typename ResultType<M, typename ResultType<M, L, R>::type, Args...>::type;
112 }; 112 };
113 113
114 // The following macros are just boilerplate for the standard arithmetic 114 // The following macros are just boilerplate for the standard arithmetic
115 // operator overloads and variadic function templates. A macro isn't the nicest 115 // operator overloads and variadic function templates. A macro isn't the nicest
116 // solution, but it beats rewriting these over and over again. 116 // solution, but it beats rewriting these over and over again.
117 #define BASE_NUMERIC_ARITHMETIC_VARIADIC(CLASS, CL_ABBR, OP_NAME) \ 117 #define BASE_NUMERIC_ARITHMETIC_VARIADIC(CLASS, CL_ABBR, OP_NAME) \
jschuh 2017/06/26 20:26:50 Finished generalizing the implementation so Clampe
118 template <typename L, typename R, typename... Args> \ 118 template <typename L, typename R, typename... Args> \
119 CLASS##Numeric<typename ResultType<CLASS##OP_NAME##Op, L, R, Args...>::type> \ 119 CLASS##Numeric<typename ResultType<CLASS##OP_NAME##Op, L, R, Args...>::type> \
120 CL_ABBR##OP_NAME(const L lhs, const R rhs, const Args... args) { \ 120 CL_ABBR##OP_NAME(const L lhs, const R rhs, const Args... args) { \
121 return ChkMathOp<CLASS##OP_NAME##Op, L, R, Args...>(lhs, rhs, args...); \ 121 return CL_ABBR##MathOp<CLASS##OP_NAME##Op, L, R, Args...>(lhs, rhs, \
122 args...); \
122 } 123 }
123 124
124 #define BASE_NUMERIC_ARITHMETIC_OPERATORS(CLASS, CL_ABBR, OP_NAME, OP, CMP_OP) \ 125 #define BASE_NUMERIC_ARITHMETIC_OPERATORS(CLASS, CL_ABBR, OP_NAME, OP, CMP_OP) \
125 /* Binary arithmetic operator for all CheckedNumeric operations. */ \ 126 /* Binary arithmetic operator for all CLASS##Numeric operations. */ \
126 template <typename L, typename R, \ 127 template <typename L, typename R, \
127 typename std::enable_if<IsCheckedOp<L, R>::value>::type* = \ 128 typename std::enable_if<Is##CLASS##Op<L, R>::value>::type* = \
128 nullptr> \ 129 nullptr> \
129 CheckedNumeric<typename MathWrapper<CLASS##OP_NAME##Op, L, R>::type> \ 130 CLASS##Numeric<typename MathWrapper<CLASS##OP_NAME##Op, L, R>::type> \
130 operator OP(const L lhs, const R rhs) { \ 131 operator OP(const L lhs, const R rhs) { \
131 return decltype(lhs OP rhs)::template MathOp<CLASS##OP_NAME##Op>(lhs, \ 132 return decltype(lhs OP rhs)::template MathOp<CLASS##OP_NAME##Op>(lhs, \
132 rhs); \ 133 rhs); \
133 } \ 134 } \
134 /* Assignment arithmetic operator implementation from CheckedNumeric. */ \ 135 /* Assignment arithmetic operator implementation from CLASS##Numeric. */ \
135 template <typename L> \ 136 template <typename L> \
136 template <typename R> \ 137 template <typename R> \
137 CheckedNumeric<L>& CheckedNumeric<L>::operator CMP_OP(const R rhs) { \ 138 CLASS##Numeric<L>& CLASS##Numeric<L>::operator CMP_OP(const R rhs) { \
138 return MathOp<CLASS##OP_NAME##Op>(rhs); \ 139 return MathOp<CLASS##OP_NAME##Op>(rhs); \
139 } \ 140 } \
140 /* Variadic arithmetic functions that return CheckedNumeric. */ \ 141 /* Variadic arithmetic functions that return CLASS##Numeric. */ \
141 BASE_NUMERIC_ARITHMETIC_VARIADIC(CLASS, CL_ABBR, OP_NAME) 142 BASE_NUMERIC_ARITHMETIC_VARIADIC(CLASS, CL_ABBR, OP_NAME)
142 143
143 } // namespace internal 144 } // namespace internal
144 } // namespace base 145 } // namespace base
145 146
146 #endif // BASE_NUMERICS_SAFE_MATH_SHARED_IMPL_H_ 147 #endif // BASE_NUMERICS_SAFE_MATH_SHARED_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698