Commit 3550ca21 authored by Max Wang's avatar Max Wang Committed by dcsommer

Allow Optional<T> {=,!}= 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
parent 71431a93
...@@ -54,10 +54,10 @@ ...@@ -54,10 +54,10 @@
* cout << *v << endl; * cout << *v << endl;
* } * }
*/ */
#include <utility>
#include <cassert> #include <cassert>
#include <cstddef> #include <cstddef>
#include <type_traits> #include <type_traits>
#include <utility>
#include <boost/operators.hpp> #include <boost/operators.hpp>
...@@ -281,11 +281,27 @@ Opt make_optional(T&& v) { ...@@ -281,11 +281,27 @@ Opt make_optional(T&& v) {
return Opt(std::forward<T>(v)); return Opt(std::forward<T>(v));
} }
///////////////////////////////////////////////////////////////////////////////
// Comparisons.
template<class V> template<class V>
bool operator< (const Optional<V>& a, const Optional<V>& b) { bool operator==(const Optional<V>& a, const V& b) {
if (a.hasValue() != b.hasValue()) { return a.hasValue() < b.hasValue(); } return a.hasValue() && a.value() == b;
if (a.hasValue()) { return a.value() < b.value(); } }
return false;
template<class V>
bool operator!=(const Optional<V>& a, const V& b) {
return !(a == b);
}
template<class V>
bool operator==(const V& a, const Optional<V&> b) {
return b.hasValue() && b.value() == a;
}
template<class V>
bool operator!=(const V& a, const Optional<V>& b) {
return !(a == b);
} }
template<class V> template<class V>
...@@ -295,19 +311,16 @@ bool operator==(const Optional<V>& a, const Optional<V>& b) { ...@@ -295,19 +311,16 @@ bool operator==(const Optional<V>& a, const Optional<V>& b) {
return true; return true;
} }
template<class V>
bool operator<=(const Optional<V>& a, const Optional<V>& b) {
return !(b < a);
}
template<class V> template<class V>
bool operator!=(const Optional<V>& a, const Optional<V>& b) { bool operator!=(const Optional<V>& a, const Optional<V>& b) {
return !(b == a); return !(a == b);
} }
template<class V> template<class V>
bool operator>=(const Optional<V>& a, const Optional<V>& b) { bool operator< (const Optional<V>& a, const Optional<V>& b) {
return !(a < b); if (a.hasValue() != b.hasValue()) { return a.hasValue() < b.hasValue(); }
if (a.hasValue()) { return a.value() < b.value(); }
return false;
} }
template<class V> template<class V>
...@@ -315,20 +328,28 @@ bool operator> (const Optional<V>& a, const Optional<V>& b) { ...@@ -315,20 +328,28 @@ bool operator> (const Optional<V>& a, const Optional<V>& b) {
return b < a; return b < a;
} }
template<class V>
bool operator<=(const Optional<V>& a, const Optional<V>& b) {
return !(b < a);
}
template<class V>
bool operator>=(const Optional<V>& a, const Optional<V>& b) {
return !(a < b);
}
// To supress comparability of Optional<T> with T, despite implicit conversion. // To supress comparability of Optional<T> with T, despite implicit conversion.
template<class V> bool operator< (const Optional<V>&, const V& other) = delete; template<class V> bool operator< (const Optional<V>&, const V& other) = delete;
template<class V> bool operator<=(const Optional<V>&, const V& other) = delete; template<class V> bool operator<=(const Optional<V>&, const V& other) = delete;
template<class V> bool operator==(const Optional<V>&, const V& other) = delete;
template<class V> bool operator!=(const Optional<V>&, const V& other) = delete;
template<class V> bool operator>=(const Optional<V>&, const V& other) = delete; template<class V> bool operator>=(const Optional<V>&, const V& other) = delete;
template<class V> bool operator> (const Optional<V>&, const V& other) = delete; template<class V> bool operator> (const Optional<V>&, const V& other) = delete;
template<class V> bool operator< (const V& other, const Optional<V>&) = delete; template<class V> bool operator< (const V& other, const Optional<V>&) = delete;
template<class V> bool operator<=(const V& other, const Optional<V>&) = delete; template<class V> bool operator<=(const V& other, const Optional<V>&) = delete;
template<class V> bool operator==(const V& other, const Optional<V>&) = delete;
template<class V> bool operator!=(const V& other, const Optional<V>&) = delete;
template<class V> bool operator>=(const V& other, const Optional<V>&) = delete; template<class V> bool operator>=(const V& other, const Optional<V>&) = delete;
template<class V> bool operator> (const V& other, const Optional<V>&) = delete; template<class V> bool operator> (const V& other, const Optional<V>&) = delete;
///////////////////////////////////////////////////////////////////////////////
} // namespace folly } // namespace folly
#endif//FOLLY_OPTIONAL_H_ #endif // FOLLY_OPTIONAL_H_
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