From 3550ca21cd965645334d5b42876fa6fa2dfcb8f1 Mon Sep 17 00:00:00 2001 From: Max Wang Date: Wed, 15 Oct 2014 13:00:18 -0700 Subject: [PATCH] Allow Optional {=,!}= T comparisons in folly::Optional Summary: I can see how {<,>}{,=} comparisons might be ambiguous, but equality should never be, so let's allow it. Test Plan: tests Reviewed By: tjackson@fb.com Subscribers: trunkagent, njormrod FB internal diff: D1618088 --- folly/Optional.h | 57 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/folly/Optional.h b/folly/Optional.h index eea1957c..032022b1 100644 --- a/folly/Optional.h +++ b/folly/Optional.h @@ -54,10 +54,10 @@ * cout << *v << endl; * } */ -#include #include #include #include +#include #include @@ -281,11 +281,27 @@ Opt make_optional(T&& v) { return Opt(std::forward(v)); } +/////////////////////////////////////////////////////////////////////////////// +// Comparisons. + template -bool operator< (const Optional& a, const Optional& b) { - if (a.hasValue() != b.hasValue()) { return a.hasValue() < b.hasValue(); } - if (a.hasValue()) { return a.value() < b.value(); } - return false; +bool operator==(const Optional& a, const V& b) { + return a.hasValue() && a.value() == b; +} + +template +bool operator!=(const Optional& a, const V& b) { + return !(a == b); +} + +template +bool operator==(const V& a, const Optional b) { + return b.hasValue() && b.value() == a; +} + +template +bool operator!=(const V& a, const Optional& b) { + return !(a == b); } template @@ -295,19 +311,16 @@ bool operator==(const Optional& a, const Optional& b) { return true; } -template -bool operator<=(const Optional& a, const Optional& b) { - return !(b < a); -} - template bool operator!=(const Optional& a, const Optional& b) { - return !(b == a); + return !(a == b); } template -bool operator>=(const Optional& a, const Optional& b) { - return !(a < b); +bool operator< (const Optional& a, const Optional& b) { + if (a.hasValue() != b.hasValue()) { return a.hasValue() < b.hasValue(); } + if (a.hasValue()) { return a.value() < b.value(); } + return false; } template @@ -315,20 +328,28 @@ bool operator> (const Optional& a, const Optional& b) { return b < a; } +template +bool operator<=(const Optional& a, const Optional& b) { + return !(b < a); +} + +template +bool operator>=(const Optional& a, const Optional& b) { + return !(a < b); +} + // To supress comparability of Optional with T, despite implicit conversion. template bool operator< (const Optional&, const V& other) = delete; template bool operator<=(const Optional&, const V& other) = delete; -template bool operator==(const Optional&, const V& other) = delete; -template bool operator!=(const Optional&, const V& other) = delete; template bool operator>=(const Optional&, const V& other) = delete; template bool operator> (const Optional&, const V& other) = delete; template bool operator< (const V& other, const Optional&) = delete; template bool operator<=(const V& other, const Optional&) = delete; -template bool operator==(const V& other, const Optional&) = delete; -template bool operator!=(const V& other, const Optional&) = delete; template bool operator>=(const V& other, const Optional&) = delete; template bool operator> (const V& other, const Optional&) = delete; +/////////////////////////////////////////////////////////////////////////////// + } // namespace folly -#endif//FOLLY_OPTIONAL_H_ +#endif // FOLLY_OPTIONAL_H_ -- 2.34.1