From e96d3fa3811ace6e8d59fe2cbd489f6f4dba0cea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Wed, 10 Oct 2018 22:14:58 +0300 Subject: [build] use chromium's optional implementation --- cmake/core.cmake | 7 +- cmake/loop-darwin.cmake | 4 + cmake/loop-uv.cmake | 1 + src/mbgl/gl/attribute.hpp | 4 + vendor/optional/LICENSE | 23 + vendor/optional/files.txt | 2 + vendor/optional/include/chromium/base/optional.hpp | 989 +++++++++++++++++++++ vendor/optional/include/experimental/optional | 20 + 8 files changed, 1049 insertions(+), 1 deletion(-) create mode 100644 vendor/optional/LICENSE create mode 100644 vendor/optional/files.txt create mode 100644 vendor/optional/include/chromium/base/optional.hpp create mode 100644 vendor/optional/include/experimental/optional diff --git a/cmake/core.cmake b/cmake/core.cmake index 1b29b4fb08..ac92b4f22e 100644 --- a/cmake/core.cmake +++ b/cmake/core.cmake @@ -1,3 +1,5 @@ +add_vendor_target(optional INTERFACE) + # Modify cmake/core-files.txt to change the source files for this target. load_sources_list(MBGL_CORE_FILES cmake/core-files.txt) add_library(mbgl-core STATIC ${MBGL_CORE_FILES}) @@ -7,7 +9,10 @@ target_include_directories(mbgl-core PRIVATE src ) -target_link_libraries(mbgl-core PRIVATE codecvt) +target_link_libraries(mbgl-core + PUBLIC optional + PRIVATE codecvt +) target_add_mason_package(mbgl-core PUBLIC geometry) target_add_mason_package(mbgl-core PUBLIC variant) diff --git a/cmake/loop-darwin.cmake b/cmake/loop-darwin.cmake index ef79eef01b..9bb8565060 100644 --- a/cmake/loop-darwin.cmake +++ b/cmake/loop-darwin.cmake @@ -7,6 +7,10 @@ target_include_directories(mbgl-loop-darwin PRIVATE src ) +target_link_libraries(mbgl-loop-darwin + PUBLIC optional +) + create_source_groups(mbgl-loop-darwin) set_target_properties(mbgl-loop-darwin PROPERTIES FOLDER "Core") diff --git a/cmake/loop-uv.cmake b/cmake/loop-uv.cmake index e1d3166b63..da502e799d 100644 --- a/cmake/loop-uv.cmake +++ b/cmake/loop-uv.cmake @@ -7,6 +7,7 @@ target_sources(mbgl-loop-uv INTERFACE ) target_include_directories(mbgl-loop-uv INTERFACE + optional ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/src ) diff --git a/src/mbgl/gl/attribute.hpp b/src/mbgl/gl/attribute.hpp index 8cdb03bfe7..0ec462b071 100644 --- a/src/mbgl/gl/attribute.hpp +++ b/src/mbgl/gl/attribute.hpp @@ -41,6 +41,10 @@ public: return std::tie(lhs.attributeType, lhs.attributeSize, lhs.attributeOffset, lhs.vertexBuffer, lhs.vertexSize, lhs.vertexOffset) == std::tie(rhs.attributeType, rhs.attributeSize, rhs.attributeOffset, rhs.vertexBuffer, rhs.vertexSize, rhs.vertexOffset); } + + friend bool operator!=(const AttributeBinding& lhs, const AttributeBinding& rhs) { + return !operator==(lhs, rhs); + } }; using AttributeBindingArray = std::vector>; diff --git a/vendor/optional/LICENSE b/vendor/optional/LICENSE new file mode 100644 index 0000000000..36b7cd93cd --- /dev/null +++ b/vendor/optional/LICENSE @@ -0,0 +1,23 @@ +Boost Software License - Version 1.0 - August 17th, 2003 + +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/vendor/optional/files.txt b/vendor/optional/files.txt new file mode 100644 index 0000000000..0ab614b04b --- /dev/null +++ b/vendor/optional/files.txt @@ -0,0 +1,2 @@ +include/experimental/optional +include/chromium/base/optional.hpp diff --git a/vendor/optional/include/chromium/base/optional.hpp b/vendor/optional/include/chromium/base/optional.hpp new file mode 100644 index 0000000000..7ad59e1b0d --- /dev/null +++ b/vendor/optional/include/chromium/base/optional.hpp @@ -0,0 +1,989 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_OPTIONAL_H_ +#define BASE_OPTIONAL_H_ + +#include +#include + +// replacement for #include "base/logging.h" +#include +#include +#define CHECK(condition) !(condition) ? abort() : (void)0 +#define DCHECK(condition) assert(condition) +// end replacement + +// replacement for #include +namespace base { + +#ifndef __has_feature +#define __has_feature(x) 0 +#endif + +#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ < 5 +// Old version of GCC don't have __is_trivially_constructible, so we're using a minimal polyfill +// with GCC 4.x's based on __has_trivial_constructor intrinsic and a few known good arguments. +template +struct is_trivially_constructible : std::false_type {}; +template struct is_trivially_constructible : std::integral_constant {}; +template struct is_trivially_constructible : std::integral_constant::value> {}; +template struct is_trivially_constructible : std::integral_constant::value> {}; +template struct is_trivially_constructible : std::integral_constant::value> {}; +#elif __has_feature(is_trivially_constructible) +// We're using the newer __is_trivially_constructible intrinsic +template +struct is_trivially_constructible : std::integral_constant {}; +#else +// Try our luck with the STL's version +template +using is_trivially_constructible = std::is_trivially_constructible; +#endif + +// We're using a polyfill based on our is_trivially_constructible polyfill. +template struct is_trivially_copy_constructible : is_trivially_constructible::type>::type> {}; +template struct is_trivially_move_constructible : is_trivially_constructible::type> {}; + +#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ <= 7 +// Workaround for g++7 and earlier family. +// Due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80654, without this +// Optional> where T is non-copyable causes a compile error. +// As we know it is not trivially copy constructible, explicitly declare so. +} // namespace base +#include +namespace base { +template struct is_trivially_copy_constructible> : std::false_type {}; +#endif + +} // namespace base + +// replacement for #include "base/thread_annotations.h" +#define NO_THREAD_SAFETY_ANALYSIS +// end replacement + +// other fixes +#include +#include +#include + +// GCC 4.9 compatibility +#if !defined(__GNUC__) || __GNUC__ >= 5 +#define OPTIONAL_CONSTEXPR constexpr +#else +#define OPTIONAL_CONSTEXPR +#endif +// end other fixes + + +namespace base { + +// Specification: +// http://en.cppreference.com/w/cpp/utility/optional/in_place_t +struct in_place_t {}; + +// Specification: +// http://en.cppreference.com/w/cpp/utility/optional/nullopt_t +struct nullopt_t { + constexpr explicit nullopt_t(int) {} +}; + +// Specification: +// http://en.cppreference.com/w/cpp/utility/optional/in_place +constexpr in_place_t in_place = {}; + +// Specification: +// http://en.cppreference.com/w/cpp/utility/optional/nullopt +constexpr nullopt_t nullopt(0); + +// Forward declaration, which is refered by following helpers. +template +class Optional; + +namespace internal { + +template ::value> +struct OptionalStorageBase { + // Initializing |empty_| here instead of using default member initializing + // to avoid errors in g++ 4.8. + constexpr OptionalStorageBase() : empty_('\0') {} + + template + constexpr explicit OptionalStorageBase(in_place_t, Args&&... args) + : is_populated_(true), value_(std::forward(args)...) {} + + // When T is not trivially destructible we must call its + // destructor before deallocating its memory. + // Note that this hides the (implicitly declared) move constructor, which + // would be used for constexpr move constructor in OptionalStorage. + // It is needed iff T is trivially move constructible. However, the current + // is_trivially_{copy,move}_constructible implementation requires + // is_trivially_destructible (which looks a bug, cf: + // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51452 and + // http://cplusplus.github.io/LWG/lwg-active.html#2116), so it is not + // necessary for this case at the moment. Please see also the destructor + // comment in "is_trivially_destructible = true" specialization below. + ~OptionalStorageBase() { + if (is_populated_) + value_.~T(); + } + + template + void Init(Args&&... args) { + DCHECK(!is_populated_); + ::new (&value_) T(std::forward(args)...); + is_populated_ = true; + } + + bool is_populated_ = false; + union { + // |empty_| exists so that the union will always be initialized, even when + // it doesn't contain a value. Union members must be initialized for the + // constructor to be 'constexpr'. + char empty_; + T value_; + }; +}; + +template +struct OptionalStorageBase { + // Initializing |empty_| here instead of using default member initializing + // to avoid errors in g++ 4.8. + constexpr OptionalStorageBase() : empty_('\0') {} + + template + constexpr explicit OptionalStorageBase(in_place_t, Args&&... args) + : is_populated_(true), value_(std::forward(args)...) {} + + // When T is trivially destructible (i.e. its destructor does nothing) there + // is no need to call it. Implicitly defined destructor is trivial, because + // both members (bool and union containing only variants which are trivially + // destructible) are trivially destructible. + // Explicitly-defaulted destructor is also trivial, but do not use it here, + // because it hides the implicit move constructor. It is needed to implement + // constexpr move constructor in OptionalStorage iff T is trivially move + // constructible. Note that, if T is trivially move constructible, the move + // constructor of OptionalStorageBase is also implicitly defined and it is + // trivially move constructor. If T is not trivially move constructible, + // "not declaring move constructor without destructor declaration" here means + // "delete move constructor", which works because any move constructor of + // OptionalStorage will not refer to it in that case. + + template + void Init(Args&&... args) { + DCHECK(!is_populated_); + ::new (&value_) T(std::forward(args)...); + is_populated_ = true; + } + + bool is_populated_ = false; + union { + // |empty_| exists so that the union will always be initialized, even when + // it doesn't contain a value. Union members must be initialized for the + // constructor to be 'constexpr'. + char empty_; + T value_; + }; +}; + +// Implement conditional constexpr copy and move constructors. These are +// constexpr if is_trivially_{copy,move}_constructible::value is true +// respectively. If each is true, the corresponding constructor is defined as +// "= default;", which generates a constexpr constructor (In this case, +// the condition of constexpr-ness is satisfied because the base class also has +// compiler generated constexpr {copy,move} constructors). Note that +// placement-new is prohibited in constexpr. +template ::value, + bool = is_trivially_move_constructible::value> +struct OptionalStorage : OptionalStorageBase { + // This is no trivially {copy,move} constructible case. Other cases are + // defined below as specializations. + + // Accessing the members of template base class requires explicit + // declaration. + using OptionalStorageBase::is_populated_; + using OptionalStorageBase::value_; + using OptionalStorageBase::Init; + + // Inherit constructors (specifically, the in_place constructor). + using OptionalStorageBase::OptionalStorageBase; + + // User defined constructor deletes the default constructor. + // Define it explicitly. + OptionalStorage() = default; + + OptionalStorage(const OptionalStorage& other) { + if (other.is_populated_) + Init(other.value_); + } + + OptionalStorage(OptionalStorage&& other) noexcept( + std::is_nothrow_move_constructible::value) { + if (other.is_populated_) + Init(std::move(other.value_)); + } +}; + +template +struct OptionalStorage + : OptionalStorageBase { + using OptionalStorageBase::is_populated_; + using OptionalStorageBase::value_; + using OptionalStorageBase::Init; + using OptionalStorageBase::OptionalStorageBase; + + OptionalStorage() = default; + OptionalStorage(const OptionalStorage& other) = default; + + OptionalStorage(OptionalStorage&& other) noexcept( + std::is_nothrow_move_constructible::value) { + if (other.is_populated_) + Init(std::move(other.value_)); + } +}; + +template +struct OptionalStorage + : OptionalStorageBase { + using OptionalStorageBase::is_populated_; + using OptionalStorageBase::value_; + using OptionalStorageBase::Init; + using OptionalStorageBase::OptionalStorageBase; + + OptionalStorage() = default; + OptionalStorage(OptionalStorage&& other) = default; + + OptionalStorage(const OptionalStorage& other) { + if (other.is_populated_) + Init(other.value_); + } +}; + +template +struct OptionalStorage + : OptionalStorageBase { + // If both trivially {copy,move} constructible are true, it is not necessary + // to use user-defined constructors. So, just inheriting constructors + // from the base class works. + using OptionalStorageBase::OptionalStorageBase; +}; + +// Base class to support conditionally usable copy-/move- constructors +// and assign operators. +template +class OptionalBase { + // This class provides implementation rather than public API, so everything + // should be hidden. Often we use composition, but we cannot in this case + // because of C++ language restriction. + protected: + constexpr OptionalBase() = default; + constexpr OptionalBase(const OptionalBase& other) = default; + constexpr OptionalBase(OptionalBase&& other) = default; + + template + constexpr explicit OptionalBase(in_place_t, Args&&... args) + : storage_(in_place, std::forward(args)...) {} + + // Implementation of converting constructors. + template + explicit OptionalBase(const OptionalBase& other) { + if (other.storage_.is_populated_) + storage_.Init(other.storage_.value_); + } + + template + explicit OptionalBase(OptionalBase&& other) { + if (other.storage_.is_populated_) + storage_.Init(std::move(other.storage_.value_)); + } + + ~OptionalBase() = default; + + OptionalBase& operator=(const OptionalBase& other) { + CopyAssign(other); + return *this; + } + + OptionalBase& operator=(OptionalBase&& other) noexcept( + std::is_nothrow_move_assignable::value&& + std::is_nothrow_move_constructible::value) { + MoveAssign(std::move(other)); + return *this; + } + + template + void CopyAssign(const OptionalBase& other) { + if (other.storage_.is_populated_) + InitOrAssign(other.storage_.value_); + else + FreeIfNeeded(); + } + + template + void MoveAssign(OptionalBase&& other) { + if (other.storage_.is_populated_) + InitOrAssign(std::move(other.storage_.value_)); + else + FreeIfNeeded(); + } + + template + void InitOrAssign(U&& value) { + if (storage_.is_populated_) + storage_.value_ = std::forward(value); + else + storage_.Init(std::forward(value)); + } + + // TODO(lukasza): Figure out how to remove the NO_THREAD_SAFETY_ANALYSIS + // annotation below. See https://crbug.com/881875#c1 for details. + void FreeIfNeeded() NO_THREAD_SAFETY_ANALYSIS { + if (!storage_.is_populated_) + return; + storage_.value_.~T(); + storage_.is_populated_ = false; + } + + // For implementing conversion, allow access to other typed OptionalBase + // class. + template + friend class OptionalBase; + + OptionalStorage storage_; +}; + +// The following {Copy,Move}{Constructible,Assignable} structs are helpers to +// implement constructor/assign-operator overloading. Specifically, if T is +// is not movable but copyable, Optional's move constructor should not +// participate in overload resolution. This inheritance trick implements that. +template +struct CopyConstructible {}; + +template <> +struct CopyConstructible { + constexpr CopyConstructible() = default; + constexpr CopyConstructible(const CopyConstructible&) = delete; + constexpr CopyConstructible(CopyConstructible&&) = default; + CopyConstructible& operator=(const CopyConstructible&) = default; + CopyConstructible& operator=(CopyConstructible&&) = default; +}; + +template +struct MoveConstructible {}; + +template <> +struct MoveConstructible { + constexpr MoveConstructible() = default; + constexpr MoveConstructible(const MoveConstructible&) = default; + constexpr MoveConstructible(MoveConstructible&&) = delete; + MoveConstructible& operator=(const MoveConstructible&) = default; + MoveConstructible& operator=(MoveConstructible&&) = default; +}; + +template +struct CopyAssignable {}; + +template <> +struct CopyAssignable { + constexpr CopyAssignable() = default; + constexpr CopyAssignable(const CopyAssignable&) = default; + constexpr CopyAssignable(CopyAssignable&&) = default; + CopyAssignable& operator=(const CopyAssignable&) = delete; + CopyAssignable& operator=(CopyAssignable&&) = default; +}; + +template +struct MoveAssignable {}; + +template <> +struct MoveAssignable { + constexpr MoveAssignable() = default; + constexpr MoveAssignable(const MoveAssignable&) = default; + constexpr MoveAssignable(MoveAssignable&&) = default; + MoveAssignable& operator=(const MoveAssignable&) = default; + MoveAssignable& operator=(MoveAssignable&&) = delete; +}; + +// Helper to conditionally enable converting constructors and assign operators. +template +struct IsConvertibleFromOptional + : std::integral_constant< + bool, + std::is_constructible&>::value || + std::is_constructible&>::value || + std::is_constructible&&>::value || + std::is_constructible&&>::value || + std::is_convertible&, T>::value || + std::is_convertible&, T>::value || + std::is_convertible&&, T>::value || + std::is_convertible&&, T>::value> {}; + +template +struct IsAssignableFromOptional + : std::integral_constant< + bool, + IsConvertibleFromOptional::value || + std::is_assignable&>::value || + std::is_assignable&>::value || + std::is_assignable&&>::value || + std::is_assignable&&>::value> {}; + +// Forward compatibility for C++17. +// Introduce one more deeper nested namespace to avoid leaking using std::swap. +namespace swappable_impl { +using std::swap; + +struct IsSwappableImpl { + // Tests if swap can be called. Check(0) returns true_type iff swap + // is available for T. Otherwise, Check's overload resolution falls back + // to Check(...) declared below thanks to SFINAE, so returns false_type. + template + static auto Check(int) + -> decltype(swap(std::declval(), std::declval()), std::true_type()); + + template + static std::false_type Check(...); +}; +} // namespace swappable_impl + +template +struct IsSwappable : decltype(swappable_impl::IsSwappableImpl::Check(0)) {}; + +// Forward compatibility for C++20. +template +using RemoveCvRefT = std::remove_cv_t>; + +} // namespace internal + +// On Windows, by default, empty-base class optimization does not work, +// which means even if the base class is empty struct, it still consumes one +// byte for its body. __declspec(empty_bases) enables the optimization. +// cf) +// https://blogs.msdn.microsoft.com/vcblog/2016/03/30/optimizing-the-layout-of-empty-base-classes-in-vs2015-update-2-3/ +#ifdef OS_WIN +#define OPTIONAL_DECLSPEC_EMPTY_BASES __declspec(empty_bases) +#else +#define OPTIONAL_DECLSPEC_EMPTY_BASES +#endif + +// base::Optional is a Chromium version of the C++17 optional class: +// std::optional documentation: +// http://en.cppreference.com/w/cpp/utility/optional +// Chromium documentation: +// https://chromium.googlesource.com/chromium/src/+/master/docs/optional.md +// +// These are the differences between the specification and the implementation: +// - Constructors do not use 'constexpr' as it is a C++14 extension. +// - 'constexpr' might be missing in some places for reasons specified locally. +// - No exceptions are thrown, because they are banned from Chromium. +// Marked noexcept for only move constructor and move assign operators. +// - All the non-members are in the 'base' namespace instead of 'std'. +// +// Note that T cannot have a constructor T(Optional) etc. Optional checks +// T's constructor (specifically via IsConvertibleFromOptional), and in the +// check whether T can be constructible from Optional, which is recursive +// so it does not work. As of Feb 2018, std::optional C++17 implementation in +// both clang and gcc has same limitation. MSVC SFINAE looks to have different +// behavior, but anyway it reports an error, too. +template +class OPTIONAL_DECLSPEC_EMPTY_BASES Optional + : public internal::OptionalBase, + public internal::CopyConstructible::value>, + public internal::MoveConstructible::value>, + public internal::CopyAssignable::value && + std::is_copy_assignable::value>, + public internal::MoveAssignable::value && + std::is_move_assignable::value> { + public: +#undef OPTIONAL_DECLSPEC_EMPTY_BASES + using value_type = T; + + // Defer default/copy/move constructor implementation to OptionalBase. + constexpr Optional() = default; + constexpr Optional(const Optional& other) = default; + constexpr Optional(Optional&& other) noexcept( + std::is_nothrow_move_constructible::value) = default; + + constexpr Optional(nullopt_t) {} // NOLINT(runtime/explicit) + + // Converting copy constructor. "explicit" only if + // std::is_convertible::value is false. It is implemented by + // declaring two almost same constructors, but that condition in enable_if_t + // is different, so that either one is chosen, thanks to SFINAE. + template < + typename U, + std::enable_if_t::value && + !internal::IsConvertibleFromOptional::value && + std::is_convertible::value, + bool> = false> + Optional(const Optional& other) : internal::OptionalBase(other) {} + + template < + typename U, + std::enable_if_t::value && + !internal::IsConvertibleFromOptional::value && + !std::is_convertible::value, + bool> = false> + explicit Optional(const Optional& other) + : internal::OptionalBase(other) {} + + // Converting move constructor. Similar to converting copy constructor, + // declaring two (explicit and non-explicit) constructors. + template < + typename U, + std::enable_if_t::value && + !internal::IsConvertibleFromOptional::value && + std::is_convertible::value, + bool> = false> + Optional(Optional&& other) : internal::OptionalBase(std::move(other)) {} + + template < + typename U, + std::enable_if_t::value && + !internal::IsConvertibleFromOptional::value && + !std::is_convertible::value, + bool> = false> + explicit Optional(Optional&& other) + : internal::OptionalBase(std::move(other)) {} + + template + constexpr explicit Optional(in_place_t, Args&&... args) + : internal::OptionalBase(in_place, std::forward(args)...) {} + + template < + class U, + class... Args, + class = std::enable_if_t&, + Args...>::value>> + constexpr explicit Optional(in_place_t, + std::initializer_list il, + Args&&... args) + : internal::OptionalBase(in_place, il, std::forward(args)...) {} + + // Forward value constructor. Similar to converting constructors, + // conditionally explicit. + template < + typename U = value_type, + std::enable_if_t< + std::is_constructible::value && + !std::is_same, in_place_t>::value && + !std::is_same, Optional>::value && + std::is_convertible::value, + bool> = false> + constexpr Optional(U&& value) + : internal::OptionalBase(in_place, std::forward(value)) {} + + template < + typename U = value_type, + std::enable_if_t< + std::is_constructible::value && + !std::is_same, in_place_t>::value && + !std::is_same, Optional>::value && + !std::is_convertible::value, + bool> = false> + constexpr explicit Optional(U&& value) + : internal::OptionalBase(in_place, std::forward(value)) {} + + ~Optional() = default; + + // Defer copy-/move- assign operator implementation to OptionalBase. + Optional& operator=(const Optional& other) = default; + Optional& operator=(Optional&& other) noexcept( + std::is_nothrow_move_assignable::value&& + std::is_nothrow_move_constructible::value) = default; + + Optional& operator=(nullopt_t) { + FreeIfNeeded(); + return *this; + } + + // Perfect-forwarded assignment. + template + std::enable_if_t< + !std::is_same, Optional>::value && + std::is_constructible::value && + std::is_assignable::value && + (!std::is_scalar::value || + !std::is_same, T>::value), + Optional&> + operator=(U&& value) { + InitOrAssign(std::forward(value)); + return *this; + } + + // Copy assign the state of other. + template + std::enable_if_t::value && + std::is_constructible::value && + std::is_assignable::value, + Optional&> + operator=(const Optional& other) { + CopyAssign(other); + return *this; + } + + // Move assign the state of other. + template + std::enable_if_t::value && + std::is_constructible::value && + std::is_assignable::value, + Optional&> + operator=(Optional&& other) { + MoveAssign(std::move(other)); + return *this; + } + + OPTIONAL_CONSTEXPR const T* operator->() const { + DCHECK(storage_.is_populated_); + return &storage_.value_; + } + + OPTIONAL_CONSTEXPR T* operator->() { + DCHECK(storage_.is_populated_); + return &storage_.value_; + } + + OPTIONAL_CONSTEXPR const T& operator*() const & { + DCHECK(storage_.is_populated_); + return storage_.value_; + } + + OPTIONAL_CONSTEXPR T& operator*() & { + DCHECK(storage_.is_populated_); + return storage_.value_; + } + + OPTIONAL_CONSTEXPR const T&& operator*() const && { + DCHECK(storage_.is_populated_); + return std::move(storage_.value_); + } + + OPTIONAL_CONSTEXPR T&& operator*() && { + DCHECK(storage_.is_populated_); + return std::move(storage_.value_); + } + + constexpr explicit operator bool() const { return storage_.is_populated_; } + + constexpr bool has_value() const { return storage_.is_populated_; } + + OPTIONAL_CONSTEXPR T& value() & { + CHECK(storage_.is_populated_); + return storage_.value_; + } + + OPTIONAL_CONSTEXPR const T& value() const & { + CHECK(storage_.is_populated_); + return storage_.value_; + } + + OPTIONAL_CONSTEXPR T&& value() && { + CHECK(storage_.is_populated_); + return std::move(storage_.value_); + } + + OPTIONAL_CONSTEXPR const T&& value() const && { + CHECK(storage_.is_populated_); + return std::move(storage_.value_); + } + + template + constexpr T value_or(U&& default_value) const& { + // TODO(mlamouri): add the following assert when possible: + // static_assert(std::is_copy_constructible::value, + // "T must be copy constructible"); + static_assert(std::is_convertible::value, + "U must be convertible to T"); + return storage_.is_populated_ + ? storage_.value_ + : static_cast(std::forward(default_value)); + } + + template + constexpr T value_or(U&& default_value) && { + // TODO(mlamouri): add the following assert when possible: + // static_assert(std::is_move_constructible::value, + // "T must be move constructible"); + static_assert(std::is_convertible::value, + "U must be convertible to T"); + return storage_.is_populated_ + ? std::move(storage_.value_) + : static_cast(std::forward(default_value)); + } + + void swap(Optional& other) { + if (!storage_.is_populated_ && !other.storage_.is_populated_) + return; + + if (storage_.is_populated_ != other.storage_.is_populated_) { + if (storage_.is_populated_) { + other.storage_.Init(std::move(storage_.value_)); + FreeIfNeeded(); + } else { + storage_.Init(std::move(other.storage_.value_)); + other.FreeIfNeeded(); + } + return; + } + + DCHECK(storage_.is_populated_ && other.storage_.is_populated_); + using std::swap; + swap(**this, *other); + } + + void reset() { FreeIfNeeded(); } + + template + T& emplace(Args&&... args) { + FreeIfNeeded(); + storage_.Init(std::forward(args)...); + return storage_.value_; + } + + template + std::enable_if_t< + std::is_constructible&, Args&&...>::value, + T&> + emplace(std::initializer_list il, Args&&... args) { + FreeIfNeeded(); + storage_.Init(il, std::forward(args)...); + return storage_.value_; + } + + private: + // Accessing template base class's protected member needs explicit + // declaration to do so. + using internal::OptionalBase::CopyAssign; + using internal::OptionalBase::FreeIfNeeded; + using internal::OptionalBase::InitOrAssign; + using internal::OptionalBase::MoveAssign; + using internal::OptionalBase::storage_; +}; + +// Here after defines comparation operators. The definition follows +// http://en.cppreference.com/w/cpp/utility/optional/operator_cmp +// while bool() casting is replaced by has_value() to meet the chromium +// style guide. +template +constexpr bool operator==(const Optional& lhs, const Optional& rhs) { + if (lhs.has_value() != rhs.has_value()) + return false; + if (!lhs.has_value()) + return true; + return *lhs == *rhs; +} + +template +constexpr bool operator!=(const Optional& lhs, const Optional& rhs) { + if (lhs.has_value() != rhs.has_value()) + return true; + if (!lhs.has_value()) + return false; + return *lhs != *rhs; +} + +template +constexpr bool operator<(const Optional& lhs, const Optional& rhs) { + if (!rhs.has_value()) + return false; + if (!lhs.has_value()) + return true; + return *lhs < *rhs; +} + +template +constexpr bool operator<=(const Optional& lhs, const Optional& rhs) { + if (!lhs.has_value()) + return true; + if (!rhs.has_value()) + return false; + return *lhs <= *rhs; +} + +template +constexpr bool operator>(const Optional& lhs, const Optional& rhs) { + if (!lhs.has_value()) + return false; + if (!rhs.has_value()) + return true; + return *lhs > *rhs; +} + +template +constexpr bool operator>=(const Optional& lhs, const Optional& rhs) { + if (!rhs.has_value()) + return true; + if (!lhs.has_value()) + return false; + return *lhs >= *rhs; +} + +template +constexpr bool operator==(const Optional& opt, nullopt_t) { + return !opt; +} + +template +constexpr bool operator==(nullopt_t, const Optional& opt) { + return !opt; +} + +template +constexpr bool operator!=(const Optional& opt, nullopt_t) { + return opt.has_value(); +} + +template +constexpr bool operator!=(nullopt_t, const Optional& opt) { + return opt.has_value(); +} + +template +constexpr bool operator<(const Optional& opt, nullopt_t) { + return false; +} + +template +constexpr bool operator<(nullopt_t, const Optional& opt) { + return opt.has_value(); +} + +template +constexpr bool operator<=(const Optional& opt, nullopt_t) { + return !opt; +} + +template +constexpr bool operator<=(nullopt_t, const Optional& opt) { + return true; +} + +template +constexpr bool operator>(const Optional& opt, nullopt_t) { + return opt.has_value(); +} + +template +constexpr bool operator>(nullopt_t, const Optional& opt) { + return false; +} + +template +constexpr bool operator>=(const Optional& opt, nullopt_t) { + return true; +} + +template +constexpr bool operator>=(nullopt_t, const Optional& opt) { + return !opt; +} + +template +constexpr bool operator==(const Optional& opt, const U& value) { + return opt.has_value() ? *opt == value : false; +} + +template +constexpr bool operator==(const U& value, const Optional& opt) { + return opt.has_value() ? value == *opt : false; +} + +template +constexpr bool operator!=(const Optional& opt, const U& value) { + return opt.has_value() ? *opt != value : true; +} + +template +constexpr bool operator!=(const U& value, const Optional& opt) { + return opt.has_value() ? value != *opt : true; +} + +template +constexpr bool operator<(const Optional& opt, const U& value) { + return opt.has_value() ? *opt < value : true; +} + +template +constexpr bool operator<(const U& value, const Optional& opt) { + return opt.has_value() ? value < *opt : false; +} + +template +constexpr bool operator<=(const Optional& opt, const U& value) { + return opt.has_value() ? *opt <= value : true; +} + +template +constexpr bool operator<=(const U& value, const Optional& opt) { + return opt.has_value() ? value <= *opt : false; +} + +template +constexpr bool operator>(const Optional& opt, const U& value) { + return opt.has_value() ? *opt > value : false; +} + +template +constexpr bool operator>(const U& value, const Optional& opt) { + return opt.has_value() ? value > *opt : true; +} + +template +constexpr bool operator>=(const Optional& opt, const U& value) { + return opt.has_value() ? *opt >= value : false; +} + +template +constexpr bool operator>=(const U& value, const Optional& opt) { + return opt.has_value() ? value >= *opt : true; +} + +template +constexpr Optional> make_optional(T&& value) { + return Optional>(std::forward(value)); +} + +template +constexpr Optional make_optional(Args&&... args) { + return Optional(in_place, std::forward(args)...); +} + +template +constexpr Optional make_optional(std::initializer_list il, + Args&&... args) { + return Optional(in_place, il, std::forward(args)...); +} + +// Partial specialization for a function template is not allowed. Also, it is +// not allowed to add overload function to std namespace, while it is allowed +// to specialize the template in std. Thus, swap() (kind of) overloading is +// defined in base namespace, instead. +template +std::enable_if_t::value && + internal::IsSwappable::value> +swap(Optional& lhs, Optional& rhs) { + lhs.swap(rhs); +} + +} // namespace base + +namespace std { + +template +struct hash> { + size_t operator()(const base::Optional& opt) const { + return opt == base::nullopt ? 0 : std::hash()(*opt); + } +}; + +} // namespace std + +#endif // BASE_OPTIONAL_H_ diff --git a/vendor/optional/include/experimental/optional b/vendor/optional/include/experimental/optional new file mode 100644 index 0000000000..572cf659c8 --- /dev/null +++ b/vendor/optional/include/experimental/optional @@ -0,0 +1,20 @@ +#pragma once + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +#pragma GCC diagnostic ignored "-Wextra" +#pragma GCC diagnostic ignored "-Wshadow" +#include +#pragma GCC diagnostic pop + +namespace std { +namespace experimental { + +template +using optional = base::Optional; + +using nullopt_t = base::nullopt_t; +constexpr nullopt_t nullopt = base::nullopt; + +} // namespace experimental +} // namespace std -- cgit v1.2.1