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

Unified Diff: base/numerics/safe_math.h

Issue 2496143003: Simplify CheckedNumeric macros (Closed)
Patch Set: nits Created 4 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/numerics/safe_math_impl.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/numerics/safe_math.h
diff --git a/base/numerics/safe_math.h b/base/numerics/safe_math.h
index acdf682f0875258de21987fea4bfc04c3cda4efb..85847a4ef51b153292e6bb817e5d9dddd5d8fc07 100644
--- a/base/numerics/safe_math.h
+++ b/base/numerics/safe_math.h
@@ -185,16 +185,6 @@ class CheckedNumeric {
static const CheckedNumeric<T>& cast(const CheckedNumeric<T>& u) { return u; }
private:
- template <typename NumericType>
- struct UnderlyingType {
- using type = NumericType;
- };
-
- template <typename NumericType>
- struct UnderlyingType<CheckedNumeric<NumericType>> {
- using type = NumericType;
- };
-
CheckedNumericState<T> state_;
};
@@ -205,71 +195,44 @@ class CheckedNumeric {
// * We skip range checks for floating points.
// * We skip range checks for destination integers with sufficient range.
// TODO(jschuh): extract these out into templates.
-#define BASE_NUMERIC_ARITHMETIC_OPERATORS(NAME, OP, COMPOUND_OP) \
- /* Binary arithmetic operator for CheckedNumerics of the same type. */ \
- template <typename T> \
- CheckedNumeric<typename ArithmeticPromotion<T>::type> operator OP( \
- const CheckedNumeric<T>& lhs, const CheckedNumeric<T>& rhs) { \
- typedef typename ArithmeticPromotion<T>::type Promotion; \
- /* Floating point always takes the fast path */ \
- if (std::numeric_limits<T>::is_iec559) \
- return CheckedNumeric<T>(lhs.ValueUnsafe() OP rhs.ValueUnsafe()); \
- if (!rhs.IsValid() || !lhs.IsValid()) \
- return CheckedNumeric<Promotion>(0, false); \
- if (IsIntegerArithmeticSafe<Promotion, T, T>::value) \
- return CheckedNumeric<Promotion>(lhs.ValueUnsafe() \
- OP rhs.ValueUnsafe()); \
- Promotion result = 0; \
- bool is_valid = \
- Checked##NAME(static_cast<Promotion>(lhs.ValueUnsafe()), \
- static_cast<Promotion>(rhs.ValueUnsafe()), &result); \
- return CheckedNumeric<Promotion>(result, is_valid); \
- } \
- /* Assignment arithmetic operator implementation from CheckedNumeric. */ \
- template <typename T> \
- template <typename Src> \
- CheckedNumeric<T>& CheckedNumeric<T>::operator COMPOUND_OP(Src rhs) { \
- *this = CheckedNumeric<T>::cast(*this) \
- OP CheckedNumeric<typename UnderlyingType<Src>::type>::cast(rhs); \
- return *this; \
- } \
- /* Binary arithmetic operator for CheckedNumeric of different type. */ \
- template <typename T, typename Src> \
- CheckedNumeric<typename ArithmeticPromotion<T, Src>::type> operator OP( \
- const CheckedNumeric<Src>& lhs, const CheckedNumeric<T>& rhs) { \
- typedef typename ArithmeticPromotion<T, Src>::type Promotion; \
- if (!rhs.IsValid() || !lhs.IsValid()) \
- return CheckedNumeric<Promotion>(0, false); \
- if (IsIntegerArithmeticSafe<Promotion, T, Src>::value) \
- return CheckedNumeric<Promotion>(lhs.ValueUnsafe() OP rhs.ValueUnsafe());\
- return CheckedNumeric<Promotion>::cast(lhs) \
- OP CheckedNumeric<Promotion>::cast(rhs); \
- } \
- /* Binary arithmetic operator for left CheckedNumeric and right numeric. */ \
- template <typename T, typename Src, \
- typename std::enable_if<std::is_arithmetic<Src>::value>::type* = \
- nullptr> \
- CheckedNumeric<typename ArithmeticPromotion<T, Src>::type> operator OP( \
- const CheckedNumeric<T>& lhs, Src rhs) { \
- typedef typename ArithmeticPromotion<T, Src>::type Promotion; \
- if (IsIntegerArithmeticSafe<Promotion, T, Src>::value) \
- return CheckedNumeric<Promotion>(lhs.ValueUnsafe() OP rhs, \
- lhs.IsValid()); \
- return CheckedNumeric<Promotion>::cast(lhs) \
- OP CheckedNumeric<Promotion>::cast(rhs); \
- } \
- /* Binary arithmetic operator for left numeric and right CheckedNumeric. */ \
- template <typename T, typename Src, \
- typename std::enable_if<std::is_arithmetic<Src>::value>::type* = \
- nullptr> \
- CheckedNumeric<typename ArithmeticPromotion<T, Src>::type> operator OP( \
- Src lhs, const CheckedNumeric<T>& rhs) { \
- typedef typename ArithmeticPromotion<T, Src>::type Promotion; \
- if (IsIntegerArithmeticSafe<Promotion, T, Src>::value) \
- return CheckedNumeric<Promotion>(lhs OP rhs.ValueUnsafe(), \
- rhs.IsValid()); \
- return CheckedNumeric<Promotion>::cast(lhs) \
- OP CheckedNumeric<Promotion>::cast(rhs); \
+#define BASE_NUMERIC_ARITHMETIC_OPERATORS(NAME, OP, COMPOUND_OP) \
+ /* Binary arithmetic operator for CheckedNumerics of the same type. */ \
+ template <typename L, typename R> \
+ CheckedNumeric<typename ArithmeticPromotion<L, R>::type> operator OP( \
+ const CheckedNumeric<L>& lhs, const CheckedNumeric<R>& rhs) { \
+ using P = typename ArithmeticPromotion<L, R>::type; \
+ if (!rhs.IsValid() || !lhs.IsValid()) \
+ return CheckedNumeric<P>(0, false); \
+ /* Floating point always takes the fast path */ \
+ if (std::is_floating_point<L>::value || std::is_floating_point<R>::value) \
+ return CheckedNumeric<P>(lhs.ValueUnsafe() OP rhs.ValueUnsafe()); \
+ P result = 0; \
+ bool is_valid = \
+ Checked##NAME(lhs.ValueUnsafe(), rhs.ValueUnsafe(), &result); \
+ return CheckedNumeric<P>(result, is_valid); \
+ } \
+ /* Assignment arithmetic operator implementation from CheckedNumeric. */ \
+ template <typename L> \
+ template <typename R> \
+ CheckedNumeric<L>& CheckedNumeric<L>::operator COMPOUND_OP(R rhs) { \
+ *this = *this OP rhs; \
+ return *this; \
+ } \
+ /* Binary arithmetic operator for left CheckedNumeric and right numeric. */ \
+ template <typename L, typename R, \
+ typename std::enable_if<std::is_arithmetic<R>::value>::type* = \
+ nullptr> \
+ CheckedNumeric<typename ArithmeticPromotion<L, R>::type> operator OP( \
+ const CheckedNumeric<L>& lhs, R rhs) { \
+ return lhs OP CheckedNumeric<R>(rhs); \
+ } \
+ /* Binary arithmetic operator for left numeric and right CheckedNumeric. */ \
+ template <typename L, typename R, \
+ typename std::enable_if<std::is_arithmetic<L>::value>::type* = \
+ nullptr> \
+ CheckedNumeric<typename ArithmeticPromotion<L, R>::type> operator OP( \
+ L lhs, const CheckedNumeric<R>& rhs) { \
+ return CheckedNumeric<L>(lhs) OP rhs; \
}
BASE_NUMERIC_ARITHMETIC_OPERATORS(Add, +, += )
« no previous file with comments | « no previous file | base/numerics/safe_math_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698