Commit 7a534c36 authored by Hidehiko Abe's avatar Hidehiko Abe Committed by Commit Bot

Update comparator operator definition of base::Optional.

To catch up c++17 definition, this CL updates the signature
and implementation of comparator operators.

BUG=784732
TEST=Ran base_unittests --gtest_filter=OptionalTest.

Change-Id: I4a21f8af1ec5ca0af9946250f3855f198077d52c
Reviewed-on: https://chromium-review.googlesource.com/832547
Commit-Queue: Hidehiko Abe <hidehiko@chromium.org>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatardanakj <danakj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#525302}
parent c2343303
...@@ -323,34 +323,62 @@ class Optional : public internal::OptionalBase<T> { ...@@ -323,34 +323,62 @@ class Optional : public internal::OptionalBase<T> {
using internal::OptionalBase<T>::storage_; using internal::OptionalBase<T>::storage_;
}; };
template <class T> // Here after defines comparation operators. The definition follows
constexpr bool operator==(const Optional<T>& lhs, const Optional<T>& rhs) { // http://en.cppreference.com/w/cpp/utility/optional/operator_cmp
return !!lhs != !!rhs ? false : lhs == nullopt || (*lhs == *rhs); // while bool() casting is replaced by has_value() to meet the chromium
} // style guide.
template <class T, class U>
template <class T> constexpr bool operator==(const Optional<T>& lhs, const Optional<U>& rhs) {
constexpr bool operator!=(const Optional<T>& lhs, const Optional<T>& rhs) { if (lhs.has_value() != rhs.has_value())
return !(lhs == rhs); return false;
} if (!lhs.has_value())
return true;
template <class T> return *lhs == *rhs;
constexpr bool operator<(const Optional<T>& lhs, const Optional<T>& rhs) { }
return rhs == nullopt ? false : (lhs == nullopt ? true : *lhs < *rhs);
} template <class T, class U>
constexpr bool operator!=(const Optional<T>& lhs, const Optional<U>& rhs) {
template <class T> if (lhs.has_value() != rhs.has_value())
constexpr bool operator<=(const Optional<T>& lhs, const Optional<T>& rhs) { return true;
return !(rhs < lhs); if (!lhs.has_value())
} return false;
return *lhs != *rhs;
template <class T> }
constexpr bool operator>(const Optional<T>& lhs, const Optional<T>& rhs) {
return rhs < lhs; template <class T, class U>
} constexpr bool operator<(const Optional<T>& lhs, const Optional<U>& rhs) {
if (!rhs.has_value())
template <class T> return false;
constexpr bool operator>=(const Optional<T>& lhs, const Optional<T>& rhs) { if (!lhs.has_value())
return !(lhs < rhs); return true;
return *lhs < *rhs;
}
template <class T, class U>
constexpr bool operator<=(const Optional<T>& lhs, const Optional<U>& rhs) {
if (!lhs.has_value())
return true;
if (!rhs.has_value())
return false;
return *lhs <= *rhs;
}
template <class T, class U>
constexpr bool operator>(const Optional<T>& lhs, const Optional<U>& rhs) {
if (!lhs.has_value())
return false;
if (!rhs.has_value())
return true;
return *lhs > *rhs;
}
template <class T, class U>
constexpr bool operator>=(const Optional<T>& lhs, const Optional<U>& rhs) {
if (!rhs.has_value())
return true;
if (!lhs.has_value())
return false;
return *lhs >= *rhs;
} }
template <class T> template <class T>
...@@ -365,12 +393,12 @@ constexpr bool operator==(nullopt_t, const Optional<T>& opt) { ...@@ -365,12 +393,12 @@ constexpr bool operator==(nullopt_t, const Optional<T>& opt) {
template <class T> template <class T>
constexpr bool operator!=(const Optional<T>& opt, nullopt_t) { constexpr bool operator!=(const Optional<T>& opt, nullopt_t) {
return !!opt; return opt.has_value();
} }
template <class T> template <class T>
constexpr bool operator!=(nullopt_t, const Optional<T>& opt) { constexpr bool operator!=(nullopt_t, const Optional<T>& opt) {
return !!opt; return opt.has_value();
} }
template <class T> template <class T>
...@@ -380,7 +408,7 @@ constexpr bool operator<(const Optional<T>& opt, nullopt_t) { ...@@ -380,7 +408,7 @@ constexpr bool operator<(const Optional<T>& opt, nullopt_t) {
template <class T> template <class T>
constexpr bool operator<(nullopt_t, const Optional<T>& opt) { constexpr bool operator<(nullopt_t, const Optional<T>& opt) {
return !!opt; return opt.has_value();
} }
template <class T> template <class T>
...@@ -395,7 +423,7 @@ constexpr bool operator<=(nullopt_t, const Optional<T>& opt) { ...@@ -395,7 +423,7 @@ constexpr bool operator<=(nullopt_t, const Optional<T>& opt) {
template <class T> template <class T>
constexpr bool operator>(const Optional<T>& opt, nullopt_t) { constexpr bool operator>(const Optional<T>& opt, nullopt_t) {
return !!opt; return opt.has_value();
} }
template <class T> template <class T>
...@@ -413,64 +441,64 @@ constexpr bool operator>=(nullopt_t, const Optional<T>& opt) { ...@@ -413,64 +441,64 @@ constexpr bool operator>=(nullopt_t, const Optional<T>& opt) {
return !opt; return !opt;
} }
template <class T> template <class T, class U>
constexpr bool operator==(const Optional<T>& opt, const T& value) { constexpr bool operator==(const Optional<T>& opt, const U& value) {
return opt != nullopt ? *opt == value : false; return opt.has_value() ? *opt == value : false;
} }
template <class T> template <class T, class U>
constexpr bool operator==(const T& value, const Optional<T>& opt) { constexpr bool operator==(const U& value, const Optional<T>& opt) {
return opt == value; return opt.has_value() ? value == *opt : false;
} }
template <class T> template <class T, class U>
constexpr bool operator!=(const Optional<T>& opt, const T& value) { constexpr bool operator!=(const Optional<T>& opt, const U& value) {
return !(opt == value); return opt.has_value() ? *opt != value : true;
} }
template <class T> template <class T, class U>
constexpr bool operator!=(const T& value, const Optional<T>& opt) { constexpr bool operator!=(const U& value, const Optional<T>& opt) {
return !(opt == value); return opt.has_value() ? value != *opt : true;
} }
template <class T> template <class T, class U>
constexpr bool operator<(const Optional<T>& opt, const T& value) { constexpr bool operator<(const Optional<T>& opt, const U& value) {
return opt != nullopt ? *opt < value : true; return opt.has_value() ? *opt < value : true;
} }
template <class T> template <class T, class U>
constexpr bool operator<(const T& value, const Optional<T>& opt) { constexpr bool operator<(const U& value, const Optional<T>& opt) {
return opt != nullopt ? value < *opt : false; return opt.has_value() ? value < *opt : false;
} }
template <class T> template <class T, class U>
constexpr bool operator<=(const Optional<T>& opt, const T& value) { constexpr bool operator<=(const Optional<T>& opt, const U& value) {
return !(opt > value); return opt.has_value() ? *opt <= value : true;
} }
template <class T> template <class T, class U>
constexpr bool operator<=(const T& value, const Optional<T>& opt) { constexpr bool operator<=(const U& value, const Optional<T>& opt) {
return !(value > opt); return opt.has_value() ? value <= *opt : false;
} }
template <class T> template <class T, class U>
constexpr bool operator>(const Optional<T>& opt, const T& value) { constexpr bool operator>(const Optional<T>& opt, const U& value) {
return value < opt; return opt.has_value() ? *opt > value : false;
} }
template <class T> template <class T, class U>
constexpr bool operator>(const T& value, const Optional<T>& opt) { constexpr bool operator>(const U& value, const Optional<T>& opt) {
return opt < value; return opt.has_value() ? value > *opt : true;
} }
template <class T> template <class T, class U>
constexpr bool operator>=(const Optional<T>& opt, const T& value) { constexpr bool operator>=(const Optional<T>& opt, const U& value) {
return !(opt < value); return opt.has_value() ? *opt >= value : false;
} }
template <class T> template <class T, class U>
constexpr bool operator>=(const T& value, const Optional<T>& opt) { constexpr bool operator>=(const U& value, const Optional<T>& opt) {
return !(value < opt); return opt.has_value() ? value >= *opt : true;
} }
template <class T> template <class T>
......
...@@ -73,9 +73,11 @@ class TestObject { ...@@ -73,9 +73,11 @@ class TestObject {
} }
bool operator==(const TestObject& other) const { bool operator==(const TestObject& other) const {
return foo_ == other.foo_ && bar_ == other.bar_; return std::tie(foo_, bar_) == std::tie(other.foo_, other.bar_);
} }
bool operator!=(const TestObject& other) const { return !(*this == other); }
int foo() const { return foo_; } int foo() const { return foo_; }
State state() const { return state_; } State state() const { return state_; }
int move_ctors_count() const { return move_ctors_count_; } int move_ctors_count() const { return move_ctors_count_; }
...@@ -606,6 +608,13 @@ TEST(OptionalTest, Equals_TwoDifferent) { ...@@ -606,6 +608,13 @@ TEST(OptionalTest, Equals_TwoDifferent) {
EXPECT_FALSE(a == b); EXPECT_FALSE(a == b);
} }
TEST(OptionalTest, Equals_DifferentType) {
Optional<int> a(0);
Optional<double> b(0);
EXPECT_TRUE(a == b);
}
TEST(OptionalTest, NotEquals_TwoEmpty) { TEST(OptionalTest, NotEquals_TwoEmpty) {
Optional<int> a; Optional<int> a;
Optional<int> b; Optional<int> b;
...@@ -634,6 +643,13 @@ TEST(OptionalTest, NotEquals_TwoDifferent) { ...@@ -634,6 +643,13 @@ TEST(OptionalTest, NotEquals_TwoDifferent) {
EXPECT_TRUE(a != b); EXPECT_TRUE(a != b);
} }
TEST(OptionalTest, NotEquals_DifferentType) {
Optional<int> a(0);
Optional<double> b(0.0);
EXPECT_FALSE(a != b);
}
TEST(OptionalTest, Less_LeftEmpty) { TEST(OptionalTest, Less_LeftEmpty) {
Optional<int> l; Optional<int> l;
Optional<int> r(1); Optional<int> r(1);
...@@ -676,6 +692,13 @@ TEST(OptionalTest, Less_BothValues) { ...@@ -676,6 +692,13 @@ TEST(OptionalTest, Less_BothValues) {
} }
} }
TEST(OptionalTest, Less_DifferentType) {
Optional<int> l(1);
Optional<double> r(2.0);
EXPECT_TRUE(l < r);
}
TEST(OptionalTest, LessEq_LeftEmpty) { TEST(OptionalTest, LessEq_LeftEmpty) {
Optional<int> l; Optional<int> l;
Optional<int> r(1); Optional<int> r(1);
...@@ -718,6 +741,13 @@ TEST(OptionalTest, LessEq_BothValues) { ...@@ -718,6 +741,13 @@ TEST(OptionalTest, LessEq_BothValues) {
} }
} }
TEST(OptionalTest, LessEq_DifferentType) {
Optional<int> l(1);
Optional<double> r(2.0);
EXPECT_TRUE(l <= r);
}
TEST(OptionalTest, Greater_BothEmpty) { TEST(OptionalTest, Greater_BothEmpty) {
Optional<int> l; Optional<int> l;
Optional<int> r; Optional<int> r;
...@@ -760,6 +790,13 @@ TEST(OptionalTest, Greater_BothValue) { ...@@ -760,6 +790,13 @@ TEST(OptionalTest, Greater_BothValue) {
} }
} }
TEST(OptionalTest, Greater_DifferentType) {
Optional<int> l(1);
Optional<double> r(2.0);
EXPECT_FALSE(l > r);
}
TEST(OptionalTest, GreaterEq_BothEmpty) { TEST(OptionalTest, GreaterEq_BothEmpty) {
Optional<int> l; Optional<int> l;
Optional<int> r; Optional<int> r;
...@@ -802,6 +839,13 @@ TEST(OptionalTest, GreaterEq_BothValue) { ...@@ -802,6 +839,13 @@ TEST(OptionalTest, GreaterEq_BothValue) {
} }
} }
TEST(OptionalTest, GreaterEq_DifferentType) {
Optional<int> l(1);
Optional<double> r(2.0);
EXPECT_FALSE(l >= r);
}
TEST(OptionalTest, OptNullEq) { TEST(OptionalTest, OptNullEq) {
{ {
Optional<int> opt; Optional<int> opt;
...@@ -950,6 +994,11 @@ TEST(OptionalTest, ValueEq_NotEmpty) { ...@@ -950,6 +994,11 @@ TEST(OptionalTest, ValueEq_NotEmpty) {
} }
} }
TEST(OptionalTest, ValueEq_DifferentType) {
Optional<int> opt(0);
EXPECT_TRUE(opt == 0.0);
}
TEST(OptionalTest, EqValue_Empty) { TEST(OptionalTest, EqValue_Empty) {
Optional<int> opt; Optional<int> opt;
EXPECT_FALSE(1 == opt); EXPECT_FALSE(1 == opt);
...@@ -966,6 +1015,11 @@ TEST(OptionalTest, EqValue_NotEmpty) { ...@@ -966,6 +1015,11 @@ TEST(OptionalTest, EqValue_NotEmpty) {
} }
} }
TEST(OptionalTest, EqValue_DifferentType) {
Optional<int> opt(0);
EXPECT_TRUE(0.0 == opt);
}
TEST(OptionalTest, ValueNotEq_Empty) { TEST(OptionalTest, ValueNotEq_Empty) {
Optional<int> opt; Optional<int> opt;
EXPECT_TRUE(opt != 1); EXPECT_TRUE(opt != 1);
...@@ -982,6 +1036,11 @@ TEST(OptionalTest, ValueNotEq_NotEmpty) { ...@@ -982,6 +1036,11 @@ TEST(OptionalTest, ValueNotEq_NotEmpty) {
} }
} }
TEST(OPtionalTest, ValueNotEq_DifferentType) {
Optional<int> opt(0);
EXPECT_FALSE(opt != 0.0);
}
TEST(OptionalTest, NotEqValue_Empty) { TEST(OptionalTest, NotEqValue_Empty) {
Optional<int> opt; Optional<int> opt;
EXPECT_TRUE(1 != opt); EXPECT_TRUE(1 != opt);
...@@ -998,6 +1057,11 @@ TEST(OptionalTest, NotEqValue_NotEmpty) { ...@@ -998,6 +1057,11 @@ TEST(OptionalTest, NotEqValue_NotEmpty) {
} }
} }
TEST(OptionalTest, NotEqValue_DifferentType) {
Optional<int> opt(0);
EXPECT_FALSE(0.0 != opt);
}
TEST(OptionalTest, ValueLess_Empty) { TEST(OptionalTest, ValueLess_Empty) {
Optional<int> opt; Optional<int> opt;
EXPECT_TRUE(opt < 1); EXPECT_TRUE(opt < 1);
...@@ -1018,6 +1082,11 @@ TEST(OptionalTest, ValueLess_NotEmpty) { ...@@ -1018,6 +1082,11 @@ TEST(OptionalTest, ValueLess_NotEmpty) {
} }
} }
TEST(OPtionalTest, ValueLess_DifferentType) {
Optional<int> opt(0);
EXPECT_TRUE(opt < 1.0);
}
TEST(OptionalTest, LessValue_Empty) { TEST(OptionalTest, LessValue_Empty) {
Optional<int> opt; Optional<int> opt;
EXPECT_FALSE(1 < opt); EXPECT_FALSE(1 < opt);
...@@ -1038,6 +1107,11 @@ TEST(OptionalTest, LessValue_NotEmpty) { ...@@ -1038,6 +1107,11 @@ TEST(OptionalTest, LessValue_NotEmpty) {
} }
} }
TEST(OptionalTest, LessValue_DifferentType) {
Optional<int> opt(0);
EXPECT_FALSE(0.0 < opt);
}
TEST(OptionalTest, ValueLessEq_Empty) { TEST(OptionalTest, ValueLessEq_Empty) {
Optional<int> opt; Optional<int> opt;
EXPECT_TRUE(opt <= 1); EXPECT_TRUE(opt <= 1);
...@@ -1058,6 +1132,11 @@ TEST(OptionalTest, ValueLessEq_NotEmpty) { ...@@ -1058,6 +1132,11 @@ TEST(OptionalTest, ValueLessEq_NotEmpty) {
} }
} }
TEST(OptionalTest, ValueLessEq_DifferentType) {
Optional<int> opt(0);
EXPECT_TRUE(opt <= 0.0);
}
TEST(OptionalTest, LessEqValue_Empty) { TEST(OptionalTest, LessEqValue_Empty) {
Optional<int> opt; Optional<int> opt;
EXPECT_FALSE(1 <= opt); EXPECT_FALSE(1 <= opt);
...@@ -1078,6 +1157,11 @@ TEST(OptionalTest, LessEqValue_NotEmpty) { ...@@ -1078,6 +1157,11 @@ TEST(OptionalTest, LessEqValue_NotEmpty) {
} }
} }
TEST(OptionalTest, LessEqValue_DifferentType) {
Optional<int> opt(0);
EXPECT_TRUE(0.0 <= opt);
}
TEST(OptionalTest, ValueGreater_Empty) { TEST(OptionalTest, ValueGreater_Empty) {
Optional<int> opt; Optional<int> opt;
EXPECT_FALSE(opt > 1); EXPECT_FALSE(opt > 1);
...@@ -1098,6 +1182,11 @@ TEST(OptionalTest, ValueGreater_NotEmpty) { ...@@ -1098,6 +1182,11 @@ TEST(OptionalTest, ValueGreater_NotEmpty) {
} }
} }
TEST(OptionalTest, ValueGreater_DifferentType) {
Optional<int> opt(0);
EXPECT_FALSE(opt > 0.0);
}
TEST(OptionalTest, GreaterValue_Empty) { TEST(OptionalTest, GreaterValue_Empty) {
Optional<int> opt; Optional<int> opt;
EXPECT_TRUE(1 > opt); EXPECT_TRUE(1 > opt);
...@@ -1118,6 +1207,11 @@ TEST(OptionalTest, GreaterValue_NotEmpty) { ...@@ -1118,6 +1207,11 @@ TEST(OptionalTest, GreaterValue_NotEmpty) {
} }
} }
TEST(OptionalTest, GreaterValue_DifferentType) {
Optional<int> opt(0);
EXPECT_FALSE(0.0 > opt);
}
TEST(OptionalTest, ValueGreaterEq_Empty) { TEST(OptionalTest, ValueGreaterEq_Empty) {
Optional<int> opt; Optional<int> opt;
EXPECT_FALSE(opt >= 1); EXPECT_FALSE(opt >= 1);
...@@ -1138,6 +1232,11 @@ TEST(OptionalTest, ValueGreaterEq_NotEmpty) { ...@@ -1138,6 +1232,11 @@ TEST(OptionalTest, ValueGreaterEq_NotEmpty) {
} }
} }
TEST(OptionalTest, ValueGreaterEq_DifferentType) {
Optional<int> opt(0);
EXPECT_TRUE(opt <= 0.0);
}
TEST(OptionalTest, GreaterEqValue_Empty) { TEST(OptionalTest, GreaterEqValue_Empty) {
Optional<int> opt; Optional<int> opt;
EXPECT_TRUE(1 >= opt); EXPECT_TRUE(1 >= opt);
...@@ -1158,6 +1257,11 @@ TEST(OptionalTest, GreaterEqValue_NotEmpty) { ...@@ -1158,6 +1257,11 @@ TEST(OptionalTest, GreaterEqValue_NotEmpty) {
} }
} }
TEST(OptionalTest, GreaterEqValue_DifferentType) {
Optional<int> opt(0);
EXPECT_TRUE(0.0 >= opt);
}
TEST(OptionalTest, NotEquals) { TEST(OptionalTest, NotEquals) {
{ {
Optional<float> a(0.1f); Optional<float> a(0.1f);
...@@ -1171,6 +1275,12 @@ TEST(OptionalTest, NotEquals) { ...@@ -1171,6 +1275,12 @@ TEST(OptionalTest, NotEquals) {
EXPECT_NE(a, b); EXPECT_NE(a, b);
} }
{
Optional<int> a(1);
Optional<double> b(2);
EXPECT_NE(a, b);
}
{ {
Optional<TestObject> a(TestObject(3, 0.1)); Optional<TestObject> a(TestObject(3, 0.1));
Optional<TestObject> b(TestObject(4, 1.0)); Optional<TestObject> b(TestObject(4, 1.0));
......
...@@ -28,6 +28,7 @@ struct CORSErrorStatus { ...@@ -28,6 +28,7 @@ struct CORSErrorStatus {
~CORSErrorStatus(); ~CORSErrorStatus();
bool operator==(const CORSErrorStatus& rhs) const; bool operator==(const CORSErrorStatus& rhs) const;
bool operator!=(const CORSErrorStatus& rhs) const { return !(*this == rhs); }
network::mojom::CORSError cors_error; network::mojom::CORSError cors_error;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment