From 410a8af026df94e8f1b43faa11aced52fd56ae8a Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Tue, 23 Oct 2012 23:10:52 +0900 Subject: [PATCH] fix optional --- sprout/optional/get.hpp | 4 +- sprout/optional/optional.hpp | 2 +- sprout/utility/value_holder.hpp | 72 +++++++++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 6 deletions(-) diff --git a/sprout/optional/get.hpp b/sprout/optional/get.hpp index b12aa338..8c2078b3 100644 --- a/sprout/optional/get.hpp +++ b/sprout/optional/get.hpp @@ -21,12 +21,12 @@ namespace sprout { template inline SPROUT_CONSTEXPR typename sprout::optional::pointer_const_type get(sprout::optional const* x) { - return x->get_ptr(); + return x->get_pointer(); } template inline typename sprout::optional::pointer_type get(sprout::optional* x) { - return x->get_ptr(); + return x->get_pointer(); } // diff --git a/sprout/optional/optional.hpp b/sprout/optional/optional.hpp index cf6fe65b..4b6af3d6 100644 --- a/sprout/optional/optional.hpp +++ b/sprout/optional/optional.hpp @@ -108,6 +108,7 @@ namespace sprout { void swap(optional& other) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout::swap(storage_, other.storage_))) { + sprout::swap(initialized_, other.initialized_); sprout::swap(storage_, other.storage_); } @@ -180,7 +181,6 @@ namespace sprout { { lhs.swap(rhs); } - } // namespace sprout #endif // #ifndef SPROUT_OPTIONAL_OPTIONAL_HPP diff --git a/sprout/utility/value_holder.hpp b/sprout/utility/value_holder.hpp index 8a1620c9..822df021 100644 --- a/sprout/utility/value_holder.hpp +++ b/sprout/utility/value_holder.hpp @@ -2,6 +2,7 @@ #define SPROUT_UTILITY_VALUE_HOLDER_HPP #include +#include namespace sprout { namespace detail { @@ -130,29 +131,45 @@ namespace sprout { typedef typename helper_type::mutable_or_const_pointer mutable_or_const_pointer; typedef typename helper_type::param_type param_type; typedef reference reference_type; - typedef const_reference reference_const_type; + typedef mutable_or_const_reference reference_const_type; typedef pointer pointer_type; - typedef const_pointer pointer_const_type; + typedef mutable_or_const_pointer pointer_const_type; typedef param_type argument_type; private: holder_type holder_; public: value_holder() = default; + value_holder(value_holder const&) = default; explicit SPROUT_CONSTEXPR value_holder(param_type p) : holder_(helper_type::hold(p)) {} + + void swap(value_holder& other) + SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout::swap(holder_, other.holder_))) + { + sprout::swap(holder_, other.holder_); + } + operator reference() { return helper_type::ref(holder_); } - SPROUT_CONSTEXPR operator const_reference() const { + SPROUT_CONSTEXPR operator mutable_or_const_reference() const { return helper_type::ref(holder_); } + pointer operator->() { + return helper_type::ptr(holder_); + } + SPROUT_CONSTEXPR mutable_or_const_pointer operator->() const { + return helper_type::ptr(holder_); + } + reference get() { return helper_type::ref(holder_); } SPROUT_CONSTEXPR mutable_or_const_reference get() const { return helper_type::ref(holder_); } + pointer get_pointer() { return helper_type::ptr(holder_); } @@ -166,6 +183,55 @@ namespace sprout { return get_pointer(); } }; + + // + // swap + // + template + inline void + swap(sprout::value_holder& lhs, sprout::value_holder& rhs) + SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs))) + { + lhs.swap(rhs); + } + + // + // get + // + template + inline SPROUT_CONSTEXPR typename sprout::value_holder::reference_const_type + get(sprout::value_holder const& x) { + return x.get(); + } + template + inline typename sprout::value_holder::reference_type + get(sprout::value_holder& x) { + return x.get(); + } + template + inline SPROUT_CONSTEXPR typename sprout::value_holder::pointer_const_type + get(sprout::value_holder const* x) { + return x->get_pointer(); + } + template + inline typename sprout::value_holder::pointer_type + get(sprout::value_holder* x) { + return x->get_pointer(); + } + + // + // get_pointer + // + template + inline SPROUT_CONSTEXPR typename sprout::value_holder::pointer_const_type + get_pointer(sprout::value_holder const& x) { + return x.get_pointer(); + } + template + inline typename sprout::value_holder::pointer_type + get_pointer(sprout::value_holder& x) { + return x.get_pointer(); + } } // namespace sprout #endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_HPP