Sprout/sprout/optional/optional.hpp

365 lines
12 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2014-01-08 07:48:12 +00:00
Copyright (c) 2011-2014 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
https://github.com/bolero-MURAKAMI/Sprout
Distributed under 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)
=============================================================================*/
2012-10-22 14:10:11 +00:00
#ifndef SPROUT_OPTIONAL_OPTIONAL_HPP
#define SPROUT_OPTIONAL_OPTIONAL_HPP
2013-05-21 10:56:41 +00:00
#include <type_traits>
#include <initializer_list>
2012-10-22 14:10:11 +00:00
#include <sprout/config.hpp>
#include <sprout/detail/predef.hpp>
2013-02-07 14:12:57 +00:00
#include <sprout/utility/value_holder/value_holder.hpp>
#include <sprout/utility/value_holder/get.hpp>
2012-10-22 14:10:11 +00:00
#include <sprout/utility/swap.hpp>
2013-05-18 12:37:54 +00:00
#include <sprout/utility/forward.hpp>
#include <sprout/utility/move.hpp>
2013-11-29 13:21:07 +00:00
#include <sprout/type_traits/std_type_traits.hpp>
2013-05-21 10:56:41 +00:00
#include <sprout/type_traits/is_convert_constructible.hpp>
2012-10-22 14:10:11 +00:00
#include <sprout/none.hpp>
2012-10-26 08:38:48 +00:00
#include <sprout/optional/nullopt.hpp>
2013-05-21 10:56:41 +00:00
#include <sprout/optional/in_place.hpp>
2013-03-31 01:09:02 +00:00
#include <sprout/optional/exceptions.hpp>
2013-03-18 10:12:21 +00:00
#include <sprout/assert.hpp>
2012-10-22 14:10:11 +00:00
namespace sprout {
//
// optional
//
template<typename T>
class optional {
public:
typedef T type;
private:
typedef sprout::value_holder<type> holder_type;
public:
typedef typename holder_type::value_type value_type;
typedef typename holder_type::lvalue_reference lvalue_reference;
typedef typename holder_type::rvalue_reference rvalue_reference;
2012-10-22 14:10:11 +00:00
typedef typename holder_type::reference reference;
typedef typename holder_type::const_lvalue_reference const_lvalue_reference;
typedef typename holder_type::const_rvalue_reference const_rvalue_reference;
2012-10-22 14:10:11 +00:00
typedef typename holder_type::const_reference const_reference;
typedef typename holder_type::pointer pointer;
typedef typename holder_type::const_pointer const_pointer;
typedef typename holder_type::lvalue_reference_type lvalue_reference_type;
typedef typename holder_type::rvalue_reference_type rvalue_reference_type;
2012-10-22 14:10:11 +00:00
typedef typename holder_type::reference_type reference_type;
typedef typename holder_type::reference_const_type reference_const_type;
typedef typename holder_type::pointer_type pointer_type;
typedef typename holder_type::pointer_const_type pointer_const_type;
public:
template<typename... Args>
struct is_constructible_args
2013-11-29 13:21:07 +00:00
: public sprout::is_constructible<T, Args&&...>
{};
public:
static SPROUT_CONSTEXPR reference_type get(optional& t) SPROUT_NOEXCEPT {
return sprout::get(t.val);
}
static SPROUT_CONSTEXPR rvalue_reference_type get(optional&& t) SPROUT_NOEXCEPT {
return static_cast<rvalue_reference_type>(get(t));
}
static SPROUT_CONSTEXPR reference_const_type get(optional const& t) SPROUT_NOEXCEPT {
return sprout::get(t.val);
}
static SPROUT_CONSTEXPR pointer_type get_pointer(optional& t) SPROUT_NOEXCEPT {
return sprout::get_pointer(t.val);
}
static SPROUT_CONSTEXPR pointer_type get_pointer(optional&& t) SPROUT_NOEXCEPT {
return get_pointer(t);
}
static SPROUT_CONSTEXPR pointer_const_type get_pointer(optional const& t) SPROUT_NOEXCEPT {
return sprout::get_pointer(t.val);
}
static SPROUT_CONSTEXPR reference_type get_value_or(optional& t, reference_type v) SPROUT_NOEXCEPT {
return t.is_initialized() ? sprout::get(t.val)
: v
;
}
static SPROUT_CONSTEXPR rvalue_reference_type get_value_or(optional&& t, rvalue_reference_type v) SPROUT_NOEXCEPT {
return static_cast<rvalue_reference_type>(get_value_or(t, v));
}
static SPROUT_CONSTEXPR reference_const_type get_value_or(optional const& t, reference_const_type v) SPROUT_NOEXCEPT {
return t.is_initialized() ? sprout::get(t.val)
: v
;
}
2012-10-22 14:10:11 +00:00
private:
2012-10-26 08:38:48 +00:00
bool init;
holder_type val;
2012-10-22 14:10:11 +00:00
private:
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR void destroy() SPROUT_NOEXCEPT {
2012-10-26 08:38:48 +00:00
init = false;
2012-10-22 14:10:11 +00:00
}
public:
2013-05-18 12:37:54 +00:00
// 20.6.4.1, constructors
2012-10-26 08:38:48 +00:00
SPROUT_CONSTEXPR optional() SPROUT_NOEXCEPT
: init(false)
2012-10-22 14:10:11 +00:00
{}
2012-10-26 08:38:48 +00:00
SPROUT_CONSTEXPR optional(sprout::nullopt_t) SPROUT_NOEXCEPT
: init(false)
2012-10-22 14:10:11 +00:00
{}
2013-05-18 12:37:54 +00:00
SPROUT_CONSTEXPR optional(optional const& v)
: init(v.init)
, val(v.is_initialized() ? holder_type(*v) : holder_type())
{}
2014-01-20 04:09:54 +00:00
#if SPROUT_GCC_IN_RANGE((4, 8, 0), (4, 8, 2))
2013-10-29 10:15:52 +00:00
optional(optional&&) = default;
#else
2013-05-18 12:37:54 +00:00
SPROUT_CONSTEXPR optional(optional&& v)
SPROUT_NOEXCEPT_EXPR(std::is_nothrow_move_constructible<T>::value)
2013-05-18 12:37:54 +00:00
: init(v.init)
, val(v.is_initialized() ? holder_type(sprout::move(get(v))) : holder_type())
2013-05-18 12:37:54 +00:00
{}
#endif
2013-05-18 12:37:54 +00:00
SPROUT_CONSTEXPR optional(T const& v)
2012-10-26 08:38:48 +00:00
: init(true)
, val(v)
2012-10-22 14:10:11 +00:00
{}
2014-01-05 13:20:27 +00:00
// !!! TEMP: for T const&
// SPROUT_CONSTEXPR optional(T&& v)
// : init(true)
// , val(sprout::move(v))
// {}
2013-05-21 10:56:41 +00:00
template<
typename... Args,
typename = typename std::enable_if<is_constructible_args<Args...>::value>::type
2013-05-21 10:56:41 +00:00
>
explicit SPROUT_CONSTEXPR optional(sprout::in_place_t, Args&&... args)
: init(true)
, val(sprout::in_place, SPROUT_FORWARD(Args, args)...)
2013-05-21 10:56:41 +00:00
{}
template<
typename U, typename... Args,
typename = typename std::enable_if<is_constructible_args<std::initializer_list<U>&, Args...>::value>::type
2013-05-21 10:56:41 +00:00
>
explicit SPROUT_CONSTEXPR optional(sprout::in_place_t, std::initializer_list<U> il, Args&&... args)
: init(true)
, val(sprout::in_place, il, SPROUT_FORWARD(Args, args)...)
2013-05-21 10:56:41 +00:00
{}
2013-05-18 12:37:54 +00:00
SPROUT_CONSTEXPR optional(bool cond, T const& v)
2012-10-26 08:38:48 +00:00
: init(cond)
, val(cond ? holder_type(v) : holder_type())
2012-10-22 14:10:11 +00:00
{}
2014-01-05 13:20:27 +00:00
// !!! TEMP: for T const&
// SPROUT_CONSTEXPR optional(bool cond, T&& v)
// : init(cond)
// , val(cond ? holder_type(sprout::move(v)) : holder_type())
// {}
2012-10-27 06:37:39 +00:00
template<typename U>
2012-10-22 14:10:11 +00:00
explicit SPROUT_CONSTEXPR optional(optional<U> const& v)
2012-10-27 06:37:39 +00:00
: init(v.is_initialized())
2013-03-31 01:09:02 +00:00
, val(v.is_initialized() ? holder_type(*v) : holder_type())
2012-10-22 14:10:11 +00:00
{}
2013-05-21 10:56:41 +00:00
template<typename U>
explicit SPROUT_CONSTEXPR optional(optional<U>&& v)
: init(v.is_initialized())
, val(v.is_initialized() ? holder_type(sprout::move(optional<U>::get(v))) : holder_type())
2013-05-21 10:56:41 +00:00
{}
2013-05-18 12:37:54 +00:00
// 20.6.4.3, assignment
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR optional& operator=(sprout::nullopt_t v) SPROUT_NOEXCEPT {
2012-10-22 14:10:11 +00:00
assign(v);
return *this;
}
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR optional& operator=(optional const& v) {
2012-10-22 14:10:11 +00:00
assign(v);
return *this;
}
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR optional& operator=(optional&& v)
2013-05-21 10:56:41 +00:00
SPROUT_NOEXCEPT_EXPR(std::is_move_constructible<T>::value && std::is_move_assignable<T>::value)
{
assign(SPROUT_FORWARD(optional, v));
2013-05-18 12:37:54 +00:00
return *this;
}
2013-05-21 10:56:41 +00:00
template<
typename U,
// typename = typename std::enable_if<std::is_constructible<T, U>::value && std::is_assignable<U, T>::value>::type
typename = typename std::enable_if<std::is_constructible<T, U&&>::value>::type
>
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR optional& operator=(U&& v) {
assign(SPROUT_FORWARD(U, v));
2013-05-18 12:37:54 +00:00
return *this;
}
2012-10-27 06:37:39 +00:00
template<typename U>
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR optional& operator=(optional<U> const& v) {
2012-10-22 14:10:11 +00:00
assign(v);
return *this;
}
2013-05-21 10:56:41 +00:00
template<typename U>
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR optional& operator=(optional<U>&& v) {
assign(SPROUT_FORWARD(optional<U>, v));
2013-05-21 10:56:41 +00:00
return *this;
}
2012-10-22 14:10:11 +00:00
template<
typename... Args,
typename = typename std::enable_if<is_constructible_args<Args...>::value>::type
>
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR void emplace(Args&&... args) {
optional temp(sprout::in_place, SPROUT_FORWARD(Args, args)...);
temp.swap(*this);
}
template<
typename U, typename... Args,
typename = typename std::enable_if<is_constructible_args<std::initializer_list<U>&, Args...>::value>::type
>
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR void emplace(std::initializer_list<U> il, Args&&... args) {
optional temp(sprout::in_place, il, SPROUT_FORWARD(Args, args)...);
temp.swap(*this);
}
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR void assign(sprout::nullopt_t) SPROUT_NOEXCEPT {
2012-10-22 14:10:11 +00:00
destroy();
}
2013-11-02 09:28:18 +00:00
SPROUT_CXX14_CONSTEXPR void assign(optional const& v) {
2012-10-22 14:10:11 +00:00
optional temp(v);
2012-10-27 06:37:39 +00:00
temp.swap(*this);
2012-10-22 14:10:11 +00:00
}
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR void assign(optional&& v)
2013-05-21 10:56:41 +00:00
SPROUT_NOEXCEPT_EXPR(std::is_move_constructible<T>::value && std::is_move_assignable<T>::value)
{
optional temp(SPROUT_FORWARD(optional, v));
2013-05-18 12:37:54 +00:00
temp.swap(*this);
}
2013-05-21 10:56:41 +00:00
template<
typename U,
// typename = typename std::enable_if<std::is_constructible<T, U>::value && std::is_assignable<U, T>::value>::type
typename = typename std::enable_if<std::is_constructible<T, U&&>::value>::type
>
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR void assign(U&& v) {
optional temp(SPROUT_FORWARD(U, v));
2013-05-18 12:37:54 +00:00
temp.swap(*this);
}
2012-10-27 06:37:39 +00:00
template<typename U>
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR void assign(optional<U> const& v) {
2012-10-22 14:10:11 +00:00
optional temp(v);
2012-10-27 06:37:39 +00:00
temp.swap(*this);
2012-10-22 14:10:11 +00:00
}
2013-05-21 10:56:41 +00:00
template<typename U>
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR void assign(optional<U>&& v) {
optional temp(SPROUT_FORWARD(optional<U>, v));
2013-05-21 10:56:41 +00:00
temp.swap(*this);
}
2012-10-22 14:10:11 +00:00
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR void reset() SPROUT_NOEXCEPT {
2012-10-22 14:10:11 +00:00
destroy();
}
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR void reset(sprout::nullopt_t v) SPROUT_NOEXCEPT {
2012-10-22 14:10:11 +00:00
assign(v);
}
2013-05-21 10:56:41 +00:00
template<
typename U,
// typename = typename std::enable_if<std::is_constructible<T, U>::value && std::is_assignable<U, T>::value>::type
typename = typename std::enable_if<std::is_constructible<T, U&&>::value>::type
>
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR void reset(U&& v) {
assign(SPROUT_FORWARD(U, v));
2013-05-18 12:37:54 +00:00
}
// 20.6.4.4, swap
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR void swap(optional& other)
2012-10-26 08:38:48 +00:00
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout::swap(val, other.val)))
2012-10-22 14:10:11 +00:00
{
2012-10-26 08:38:48 +00:00
sprout::swap(init, other.init);
sprout::swap(val, other.val);
2012-10-22 14:10:11 +00:00
}
2013-05-18 12:37:54 +00:00
// 20.6.4.5, observers
SPROUT_CONSTEXPR pointer_const_type operator->() const {
return SPROUT_ASSERT(is_initialized()),
val.get_pointer()
;
}
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR pointer_type operator->() {
2013-05-18 12:37:54 +00:00
return SPROUT_ASSERT(is_initialized()),
val.get_pointer()
;
}
SPROUT_CONSTEXPR pointer_const_type get_pointer() const {
return is_initialized() ? val.get_pointer()
: 0
;
}
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR pointer_type get_pointer() {
2013-05-18 12:37:54 +00:00
return is_initialized() ? val.get_pointer()
: 0
;
}
SPROUT_CONSTEXPR pointer_const_type get_ptr() const {
return get_pointer();
}
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR pointer_type get_ptr() {
2013-05-18 12:37:54 +00:00
return get_pointer();
}
2012-10-27 06:37:39 +00:00
SPROUT_CONSTEXPR reference_const_type operator*() const {
2013-03-31 01:09:02 +00:00
return (SPROUT_ASSERT(is_initialized()), true) ? val.get()
: val.get()
;
2012-10-27 06:37:39 +00:00
}
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR reference_type operator*() {
2013-03-19 07:32:48 +00:00
return (SPROUT_ASSERT(is_initialized()), true) ? val.get()
2013-03-18 10:12:21 +00:00
: val.get()
2012-10-22 14:10:11 +00:00
;
}
2013-05-18 12:37:54 +00:00
SPROUT_CONSTEXPR reference_const_type value() const {
return get();
}
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR reference_type value() {
2013-05-18 12:37:54 +00:00
return get();
}
2013-03-31 01:09:02 +00:00
SPROUT_CONSTEXPR reference_const_type get() const {
return is_initialized() ? val.get()
: (throw sprout::bad_optional_access("optional<>: bad optional access"), val.get())
;
}
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR reference_type get() {
2013-03-31 01:09:02 +00:00
return is_initialized() ? val.get()
: (throw sprout::bad_optional_access("optional<>: bad optional access"), val.get())
2012-10-22 14:10:11 +00:00
;
}
2013-03-31 01:09:02 +00:00
SPROUT_CONSTEXPR reference_const_type value_or(reference_const_type& v) const {
return get_value_or(v);
}
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR reference_type value_or(reference_type& v) {
2013-03-31 01:09:02 +00:00
return get_value_or(v);
}
2013-05-18 12:37:54 +00:00
SPROUT_CONSTEXPR reference_const_type get_value_or(reference_const_type& v) const {
return is_initialized() ? val.get()
: v
2012-10-22 14:10:11 +00:00
;
}
2013-10-29 10:15:52 +00:00
SPROUT_CXX14_CONSTEXPR reference_type get_value_or(reference_type& v) {
2013-05-18 12:37:54 +00:00
return is_initialized() ? val.get()
: v
2012-10-22 14:10:11 +00:00
;
}
SPROUT_EXPLICIT_CONVERSION SPROUT_CONSTEXPR operator bool() const SPROUT_NOEXCEPT {
2012-10-22 14:10:11 +00:00
return is_initialized();
}
2012-10-26 08:38:48 +00:00
SPROUT_CONSTEXPR bool operator!() const SPROUT_NOEXCEPT {
2012-10-22 14:10:11 +00:00
return !is_initialized();
}
2012-10-26 08:38:48 +00:00
SPROUT_CONSTEXPR bool is_initialized() const SPROUT_NOEXCEPT {
return init;
2012-10-22 14:10:11 +00:00
}
};
//
// swap
//
template<typename T>
2013-10-29 10:15:52 +00:00
inline SPROUT_CXX14_CONSTEXPR void
2012-10-22 14:10:11 +00:00
swap(sprout::optional<T>& lhs, sprout::optional<T>& rhs)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs)))
{
lhs.swap(rhs);
}
} // namespace sprout
#endif // #ifndef SPROUT_OPTIONAL_OPTIONAL_HPP