1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2025-08-03 12:49:50 +00:00

move support: optional class template

This commit is contained in:
bolero-MURAKAMI 2013-05-18 21:37:54 +09:00
parent d732a73431
commit e6420f2bbd
3 changed files with 130 additions and 71 deletions

View file

@ -4,6 +4,8 @@
#include <sprout/config.hpp>
#include <sprout/utility/value_holder/value_holder.hpp>
#include <sprout/utility/swap.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/utility/move.hpp>
#include <sprout/none.hpp>
#include <sprout/optional/nullopt.hpp>
#include <sprout/optional/exceptions.hpp>
@ -30,6 +32,7 @@ namespace sprout {
typedef typename holder_type::pointer_type pointer_type;
typedef typename holder_type::pointer_const_type pointer_const_type;
typedef typename holder_type::argument_type argument_type;
typedef typename holder_type::movable_argument_type movable_argument_type;
private:
bool init;
holder_type val;
@ -38,43 +41,63 @@ namespace sprout {
init = false;
}
public:
// 20.5.4.1, constructors
// 20.6.4.1, constructors
SPROUT_CONSTEXPR optional() SPROUT_NOEXCEPT
: init(false)
{}
SPROUT_CONSTEXPR optional(sprout::nullopt_t) SPROUT_NOEXCEPT
: init(false)
{}
SPROUT_CONSTEXPR optional(argument_type v)
: init(true)
, val(v)
{}
SPROUT_CONSTEXPR optional(bool cond, argument_type v)
: init(cond)
, val(cond ? holder_type(v) : holder_type())
{}
SPROUT_CONSTEXPR optional(optional const& v)
: init(v.init)
, val(v.is_initialized() ? holder_type(*v) : holder_type())
{}
SPROUT_CONSTEXPR optional(optional&& v)
: init(v.init)
, val(v.is_initialized() ? holder_type(sprout::move(*v)) : holder_type())
{}
SPROUT_CONSTEXPR optional(T const& v)
: init(true)
, val(v)
{}
SPROUT_CONSTEXPR optional(T&& v)
: init(true)
, val(sprout::move(v))
{}
SPROUT_CONSTEXPR optional(bool cond, T const& v)
: init(cond)
, val(cond ? holder_type(v) : holder_type())
{}
SPROUT_CONSTEXPR optional(bool cond, T&& v)
: init(cond)
, val(cond ? holder_type(sprout::move(v)) : holder_type())
{}
template<typename U>
explicit SPROUT_CONSTEXPR optional(optional<U> const& v)
: init(v.is_initialized())
, val(v.is_initialized() ? holder_type(*v) : holder_type())
{}
// 20.5.4.3, assignment
// 20.6.4.3, assignment
optional& operator=(sprout::nullopt_t v) SPROUT_NOEXCEPT {
assign(v);
return *this;
}
optional& operator=(argument_type v) {
assign(v);
return *this;
}
optional& operator=(optional const& v) {
assign(v);
return *this;
}
optional& operator=(optional&& v) {
assign(sprout::forward<optional>(v));
return *this;
}
optional& operator=(T const& v) {
assign(v);
return *this;
}
optional& operator=(T&& v) {
assign(sprout::forward<T>(v));
return *this;
}
template<typename U>
optional& operator=(optional<U> const& v) {
assign(v);
@ -84,14 +107,22 @@ namespace sprout {
void assign(sprout::nullopt_t) SPROUT_NOEXCEPT {
destroy();
}
void assign(argument_type v) {
void assign(optional const& v) {
optional temp(v);
temp.swap(*this);
}
void assign(optional const& v) {
void assign(optional&& v) {
optional temp(sprout::forward<optional>(v));
temp.swap(*this);
}
void assign(T const& v) {
optional temp(v);
temp.swap(*this);
}
void assign(T&& v) {
optional temp(sprout::forward<T>(v));
temp.swap(*this);
}
template<typename U>
void assign(optional<U> const& v) {
optional temp(v);
@ -104,61 +135,20 @@ namespace sprout {
void reset(sprout::nullopt_t v) SPROUT_NOEXCEPT {
assign(v);
}
void reset(argument_type v) {
void reset(T const& v) {
assign(v);
}
// 20.5.4.4, swap
void reset(T&& v) {
assign(sprout::forward<T>(v));
}
// 20.6.4.4, swap
void swap(optional& other)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout::swap(val, other.val)))
{
sprout::swap(init, other.init);
sprout::swap(val, other.val);
}
// 20.5.4.5, observers
SPROUT_CONSTEXPR reference_const_type operator*() const {
return (SPROUT_ASSERT(is_initialized()), true) ? val.get()
: val.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
;
}
reference_type get_value_or(reference_type& v) {
return is_initialized() ? val.get()
: v
;
}
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);
}
// 20.6.4.5, observers
SPROUT_CONSTEXPR pointer_const_type operator->() const {
return SPROUT_ASSERT(is_initialized()),
val.get_pointer()
@ -186,6 +176,50 @@ namespace sprout {
return get_pointer();
}
SPROUT_CONSTEXPR reference_const_type operator*() const {
return (SPROUT_ASSERT(is_initialized()), true) ? val.get()
: val.get()
;
}
reference_type operator*() {
return (SPROUT_ASSERT(is_initialized()), true) ? val.get()
: val.get()
;
}
SPROUT_CONSTEXPR reference_const_type value() const {
return get();
}
reference_type value() {
return 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 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 reference_const_type get_value_or(reference_const_type& v) const {
return is_initialized() ? val.get()
: v
;
}
reference_type get_value_or(reference_type& v) {
return is_initialized() ? val.get()
: v
;
}
SPROUT_CONSTEXPR operator bool() const SPROUT_NOEXCEPT {
return is_initialized();
}