2012-10-22 14:10:11 +00:00
|
|
|
#ifndef SPROUT_OPTIONAL_OPTIONAL_HPP
|
|
|
|
#define SPROUT_OPTIONAL_OPTIONAL_HPP
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/utility/value_holder.hpp>
|
|
|
|
#include <sprout/utility/swap.hpp>
|
|
|
|
#include <sprout/none.hpp>
|
2012-10-26 08:38:48 +00:00
|
|
|
#include <sprout/optional/nullopt.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::reference reference;
|
|
|
|
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::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;
|
|
|
|
typedef typename holder_type::argument_type argument_type;
|
|
|
|
private:
|
2012-10-26 08:38:48 +00:00
|
|
|
bool init;
|
|
|
|
holder_type val;
|
2012-10-22 14:10:11 +00:00
|
|
|
private:
|
2012-10-26 08:38:48 +00:00
|
|
|
void destroy() SPROUT_NOEXCEPT {
|
|
|
|
init = false;
|
2012-10-22 14:10:11 +00:00
|
|
|
}
|
|
|
|
public:
|
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
|
|
|
{}
|
|
|
|
SPROUT_CONSTEXPR optional(argument_type v)
|
2012-10-26 08:38:48 +00:00
|
|
|
: init(true)
|
|
|
|
, val(v)
|
2012-10-22 14:10:11 +00:00
|
|
|
{}
|
|
|
|
SPROUT_CONSTEXPR optional(bool cond, argument_type 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
|
|
|
{}
|
|
|
|
SPROUT_CONSTEXPR optional(optional const& v)
|
2012-10-26 08:38:48 +00:00
|
|
|
: init(v.init)
|
|
|
|
, val(v.val)
|
2012-10-22 14:10:11 +00:00
|
|
|
{}
|
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())
|
|
|
|
, val(v.get())
|
2012-10-22 14:10:11 +00:00
|
|
|
{}
|
|
|
|
|
2012-10-26 08:38:48 +00:00
|
|
|
optional& operator=(sprout::nullopt_t v) SPROUT_NOEXCEPT {
|
2012-10-22 14:10:11 +00:00
|
|
|
assign(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
optional& operator=(argument_type v) {
|
|
|
|
assign(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
optional& operator=(optional const& v) {
|
|
|
|
assign(v);
|
|
|
|
return *this;
|
|
|
|
}
|
2012-10-27 06:37:39 +00:00
|
|
|
template<typename U>
|
2012-10-22 14:10:11 +00:00
|
|
|
optional& operator=(optional<U> const& v) {
|
|
|
|
assign(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-10-26 08:38:48 +00:00
|
|
|
void assign(sprout::nullopt_t) SPROUT_NOEXCEPT {
|
2012-10-22 14:10:11 +00:00
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
void assign(argument_type v) {
|
|
|
|
optional temp(v);
|
2012-10-27 06:37:39 +00:00
|
|
|
temp.swap(*this);
|
2012-10-22 14:10:11 +00:00
|
|
|
}
|
|
|
|
void assign(optional const& v) {
|
|
|
|
optional temp(v);
|
2012-10-27 06:37:39 +00:00
|
|
|
temp.swap(*this);
|
2012-10-22 14:10:11 +00:00
|
|
|
}
|
2012-10-27 06:37:39 +00:00
|
|
|
template<typename U>
|
2012-10-22 14:10:11 +00:00
|
|
|
void assign(optional<U> const& v) {
|
|
|
|
optional temp(v);
|
2012-10-27 06:37:39 +00:00
|
|
|
temp.swap(*this);
|
2012-10-22 14:10:11 +00:00
|
|
|
}
|
|
|
|
|
2012-10-26 08:38:48 +00:00
|
|
|
void reset() SPROUT_NOEXCEPT {
|
2012-10-22 14:10:11 +00:00
|
|
|
destroy();
|
|
|
|
}
|
2012-10-26 08:38:48 +00:00
|
|
|
void reset(sprout::nullopt_t v) SPROUT_NOEXCEPT {
|
2012-10-22 14:10:11 +00:00
|
|
|
assign(v);
|
|
|
|
}
|
|
|
|
void reset(argument_type v) {
|
|
|
|
assign(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-10-27 06:37:39 +00:00
|
|
|
SPROUT_CONSTEXPR reference_const_type operator*() const {
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
reference_type operator*() {
|
|
|
|
return get();
|
|
|
|
}
|
2012-10-22 14:10:11 +00:00
|
|
|
SPROUT_CONSTEXPR reference_const_type get() const {
|
2012-10-26 08:38:48 +00:00
|
|
|
return is_initialized() ? val.get()
|
2012-10-27 06:37:39 +00:00
|
|
|
: (throw std::domain_error("optional: value not initialized"), val.get())
|
2012-10-22 14:10:11 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
reference_type get() {
|
2012-10-26 08:38:48 +00:00
|
|
|
return is_initialized() ? val.get()
|
2012-10-27 06:37:39 +00:00
|
|
|
: (throw std::domain_error("optional: value not initialized"), val.get())
|
2012-10-22 14:10:11 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR reference_const_type get_value_or(reference_const_type& v) const {
|
2012-10-26 08:38:48 +00:00
|
|
|
return is_initialized() ? val.get()
|
2012-10-22 14:10:11 +00:00
|
|
|
: v
|
|
|
|
;
|
|
|
|
}
|
|
|
|
reference_type get_value_or(reference_type& v) {
|
2012-10-26 08:38:48 +00:00
|
|
|
return is_initialized() ? val.get()
|
2012-10-22 14:10:11 +00:00
|
|
|
: v
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
SPROUT_CONSTEXPR pointer_const_type operator->() const {
|
2012-10-26 08:38:48 +00:00
|
|
|
return is_initialized() ? val.get_pointer()
|
2012-10-22 14:10:11 +00:00
|
|
|
: throw std::domain_error("optional: value not initialized")
|
|
|
|
;
|
|
|
|
}
|
|
|
|
pointer_type operator->() {
|
2012-10-26 08:38:48 +00:00
|
|
|
return is_initialized() ? val.get_pointer()
|
2012-10-22 14:10:11 +00:00
|
|
|
: throw std::domain_error("optional: value not initialized")
|
|
|
|
;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR pointer_const_type get_pointer() const {
|
2012-10-26 08:38:48 +00:00
|
|
|
return is_initialized() ? val.get_pointer()
|
2012-10-22 14:10:11 +00:00
|
|
|
: throw std::domain_error("optional: value not initialized")
|
|
|
|
;
|
|
|
|
}
|
|
|
|
pointer_type get_pointer() {
|
2012-10-26 08:38:48 +00:00
|
|
|
return is_initialized() ? val.get_pointer()
|
2012-10-22 14:10:11 +00:00
|
|
|
: throw std::domain_error("optional: value not initialized")
|
|
|
|
;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR pointer_const_type get_ptr() const {
|
|
|
|
return get_pointer();
|
|
|
|
}
|
|
|
|
pointer_type get_ptr() {
|
|
|
|
return get_pointer();
|
|
|
|
}
|
|
|
|
|
2012-10-26 08:38:48 +00:00
|
|
|
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>
|
|
|
|
inline void
|
|
|
|
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
|