/*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* cout << *v << endl;
* }
*/
-#include <utility>
#include <cassert>
#include <cstddef>
#include <type_traits>
+#include <utility>
#include <boost/operators.hpp>
public:
static_assert(!std::is_reference<Value>::value,
"Optional may not be used with reference types");
+ static_assert(!std::is_abstract<Value>::value,
+ "Optional may not be used with abstract types");
Optional()
: hasValue_(false) {
}
void assign(Optional&& src) {
- if (src.hasValue()) {
- assign(std::move(src.value()));
- src.clear();
- } else {
- clear();
+ if (this != &src) {
+ if (src.hasValue()) {
+ assign(std::move(src.value()));
+ src.clear();
+ } else {
+ clear();
+ }
}
}
const Value* operator->() const { return &value(); }
Value* operator->() { return &value(); }
+ // Return a copy of the value if set, or a given default if not.
+ template <class U>
+ Value value_or(U&& dflt) const& {
+ return hasValue_ ? value_ : std::forward<U>(dflt);
+ }
+
+ template <class U>
+ Value value_or(U&& dflt) && {
+ return hasValue_ ? std::move(value_) : std::forward<U>(dflt);
+ }
+
private:
template<class... Args>
void construct(Args&&... args) {
return Opt(std::forward<T>(v));
}
+///////////////////////////////////////////////////////////////////////////////
+// Comparisons.
+
template<class V>
-bool operator< (const Optional<V>& a, const Optional<V>& 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<V>& a, const V& b) {
+ return a.hasValue() && a.value() == b;
+}
+
+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>
return true;
}
-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 !(b == a);
+ return !(a == b);
}
template<class V>
-bool operator>=(const Optional<V>& a, const Optional<V>& b) {
- return !(a < b);
+bool operator< (const Optional<V>& a, const Optional<V>& b) {
+ if (a.hasValue() != b.hasValue()) { return a.hasValue() < b.hasValue(); }
+ if (a.hasValue()) { return a.value() < b.value(); }
+ return false;
}
template<class V>
return b < a;
}
-// To supress comparability of Optional<T> with T, despite implicit conversion.
+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);
+}
+
+// Suppress 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 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
-#endif//FOLLY_OPTIONAL_H_
+#endif // FOLLY_OPTIONAL_H_