/*
- * Copyright 2013 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 <stdexcept>
#include <type_traits>
+#include <utility>
#include <boost/operators.hpp>
+#include <folly/Portability.h>
+
namespace folly {
namespace detail { struct NoneHelper {}; }
* gcc-4.7 warns about use of uninitialized memory around the use of storage_
* even though this is explicitly initialized at each point.
*/
-#ifdef __GNUC__
+#if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wuninitialized"
# pragma GCC diagnostic ignored "-Wpragmas"
# pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif // __GNUC__
+class OptionalEmptyException : public std::runtime_error {
+ public:
+ OptionalEmptyException()
+ : std::runtime_error("Empty Optional cannot be unwrapped") {}
+};
+
template<class Value>
-class Optional : boost::totally_ordered<Optional<Value>,
- boost::totally_ordered<Optional<Value>, Value>> {
- typedef void (Optional::*bool_type)() const;
- void truthy() const {};
+class Optional {
public:
+ typedef Value value_type;
+
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()
+ Optional() noexcept
: hasValue_(false) {
}
- Optional(const Optional& src) {
+ Optional(const Optional& src)
+ noexcept(std::is_nothrow_copy_constructible<Value>::value) {
+
if (src.hasValue()) {
construct(src.value());
} else {
}
}
- Optional(Optional&& src) {
+ Optional(Optional&& src)
+ noexcept(std::is_nothrow_move_constructible<Value>::value) {
+
if (src.hasValue()) {
construct(std::move(src.value()));
src.clear();
}
}
- /* implicit */ Optional(const None& empty)
+ /* implicit */ Optional(const None&) noexcept
: hasValue_(false) {
}
- /* implicit */ Optional(Value&& newValue) {
+ /* implicit */ Optional(Value&& newValue)
+ noexcept(std::is_nothrow_move_constructible<Value>::value) {
construct(std::move(newValue));
}
- /* implicit */ Optional(const Value& newValue) {
+ /* implicit */ Optional(const Value& newValue)
+ noexcept(std::is_nothrow_copy_constructible<Value>::value) {
construct(newValue);
}
- ~Optional() {
+ ~Optional() noexcept {
clear();
}
}
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();
+ }
}
}
return *this;
}
- bool operator<(const Optional& other) const {
- if (hasValue() != other.hasValue()) {
- return hasValue() < other.hasValue();
- }
- if (hasValue()) {
- return value() < other.value();
- }
- return false; // both empty
- }
+ Optional& operator=(Optional &&other)
+ noexcept (std::is_nothrow_move_assignable<Value>::value) {
- bool operator<(const Value& other) const {
- return !hasValue() || value() < other;
+ assign(std::move(other));
+ return *this;
}
- bool operator==(const Optional& other) const {
- if (hasValue()) {
- return other.hasValue() && value() == other.value();
- } else {
- return !other.hasValue();
- }
- }
+ Optional& operator=(const Optional &other)
+ noexcept (std::is_nothrow_copy_assignable<Value>::value) {
- bool operator==(const Value& other) const {
- return hasValue() && value() == other;
+ assign(other);
+ return *this;
}
template<class... Args>
}
}
- const Value& value() const {
- assert(hasValue());
+ const Value& value() const& {
+ require_value();
return value_;
}
- Value& value() {
- assert(hasValue());
+ Value& value() & {
+ require_value();
return value_;
}
+ Value value() && {
+ require_value();
+ return std::move(value_);
+ }
+
+ const Value* get_pointer() const& { return hasValue_ ? &value_ : nullptr; }
+ Value* get_pointer() & { return hasValue_ ? &value_ : nullptr; }
+ Value* get_pointer() && = delete;
+
bool hasValue() const { return hasValue_; }
- /* safe bool idiom */
- operator bool_type() const {
- return hasValue() ? &Optional::truthy : nullptr;
+ explicit operator bool() const {
+ return hasValue();
}
- const Value& operator*() const { return value(); }
- Value& operator*() { return value(); }
+ const Value& operator*() const& { return value(); }
+ Value& operator*() & { return value(); }
+ Value operator*() && { return std::move(value()); }
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:
+ void require_value() const {
+ if (!hasValue_) {
+ throw OptionalEmptyException();
+ }
+ }
+
template<class... Args>
void construct(Args&&... args) {
const void* ptr = &value_;
bool hasValue_;
};
+#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
+#endif
template<class T>
const T* get_pointer(const Optional<T>& opt) {
- return opt ? &opt.value() : nullptr;
+ return opt.get_pointer();
}
template<class T>
T* get_pointer(Optional<T>& opt) {
- return opt ? &opt.value() : nullptr;
+ return opt.get_pointer();
}
template<class T>
}
}
-}// namespace folly
+template<class T,
+ class Opt = Optional<typename std::decay<T>::type>>
+Opt make_optional(T&& v) {
+ return Opt(std::forward<T>(v));
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// Comparisons.
+
+template<class V>
+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>
+bool operator==(const Optional<V>& a, const Optional<V>& b) {
+ if (a.hasValue() != b.hasValue()) { return false; }
+ if (a.hasValue()) { return a.value() == b.value(); }
+ return true;
+}
+
+template<class V>
+bool operator!=(const Optional<V>& a, const Optional<V>& b) {
+ return !(a == b);
+}
+
+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;
+}
+
+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);
+}
+
+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 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_