athena/include/optional.hpp

982 lines
27 KiB
C++
Raw Normal View History

2016-03-26 02:22:04 +00:00
// Copyright (C) 2011 - 2012 Andrzej Krzemienski.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// The idea and interface is based on Boost.Optional library
// authored by Fernando Luis Cacciola Carballal
2018-12-08 05:18:17 +00:00
#ifndef ___OPTIONAL_HPP___
#define ___OPTIONAL_HPP___
#include <utility>
#include <type_traits>
#include <initializer_list>
#include <cassert>
#include <functional>
#include <string>
#include "athena/Global.hpp"
#define TR2_OPTIONAL_REQUIRES(...) typename enable_if<__VA_ARGS__::value, bool>::type = false
#if defined __GNUC__ // NOTE: GNUC is also defined for Clang
#if (__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)
#define TR2_OPTIONAL_GCC_4_8_AND_HIGHER___
#elif (__GNUC__ > 4)
#define TR2_OPTIONAL_GCC_4_8_AND_HIGHER___
#endif
2016-03-26 02:22:04 +00:00
#
2018-12-08 05:18:17 +00:00
#if (__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)
#define TR2_OPTIONAL_GCC_4_7_AND_HIGHER___
#elif (__GNUC__ > 4)
#define TR2_OPTIONAL_GCC_4_7_AND_HIGHER___
#endif
2016-03-26 02:22:04 +00:00
#
2018-12-08 05:18:17 +00:00
#if (__GNUC__ == 4) && (__GNUC_MINOR__ == 8) && (__GNUC_PATCHLEVEL__ >= 1)
#define TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___
#elif (__GNUC__ == 4) && (__GNUC_MINOR__ >= 9)
#define TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___
#elif (__GNUC__ > 4)
#define TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___
#endif
#endif
2016-03-26 02:22:04 +00:00
#
2018-12-08 05:18:17 +00:00
#if defined __clang_major__
#if (__clang_major__ == 3 && __clang_minor__ >= 5)
#define TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_
#elif (__clang_major__ > 3)
#define TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_
#endif
#if defined TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_
#define TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_
#elif (__clang_major__ == 3 && __clang_minor__ == 4 && __clang_patchlevel__ >= 2)
#define TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_
#endif
#endif
2016-03-26 02:22:04 +00:00
#
2018-12-08 05:18:17 +00:00
#if defined _MSC_VER
#if (_MSC_VER >= 1900)
#define TR2_OPTIONAL_MSVC_2015_AND_HIGHER___
#endif
#endif
#if defined __clang__
#if (__clang_major__ > 2) || (__clang_major__ == 2) && (__clang_minor__ >= 9)
#define OPTIONAL_HAS_THIS_RVALUE_REFS 1
#else
#define OPTIONAL_HAS_THIS_RVALUE_REFS 0
#endif
#elif defined TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___
#define OPTIONAL_HAS_THIS_RVALUE_REFS 1
#elif defined TR2_OPTIONAL_MSVC_2015_AND_HIGHER___
#define OPTIONAL_HAS_THIS_RVALUE_REFS 1
#else
#define OPTIONAL_HAS_THIS_RVALUE_REFS 0
#endif
#if defined TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___
#define OPTIONAL_HAS_CONSTEXPR_INIT_LIST 1
#define OPTIONAL_CONSTEXPR_INIT_LIST constexpr
#else
#define OPTIONAL_HAS_CONSTEXPR_INIT_LIST 0
#define OPTIONAL_CONSTEXPR_INIT_LIST
#endif
#if defined TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_ && (defined __cplusplus) && (__cplusplus != 201103L)
#define OPTIONAL_HAS_MOVE_ACCESSORS 1
#else
#define OPTIONAL_HAS_MOVE_ACCESSORS 0
#endif
#// In C++11 constexpr implies const, so we need to make non-const members also non-constexpr
#if (defined __cplusplus) && (__cplusplus == 201103L)
#define OPTIONAL_MUTABLE_CONSTEXPR
#else
#define OPTIONAL_MUTABLE_CONSTEXPR constexpr
#endif
namespace std {
namespace experimental {
2016-03-26 02:22:04 +00:00
// BEGIN workaround for missing is_trivially_destructible
2018-12-08 05:18:17 +00:00
#if defined TR2_OPTIONAL_GCC_4_8_AND_HIGHER___
// leave it: it is already there
#elif defined TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_
// leave it: it is already there
#elif defined TR2_OPTIONAL_MSVC_2015_AND_HIGHER___
// leave it: it is already there
#elif defined TR2_OPTIONAL_DISABLE_EMULATION_OF_TYPE_TRAITS
// leave it: the user doesn't want it
#else
template <typename T>
using is_trivially_destructible = std::has_trivial_destructor<T>;
#endif
2016-03-26 02:22:04 +00:00
// END workaround for missing is_trivially_destructible
2018-12-08 05:18:17 +00:00
#if (defined TR2_OPTIONAL_GCC_4_7_AND_HIGHER___)
// leave it; our metafunctions are already defined.
#elif defined TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_
// leave it; our metafunctions are already defined.
#elif defined TR2_OPTIONAL_MSVC_2015_AND_HIGHER___
// leave it: it is already there
#elif defined TR2_OPTIONAL_DISABLE_EMULATION_OF_TYPE_TRAITS
// leave it: the user doesn't want it
#else
2016-03-26 02:22:04 +00:00
// workaround for missing traits in GCC and CLANG
template <class T>
2018-12-08 05:18:17 +00:00
struct is_nothrow_move_constructible {
2016-03-26 02:22:04 +00:00
constexpr static bool value = std::is_nothrow_constructible<T, T&&>::value;
};
template <class T, class U>
2018-12-08 05:18:17 +00:00
struct is_assignable {
2016-03-26 02:22:04 +00:00
template <class X, class Y>
2018-12-08 05:18:17 +00:00
constexpr static bool has_assign(...) {
return false;
}
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
template <class X, class Y, size_t S = sizeof((std::declval<X>() = std::declval<Y>(), true))>
2016-03-26 02:22:04 +00:00
// the comma operator is necessary for the cases where operator= returns void
2018-12-08 05:18:17 +00:00
constexpr static bool has_assign(bool) {
return true;
}
2016-03-26 02:22:04 +00:00
constexpr static bool value = has_assign<T, U>(true);
};
template <class T>
2018-12-08 05:18:17 +00:00
struct is_nothrow_move_assignable {
2016-03-26 02:22:04 +00:00
template <class X, bool has_any_move_assign>
struct has_nothrow_move_assign {
constexpr static bool value = false;
};
template <class X>
struct has_nothrow_move_assign<X, true> {
2018-12-08 05:18:17 +00:00
constexpr static bool value = noexcept(std::declval<X&>() = std::declval<X&&>());
2016-03-26 02:22:04 +00:00
};
constexpr static bool value = has_nothrow_move_assign<T, is_assignable<T&, T&&>::value>::value;
};
// end workaround
2018-12-08 05:18:17 +00:00
#endif
2016-03-26 02:22:04 +00:00
// 20.5.4, optional for object types
2018-12-08 05:18:17 +00:00
template <class T>
class optional;
2016-03-26 02:22:04 +00:00
// 20.5.5, optional for lvalue reference types
2018-12-08 05:18:17 +00:00
template <class T>
class optional<T&>;
2016-03-26 02:22:04 +00:00
// workaround: std utility functions aren't constexpr yet
2018-12-08 05:18:17 +00:00
template <class T>
inline constexpr T&& constexpr_forward(typename std::remove_reference<T>::type& t) noexcept {
2016-03-26 02:22:04 +00:00
return static_cast<T&&>(t);
}
2018-12-08 05:18:17 +00:00
template <class T>
inline constexpr T&& constexpr_forward(typename std::remove_reference<T>::type&& t) noexcept {
static_assert(!std::is_lvalue_reference<T>::value, "!!");
return static_cast<T&&>(t);
2016-03-26 02:22:04 +00:00
}
2018-12-08 05:18:17 +00:00
template <class T>
inline constexpr typename std::remove_reference<T>::type&& constexpr_move(T&& t) noexcept {
return static_cast<typename std::remove_reference<T>::type&&>(t);
2016-03-26 02:22:04 +00:00
}
#if defined NDEBUG
2018-12-08 05:18:17 +00:00
#define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR) (EXPR)
2016-03-26 02:22:04 +00:00
#else
2018-12-08 05:18:17 +00:00
#define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR) ((CHECK) ? (EXPR) : ([] { assert(!#CHECK); }(), (EXPR)))
2016-03-26 02:22:04 +00:00
#endif
2018-12-08 05:18:17 +00:00
namespace detail_ {
2016-03-26 02:22:04 +00:00
// static_addressof: a constexpr version of addressof
template <typename T>
2018-12-08 05:18:17 +00:00
struct has_overloaded_addressof {
2016-03-26 02:22:04 +00:00
template <class X>
2018-12-08 05:18:17 +00:00
constexpr static bool has_overload(...) {
return false;
}
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
template <class X, size_t S = sizeof(std::declval<X&>().operator&())>
constexpr static bool has_overload(bool) {
return true;
}
2016-03-26 02:22:04 +00:00
constexpr static bool value = has_overload<T>(true);
};
template <typename T, TR2_OPTIONAL_REQUIRES(!has_overloaded_addressof<T>)>
2018-12-08 05:18:17 +00:00
constexpr T* static_addressof(T& ref) {
2016-03-26 02:22:04 +00:00
return &ref;
}
template <typename T, TR2_OPTIONAL_REQUIRES(has_overloaded_addressof<T>)>
2018-12-08 05:18:17 +00:00
T* static_addressof(T& ref) {
2016-03-26 02:22:04 +00:00
return std::addressof(ref);
}
// the call to convert<A>(b) has return type A and converts b to type A iff b decltype(b) is implicitly convertible to A
template <class U>
2018-12-08 05:18:17 +00:00
U convert(U v) {
return v;
}
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
} // namespace detail_
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
constexpr struct trivial_init_t {
} trivial_init{};
2016-03-26 02:22:04 +00:00
// 20.5.6, In-place construction
2018-12-08 05:18:17 +00:00
constexpr struct in_place_t {
} in_place{};
2016-03-26 02:22:04 +00:00
// 20.5.7, Disengaged state indicator
2018-12-08 05:18:17 +00:00
struct nullopt_t {
struct init {};
constexpr explicit nullopt_t(init) {}
2016-03-26 02:22:04 +00:00
};
constexpr nullopt_t nullopt{nullopt_t::init()};
// 20.5.8, class bad_optional_access
class bad_optional_access : public logic_error {
public:
explicit bad_optional_access(const string& what_arg) : logic_error{what_arg} {}
explicit bad_optional_access(const char* what_arg) : logic_error{what_arg} {}
};
template <class T>
2018-12-08 05:18:17 +00:00
union storage_t {
2016-03-26 02:22:04 +00:00
unsigned char dummy_;
T value_;
2018-12-08 05:18:17 +00:00
constexpr storage_t(trivial_init_t) noexcept : dummy_(){};
2016-03-26 02:22:04 +00:00
template <class... Args>
2018-12-08 05:18:17 +00:00
constexpr storage_t(Args&&... args) : value_(constexpr_forward<Args>(args)...) {}
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
~storage_t() {}
2016-03-26 02:22:04 +00:00
};
template <class T>
2018-12-08 05:18:17 +00:00
union constexpr_storage_t {
unsigned char dummy_;
T value_;
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
constexpr constexpr_storage_t(trivial_init_t) noexcept : dummy_(){};
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
template <class... Args>
constexpr constexpr_storage_t(Args&&... args) : value_(constexpr_forward<Args>(args)...) {}
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
~constexpr_storage_t() = default;
2016-03-26 02:22:04 +00:00
};
template <class T>
2018-12-08 05:18:17 +00:00
struct optional_base {
bool init_;
storage_t<T> storage_;
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
constexpr optional_base() noexcept : init_(false), storage_(trivial_init){};
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
explicit constexpr optional_base(const T& v) : init_(true), storage_(v) {}
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
explicit constexpr optional_base(T&& v) : init_(true), storage_(constexpr_move(v)) {}
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
template <class... Args>
explicit optional_base(in_place_t, Args&&... args) : init_(true), storage_(constexpr_forward<Args>(args)...) {}
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
template <class U, class... Args, TR2_OPTIONAL_REQUIRES(is_constructible<T, std::initializer_list<U>>)>
explicit optional_base(in_place_t, std::initializer_list<U> il, Args&&... args)
: init_(true), storage_(il, std::forward<Args>(args)...) {}
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
~optional_base() {
if (init_)
storage_.value_.T::~T();
}
2016-03-26 02:22:04 +00:00
};
template <class T>
2018-12-08 05:18:17 +00:00
struct constexpr_optional_base {
bool init_;
constexpr_storage_t<T> storage_;
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
constexpr constexpr_optional_base() noexcept : init_(false), storage_(trivial_init){};
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
explicit constexpr constexpr_optional_base(const T& v) : init_(true), storage_(v) {}
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
explicit constexpr constexpr_optional_base(T&& v) : init_(true), storage_(constexpr_move(v)) {}
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
template <class... Args>
explicit constexpr constexpr_optional_base(in_place_t, Args&&... args)
: init_(true), storage_(constexpr_forward<Args>(args)...) {}
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
template <class U, class... Args, TR2_OPTIONAL_REQUIRES(is_constructible<T, std::initializer_list<U>>)>
OPTIONAL_CONSTEXPR_INIT_LIST explicit constexpr_optional_base(in_place_t, std::initializer_list<U> il, Args&&... args)
: init_(true), storage_(il, std::forward<Args>(args)...) {}
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
~constexpr_optional_base() = default;
2016-03-26 02:22:04 +00:00
};
template <class T>
2018-12-08 05:18:17 +00:00
using OptionalBase =
typename std::conditional<is_trivially_destructible<T>::value, constexpr_optional_base<T>, optional_base<T>>::type;
2016-03-26 02:22:04 +00:00
template <class T>
2018-12-08 05:18:17 +00:00
class optional : private OptionalBase<T> {
static_assert(!std::is_same<typename std::decay<T>::type, nullopt_t>::value, "bad T");
static_assert(!std::is_same<typename std::decay<T>::type, in_place_t>::value, "bad T");
2016-03-26 02:22:04 +00:00
constexpr bool initialized() const noexcept { return OptionalBase<T>::init_; }
2018-12-08 05:18:17 +00:00
T* dataptr() { return std::addressof(OptionalBase<T>::storage_.value_); }
2016-03-26 02:22:04 +00:00
constexpr const T* dataptr() const { return detail_::static_addressof(OptionalBase<T>::storage_.value_); }
2018-12-08 05:18:17 +00:00
#if OPTIONAL_HAS_THIS_RVALUE_REFS == 1
2016-03-26 02:22:04 +00:00
constexpr const T& contained_val() const& { return OptionalBase<T>::storage_.value_; }
2018-12-08 05:18:17 +00:00
#if OPTIONAL_HAS_MOVE_ACCESSORS == 1
2016-03-26 02:22:04 +00:00
OPTIONAL_MUTABLE_CONSTEXPR T&& contained_val() && { return std::move(OptionalBase<T>::storage_.value_); }
OPTIONAL_MUTABLE_CONSTEXPR T& contained_val() & { return OptionalBase<T>::storage_.value_; }
2018-12-08 05:18:17 +00:00
#else
2016-03-26 02:22:04 +00:00
T& contained_val() & { return OptionalBase<T>::storage_.value_; }
T&& contained_val() && { return std::move(OptionalBase<T>::storage_.value_); }
2018-12-08 05:18:17 +00:00
#endif
#else
2016-03-26 02:22:04 +00:00
constexpr const T& contained_val() const { return OptionalBase<T>::storage_.value_; }
T& contained_val() { return OptionalBase<T>::storage_.value_; }
2018-12-08 05:18:17 +00:00
#endif
2016-03-26 02:22:04 +00:00
void clear() noexcept {
2018-12-08 05:18:17 +00:00
if (initialized())
dataptr()->T::~T();
2016-03-26 02:22:04 +00:00
OptionalBase<T>::init_ = false;
}
template <class... Args>
2018-12-08 05:18:17 +00:00
void initialize(Args&&... args) noexcept(noexcept(T(std::forward<Args>(args)...))) {
2016-03-26 02:22:04 +00:00
assert(!OptionalBase<T>::init_);
::new (static_cast<void*>(dataptr())) T(std::forward<Args>(args)...);
OptionalBase<T>::init_ = true;
}
template <class U, class... Args>
2018-12-08 05:18:17 +00:00
void initialize(std::initializer_list<U> il, Args&&... args) noexcept(noexcept(T(il, std::forward<Args>(args)...))) {
2016-03-26 02:22:04 +00:00
assert(!OptionalBase<T>::init_);
::new (static_cast<void*>(dataptr())) T(il, std::forward<Args>(args)...);
OptionalBase<T>::init_ = true;
}
public:
typedef T value_type;
// 20.5.5.1, constructors
2018-12-08 05:18:17 +00:00
constexpr optional() noexcept : OptionalBase<T>(){};
constexpr optional(nullopt_t) noexcept : OptionalBase<T>(){};
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
optional(const optional& rhs) : OptionalBase<T>() {
2016-03-26 02:22:04 +00:00
if (rhs.initialized()) {
2018-12-08 05:18:17 +00:00
::new (static_cast<void*>(dataptr())) T(*rhs);
OptionalBase<T>::init_ = true;
2016-03-26 02:22:04 +00:00
}
}
2018-12-08 05:18:17 +00:00
optional(optional&& rhs) noexcept(is_nothrow_move_constructible<T>::value) : OptionalBase<T>() {
2016-03-26 02:22:04 +00:00
if (rhs.initialized()) {
2018-12-08 05:18:17 +00:00
::new (static_cast<void*>(dataptr())) T(std::move(*rhs));
OptionalBase<T>::init_ = true;
2016-03-26 02:22:04 +00:00
}
}
constexpr optional(const T& v) : OptionalBase<T>(v) {}
constexpr optional(T&& v) : OptionalBase<T>(constexpr_move(v)) {}
template <class... Args>
explicit constexpr optional(in_place_t, Args&&... args)
: OptionalBase<T>(in_place_t{}, constexpr_forward<Args>(args)...) {}
template <class U, class... Args, TR2_OPTIONAL_REQUIRES(is_constructible<T, std::initializer_list<U>>)>
OPTIONAL_CONSTEXPR_INIT_LIST explicit optional(in_place_t, std::initializer_list<U> il, Args&&... args)
: OptionalBase<T>(in_place_t{}, il, constexpr_forward<Args>(args)...) {}
// 20.5.4.2, Destructor
~optional() = default;
// 20.5.4.3, assignment
2018-12-08 05:18:17 +00:00
optional& operator=(nullopt_t) noexcept {
2016-03-26 02:22:04 +00:00
clear();
return *this;
}
2018-12-08 05:18:17 +00:00
optional& operator=(const optional& rhs) {
if (initialized() == true && rhs.initialized() == false)
clear();
else if (initialized() == false && rhs.initialized() == true)
initialize(*rhs);
else if (initialized() == true && rhs.initialized() == true)
contained_val() = *rhs;
2016-03-26 02:22:04 +00:00
return *this;
}
2018-12-08 05:18:17 +00:00
optional&
operator=(optional&& rhs) noexcept(is_nothrow_move_assignable<T>::value&& is_nothrow_move_constructible<T>::value) {
if (initialized() == true && rhs.initialized() == false)
clear();
else if (initialized() == false && rhs.initialized() == true)
initialize(std::move(*rhs));
else if (initialized() == true && rhs.initialized() == true)
contained_val() = std::move(*rhs);
2016-03-26 02:22:04 +00:00
return *this;
}
template <class U>
2018-12-08 05:18:17 +00:00
auto operator=(U&& v) -> typename enable_if<is_same<typename decay<U>::type, T>::value, optional&>::type {
if (initialized()) {
contained_val() = std::forward<U>(v);
} else {
initialize(std::forward<U>(v));
}
2016-03-26 02:22:04 +00:00
return *this;
}
template <class... Args>
2018-12-08 05:18:17 +00:00
void emplace(Args&&... args) {
2016-03-26 02:22:04 +00:00
clear();
initialize(std::forward<Args>(args)...);
}
template <class U, class... Args>
2018-12-08 05:18:17 +00:00
void emplace(initializer_list<U> il, Args&&... args) {
2016-03-26 02:22:04 +00:00
clear();
initialize<U, Args...>(il, std::forward<Args>(args)...);
}
// 20.5.4.4, Swap
2018-12-08 05:18:17 +00:00
void swap(optional<T>& rhs) noexcept(is_nothrow_move_constructible<T>::value&& noexcept(swap(declval<T&>(),
declval<T&>()))) {
if (initialized() == true && rhs.initialized() == false) {
rhs.initialize(std::move(**this));
clear();
} else if (initialized() == false && rhs.initialized() == true) {
initialize(std::move(*rhs));
rhs.clear();
} else if (initialized() == true && rhs.initialized() == true) {
using std::swap;
swap(**this, *rhs);
}
2016-03-26 02:22:04 +00:00
}
// 20.5.4.5, Observers
explicit constexpr operator bool() const noexcept { return initialized(); }
2018-12-08 05:18:17 +00:00
constexpr T const* operator->() const { return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), dataptr()); }
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
#if OPTIONAL_HAS_MOVE_ACCESSORS == 1
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
OPTIONAL_MUTABLE_CONSTEXPR T* operator->() {
assert(initialized());
2016-03-26 02:22:04 +00:00
return dataptr();
}
2018-12-08 05:18:17 +00:00
constexpr T const& operator*() const& { return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), contained_val()); }
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
OPTIONAL_MUTABLE_CONSTEXPR T& operator*() & {
assert(initialized());
2016-03-26 02:22:04 +00:00
return contained_val();
}
2018-12-08 05:18:17 +00:00
OPTIONAL_MUTABLE_CONSTEXPR T&& operator*() && {
assert(initialized());
2016-03-26 02:22:04 +00:00
return constexpr_move(contained_val());
}
constexpr T const& value() const& {
2018-12-08 05:18:17 +00:00
if (!initialized())
atFatal("bad optional access");
2016-03-26 02:22:04 +00:00
return contained_val();
}
OPTIONAL_MUTABLE_CONSTEXPR T& value() & {
2018-12-08 05:18:17 +00:00
if (!initialized())
atFatal("bad optional access");
2016-03-26 02:22:04 +00:00
return contained_val();
}
OPTIONAL_MUTABLE_CONSTEXPR T&& value() && {
2018-12-08 05:18:17 +00:00
if (!initialized())
atFatal("bad optional access");
2016-03-26 02:22:04 +00:00
return std::move(contained_val());
}
2018-12-08 05:18:17 +00:00
#else
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
T* operator->() {
assert(initialized());
2016-03-26 02:22:04 +00:00
return dataptr();
}
2018-12-08 05:18:17 +00:00
constexpr T const& operator*() const { return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), contained_val()); }
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
T& operator*() {
assert(initialized());
2016-03-26 02:22:04 +00:00
return contained_val();
}
constexpr T const& value() const {
2016-04-15 20:41:57 +00:00
if (!initialized())
atFatal("bad optional access");
return contained_val();
2016-03-26 02:22:04 +00:00
}
T& value() {
2016-04-15 20:41:57 +00:00
if (!initialized())
atFatal("bad optional access");
return contained_val();
2016-03-26 02:22:04 +00:00
}
2018-12-08 05:18:17 +00:00
#endif
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
#if OPTIONAL_HAS_THIS_RVALUE_REFS == 1
2016-03-26 02:22:04 +00:00
template <class V>
2018-12-08 05:18:17 +00:00
constexpr T value_or(V&& v) const& {
2016-03-26 02:22:04 +00:00
return *this ? **this : detail_::convert<T>(constexpr_forward<V>(v));
}
2018-12-08 05:18:17 +00:00
#if OPTIONAL_HAS_MOVE_ACCESSORS == 1
2016-03-26 02:22:04 +00:00
template <class V>
2018-12-08 05:18:17 +00:00
OPTIONAL_MUTABLE_CONSTEXPR T value_or(V&& v) && {
return *this ? constexpr_move(const_cast<optional<T>&>(*this).contained_val())
: detail_::convert<T>(constexpr_forward<V>(v));
2016-03-26 02:22:04 +00:00
}
2018-12-08 05:18:17 +00:00
#else
2016-03-26 02:22:04 +00:00
template <class V>
2018-12-08 05:18:17 +00:00
T value_or(V&& v) && {
return *this ? constexpr_move(const_cast<optional<T>&>(*this).contained_val())
: detail_::convert<T>(constexpr_forward<V>(v));
2016-03-26 02:22:04 +00:00
}
2018-12-08 05:18:17 +00:00
#endif
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
#else
2016-03-26 02:22:04 +00:00
template <class V>
2018-12-08 05:18:17 +00:00
constexpr T value_or(V&& v) const {
2016-03-26 02:22:04 +00:00
return *this ? **this : detail_::convert<T>(constexpr_forward<V>(v));
}
2018-12-08 05:18:17 +00:00
#endif
2016-03-26 02:22:04 +00:00
};
template <class T>
2018-12-08 05:18:17 +00:00
class optional<T&> {
static_assert(!std::is_same<T, nullopt_t>::value, "bad T");
static_assert(!std::is_same<T, in_place_t>::value, "bad T");
2016-03-26 02:22:04 +00:00
T* ref;
public:
// 20.5.5.1, construction/destruction
constexpr optional() noexcept : ref(nullptr) {}
constexpr optional(nullopt_t) noexcept : ref(nullptr) {}
constexpr optional(T& v) noexcept : ref(detail_::static_addressof(v)) {}
optional(T&&) = delete;
constexpr optional(const optional& rhs) noexcept : ref(rhs.ref) {}
explicit constexpr optional(in_place_t, T& v) noexcept : ref(detail_::static_addressof(v)) {}
explicit optional(in_place_t, T&&) = delete;
~optional() = default;
// 20.5.5.2, mutation
optional& operator=(nullopt_t) noexcept {
ref = nullptr;
return *this;
}
// optional& operator=(const optional& rhs) noexcept {
2018-12-08 05:18:17 +00:00
// ref = rhs.ref;
// return *this;
2016-03-26 02:22:04 +00:00
// }
// optional& operator=(optional&& rhs) noexcept {
2018-12-08 05:18:17 +00:00
// ref = rhs.ref;
// return *this;
2016-03-26 02:22:04 +00:00
// }
template <typename U>
2018-12-08 05:18:17 +00:00
auto operator=(U&& rhs) noexcept ->
typename enable_if<is_same<typename decay<U>::type, optional<T&>>::value, optional&>::type {
2016-03-26 02:22:04 +00:00
ref = rhs.ref;
return *this;
}
template <typename U>
2018-12-08 05:18:17 +00:00
auto operator=(U&& rhs) noexcept ->
typename enable_if<!is_same<typename decay<U>::type, optional<T&>>::value, optional&>::type = delete;
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
void emplace(T& v) noexcept { ref = detail_::static_addressof(v); }
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
void emplace(T&&) = delete;
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
void swap(optional<T&>& rhs) noexcept { std::swap(ref, rhs.ref); }
2016-03-26 02:22:04 +00:00
// 20.5.5.3, observers
2018-12-08 05:18:17 +00:00
constexpr T* operator->() const { return TR2_OPTIONAL_ASSERTED_EXPRESSION(ref, ref); }
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
constexpr T& operator*() const { return TR2_OPTIONAL_ASSERTED_EXPRESSION(ref, *ref); }
2016-03-26 02:22:04 +00:00
constexpr T& value() const {
2018-12-08 05:18:17 +00:00
if (!ref)
atFatal("bad optional access");
2016-03-26 02:22:04 +00:00
return *ref;
}
2018-12-08 05:18:17 +00:00
explicit constexpr operator bool() const noexcept { return ref != nullptr; }
2016-03-26 02:22:04 +00:00
template <class V>
2018-12-08 05:18:17 +00:00
constexpr typename decay<T>::type value_or(V&& v) const {
2016-03-26 02:22:04 +00:00
return *this ? **this : detail_::convert<typename decay<T>::type>(constexpr_forward<V>(v));
}
};
template <class T>
2018-12-08 05:18:17 +00:00
class optional<T&&> {
static_assert(sizeof(T) == 0, "optional rvalue references disallowed");
2016-03-26 02:22:04 +00:00
};
// 20.5.8, Relational operators
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator==(const optional<T>& x, const optional<T>& y) {
2016-03-26 02:22:04 +00:00
return bool(x) != bool(y) ? false : bool(x) == false ? true : *x == *y;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator!=(const optional<T>& x, const optional<T>& y) {
2016-03-26 02:22:04 +00:00
return !(x == y);
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator<(const optional<T>& x, const optional<T>& y) {
2016-03-26 02:22:04 +00:00
return (!y) ? false : (!x) ? true : *x < *y;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator>(const optional<T>& x, const optional<T>& y) {
2016-03-26 02:22:04 +00:00
return (y < x);
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator<=(const optional<T>& x, const optional<T>& y) {
2016-03-26 02:22:04 +00:00
return !(y < x);
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator>=(const optional<T>& x, const optional<T>& y) {
2016-03-26 02:22:04 +00:00
return !(x < y);
}
// 20.5.9, Comparison with nullopt
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator==(const optional<T>& x, nullopt_t) noexcept {
2016-03-26 02:22:04 +00:00
return (!x);
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator==(nullopt_t, const optional<T>& x) noexcept {
2016-03-26 02:22:04 +00:00
return (!x);
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator!=(const optional<T>& x, nullopt_t) noexcept {
2016-03-26 02:22:04 +00:00
return bool(x);
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator!=(nullopt_t, const optional<T>& x) noexcept {
2016-03-26 02:22:04 +00:00
return bool(x);
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator<(const optional<T>&, nullopt_t) noexcept {
2016-03-26 02:22:04 +00:00
return false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator<(nullopt_t, const optional<T>& x) noexcept {
2016-03-26 02:22:04 +00:00
return bool(x);
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator<=(const optional<T>& x, nullopt_t) noexcept {
2016-03-26 02:22:04 +00:00
return (!x);
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept {
2016-03-26 02:22:04 +00:00
return true;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator>(const optional<T>& x, nullopt_t) noexcept {
2016-03-26 02:22:04 +00:00
return bool(x);
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator>(nullopt_t, const optional<T>&) noexcept {
2016-03-26 02:22:04 +00:00
return false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept {
2016-03-26 02:22:04 +00:00
return true;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator>=(nullopt_t, const optional<T>& x) noexcept {
2016-03-26 02:22:04 +00:00
return (!x);
}
// 20.5.10, Comparison with T
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator==(const optional<T>& x, const T& v) {
2016-03-26 02:22:04 +00:00
return bool(x) ? *x == v : false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator==(const T& v, const optional<T>& x) {
2016-03-26 02:22:04 +00:00
return bool(x) ? v == *x : false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator!=(const optional<T>& x, const T& v) {
2016-03-26 02:22:04 +00:00
return bool(x) ? *x != v : true;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator!=(const T& v, const optional<T>& x) {
2016-03-26 02:22:04 +00:00
return bool(x) ? v != *x : true;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator<(const optional<T>& x, const T& v) {
2016-03-26 02:22:04 +00:00
return bool(x) ? *x < v : true;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator>(const T& v, const optional<T>& x) {
2016-03-26 02:22:04 +00:00
return bool(x) ? v > *x : true;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator>(const optional<T>& x, const T& v) {
2016-03-26 02:22:04 +00:00
return bool(x) ? *x > v : false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator<(const T& v, const optional<T>& x) {
2016-03-26 02:22:04 +00:00
return bool(x) ? v < *x : false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator>=(const optional<T>& x, const T& v) {
2016-03-26 02:22:04 +00:00
return bool(x) ? *x >= v : false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator<=(const T& v, const optional<T>& x) {
2016-03-26 02:22:04 +00:00
return bool(x) ? v <= *x : false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator<=(const optional<T>& x, const T& v) {
2016-03-26 02:22:04 +00:00
return bool(x) ? *x <= v : true;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator>=(const T& v, const optional<T>& x) {
2016-03-26 02:22:04 +00:00
return bool(x) ? v >= *x : true;
}
// Comparison of optional<T&> with T
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator==(const optional<T&>& x, const T& v) {
2016-03-26 02:22:04 +00:00
return bool(x) ? *x == v : false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator==(const T& v, const optional<T&>& x) {
2016-03-26 02:22:04 +00:00
return bool(x) ? v == *x : false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator!=(const optional<T&>& x, const T& v) {
2016-03-26 02:22:04 +00:00
return bool(x) ? *x != v : true;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator!=(const T& v, const optional<T&>& x) {
2016-03-26 02:22:04 +00:00
return bool(x) ? v != *x : true;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator<(const optional<T&>& x, const T& v) {
2016-03-26 02:22:04 +00:00
return bool(x) ? *x < v : true;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator>(const T& v, const optional<T&>& x) {
2016-03-26 02:22:04 +00:00
return bool(x) ? v > *x : true;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator>(const optional<T&>& x, const T& v) {
2016-03-26 02:22:04 +00:00
return bool(x) ? *x > v : false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator<(const T& v, const optional<T&>& x) {
2016-03-26 02:22:04 +00:00
return bool(x) ? v < *x : false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator>=(const optional<T&>& x, const T& v) {
2016-03-26 02:22:04 +00:00
return bool(x) ? *x >= v : false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator<=(const T& v, const optional<T&>& x) {
2016-03-26 02:22:04 +00:00
return bool(x) ? v <= *x : false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator<=(const optional<T&>& x, const T& v) {
2016-03-26 02:22:04 +00:00
return bool(x) ? *x <= v : true;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator>=(const T& v, const optional<T&>& x) {
2016-03-26 02:22:04 +00:00
return bool(x) ? v >= *x : true;
}
// Comparison of optional<T const&> with T
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator==(const optional<const T&>& x, const T& v) {
2016-03-26 02:22:04 +00:00
return bool(x) ? *x == v : false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator==(const T& v, const optional<const T&>& x) {
2016-03-26 02:22:04 +00:00
return bool(x) ? v == *x : false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator!=(const optional<const T&>& x, const T& v) {
2016-03-26 02:22:04 +00:00
return bool(x) ? *x != v : true;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator!=(const T& v, const optional<const T&>& x) {
2016-03-26 02:22:04 +00:00
return bool(x) ? v != *x : true;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator<(const optional<const T&>& x, const T& v) {
2016-03-26 02:22:04 +00:00
return bool(x) ? *x < v : true;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator>(const T& v, const optional<const T&>& x) {
2016-03-26 02:22:04 +00:00
return bool(x) ? v > *x : true;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator>(const optional<const T&>& x, const T& v) {
2016-03-26 02:22:04 +00:00
return bool(x) ? *x > v : false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator<(const T& v, const optional<const T&>& x) {
2016-03-26 02:22:04 +00:00
return bool(x) ? v < *x : false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator>=(const optional<const T&>& x, const T& v) {
2016-03-26 02:22:04 +00:00
return bool(x) ? *x >= v : false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator<=(const T& v, const optional<const T&>& x) {
2016-03-26 02:22:04 +00:00
return bool(x) ? v <= *x : false;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator<=(const optional<const T&>& x, const T& v) {
2016-03-26 02:22:04 +00:00
return bool(x) ? *x <= v : true;
}
2018-12-08 05:18:17 +00:00
template <class T>
constexpr bool operator>=(const T& v, const optional<const T&>& x) {
2016-03-26 02:22:04 +00:00
return bool(x) ? v >= *x : true;
}
// 20.5.12, Specialized algorithms
template <class T>
2018-12-08 05:18:17 +00:00
void swap(optional<T>& x, optional<T>& y) noexcept(noexcept(x.swap(y))) {
2016-03-26 02:22:04 +00:00
x.swap(y);
}
template <class T>
2018-12-08 05:18:17 +00:00
constexpr optional<typename decay<T>::type> make_optional(T&& v) {
2016-03-26 02:22:04 +00:00
return optional<typename decay<T>::type>(constexpr_forward<T>(v));
}
template <class X>
2018-12-08 05:18:17 +00:00
constexpr optional<X&> make_optional(reference_wrapper<X> v) {
2016-03-26 02:22:04 +00:00
return optional<X&>(v.get());
}
} // namespace experimental
} // namespace std
2018-12-08 05:18:17 +00:00
namespace std {
template <typename T>
struct hash<std::experimental::optional<T>> {
typedef typename hash<T>::result_type result_type;
typedef std::experimental::optional<T> argument_type;
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
constexpr result_type operator()(argument_type const& arg) const {
return arg ? std::hash<T>{}(*arg) : result_type{};
}
};
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
template <typename T>
struct hash<std::experimental::optional<T&>> {
typedef typename hash<T>::result_type result_type;
typedef std::experimental::optional<T&> argument_type;
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
constexpr result_type operator()(argument_type const& arg) const {
return arg ? std::hash<T>{}(*arg) : result_type{};
}
};
} // namespace std
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
#undef TR2_OPTIONAL_REQUIRES
#undef TR2_OPTIONAL_ASSERTED_EXPRESSION
2016-03-26 02:22:04 +00:00
2018-12-08 05:18:17 +00:00
#endif //___OPTIONAL_HPP___