Index: llvm/trunk/include/llvm/ADT/Optional.h =================================================================== --- llvm/trunk/include/llvm/ADT/Optional.h +++ llvm/trunk/include/llvm/ADT/Optional.h @@ -150,18 +150,43 @@ 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 @@ 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 Index: llvm/trunk/unittests/ADT/OptionalTest.cpp =================================================================== --- llvm/trunk/unittests/ADT/OptionalTest.cpp +++ llvm/trunk/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 @@ #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