From 0fdb2daa8ddf42d50edb74ab0dc1899c86e8366d Mon Sep 17 00:00:00 2001 From: Tim Shen Date: Thu, 11 Aug 2016 20:10:15 +0000 Subject: [PATCH] [ADT] Add relation operators for Optional Summary: Make Optional's behavior the same as the coming std::optional. Reviewers: dblaikie Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D23178 llvm-svn: 278397 --- llvm/include/llvm/ADT/Optional.h | 167 ++++++++++++++++++++++++------------ llvm/unittests/ADT/OptionalTest.cpp | 150 +++++++++++++++++++++++++++++--- 2 files changed, 253 insertions(+), 64 deletions(-) diff --git a/llvm/include/llvm/ADT/Optional.h b/llvm/include/llvm/ADT/Optional.h index d9acaf6..20568c5 100644 --- a/llvm/include/llvm/ADT/Optional.h +++ b/llvm/include/llvm/ADT/Optional.h @@ -150,18 +150,43 @@ template struct isPodLike > { static const bool value = isPodLike::value; }; -/// \brief Poison comparison between two \c Optional objects. Clients needs to -/// explicitly compare the underlying values and account for empty \c Optional -/// objects. -/// -/// This routine will never be defined. It returns \c void to help diagnose -/// errors at compile time. -template -void operator==(const Optional &X, const Optional &Y); +template +bool operator==(const Optional &X, const Optional &Y) { + if (X && Y) + return *X == *Y; + return X.hasValue() == Y.hasValue(); +} + +template +bool operator!=(const Optional &X, const Optional &Y) { + return !(X == Y); +} + +template +bool operator<(const Optional &X, const Optional &Y) { + if (X && Y) + return *X < *Y; + return X.hasValue() < Y.hasValue(); +} + +template +bool operator<=(const Optional &X, const Optional &Y) { + return !(Y < X); +} + +template +bool operator>(const Optional &X, const Optional &Y) { + return Y < X; +} + +template +bool operator>=(const Optional &X, const Optional &Y) { + return !(X < Y); +} template bool operator==(const Optional &X, NoneType) { - return !X.hasValue(); + return !X; } template @@ -178,50 +203,86 @@ template bool operator!=(NoneType, const Optional &X) { return X != None; } -/// \brief Poison comparison between two \c Optional objects. Clients needs to -/// explicitly compare the underlying values and account for empty \c Optional -/// objects. -/// -/// This routine will never be defined. It returns \c void to help diagnose -/// errors at compile time. -template -void operator!=(const Optional &X, const Optional &Y); - -/// \brief Poison comparison between two \c Optional objects. Clients needs to -/// explicitly compare the underlying values and account for empty \c Optional -/// objects. -/// -/// This routine will never be defined. It returns \c void to help diagnose -/// errors at compile time. -template -void operator<(const Optional &X, const Optional &Y); - -/// \brief Poison comparison between two \c Optional objects. Clients needs to -/// explicitly compare the underlying values and account for empty \c Optional -/// objects. -/// -/// This routine will never be defined. It returns \c void to help diagnose -/// errors at compile time. -template -void operator<=(const Optional &X, const Optional &Y); - -/// \brief Poison comparison between two \c Optional objects. Clients needs to -/// explicitly compare the underlying values and account for empty \c Optional -/// objects. -/// -/// This routine will never be defined. It returns \c void to help diagnose -/// errors at compile time. -template -void operator>=(const Optional &X, const Optional &Y); - -/// \brief Poison comparison between two \c Optional objects. Clients needs to -/// explicitly compare the underlying values and account for empty \c Optional -/// objects. -/// -/// This routine will never be defined. It returns \c void to help diagnose -/// errors at compile time. -template -void operator>(const Optional &X, const Optional &Y); + +template bool operator<(const Optional &X, NoneType) { + return false; +} + +template bool operator<(NoneType, const Optional &X) { + return X.hasValue(); +} + +template bool operator<=(const Optional &X, NoneType) { + return !(None < X); +} + +template bool operator<=(NoneType, const Optional &X) { + return !(X < None); +} + +template bool operator>(const Optional &X, NoneType) { + return None < X; +} + +template bool operator>(NoneType, const Optional &X) { + return X < None; +} + +template bool operator>=(const Optional &X, NoneType) { + return None <= X; +} + +template bool operator>=(NoneType, const Optional &X) { + return X <= None; +} + +template bool operator==(const Optional &X, const T &Y) { + return X && *X == Y; +} + +template bool operator==(const T &X, const Optional &Y) { + return Y && X == *Y; +} + +template bool operator!=(const Optional &X, const T &Y) { + return !(X == Y); +} + +template bool operator!=(const T &X, const Optional &Y) { + return !(X == Y); +} + +template bool operator<(const Optional &X, const T &Y) { + return !X || *X < Y; +} + +template bool operator<(const T &X, const Optional &Y) { + return Y && X < *Y; +} + +template bool operator<=(const Optional &X, const T &Y) { + return !(Y < X); +} + +template bool operator<=(const T &X, const Optional &Y) { + return !(Y < X); +} + +template bool operator>(const Optional &X, const T &Y) { + return Y < X; +} + +template bool operator>(const T &X, const Optional &Y) { + return Y < X; +} + +template bool operator>=(const Optional &X, const T &Y) { + return !(X < Y); +} + +template bool operator>=(const T &X, const Optional &Y) { + return !(X < Y); +} } // end llvm namespace diff --git a/llvm/unittests/ADT/OptionalTest.cpp b/llvm/unittests/ADT/OptionalTest.cpp index 18b59e3..4c0c993 100644 --- a/llvm/unittests/ADT/OptionalTest.cpp +++ b/llvm/unittests/ADT/OptionalTest.cpp @@ -9,6 +9,7 @@ #include "gtest/gtest.h" #include "llvm/ADT/Optional.h" + using namespace llvm; namespace { @@ -377,17 +378,144 @@ TEST_F(OptionalTest, MoveGetValueOr) { #endif // LLVM_HAS_RVALUE_REFERENCE_THIS -TEST_F(OptionalTest, NoneComparison) { - Optional o; - EXPECT_EQ(o, None); - EXPECT_EQ(None, o); - EXPECT_FALSE(o != None); - EXPECT_FALSE(None != o); - o = 3; - EXPECT_FALSE(o == None); - EXPECT_FALSE(None == o); - EXPECT_TRUE(o != None); - EXPECT_TRUE(None != o); +struct EqualTo { + template static bool apply(const T &X, const U &Y) { + return X == Y; + } +}; + +struct NotEqualTo { + template static bool apply(const T &X, const U &Y) { + return X != Y; + } +}; + +struct Less { + template static bool apply(const T &X, const U &Y) { + return X < Y; + } +}; + +struct Greater { + template static bool apply(const T &X, const U &Y) { + return X > Y; + } +}; + +struct LessEqual { + template static bool apply(const T &X, const U &Y) { + return X <= Y; + } +}; + +struct GreaterEqual { + template static bool apply(const T &X, const U &Y) { + return X >= Y; + } +}; + +template +void CheckRelation(const Optional &Lhs, const Optional &Rhs, + bool Expected) { + EXPECT_EQ(Expected, OperatorT::apply(Lhs, Rhs)); + + if (Lhs) + EXPECT_EQ(Expected, OperatorT::apply(*Lhs, Rhs)); + else + EXPECT_EQ(Expected, OperatorT::apply(None, Rhs)); + + if (Rhs) + EXPECT_EQ(Expected, OperatorT::apply(Lhs, *Rhs)); + else + EXPECT_EQ(Expected, OperatorT::apply(Lhs, None)); +} + +struct EqualityMock {}; +const Optional NoneEq, EqualityLhs((EqualityMock())), + EqualityRhs((EqualityMock())); +bool IsEqual; + +bool operator==(const EqualityMock &Lhs, const EqualityMock &Rhs) { + EXPECT_EQ(&*EqualityLhs, &Lhs); + EXPECT_EQ(&*EqualityRhs, &Rhs); + return IsEqual; +} + +TEST_F(OptionalTest, OperatorEqual) { + CheckRelation(NoneEq, NoneEq, true); + CheckRelation(NoneEq, EqualityRhs, false); + CheckRelation(EqualityLhs, NoneEq, false); + + IsEqual = false; + CheckRelation(EqualityLhs, EqualityRhs, IsEqual); + IsEqual = true; + CheckRelation(EqualityLhs, EqualityRhs, IsEqual); +} + +TEST_F(OptionalTest, OperatorNotEqual) { + CheckRelation(NoneEq, NoneEq, false); + CheckRelation(NoneEq, EqualityRhs, true); + CheckRelation(EqualityLhs, NoneEq, true); + + IsEqual = false; + CheckRelation(EqualityLhs, EqualityRhs, !IsEqual); + IsEqual = true; + CheckRelation(EqualityLhs, EqualityRhs, !IsEqual); +} + +struct InequalityMock {}; +const Optional NoneIneq, InequalityLhs((InequalityMock())), + InequalityRhs((InequalityMock())); +bool IsLess; + +bool operator<(const InequalityMock &Lhs, const InequalityMock &Rhs) { + EXPECT_EQ(&*InequalityLhs, &Lhs); + EXPECT_EQ(&*InequalityRhs, &Rhs); + return IsLess; +} + +TEST_F(OptionalTest, OperatorLess) { + CheckRelation(NoneIneq, NoneIneq, false); + CheckRelation(NoneIneq, InequalityRhs, true); + CheckRelation(InequalityLhs, NoneIneq, false); + + IsLess = false; + CheckRelation(InequalityLhs, InequalityRhs, IsLess); + IsLess = true; + CheckRelation(InequalityLhs, InequalityRhs, IsLess); +} + +TEST_F(OptionalTest, OperatorGreater) { + CheckRelation(NoneIneq, NoneIneq, false); + CheckRelation(NoneIneq, InequalityRhs, false); + CheckRelation(InequalityLhs, NoneIneq, true); + + IsLess = false; + CheckRelation(InequalityRhs, InequalityLhs, IsLess); + IsLess = true; + CheckRelation(InequalityRhs, InequalityLhs, IsLess); +} + +TEST_F(OptionalTest, OperatorLessEqual) { + CheckRelation(NoneIneq, NoneIneq, true); + CheckRelation(NoneIneq, InequalityRhs, true); + CheckRelation(InequalityLhs, NoneIneq, false); + + IsLess = false; + CheckRelation(InequalityRhs, InequalityLhs, !IsLess); + IsLess = true; + CheckRelation(InequalityRhs, InequalityLhs, !IsLess); +} + +TEST_F(OptionalTest, OperatorGreaterEqual) { + CheckRelation(NoneIneq, NoneIneq, true); + CheckRelation(NoneIneq, InequalityRhs, false); + CheckRelation(InequalityLhs, NoneIneq, true); + + IsLess = false; + CheckRelation(InequalityLhs, InequalityRhs, !IsLess); + IsLess = true; + CheckRelation(InequalityLhs, InequalityRhs, !IsLess); } } // end anonymous namespace -- 2.7.4