2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2015-01-10 10:13:57 +00:00
|
|
|
Copyright (c) 2011-2015 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-24 11:52:09 +00:00
|
|
|
#ifndef SPROUT_UTILITY_VALUE_HOLDER_VALUE_HOLDER_HPP
|
|
|
|
#define SPROUT_UTILITY_VALUE_HOLDER_VALUE_HOLDER_HPP
|
|
|
|
|
2013-05-21 10:56:41 +00:00
|
|
|
#include <type_traits>
|
|
|
|
#include <initializer_list>
|
2012-10-24 11:52:09 +00:00
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/utility/swap.hpp>
|
2013-05-21 10:56:41 +00:00
|
|
|
#include <sprout/utility/forward.hpp>
|
2013-05-18 12:37:54 +00:00
|
|
|
#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/optional/in_place.hpp>
|
2012-10-24 11:52:09 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace detail {
|
|
|
|
template<typename T>
|
|
|
|
struct holder_helper {
|
|
|
|
public:
|
|
|
|
typedef T value_type;
|
2013-06-04 14:15:05 +00:00
|
|
|
typedef T& lvalue_reference;
|
|
|
|
typedef T&& rvalue_reference;
|
|
|
|
typedef lvalue_reference reference;
|
|
|
|
typedef T const& const_lvalue_reference;
|
|
|
|
typedef T const&& const_rvalue_reference;
|
|
|
|
typedef const_lvalue_reference const_reference;
|
2012-10-24 11:52:09 +00:00
|
|
|
typedef T const& mutable_or_const_reference;
|
|
|
|
typedef T* pointer;
|
|
|
|
typedef T const* const_pointer;
|
|
|
|
typedef T const* mutable_or_const_pointer;
|
|
|
|
typedef T const& param_type;
|
2013-05-18 12:37:54 +00:00
|
|
|
typedef T&& movable_param_type;
|
2012-10-24 11:52:09 +00:00
|
|
|
typedef T holder_type;
|
|
|
|
public:
|
2012-10-26 08:38:48 +00:00
|
|
|
static SPROUT_CONSTEXPR holder_type const& hold(param_type p) SPROUT_NOEXCEPT {
|
2012-10-24 11:52:09 +00:00
|
|
|
return p;
|
|
|
|
}
|
2013-05-18 12:37:54 +00:00
|
|
|
static SPROUT_CONSTEXPR holder_type&& hold(movable_param_type p) SPROUT_NOEXCEPT {
|
|
|
|
return sprout::move(p);
|
|
|
|
}
|
2012-10-24 11:52:09 +00:00
|
|
|
static SPROUT_CONSTEXPR reference ref(holder_type& r) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR const_reference ref(holder_type const& r) {
|
|
|
|
return r;
|
|
|
|
}
|
2012-10-26 08:38:48 +00:00
|
|
|
static SPROUT_CONSTEXPR pointer ptr(holder_type& r) SPROUT_NOEXCEPT {
|
2012-10-24 11:52:09 +00:00
|
|
|
return &r;
|
|
|
|
}
|
2012-10-26 08:38:48 +00:00
|
|
|
static SPROUT_CONSTEXPR const_pointer ptr(holder_type const& r) SPROUT_NOEXCEPT {
|
2012-10-24 11:52:09 +00:00
|
|
|
return &r;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<typename T>
|
|
|
|
struct holder_helper<T const> {
|
|
|
|
public:
|
|
|
|
typedef T value_type;
|
2013-06-04 14:15:05 +00:00
|
|
|
typedef T const& lvalue_reference;
|
|
|
|
typedef T const&& rvalue_reference;
|
|
|
|
typedef lvalue_reference reference;
|
|
|
|
typedef T const& const_lvalue_reference;
|
|
|
|
typedef T const&& const_rvalue_reference;
|
|
|
|
typedef const_lvalue_reference const_reference;
|
2012-10-24 11:52:09 +00:00
|
|
|
typedef T const& mutable_or_const_reference;
|
|
|
|
typedef T const* pointer;
|
|
|
|
typedef T const* const_pointer;
|
|
|
|
typedef T const* mutable_or_const_pointer;
|
|
|
|
typedef T const& param_type;
|
2013-05-18 12:37:54 +00:00
|
|
|
typedef T const&& movable_param_type;
|
2012-10-24 11:52:09 +00:00
|
|
|
typedef T holder_type;
|
|
|
|
public:
|
2012-10-26 08:38:48 +00:00
|
|
|
static SPROUT_CONSTEXPR holder_type const& hold(param_type p) SPROUT_NOEXCEPT {
|
2013-05-18 12:37:54 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR holder_type const&& hold(movable_param_type p) SPROUT_NOEXCEPT {
|
|
|
|
return sprout::move(p);
|
2012-10-24 11:52:09 +00:00
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR reference ref(holder_type& r) {
|
|
|
|
return *r;
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR const_reference ref(holder_type const& r) {
|
|
|
|
return *r;
|
|
|
|
}
|
2012-10-26 08:38:48 +00:00
|
|
|
static SPROUT_CONSTEXPR pointer ptr(holder_type& r) SPROUT_NOEXCEPT {
|
2012-10-24 11:52:09 +00:00
|
|
|
return &r;
|
|
|
|
}
|
2012-10-26 08:38:48 +00:00
|
|
|
static SPROUT_CONSTEXPR const_pointer ptr(holder_type const& r) SPROUT_NOEXCEPT {
|
2012-10-24 11:52:09 +00:00
|
|
|
return &r;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<typename T>
|
|
|
|
struct holder_helper<T&> {
|
|
|
|
public:
|
|
|
|
typedef T value_type;
|
2013-06-04 14:15:05 +00:00
|
|
|
typedef T& lvalue_reference;
|
|
|
|
typedef T& rvalue_reference;
|
|
|
|
typedef lvalue_reference reference;
|
|
|
|
typedef T const& const_lvalue_reference;
|
|
|
|
typedef T const& const_rvalue_reference;
|
|
|
|
typedef const_lvalue_reference const_reference;
|
2012-10-24 11:52:09 +00:00
|
|
|
typedef T& mutable_or_const_reference;
|
|
|
|
typedef T* pointer;
|
|
|
|
typedef T const* const_pointer;
|
|
|
|
typedef T* mutable_or_const_pointer;
|
|
|
|
typedef T& param_type;
|
2013-05-18 12:37:54 +00:00
|
|
|
typedef T&& movable_param_type;
|
2012-10-24 11:52:09 +00:00
|
|
|
typedef T* holder_type;
|
|
|
|
public:
|
2012-10-26 08:38:48 +00:00
|
|
|
static SPROUT_CONSTEXPR holder_type hold(param_type p) SPROUT_NOEXCEPT {
|
2012-10-24 11:52:09 +00:00
|
|
|
return &p;
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR reference ref(holder_type r) {
|
|
|
|
return *r;
|
|
|
|
}
|
2012-10-26 08:38:48 +00:00
|
|
|
static SPROUT_CONSTEXPR pointer ptr(holder_type r) SPROUT_NOEXCEPT {
|
2012-10-24 11:52:09 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<typename T>
|
|
|
|
struct holder_helper<T const&> {
|
|
|
|
public:
|
|
|
|
typedef T value_type;
|
2013-06-04 14:15:05 +00:00
|
|
|
typedef T const& lvalue_reference;
|
|
|
|
typedef T const& rvalue_reference;
|
|
|
|
typedef lvalue_reference reference;
|
|
|
|
typedef T const& const_lvalue_reference;
|
|
|
|
typedef T const& const_rvalue_reference;
|
|
|
|
typedef const_lvalue_reference const_reference;
|
2012-10-24 11:52:09 +00:00
|
|
|
typedef T const& mutable_or_const_reference;
|
|
|
|
typedef T const* pointer;
|
|
|
|
typedef T const* const_pointer;
|
|
|
|
typedef T const* mutable_or_const_pointer;
|
|
|
|
typedef T const& param_type;
|
2013-05-18 12:37:54 +00:00
|
|
|
typedef T const&& movable_param_type;
|
2012-10-24 11:52:09 +00:00
|
|
|
typedef T const* holder_type;
|
|
|
|
public:
|
2012-10-26 08:38:48 +00:00
|
|
|
static SPROUT_CONSTEXPR holder_type hold(param_type p) SPROUT_NOEXCEPT {
|
2012-10-24 11:52:09 +00:00
|
|
|
return &p;
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR reference ref(holder_type r) {
|
|
|
|
return *r;
|
|
|
|
}
|
2012-10-26 08:38:48 +00:00
|
|
|
static SPROUT_CONSTEXPR pointer ptr(holder_type r) SPROUT_NOEXCEPT {
|
2012-10-24 11:52:09 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace detail
|
|
|
|
//
|
|
|
|
// value_holder
|
|
|
|
//
|
|
|
|
template<typename T>
|
|
|
|
class value_holder {
|
|
|
|
public:
|
|
|
|
typedef T type;
|
|
|
|
private:
|
|
|
|
typedef sprout::detail::holder_helper<type> helper_type;
|
|
|
|
typedef typename helper_type::holder_type holder_type;
|
|
|
|
public:
|
|
|
|
typedef typename helper_type::value_type value_type;
|
2013-06-04 14:15:05 +00:00
|
|
|
typedef typename helper_type::lvalue_reference lvalue_reference;
|
|
|
|
typedef typename helper_type::rvalue_reference rvalue_reference;
|
2012-10-24 11:52:09 +00:00
|
|
|
typedef typename helper_type::reference reference;
|
2013-06-04 14:15:05 +00:00
|
|
|
typedef typename helper_type::const_lvalue_reference const_lvalue_reference;
|
|
|
|
typedef typename helper_type::const_rvalue_reference const_rvalue_reference;
|
2012-10-24 11:52:09 +00:00
|
|
|
typedef typename helper_type::const_reference const_reference;
|
|
|
|
typedef typename helper_type::mutable_or_const_reference mutable_or_const_reference;
|
|
|
|
typedef typename helper_type::pointer pointer;
|
|
|
|
typedef typename helper_type::const_pointer const_pointer;
|
|
|
|
typedef typename helper_type::mutable_or_const_pointer mutable_or_const_pointer;
|
|
|
|
typedef typename helper_type::param_type param_type;
|
2013-05-18 12:37:54 +00:00
|
|
|
typedef typename helper_type::movable_param_type movable_param_type;
|
2013-06-04 14:15:05 +00:00
|
|
|
typedef lvalue_reference lvalue_reference_type;
|
|
|
|
typedef rvalue_reference rvalue_reference_type;
|
2012-10-24 11:52:09 +00:00
|
|
|
typedef reference reference_type;
|
|
|
|
typedef mutable_or_const_reference reference_const_type;
|
|
|
|
typedef pointer pointer_type;
|
|
|
|
typedef mutable_or_const_pointer pointer_const_type;
|
|
|
|
typedef param_type argument_type;
|
2013-05-18 12:37:54 +00:00
|
|
|
typedef movable_param_type movable_argument_type;
|
2013-07-01 07:57:56 +00:00
|
|
|
public:
|
|
|
|
template<typename... Args>
|
|
|
|
struct is_constructible_args
|
2013-11-29 13:21:07 +00:00
|
|
|
: public sprout::is_constructible<T, Args&&...>
|
2013-07-01 07:57:56 +00:00
|
|
|
{};
|
2013-06-04 14:15:05 +00:00
|
|
|
public:
|
|
|
|
static SPROUT_CONSTEXPR reference get(value_holder& t) SPROUT_NOEXCEPT {
|
|
|
|
return helper_type::ref(t.holder_);
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR rvalue_reference get(value_holder&& t) SPROUT_NOEXCEPT {
|
|
|
|
return static_cast<rvalue_reference>(get(t));
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR mutable_or_const_reference get(value_holder const& t) SPROUT_NOEXCEPT {
|
|
|
|
return helper_type::ref(t.holder_);
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR pointer get_pointer(value_holder& t) SPROUT_NOEXCEPT {
|
|
|
|
return helper_type::ptr(t.holder_);
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR pointer get_pointer(value_holder&& t) SPROUT_NOEXCEPT {
|
|
|
|
return get_pointer(t);
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR mutable_or_const_pointer get_pointer(value_holder const& t) SPROUT_NOEXCEPT {
|
|
|
|
return helper_type::ptr(t.holder_);
|
|
|
|
}
|
2012-10-24 11:52:09 +00:00
|
|
|
private:
|
|
|
|
holder_type holder_;
|
|
|
|
public:
|
2012-10-27 06:37:39 +00:00
|
|
|
SPROUT_CONSTEXPR value_holder()
|
|
|
|
: holder_()
|
|
|
|
{}
|
2013-10-29 10:15:52 +00:00
|
|
|
value_holder(value_holder const&) = default;
|
|
|
|
value_holder(value_holder&&) = default;
|
2013-06-04 14:15:05 +00:00
|
|
|
explicit SPROUT_CONSTEXPR value_holder(argument_type p)
|
2012-10-24 11:52:09 +00:00
|
|
|
: holder_(helper_type::hold(p))
|
|
|
|
{}
|
2013-05-18 12:37:54 +00:00
|
|
|
explicit SPROUT_CONSTEXPR value_holder(movable_argument_type p)
|
|
|
|
: holder_(helper_type::hold(sprout::move(p)))
|
|
|
|
{}
|
2013-05-21 10:56:41 +00:00
|
|
|
template<
|
|
|
|
typename... Args,
|
2013-07-01 07:57:56 +00:00
|
|
|
typename = typename std::enable_if<is_constructible_args<Args...>::value>::type
|
2013-05-21 10:56:41 +00:00
|
|
|
>
|
|
|
|
explicit SPROUT_CONSTEXPR value_holder(sprout::in_place_t, Args&&... args)
|
2014-02-22 07:32:51 +00:00
|
|
|
: holder_(SPROUT_FORWARD(Args, args)...)
|
2013-05-21 10:56:41 +00:00
|
|
|
{}
|
|
|
|
template<
|
|
|
|
typename U, typename... Args,
|
2013-07-01 07:57:56 +00:00
|
|
|
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 value_holder(sprout::in_place_t, std::initializer_list<U> il, Args&&... args)
|
2014-02-22 07:32:51 +00:00
|
|
|
: holder_(il, SPROUT_FORWARD(Args, args)...)
|
2013-05-21 10:56:41 +00:00
|
|
|
{}
|
2012-10-24 11:52:09 +00:00
|
|
|
|
2013-10-29 10:15:52 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR value_holder& operator=(value_holder const& rhs) {
|
|
|
|
value_holder temp(rhs);
|
|
|
|
temp.swap(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
SPROUT_CXX14_CONSTEXPR value_holder& operator=(value_holder&& rhs) {
|
|
|
|
value_holder temp(sprout::move(rhs));
|
|
|
|
temp.swap(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
2013-10-15 05:01:34 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR value_holder& operator=(argument_type p) {
|
2013-06-15 13:14:24 +00:00
|
|
|
value_holder temp(p);
|
|
|
|
temp.swap(*this);
|
2013-05-18 12:37:54 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2013-10-15 05:01:34 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR value_holder& operator=(movable_argument_type p) {
|
2013-06-15 13:14:24 +00:00
|
|
|
value_holder temp(sprout::move(p));
|
|
|
|
temp.swap(*this);
|
2012-10-24 11:52:09 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-10-15 05:01:34 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR void swap(value_holder& other)
|
2014-08-14 13:55:49 +00:00
|
|
|
SPROUT_NOEXCEPT_IF_EXPR(sprout::swap(holder_, other.holder_))
|
2012-10-24 11:52:09 +00:00
|
|
|
{
|
|
|
|
sprout::swap(holder_, other.holder_);
|
|
|
|
}
|
|
|
|
|
2013-10-15 05:01:34 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR operator reference() {
|
2012-10-24 11:52:09 +00:00
|
|
|
return helper_type::ref(holder_);
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR operator mutable_or_const_reference() const {
|
|
|
|
return helper_type::ref(holder_);
|
|
|
|
}
|
2013-10-15 05:01:34 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR reference operator*() {
|
2012-10-24 11:52:09 +00:00
|
|
|
return helper_type::ref(holder_);
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR mutable_or_const_reference operator*() const {
|
|
|
|
return helper_type::ref(holder_);
|
|
|
|
}
|
2013-10-15 05:01:34 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR reference get() {
|
2012-10-24 11:52:09 +00:00
|
|
|
return helper_type::ref(holder_);
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR mutable_or_const_reference get() const {
|
|
|
|
return helper_type::ref(holder_);
|
|
|
|
}
|
2013-10-15 05:01:34 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR reference value() {
|
2013-06-04 14:15:05 +00:00
|
|
|
return get();
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR mutable_or_const_reference value() const {
|
|
|
|
return get();
|
|
|
|
}
|
2012-10-24 11:52:09 +00:00
|
|
|
|
2013-10-15 05:01:34 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR pointer operator->() SPROUT_NOEXCEPT {
|
2012-10-24 11:52:09 +00:00
|
|
|
return helper_type::ptr(holder_);
|
|
|
|
}
|
2012-10-26 08:38:48 +00:00
|
|
|
SPROUT_CONSTEXPR mutable_or_const_pointer operator->() const SPROUT_NOEXCEPT {
|
2012-10-24 11:52:09 +00:00
|
|
|
return helper_type::ptr(holder_);
|
|
|
|
}
|
2013-10-15 05:01:34 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR pointer get_pointer() SPROUT_NOEXCEPT {
|
2012-10-24 11:52:09 +00:00
|
|
|
return helper_type::ptr(holder_);
|
|
|
|
}
|
2012-10-26 08:38:48 +00:00
|
|
|
SPROUT_CONSTEXPR mutable_or_const_pointer get_pointer() const SPROUT_NOEXCEPT {
|
2012-10-24 11:52:09 +00:00
|
|
|
return helper_type::ptr(holder_);
|
|
|
|
}
|
2013-10-15 05:01:34 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR pointer get_ptr() SPROUT_NOEXCEPT {
|
2012-10-24 11:52:09 +00:00
|
|
|
return get_pointer();
|
|
|
|
}
|
2012-10-26 08:38:48 +00:00
|
|
|
SPROUT_CONSTEXPR mutable_or_const_pointer get_ptr() const SPROUT_NOEXCEPT {
|
2012-10-24 11:52:09 +00:00
|
|
|
return get_pointer();
|
|
|
|
}
|
2013-02-04 01:24:23 +00:00
|
|
|
SPROUT_CONSTEXPR bool is_initialized() const SPROUT_NOEXCEPT {
|
|
|
|
return !!get_pointer();
|
|
|
|
}
|
2012-10-24 11:52:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// swap
|
|
|
|
//
|
|
|
|
template<typename T>
|
2013-10-15 05:01:34 +00:00
|
|
|
inline SPROUT_CXX14_CONSTEXPR void
|
2012-10-24 11:52:09 +00:00
|
|
|
swap(sprout::value_holder<T>& lhs, sprout::value_holder<T>& rhs)
|
2014-08-14 13:55:49 +00:00
|
|
|
SPROUT_NOEXCEPT_IF_EXPR(lhs.swap(rhs))
|
2012-10-24 11:52:09 +00:00
|
|
|
{
|
|
|
|
lhs.swap(rhs);
|
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
2013-03-22 05:24:19 +00:00
|
|
|
#endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_VALUE_HOLDER_HPP
|