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> {
using internal::OptionalBase<T>::storage_;
};
template <class T>
constexpr bool operator==(const Optional<T>& lhs, const Optional<T>& rhs) {
return !!lhs != !!rhs ? false : lhs == nullopt || (*lhs == *rhs);
}
template <class T>
constexpr bool operator!=(const Optional<T>& lhs, const Optional<T>& rhs) {
return !(lhs == rhs);
}
template <class T>
constexpr bool operator<(const Optional<T>& lhs, const Optional<T>& rhs) {
return rhs == nullopt ? false : (lhs == nullopt ? true : *lhs < *rhs);
}
template <class T>
constexpr bool operator<=(const Optional<T>& lhs, const Optional<T>& rhs) {
return !(rhs < lhs);
}
template <class T>
constexpr bool operator>(const Optional<T>& lhs, const Optional<T>& rhs) {
return rhs < lhs;
}
template <class T>
constexpr bool operator>=(const Optional<T>& lhs, const Optional<T>& rhs) {
return !(lhs < rhs);
// Here after defines comparation operators. The definition follows
// http://en.cppreference.com/w/cpp/utility/optional/operator_cmp
// while bool() casting is replaced by has_value() to meet the chromium
// style guide.
template <class T, class U>
constexpr bool operator==(const Optional<T>& lhs, const Optional<U>& rhs) {
if (lhs.has_value() != rhs.has_value())
return false;
if (!lhs.has_value())
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() != rhs.has_value())
return true;
if (!lhs.has_value())
return false;
return *lhs != *rhs;
}
template <class T, class U>
constexpr bool operator<(const Optional<T>& lhs, const Optional<U>& rhs) {
if (!rhs.has_value())
return false;
if (!lhs.has_value())
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>
......@@ -365,12 +393,12 @@ constexpr bool operator==(nullopt_t, const Optional<T>& opt) {
template <class T>
constexpr bool operator!=(const Optional<T>& opt, nullopt_t) {
return !!opt;
return opt.has_value();
}
template <class T>
constexpr bool operator!=(nullopt_t, const Optional<T>& opt) {
return !!opt;
return opt.has_value();
}
template <class T>
......@@ -380,7 +408,7 @@ constexpr bool operator<(const Optional<T>& opt, nullopt_t) {
template <class T>
constexpr bool operator<(nullopt_t, const Optional<T>& opt) {
return !!opt;
return opt.has_value();
}
template <class T>
......@@ -395,7 +423,7 @@ constexpr bool operator<=(nullopt_t, const Optional<T>& opt) {
template <class T>
constexpr bool operator>(const Optional<T>& opt, nullopt_t) {
return !!opt;
return opt.has_value();
}
template <class T>
......@@ -413,64 +441,64 @@ constexpr bool operator>=(nullopt_t, const Optional<T>& opt) {
return !opt;
}
template <class T>
constexpr bool operator==(const Optional<T>& opt, const T& value) {
return opt != nullopt ? *opt == value : false;
template <class T, class U>
constexpr bool operator==(const Optional<T>& opt, const U& value) {
return opt.has_value() ? *opt == value : false;
}
template <class T>
constexpr bool operator==(const T& value, const Optional<T>& opt) {
return opt == value;
template <class T, class U>
constexpr bool operator==(const U& value, const Optional<T>& opt) {
return opt.has_value() ? value == *opt : false;
}
template <class T>
constexpr bool operator!=(const Optional<T>& opt, const T& value) {
return !(opt == value);
template <class T, class U>
constexpr bool operator!=(const Optional<T>& opt, const U& value) {
return opt.has_value() ? *opt != value : true;
}
template <class T>
constexpr bool operator!=(const T& value, const Optional<T>& opt) {
return !(opt == value);
template <class T, class U>
constexpr bool operator!=(const U& value, const Optional<T>& opt) {
return opt.has_value() ? value != *opt : true;
}
template <class T>
constexpr bool operator<(const Optional<T>& opt, const T& value) {
return opt != nullopt ? *opt < value : true;
template <class T, class U>
constexpr bool operator<(const Optional<T>& opt, const U& value) {
return opt.has_value() ? *opt < value : true;
}
template <class T>
constexpr bool operator<(const T& value, const Optional<T>& opt) {
return opt != nullopt ? value < *opt : false;
template <class T, class U>
constexpr bool operator<(const U& value, const Optional<T>& opt) {
return opt.has_value() ? value < *opt : false;
}
template <class T>
constexpr bool operator<=(const Optional<T>& opt, const T& value) {
return !(opt > value);
template <class T, class U>
constexpr bool operator<=(const Optional<T>& opt, const U& value) {
return opt.has_value() ? *opt <= value : true;
}
template <class T>
constexpr bool operator<=(const T& value, const Optional<T>& opt) {
return !(value > opt);
template <class T, class U>
constexpr bool operator<=(const U& value, const Optional<T>& opt) {
return opt.has_value() ? value <= *opt : false;
}
template <class T>
constexpr bool operator>(const Optional<T>& opt, const T& value) {
return value < opt;
template <class T, class U>
constexpr bool operator>(const Optional<T>& opt, const U& value) {
return opt.has_value() ? *opt > value : false;
}
template <class T>
constexpr bool operator>(const T& value, const Optional<T>& opt) {
return opt < value;
template <class T, class U>
constexpr bool operator>(const U& value, const Optional<T>& opt) {
return opt.has_value() ? value > *opt : true;
}
template <class T>
constexpr bool operator>=(const Optional<T>& opt, const T& value) {
return !(opt < value);
template <class T, class U>
constexpr bool operator>=(const Optional<T>& opt, const U& value) {
return opt.has_value() ? *opt >= value : false;
}
template <class T>
constexpr bool operator>=(const T& value, const Optional<T>& opt) {
return !(value < opt);
template <class T, class U>
constexpr bool operator>=(const U& value, const Optional<T>& opt) {
return opt.has_value() ? value >= *opt : true;
}
template <class T>
......
......@@ -73,9 +73,11 @@ class TestObject {
}
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_; }
State state() const { return state_; }
int move_ctors_count() const { return move_ctors_count_; }
......@@ -606,6 +608,13 @@ TEST(OptionalTest, Equals_TwoDifferent) {
EXPECT_FALSE(a == b);
}
TEST(OptionalTest, Equals_DifferentType) {
Optional<int> a(0);
Optional<double> b(0);
EXPECT_TRUE(a == b);
}
TEST(OptionalTest, NotEquals_TwoEmpty) {
Optional<int> a;
Optional<int> b;
......@@ -634,6 +643,13 @@ TEST(OptionalTest, NotEquals_TwoDifferent) {
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) {
Optional<int> l;
Optional<int> r(1);
......@@ -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) {
Optional<int> l;
Optional<int> r(1);
......@@ -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) {
Optional<int> l;
Optional<int> r;
......@@ -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) {
Optional<int> l;
Optional<int> r;
......@@ -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) {
{
Optional<int> opt;
......@@ -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) {
Optional<int> opt;
EXPECT_FALSE(1 == opt);
......@@ -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) {
Optional<int> opt;
EXPECT_TRUE(opt != 1);
......@@ -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) {
Optional<int> opt;
EXPECT_TRUE(1 != opt);
......@@ -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) {
Optional<int> opt;
EXPECT_TRUE(opt < 1);
......@@ -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) {
Optional<int> opt;
EXPECT_FALSE(1 < opt);
......@@ -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) {
Optional<int> opt;
EXPECT_TRUE(opt <= 1);
......@@ -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) {
Optional<int> opt;
EXPECT_FALSE(1 <= opt);
......@@ -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) {
Optional<int> opt;
EXPECT_FALSE(opt > 1);
......@@ -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) {
Optional<int> opt;
EXPECT_TRUE(1 > opt);
......@@ -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) {
Optional<int> opt;
EXPECT_FALSE(opt >= 1);
......@@ -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) {
Optional<int> opt;
EXPECT_TRUE(1 >= opt);
......@@ -1158,6 +1257,11 @@ TEST(OptionalTest, GreaterEqValue_NotEmpty) {
}
}
TEST(OptionalTest, GreaterEqValue_DifferentType) {
Optional<int> opt(0);
EXPECT_TRUE(0.0 >= opt);
}
TEST(OptionalTest, NotEquals) {
{
Optional<float> a(0.1f);
......@@ -1171,6 +1275,12 @@ TEST(OptionalTest, NotEquals) {
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> b(TestObject(4, 1.0));
......
......@@ -28,6 +28,7 @@ struct CORSErrorStatus {
~CORSErrorStatus();
bool operator==(const CORSErrorStatus& rhs) const;
bool operator!=(const CORSErrorStatus& rhs) const { return !(*this == rhs); }
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