fix optional

This commit is contained in:
bolero-MURAKAMI 2013-03-31 10:09:02 +09:00
parent a1c119a9bb
commit 6a78f64feb
9 changed files with 233 additions and 67 deletions

View file

@ -9,5 +9,6 @@
#include <sprout/optional/make_optional.hpp>
#include <sprout/optional/get.hpp>
#include <sprout/optional/hash.hpp>
#include <sprout/optional/exceptions.hpp>
#endif // #ifndef SPROUT_OPTIONAL_HPP

View file

@ -0,0 +1,25 @@
#ifndef SPROUT_OPTIONAL_EXCEPTIONS_HPP
#define SPROUT_OPTIONAL_EXCEPTIONS_HPP
#include <string>
#include <stdexcept>
#include <sprout/config.hpp>
namespace sprout {
//
// bad_optional_access
//
class bad_optional_access
: public std::logic_error
{
public:
explicit bad_optional_access(std::string const& what_arg)
: std::logic_error(what_arg)
{}
explicit bad_optional_access(char const* what_arg)
: std::logic_error(what_arg)
{}
};
} // namespace sprout
#endif // SPROUT_OPTIONAL_EXCEPTIONS_HPP

View file

@ -6,6 +6,7 @@
#include <sprout/utility/swap.hpp>
#include <sprout/none.hpp>
#include <sprout/optional/nullopt.hpp>
#include <sprout/optional/exceptions.hpp>
#include <sprout/assert.hpp>
namespace sprout {
@ -53,12 +54,12 @@ namespace sprout {
{}
SPROUT_CONSTEXPR optional(optional const& v)
: init(v.init)
, val(v.val)
, val(v.is_initialized() ? holder_type(*v) : holder_type())
{}
template<typename U>
explicit SPROUT_CONSTEXPR optional(optional<U> const& v)
: init(v.is_initialized())
, val(v.is_initialized() ? v.get() : holder_type())
, val(v.is_initialized() ? holder_type(*v) : holder_type())
{}
optional& operator=(sprout::nullopt_t v) SPROUT_NOEXCEPT {
@ -114,21 +115,25 @@ namespace sprout {
}
SPROUT_CONSTEXPR reference_const_type operator*() const {
return get();
}
reference_type operator*() {
return get();
}
SPROUT_CONSTEXPR reference_const_type get() const {
return (SPROUT_ASSERT(is_initialized()), true) ? val.get()
: val.get()
;
}
reference_type get() {
reference_type operator*() {
return (SPROUT_ASSERT(is_initialized()), true) ? val.get()
: val.get()
;
}
SPROUT_CONSTEXPR reference_const_type get() const {
return is_initialized() ? val.get()
: (throw sprout::bad_optional_access("optional<>: bad optional access"), val.get())
;
}
reference_type get() {
return is_initialized() ? val.get()
: (throw sprout::bad_optional_access("optional<>: bad optional access"), val.get())
;
}
SPROUT_CONSTEXPR reference_const_type get_value_or(reference_const_type& v) const {
return is_initialized() ? val.get()
: v
@ -140,6 +145,19 @@ namespace sprout {
;
}
SPROUT_CONSTEXPR reference_const_type value() const {
return get();
}
reference_type value() {
return get();
}
SPROUT_CONSTEXPR reference_const_type value_or(reference_const_type& v) const {
return get_value_or(v);
}
reference_type value_or(reference_type& v) {
return get_value_or(v);
}
SPROUT_CONSTEXPR pointer_const_type operator->() const {
return SPROUT_ASSERT(is_initialized()),
val.get_pointer()
@ -151,13 +169,13 @@ namespace sprout {
;
}
SPROUT_CONSTEXPR pointer_const_type get_pointer() const {
return SPROUT_ASSERT(is_initialized()),
val.get_pointer()
return is_initialized() ? val.get_pointer()
: 0
;
}
pointer_type get_pointer() {
return SPROUT_ASSERT(is_initialized()),
val.get_pointer()
return is_initialized() ? val.get_pointer()
: 0
;
}
SPROUT_CONSTEXPR pointer_const_type get_ptr() const {

View file

@ -1,5 +1,5 @@
#ifndef SPROUT_TUPLE_TUPLE_FREXIBLY_CONSTRUCT_HPP
#define SPROUT_TUPLE_TUPLE_FREXIBLY_CONSTRUCT_HPP
#ifndef SPROUT_TUPLE_FREXIBLY_CONSTRUCT_HPP
#define SPROUT_TUPLE_FREXIBLY_CONSTRUCT_HPP
#include <sprout/config.hpp>
@ -17,4 +17,4 @@ namespace sprout {
using sprout::tuples::flexibly_construct;
} // namespace sprout
#endif // #ifndef SPROUT_TUPLE_TUPLE_FREXIBLY_CONSTRUCT_HPP
#endif // #ifndef SPROUT_TUPLE_FREXIBLY_CONSTRUCT_HPP

View file

@ -11,6 +11,6 @@
#include <sprout/tuple/tuple/ignore.hpp>
#include <sprout/tuple/tuple/make_tuple.hpp>
#include <sprout/tuple/tuple/type_traits.hpp>
#include <sprout/tuple/tuple/flexibly_construct.hpp>
#include <sprout/tuple/flexibly_construct.hpp>
#endif // #ifndef SPROUT_TUPLE_TUPLE_HPP

View file

@ -11,7 +11,7 @@
#include <sprout/type_traits/is_convert_constructible.hpp>
#include <sprout/tpp/algorithm/all_of.hpp>
#include <sprout/tuple/tuple/tuple_decl.hpp>
#include <sprout/tuple/tuple/flexibly_construct.hpp>
#include <sprout/tuple/flexibly_construct.hpp>
namespace sprout {
namespace tuples {
@ -71,6 +71,7 @@ namespace sprout {
inline SPROUT_CONSTEXPR sprout::tuples::tuple<Types...>::tuple(sprout::pair<UTypes...>&& t)
: impl_type(sprout::forward<typename sprout::pair<UTypes...>::first_type>(t.first), sprout::forward<typename sprout::pair<UTypes...>::second_type>(t.second))
{}
template<typename... Types>
template<
typename... UTypes,

View file

@ -15,7 +15,7 @@
#include <sprout/type_traits/is_convert_constructible.hpp>
#include <sprout/tpp/algorithm/all_of.hpp>
#include <sprout/tuple/tuple/tuple_fwd.hpp>
#include <sprout/tuple/tuple/flexibly_construct.hpp>
#include <sprout/tuple/flexibly_construct.hpp>
namespace sprout {
namespace tuples {
@ -245,9 +245,9 @@ namespace sprout {
typedef sprout::tuples::detail::tuple_impl<0, Types...> impl_type;
private:
template<typename TndexTuple, typename... Utypes>
struct is_flexibly_constructible_impl;
struct is_flexibly_convert_constructible_impl;
template<sprout::index_t... Indexes, typename... Utypes>
struct is_flexibly_constructible_impl<sprout::index_tuple<Indexes...>, Utypes...>
struct is_flexibly_convert_constructible_impl<sprout::index_tuple<Indexes...>, Utypes...>
: public sprout::tpp::all_of<
sprout::is_convert_constructible<
typename sprout::tppack_at<Indexes, Types...>::type,
@ -262,19 +262,19 @@ namespace sprout {
>
{};
template<typename... UTypes>
struct is_flexibly_constructible
: public is_flexibly_constructible_impl<
struct is_flexibly_convert_constructible
: public is_flexibly_convert_constructible_impl<
typename sprout::index_range<0, (sizeof...(UTypes) < sizeof...(Types) ? sizeof...(UTypes) : sizeof...(Types))>::type,
UTypes...
>
{};
template<typename... UTypes>
struct is_rvref_flexibly_constructible
: public is_flexibly_constructible<UTypes&&...>
struct is_rvref_flexibly_convert_constructible
: public is_flexibly_convert_constructible<UTypes&&...>
{};
template<typename... UTypes>
struct is_clvref_flexibly_constructible
: public is_flexibly_constructible<UTypes const&...>
struct is_clvref_flexibly_convert_constructible
: public is_flexibly_convert_constructible<UTypes const&...>
{};
public:
// tuple construction
@ -317,38 +317,39 @@ namespace sprout {
>::type
>
SPROUT_CONSTEXPR tuple(sprout::pair<UTypes...>&& t);
template<
typename... UTypes,
typename = typename std::enable_if<
is_rvref_flexibly_constructible<UTypes...>::value
is_rvref_flexibly_convert_constructible<UTypes...>::value
>::type
>
explicit SPROUT_CONSTEXPR tuple(sprout::tuples::flexibly_construct_t, UTypes&&... elements);
template<
typename... UTypes,
typename = typename std::enable_if<
is_clvref_flexibly_constructible<UTypes...>::value
is_clvref_flexibly_convert_constructible<UTypes...>::value
>::type
>
SPROUT_CONSTEXPR tuple(sprout::tuples::flexibly_construct_t, sprout::tuples::tuple<UTypes...> const& t);
template<
typename... UTypes,
typename = typename std::enable_if<
is_rvref_flexibly_constructible<UTypes...>::value
is_rvref_flexibly_convert_constructible<UTypes...>::value
>::type
>
SPROUT_CONSTEXPR tuple(sprout::tuples::flexibly_construct_t, sprout::tuples::tuple<UTypes...>&& t);
template<
typename... UTypes,
typename = typename std::enable_if<
is_clvref_flexibly_constructible<UTypes...>::value
is_clvref_flexibly_convert_constructible<UTypes...>::value
>::type
>
SPROUT_CONSTEXPR tuple(sprout::tuples::flexibly_construct_t, sprout::pair<UTypes...> const& t);
template<
typename... UTypes,
typename = typename std::enable_if<
is_rvref_flexibly_constructible<UTypes...>::value
is_rvref_flexibly_convert_constructible<UTypes...>::value
>::type
>
SPROUT_CONSTEXPR tuple(sprout::tuples::flexibly_construct_t, sprout::pair<UTypes...>&& t);

View file

@ -1,11 +1,11 @@
#ifndef SPROUT_UTILITY_PAIR_PAIR_HPP
#define SPROUT_UTILITY_PAIR_PAIR_HPP
#include <utility>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/index_tuple.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/utility/move.hpp>
#include <sprout/utility/swap.hpp>
#include <sprout/utility/pair/pair_decl.hpp>
#include <sprout/tuple/tuple/tuple.hpp>
@ -41,29 +41,41 @@ namespace sprout {
, second(y)
{}
template <typename T1, typename T2>
template<typename U, typename V>
template<
typename U, typename V,
typename
>
inline SPROUT_CONSTEXPR sprout::pair<T1, T2>::pair(U&& x, V&& y)
: first(sprout::forward<U>(x))
, second(sprout::forward<V>(y))
{}
template <typename T1, typename T2>
template<typename U, typename V>
template<
typename U, typename V,
typename
>
inline SPROUT_CONSTEXPR sprout::pair<T1, T2>::pair(sprout::pair<U, V> const& other)
: first(other.first)
, second(other.second)
{}
template <typename T1, typename T2>
template<typename U, typename V>
template<
typename U, typename V,
typename
>
inline SPROUT_CONSTEXPR sprout::pair<T1, T2>::pair(sprout::pair<U, V>&& other)
: first(sprout::move(other.first))
, second(sprout::move(other.second))
: first(sprout::forward<U>(other.first))
, second(sprout::forward<V>(other.second))
{}
#if SPROUT_USE_DELEGATING_CONSTRUCTORS
template <typename T1, typename T2>
template <typename... Args1, typename... Args2>
template<
typename... Args1, typename... Args2,
typename
>
inline SPROUT_CONSTEXPR sprout::pair<T1, T2>::pair(
sprout::tuples::tuple<Args1...> first_args,
sprout::tuples::tuple<Args2...> second_args
std::piecewise_construct_t,
sprout::tuples::tuple<Args1...> first_args, sprout::tuples::tuple<Args2...> second_args
)
: pair(
first_args,
@ -74,29 +86,75 @@ namespace sprout {
{}
#endif // #if SPROUT_USE_DELEGATING_CONSTRUCTORS
template <typename T1, typename T2>
inline sprout::pair<T1, T2>& sprout::pair<T1, T2>::operator=(pair const& other) = default;
template<
typename U, typename V,
typename
>
inline SPROUT_CONSTEXPR sprout::pair<T1, T2>::pair(sprout::tuples::tuple<U, V> const& other)
: first(sprout::tuples::get<0>(other))
, second(sprout::tuples::get<1>(other))
{}
template <typename T1, typename T2>
template<typename U, typename V>
inline sprout::pair<T1, T2>& sprout::pair<T1, T2>::operator=(sprout::pair<U, V> const& other) {
first = other.first;
second = other.second;
return *this;
}
template<
typename U, typename V,
typename
>
inline SPROUT_CONSTEXPR sprout::pair<T1, T2>::pair(sprout::tuples::tuple<U, V>&& other)
: first(sprout::forward<U>(sprout::tuples::get<0>(other)))
, second(sprout::forward<V>(sprout::tuples::get<1>(other)))
{}
template <typename T1, typename T2>
inline sprout::pair<T1, T2>& sprout::pair<T1, T2>::operator=(pair&& other)
inline sprout::pair<T1, T2>& sprout::pair<T1, T2>::operator=(pair const& rhs) = default;
template <typename T1, typename T2>
inline sprout::pair<T1, T2>& sprout::pair<T1, T2>::operator=(pair&& rhs)
SPROUT_NOEXCEPT_EXPR(std::is_nothrow_move_assignable<T1>::value && std::is_nothrow_move_assignable<T2>::value)
{
first = sprout::move(other.first);
second = sprout::move(other.second);
first = sprout::forward<T1>(rhs.first);
second = sprout::forward<T2>(rhs.second);
return *this;
}
template <typename T1, typename T2>
template<typename U, typename V>
inline sprout::pair<T1, T2>& sprout::pair<T1, T2>::operator=(sprout::pair<U, V>&& other) {
first = sprout::move(other.first);
second = sprout::move(other.second);
template<
typename U, typename V,
typename
>
inline sprout::pair<T1, T2>& sprout::pair<T1, T2>::operator=(sprout::pair<U, V> const& rhs) {
first = rhs.first;
second = rhs.second;
return *this;
}
template <typename T1, typename T2>
template<
typename U, typename V,
typename
>
inline sprout::pair<T1, T2>& sprout::pair<T1, T2>::operator=(sprout::pair<U, V>&& rhs) {
first = sprout::forward<U>(rhs.first);
second = sprout::forward<V>(rhs.second);
return *this;
}
template <typename T1, typename T2>
template<
typename U, typename V,
typename
>
inline sprout::pair<T1, T2>& sprout::pair<T1, T2>::operator=(sprout::tuples::tuple<U, V> const& rhs) {
first = sprout::tuples::get<0>(rhs);
second = sprout::tuples::get<0>(rhs);
return *this;
}
template <typename T1, typename T2>
template<
typename U, typename V,
typename
>
inline sprout::pair<T1, T2>& sprout::pair<T1, T2>::operator=(sprout::tuples::tuple<U, V>&& rhs) {
first = sprout::forward<U>(sprout::tuples::get<0>(rhs));
second = sprout::forward<V>(sprout::tuples::get<1>(rhs));
return *this;
}
template <typename T1, typename T2>
inline void sprout::pair<T1, T2>::swap(pair& other)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout::swap(first, other.first)) && SPROUT_NOEXCEPT_EXPR(sprout::swap(second, other.second))) {

View file

@ -1,6 +1,7 @@
#ifndef SPROUT_UTILITY_PAIR_PAIR_DECL_HPP
#define SPROUT_UTILITY_PAIR_PAIR_DECL_HPP
#include <utility>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/index_tuple.hpp>
@ -34,24 +35,85 @@ namespace sprout {
SPROUT_CONSTEXPR pair(pair&&);
SPROUT_CONSTEXPR pair();
SPROUT_CONSTEXPR pair(T1 const& x, T2 const& y);
template<typename U, typename V>
template<
typename U, typename V,
typename = typename std::enable_if<
std::is_constructible<first_type, U&&>::value && std::is_constructible<second_type, V&&>::value
>::type
>
SPROUT_CONSTEXPR pair(U&& x, V&& y);
template<typename U, typename V>
template<
typename U, typename V,
typename = typename std::enable_if<
std::is_constructible<first_type, U const&>::value && std::is_constructible<second_type, V const&>::value
>::type
>
SPROUT_CONSTEXPR pair(sprout::pair<U, V> const& other);
template<typename U, typename V>
template<
typename U, typename V,
typename = typename std::enable_if<
std::is_constructible<first_type, U&&>::value && std::is_constructible<second_type, V&&>::value
>::type
>
SPROUT_CONSTEXPR pair(sprout::pair<U, V>&& other);
template <typename... Args1, typename... Args2>
template<
typename... Args1, typename... Args2,
typename = typename std::enable_if<
std::is_constructible<first_type, Args1&&...>::value && std::is_constructible<second_type, Args2&&...>::value
>::type
>
SPROUT_CONSTEXPR pair(
sprout::tuples::tuple<Args1...> first_args,
sprout::tuples::tuple<Args2...> second_args
std::piecewise_construct_t,
sprout::tuples::tuple<Args1...> first_args, sprout::tuples::tuple<Args2...> second_args
);
pair& operator=(pair const& other);
template<typename U, typename V>
pair& operator=(sprout::pair<U, V> const& other);
pair& operator=(pair&& other)
template<
typename U, typename V,
typename = typename std::enable_if<
std::is_constructible<first_type, U const&>::value && std::is_constructible<second_type, V const&>::value
>::type
>
SPROUT_CONSTEXPR pair(sprout::tuples::tuple<U, V> const& other);
template<
typename U, typename V,
typename = typename std::enable_if<
std::is_constructible<first_type, U&&>::value && std::is_constructible<second_type, V&&>::value
>::type
>
SPROUT_CONSTEXPR pair(sprout::tuples::tuple<U, V>&& other);
pair& operator=(pair const& rhs);
pair& operator=(pair&& rhs)
SPROUT_NOEXCEPT_EXPR(std::is_nothrow_move_assignable<T1>::value && std::is_nothrow_move_assignable<T2>::value);
template<typename U, typename V>
pair& operator=(sprout::pair<U, V>&& other);
template<
typename U, typename V,
typename = typename std::enable_if<
std::is_assignable<first_type&, U const&>::value && std::is_assignable<second_type&, V const&>::value
>::type
>
pair& operator=(sprout::pair<U, V> const& rhs);
template<
typename U, typename V,
typename = typename std::enable_if<
std::is_assignable<first_type&, U&&>::value && std::is_assignable<second_type&, V&&>::value
>::type
>
pair& operator=(sprout::pair<U, V>&& rhs);
template<
typename U, typename V,
typename = typename std::enable_if<
std::is_assignable<first_type&, U const&>::value && std::is_assignable<second_type&, V const&>::value
>::type
>
pair& operator=(sprout::tuples::tuple<U, V> const& rhs);
template<
typename U, typename V,
typename = typename std::enable_if<
std::is_assignable<first_type&, U&&>::value && std::is_assignable<second_type&, V&&>::value
>::type
>
pair& operator=(sprout::tuples::tuple<U, V>&& rhs);
void swap(pair& other)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout::swap(first, other.first)) && SPROUT_NOEXCEPT_EXPR(sprout::swap(second, other.second)));
};