Index: include/llvm/ADT/Optional.h =================================================================== --- include/llvm/ADT/Optional.h +++ 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; +} + +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: unittests/ADT/OptionalTest.cpp =================================================================== --- unittests/ADT/OptionalTest.cpp +++ 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); +template +void CheckOptRelations(const Optional (&Opts)[3], + const bool (&Results)[3][3]) { + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + EXPECT_EQ(Results[i][j], OperatorT::apply(Opts[i], Opts[j])); + + if (Opts[i]) + EXPECT_EQ(Results[i][j], OperatorT::apply(*Opts[i], Opts[j])); + else + EXPECT_EQ(Results[i][j], OperatorT::apply(None, Opts[j])); + + if (Opts[j]) + EXPECT_EQ(Results[i][j], OperatorT::apply(Opts[i], *Opts[j])); + else + EXPECT_EQ(Results[i][j], OperatorT::apply(Opts[i], None)); + } + } +} + +struct EqualTo { + template static bool apply(const T &X, const U &Y) { + return X == Y; + } +}; + +TEST_F(OptionalTest, OperatorEqual) { + // None 3 4 + // None = < < + // 3 > = < + // 4 > > = + CheckOptRelations( + {None, 3, 4}, + { + {true, false, false}, + {false, true, false}, + {false, false, true}, + }); +} + +struct NotEqualTo { + template static bool apply(const T &X, const U &Y) { + return X != Y; + } +}; + +TEST_F(OptionalTest, OperatorNotEqual) { + // None 3 4 + // None = < < + // 3 > = < + // 4 > > = + CheckOptRelations( + {None, 3, 4}, + { + {false, true, true}, + {true, false, true}, + {true, true, false}, + }); +} + +struct Less { + template static bool apply(const T &X, const U &Y) { + return X < Y; + } +}; + +TEST_F(OptionalTest, OperatorLess) { + // None 3 4 + // None = < < + // 3 > = < + // 4 > > = + CheckOptRelations( + {None, 3, 4}, + { + {false, true, true}, + {false, false, true}, + {false, false, false}, + }); +} + +struct Greater { + template static bool apply(const T &X, const U &Y) { + return X > Y; + } +}; + +TEST_F(OptionalTest, OperatorGreater) { + // None 3 4 + // None = < < + // 3 > = < + // 4 > > = + CheckOptRelations( + {None, 3, 4}, + { + {false, false, false}, + {true, false, false}, + {true, true, false}, + }); +} + +struct LessEqual { + template static bool apply(const T &X, const U &Y) { + return X <= Y; + } +}; + +TEST_F(OptionalTest, OperatorLessEqual) { + // None 3 4 + // None = < < + // 3 > = < + // 4 > > = + CheckOptRelations( + {None, 3, 4}, + { + {true, true, true}, + {false, true, true}, + {false, false, true}, + }); +} + +struct GreaterEqual { + template static bool apply(const T &X, const U &Y) { + return X >= Y; + } +}; + +TEST_F(OptionalTest, OperatorGreaterEqual) { + // None 3 4 + // None = < < + // 3 > = < + // 4 > > = + CheckOptRelations( + {None, 3, 4}, + { + {true, false, false}, + {true, true, false}, + {true, true, true}, + }); } } // end anonymous namespace