From: Phil Willoughby Date: Wed, 26 Jul 2017 12:12:29 +0000 (-0700) Subject: clang-format Optional.h X-Git-Tag: v2017.07.31.00~27 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=215da6781da292b5255cc78e73f962ee89abe169;p=folly.git clang-format Optional.h Reviewed By: yfeldblum Differential Revision: D5497522 fbshipit-source-id: bb208aeb37b5b9ce99619193d4cd2d0f3fb39178 --- diff --git a/folly/Optional.h b/folly/Optional.h index ce8bcf1a..54d9d198 100644 --- a/folly/Optional.h +++ b/folly/Optional.h @@ -84,46 +84,44 @@ class OptionalEmptyException : public std::runtime_error { : std::runtime_error("Empty Optional cannot be unwrapped") {} }; -template +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"); + 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() noexcept { - } - - Optional(const Optional& src) - noexcept(std::is_nothrow_copy_constructible::value) { + Optional() noexcept {} + Optional(const Optional& src) noexcept( + std::is_nothrow_copy_constructible::value) { if (src.hasValue()) { construct(src.value()); } } - Optional(Optional&& src) - noexcept(std::is_nothrow_move_constructible::value) { - + Optional(Optional&& src) noexcept( + std::is_nothrow_move_constructible::value) { if (src.hasValue()) { construct(std::move(src.value())); src.clear(); } } - /* implicit */ Optional(const None&) noexcept { - } + /* implicit */ Optional(const None&) noexcept {} - /* implicit */ Optional(Value&& newValue) - noexcept(std::is_nothrow_move_constructible::value) { + /* implicit */ Optional(Value&& newValue) noexcept( + std::is_nothrow_move_constructible::value) { construct(std::move(newValue)); } - /* implicit */ Optional(const Value& newValue) - noexcept(std::is_nothrow_copy_constructible::value) { + /* implicit */ Optional(const Value& newValue) noexcept( + std::is_nothrow_copy_constructible::value) { construct(newValue); } @@ -172,27 +170,25 @@ class Optional { } } - template + template Optional& operator=(Arg&& arg) { assign(std::forward(arg)); return *this; } - Optional& operator=(Optional &&other) - noexcept (std::is_nothrow_move_assignable::value) { - + Optional& operator=(Optional&& other) noexcept( + std::is_nothrow_move_assignable::value) { assign(std::move(other)); return *this; } - Optional& operator=(const Optional &other) - noexcept (std::is_nothrow_copy_assignable::value) { - + Optional& operator=(const Optional& other) noexcept( + std::is_nothrow_copy_assignable::value) { assign(other); return *this; } - template + template void emplace(Args&&... args) { clear(); construct(std::forward(args)...); @@ -202,7 +198,7 @@ class Optional { storage_.clear(); } - const Value& value() const& { + const Value& value() const & { require_value(); return storage_.value; } @@ -217,12 +213,12 @@ class Optional { return std::move(storage_.value); } - const Value&& value() const&& { + const Value&& value() const && { require_value(); return std::move(storage_.value); } - const Value* get_pointer() const& { + const Value* get_pointer() const & { return storage_.hasValue ? &storage_.value : nullptr; } Value* get_pointer() & { @@ -230,23 +226,37 @@ class Optional { } Value* get_pointer() && = delete; - bool hasValue() const { return storage_.hasValue; } + bool hasValue() const { + return storage_.hasValue; + } explicit operator bool() const { return hasValue(); } - 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 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& { + Value value_or(U&& dflt) const & { if (storage_.hasValue) { return storage_.value; } @@ -270,11 +280,11 @@ class Optional { } } - template + template void construct(Args&&... args) { const void* ptr = &storage_.value; // for supporting const types - new(const_cast(ptr)) Value(std::forward(args)...); + new (const_cast(ptr)) Value(std::forward(args)...); storage_.hasValue = true; } @@ -328,25 +338,25 @@ class Optional { } }; - using Storage = - typename std::conditional::value, - StorageTriviallyDestructible, - StorageNonTriviallyDestructible>::type; + using Storage = typename std::conditional< + std::is_trivially_destructible::value, + StorageTriviallyDestructible, + StorageNonTriviallyDestructible>::type; Storage storage_; }; -template +template const T* get_pointer(const Optional& opt) { return opt.get_pointer(); } -template +template T* get_pointer(Optional& opt) { return opt.get_pointer(); } -template +template void swap(Optional& a, Optional& b) { if (a.hasValue() && b.hasValue()) { // both full @@ -357,8 +367,7 @@ void swap(Optional& a, Optional& b) { } } -template::type>> +template ::type>> Opt make_optional(T&& v) { return Opt(std::forward(v)); } @@ -388,8 +397,12 @@ bool operator!=(const U& a, const Optional& 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(); } + if (a.hasValue() != b.hasValue()) { + return false; + } + if (a.hasValue()) { + return a.value() == b.value(); + } return true; } @@ -400,8 +413,12 @@ bool operator!=(const Optional& a, const Optional& 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(); } + if (a.hasValue() != b.hasValue()) { + return a.hasValue() < b.hasValue(); + } + if (a.hasValue()) { + return a.value() < b.value(); + } return false; } @@ -421,14 +438,22 @@ bool operator>=(const Optional& a, const Optional& 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; +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; ///////////////////////////////////////////////////////////////////////////////