1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/optional/optional.hpp

212 lines
5.4 KiB
C++
Raw Normal View History

2012-10-22 14:10:11 +00:00
#ifndef SPROUT_OPTIONAL_OPTIONAL_HPP
#define SPROUT_OPTIONAL_OPTIONAL_HPP
#include <sprout/config.hpp>
2013-02-07 14:12:57 +00:00
#include <sprout/utility/value_holder/value_holder.hpp>
2012-10-22 14:10:11 +00:00
#include <sprout/utility/swap.hpp>
#include <sprout/none.hpp>
2012-10-26 08:38:48 +00:00
#include <sprout/optional/nullopt.hpp>
2013-03-31 01:09:02 +00:00
#include <sprout/optional/exceptions.hpp>
2013-03-18 10:12:21 +00:00
#include <sprout/assert.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)
2013-03-31 01:09:02 +00:00
, val(v.is_initialized() ? holder_type(*v) : holder_type())
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())
2013-03-31 01:09:02 +00:00
, val(v.is_initialized() ? holder_type(*v) : holder_type())
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 {
2013-03-31 01:09:02 +00:00
return (SPROUT_ASSERT(is_initialized()), true) ? val.get()
: val.get()
;
2012-10-27 06:37:39 +00:00
}
reference_type operator*() {
2013-03-19 07:32:48 +00:00
return (SPROUT_ASSERT(is_initialized()), true) ? val.get()
2013-03-18 10:12:21 +00:00
: val.get()
2012-10-22 14:10:11 +00:00
;
}
2013-03-31 01:09:02 +00:00
SPROUT_CONSTEXPR reference_const_type get() const {
return is_initialized() ? val.get()
: (throw sprout::bad_optional_access("optional<>: bad optional access"), val.get())
;
}
2012-10-22 14:10:11 +00:00
reference_type get() {
2013-03-31 01:09:02 +00:00
return is_initialized() ? val.get()
: (throw sprout::bad_optional_access("optional<>: bad optional access"), 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
;
}
2013-03-31 01:09:02 +00:00
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);
}
2012-10-22 14:10:11 +00:00
SPROUT_CONSTEXPR pointer_const_type operator->() const {
2013-03-18 10:12:21 +00:00
return SPROUT_ASSERT(is_initialized()),
val.get_pointer()
2012-10-22 14:10:11 +00:00
;
}
pointer_type operator->() {
2013-03-18 10:12:21 +00:00
return SPROUT_ASSERT(is_initialized()),
val.get_pointer()
2012-10-22 14:10:11 +00:00
;
}
SPROUT_CONSTEXPR pointer_const_type get_pointer() const {
2013-03-31 01:09:02 +00:00
return is_initialized() ? val.get_pointer()
: 0
2012-10-22 14:10:11 +00:00
;
}
pointer_type get_pointer() {
2013-03-31 01:09:02 +00:00
return is_initialized() ? val.get_pointer()
: 0
2012-10-22 14:10:11 +00:00
;
}
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