X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FOptional.h;h=f8d39cb8cc39e0ce9b4407041e5a9d329be7e825;hb=ea815395588bf55ce57f505e05f5df469edf3436;hp=871357ceabb3168b29b3da49c13886482d1d11dd;hpb=fd20c97c69af72155fc5d611630b4799eeda6db8;p=folly.git diff --git a/folly/Optional.h b/folly/Optional.h index 871357ce..f8d39cb8 100644 --- a/folly/Optional.h +++ b/folly/Optional.h @@ -1,5 +1,5 @@ /* - * Copyright 2013 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,8 +14,7 @@ * limitations under the License. */ -#ifndef FOLLY_OPTIONAL_H_ -#define FOLLY_OPTIONAL_H_ +#pragma once /* * Optional - For conditional initialization of values, like boost::optional, @@ -54,76 +53,84 @@ * cout << *v << endl; * } */ -#include -#include + #include +#include +#include +#include #include +#include -#include +#include +#include +#include namespace folly { -namespace detail { struct NoneHelper {}; } +namespace detail { +struct NoneHelper {}; + +// Allow each translation unit to control its own -fexceptions setting. +// If exceptions are disabled, std::terminate() will be called instead of +// throwing OptionalEmptyException when the condition fails. +[[noreturn]] void throw_optional_empty_exception(); +} typedef int detail::NoneHelper::*None; const None none = nullptr; -/** - * gcc-4.7 warns about use of uninitialized memory around the use of storage_ - * even though this is explicitly initialized at each point. - */ -#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__ - -template -class Optional : boost::totally_ordered, - boost::totally_ordered, Value>> { - typedef void (Optional::*bool_type)() const; - void truthy() const {}; +class OptionalEmptyException : public std::runtime_error { public: - static_assert(!std::is_reference::value, - "Optional may not be used with reference types"); + OptionalEmptyException() + : std::runtime_error("Empty Optional cannot be unwrapped") {} +}; - Optional() - : hasValue_(false) { - } +template +class Optional { + public: + typedef Value value_type; + + static_assert( + !std::is_reference::value, + "Optional may not be used with reference types"); + static_assert( + !std::is_abstract::value, + "Optional may not be used with abstract types"); - Optional(const Optional& src) { + Optional() noexcept {} + + Optional(const Optional& src) noexcept( + std::is_nothrow_copy_constructible::value) { if (src.hasValue()) { - construct(src.value()); - } else { - hasValue_ = false; + storage_.construct(src.value()); } } - Optional(Optional&& src) { + Optional(Optional&& src) noexcept( + std::is_nothrow_move_constructible::value) { if (src.hasValue()) { - construct(std::move(src.value())); + storage_.construct(std::move(src.value())); src.clear(); - } else { - hasValue_ = false; } } - /* implicit */ Optional(const None& empty) - : hasValue_(false) { - } + /* implicit */ Optional(const None&) noexcept {} - /* implicit */ Optional(Value&& newValue) { - construct(std::move(newValue)); + /* implicit */ Optional(Value&& newValue) noexcept( + std::is_nothrow_move_constructible::value) { + storage_.construct(std::move(newValue)); } - /* implicit */ Optional(const Value& newValue) { - construct(newValue); + /* implicit */ Optional(const Value& newValue) noexcept( + std::is_nothrow_copy_constructible::value) { + storage_.construct(newValue); } - ~Optional() { - clear(); + template + explicit Optional(in_place_t, Args&&... args) noexcept( + std::is_nothrow_constructible::value) { + storage_.construct(std::forward(args)...); } void assign(const None&) { @@ -131,11 +138,13 @@ class Optional : boost::totally_ordered, } 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(); + } } } @@ -149,127 +158,211 @@ class Optional : boost::totally_ordered, void assign(Value&& newValue) { if (hasValue()) { - value_ = std::move(newValue); + *storage_.value_pointer() = std::move(newValue); } else { - construct(std::move(newValue)); + storage_.construct(std::move(newValue)); } } void assign(const Value& newValue) { if (hasValue()) { - value_ = newValue; + *storage_.value_pointer() = newValue; } else { - construct(newValue); + storage_.construct(newValue); } } - template + template Optional& operator=(Arg&& arg) { assign(std::forward(arg)); return *this; } - Optional& operator=(Optional &&other) { + Optional& operator=(Optional&& other) noexcept( + std::is_nothrow_move_assignable::value) { assign(std::move(other)); return *this; } - Optional& operator=(const Optional &other) { + Optional& operator=(const Optional& other) noexcept( + std::is_nothrow_copy_assignable::value) { assign(other); 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 + template + void emplace(Args&&... args) { + clear(); + storage_.construct(std::forward(args)...); } - bool operator<(const Value& other) const { - return !hasValue() || value() < other; + void clear() { + storage_.clear(); } - bool operator==(const Optional& other) const { - if (hasValue()) { - return other.hasValue() && value() == other.value(); - } else { - return !other.hasValue(); - } + const Value& value() const & { + require_value(); + return *storage_.value_pointer(); } - bool operator==(const Value& other) const { - return hasValue() && value() == other; + Value& value() & { + require_value(); + return *storage_.value_pointer(); } - template - void emplace(Args&&... args) { - clear(); - construct(std::forward(args)...); + Value&& value() && { + require_value(); + return std::move(*storage_.value_pointer()); } - void clear() { - if (hasValue()) { - hasValue_ = false; - value_.~Value(); - } + const Value&& value() const && { + require_value(); + return std::move(*storage_.value_pointer()); } - const Value& value() const { - assert(hasValue()); - return value_; + const Value* get_pointer() const & { + return storage_.value_pointer(); + } + Value* get_pointer() & { + return storage_.value_pointer(); } + Value* get_pointer() && = delete; - Value& value() { - assert(hasValue()); - return value_; + bool hasValue() const { + return storage_.hasValue(); } - bool hasValue() const { return hasValue_; } + explicit operator bool() const { + return hasValue(); + } - /* safe bool idiom */ - operator bool_type() const { - return hasValue() ? &Optional::truthy : nullptr; + const Value& operator*() const & { + return value(); + } + Value& operator*() & { + return value(); + } + const Value&& operator*() const && { + return std::move(value()); + } + Value&& operator*() && { + return std::move(value()); + } + + const Value* operator->() const { + return &value(); + } + Value* operator->() { + return &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 + Value value_or(U&& dflt) const & { + if (storage_.hasValue()) { + return *storage_.value_pointer(); + } + + return std::forward(dflt); + } - const Value* operator->() const { return &value(); } - Value* operator->() { return &value(); } + template + Value value_or(U&& dflt) && { + if (storage_.hasValue()) { + return std::move(*storage_.value_pointer()); + } + + return std::forward(dflt); + } private: - template - void construct(Args&&... args) { - const void* ptr = &value_; - // for supporting const types - new(const_cast(ptr)) Value(std::forward(args)...); - hasValue_ = true; + void require_value() const { + if (!storage_.hasValue()) { + detail::throw_optional_empty_exception(); + } } - // uninitialized - union { Value value_; }; - bool hasValue_; -}; + struct StorageTriviallyDestructible { + protected: + bool hasValue_; + typename std::aligned_storage::type + value_[1]; -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic pop -#endif + public: + StorageTriviallyDestructible() : hasValue_{false} {} + void clear() { + hasValue_ = false; + } + }; + + struct StorageNonTriviallyDestructible { + protected: + bool hasValue_; + typename std::aligned_storage::type + value_[1]; + + public: + StorageNonTriviallyDestructible() : hasValue_{false} {} + ~StorageNonTriviallyDestructible() { + clear(); + } + + void clear() { + if (hasValue_) { + hasValue_ = false; + launder(reinterpret_cast(value_))->~Value(); + } + } + }; + + struct Storage : std::conditional< + std::is_trivially_destructible::value, + StorageTriviallyDestructible, + StorageNonTriviallyDestructible>::type { + bool hasValue() const { + return this->hasValue_; + } + + Value* value_pointer() { + if (this->hasValue_) { + return launder(reinterpret_cast(this->value_)); + } + return nullptr; + } -template + Value const* value_pointer() const { + if (this->hasValue_) { + return launder(reinterpret_cast(this->value_)); + } + return nullptr; + } + + template + void construct(Args&&... args) { + new (raw_pointer()) Value(std::forward(args)...); + this->hasValue_ = true; + } + + private: + void* raw_pointer() { + return static_cast(this->value_); + } + }; + + Storage storage_; +}; + +template const T* get_pointer(const Optional& opt) { - return opt ? &opt.value() : nullptr; + return opt.get_pointer(); } -template +template T* get_pointer(Optional& opt) { - return opt ? &opt.value() : nullptr; + return opt.get_pointer(); } -template +template void swap(Optional& a, Optional& b) { if (a.hasValue() && b.hasValue()) { // both full @@ -280,12 +373,107 @@ void swap(Optional& a, Optional& b) { } } -template::type>> +template ::type>> Opt make_optional(T&& v) { return Opt(std::forward(v)); } +/////////////////////////////////////////////////////////////////////////////// +// Comparisons. + +template +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 U& a, const Optional& b) { + return b.hasValue() && b.value() == a; +} + +template +bool operator!=(const U& a, const Optional& b) { + return !(a == b); +} + +template +bool operator==(const Optional& a, const Optional& b) { + if (a.hasValue() != b.hasValue()) { + return false; + } + if (a.hasValue()) { + return a.value() == b.value(); + } + return true; +} + +template +bool operator!=(const Optional& a, const Optional& b) { + return !(a == b); +} + +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; +} + +template +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); +} + +// Suppress 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 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_ +// Allow usage of Optional in std::unordered_map and std::unordered_set +FOLLY_NAMESPACE_STD_BEGIN +template +struct hash> { + size_t operator()(folly::Optional const& obj) const { + if (!obj.hasValue()) { + return 0; + } + return hash::type>()(*obj); + } +}; +FOLLY_NAMESPACE_STD_END