add sprout::optional

This commit is contained in:
bolero-MURAKAMI 2012-10-22 23:10:11 +09:00
parent 9b9956f810
commit 8cb432dee1
15 changed files with 574 additions and 81 deletions

View file

@ -78,4 +78,6 @@
# define SPROUT_CONFIG_SUPPORT_TEMPORARY_CONTAINER_ITERATION # define SPROUT_CONFIG_SUPPORT_TEMPORARY_CONTAINER_ITERATION
#endif // #ifndef SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION #endif // #ifndef SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION
#include <sprout/config/suffix.hpp>
#endif // #ifndef SPROUT_CONFIG_AUTO_CONFIG_HPP #endif // #ifndef SPROUT_CONFIG_AUTO_CONFIG_HPP

View file

@ -47,4 +47,6 @@
# include <sprout/config/compiler/visualc.hpp> # include <sprout/config/compiler/visualc.hpp>
#endif #endif
#include <sprout/config/compiler/cxx11.hpp>
#endif // #ifndef SPROUT_CONFIG_COMPILER_HPP #endif // #ifndef SPROUT_CONFIG_COMPILER_HPP

View file

@ -0,0 +1,20 @@
#ifndef SPROUT_CONFIG_COMPILER_CXX11_HPP
#define SPROUT_CONFIG_COMPILER_CXX11_HPP
#ifdef SPROUT_NO_CONSTEXPR
# define SPROUT_NO_CXX11_CONSTEXPR
#endif
#ifdef SPROUT_NO_NOEXCEPT
# define SPROUT_NO_CXX11_CNOEXCEPT
#endif
#ifdef SPROUT_NO_TEMPLATE_ALIASES
# define SPROUT_NO_CXX11_CTEMPLATE_ALIASES
#endif
#ifdef SPROUT_NO_USER_DEFINED_LITERALS
# define SPROUT_NO_CXX11_CUSER_DEFINED_LITERALS
#endif
#ifdef SPROUT_NO_DELEGATING_CONSTRUCTORS
# define SPROUT_NO_CXX11_CDELEGATING_CONSTRUCTORS
#endif
#endif // #ifndef SPROUT_CONFIG_COMPILER_CXX11_HPP

View file

@ -1,89 +1,11 @@
#ifndef SPROUT_CONFIG_CONFIG_HPP #ifndef SPROUT_CONFIG_CONFIG_HPP
#define SPROUT_CONFIG_CONFIG_HPP #define SPROUT_CONFIG_CONFIG_HPP
#include <sprout/config/compiler.hpp>
#ifndef SPROUT_CONFIG_DISABLE_AUTO_CONFIG #ifndef SPROUT_CONFIG_DISABLE_AUTO_CONFIG
# include <sprout/config/auto_config.hpp> # include <sprout/config/auto_config.hpp>
#endif // #ifndef SPROUT_CONFIG_DISABLE_AUTO_CONFIG #endif // #ifndef SPROUT_CONFIG_DISABLE_AUTO_CONFIG
#ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR #include <sprout/config/compiler.hpp>
# define SPROUT_CONSTEXPR constexpr #include <sprout/config/suffix.hpp>
# define SPROUT_CONSTEXPR_OR_CONST constexpr
# define SPROUT_STATIC_CONSTEXPR static constexpr
#
# define SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_INNER(EXPR) = EXPR
# define SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_OUTER(EXPR)
#else // #ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
# define SPROUT_CONSTEXPR
# define SPROUT_CONSTEXPR_OR_CONST const
# define SPROUT_STATIC_CONSTEXPR static const
#
# define SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_INNER(EXPR)
# define SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_OUTER(EXPR) = EXPR
#endif // #ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
#ifndef SPROUT_CONFIG_DISABLE_NOEXCEPT
# define SPROUT_NOEXCEPT noexcept
# define SPROUT_NOEXCEPT_EXPR(EXPR) noexcept(EXPR)
# define SPROUT_NOEXCEPT_EXPR_OR_DEFAULT(EXPR, C) noexcept(EXPR)
#else // #ifndef SPROUT_CONFIG_DISABLE_NOEXCEPT
# define SPROUT_NOEXCEPT
# define SPROUT_NOEXCEPT_EXPR(EXPR)
# define SPROUT_NOEXCEPT_EXPR_OR_DEFAULT(EXPR, C) C
#endif // #ifndef SPROUT_CONFIG_DISABLE_NOEXCEPT
#ifndef SPROUT_CONFIG_DISABLE_TEMPLATE_ALIASES
# define SPROUT_USE_TEMPLATE_ALIASES 1
#else // #ifndef SPROUT_CONFIG_DISABLE_TEMPLATE_ALIASES
# define SPROUT_USE_TEMPLATE_ALIASES 0
#endif // #ifndef SPROUT_CONFIG_DISABLE_TEMPLATE_ALIASES
#ifndef SPROUT_CONFIG_DISABLE_USER_DEFINED_LITERALS
# define SPROUT_USE_USER_DEFINED_LITERALS 1
#else // #ifndef SPROUT_CONFIG_DISABLE_USER_DEFINED_LITERALS
# define SPROUT_USE_USER_DEFINED_LITERALS 0
#endif // #ifndef SPROUT_CONFIG_DISABLE_USER_DEFINED_LITERALS
#ifndef SPROUT_CONFIG_DISABLE_DELEGATING_CONSTRUCTORS
# define SPROUT_USE_DELEGATING_CONSTRUCTORS 1
#else // #ifndef SPROUT_CONFIG_DISABLE_DELEGATING_CONSTRUCTORS
# define SPROUT_USE_DELEGATING_CONSTRUCTORS 0
#endif // #ifndef SPROUT_CONFIG_DISABLE_DELEGATING_CONSTRUCTORS
#ifndef SPROUT_CONFIG_USE_SSCRISK_CEL
# define HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT <sprout/functional/functor.hpp>
# define HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT <sprout/algorithm/non_modifying.hpp>
# define HDR_NUMERIC_SSCRISK_CEL_OR_SPROUT <sprout/numeric/non_modifying.hpp>
# define HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT <sprout/iterator/operation.hpp>
# define NS_SSCRISK_CEL_OR_SPROUT sprout
#else // #ifndef SPROUT_CONFIG_USE_SSCRISK_CEL
# define HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT <sscrisk/cel/functional.hpp>
# define HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT <sscrisk/cel/algorithm.hpp>
# define HDR_NUMERIC_SSCRISK_CEL_OR_SPROUT <sscrisk/cel/numeric.hpp>
# define HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT <sscrisk/cel/iterator.hpp>
# define NS_SSCRISK_CEL_OR_SPROUT sscrisk::cel
#endif // #ifndef SPROUT_CONFIG_USE_SSCRISK_CEL
#ifndef SPROUT_CONFIG_DISABLE_BUILTIN_CMATH_FUNCTION
# define SPROUT_USE_BUILTIN_CMATH_FUNCTION 1
#else // #ifndef SPROUT_CONFIG_DISABLE_BUILTIN_CMATH_FUNCTION
# define SPROUT_USE_BUILTIN_CMATH_FUNCTION 0
#endif // #ifndef SPROUT_CONFIG_DISABLE_BUILTIN_CMATH_FUNCTION
#ifndef SPROUT_CONFIG_DISABLE_BUILTIN_BIT_OPERATION
# define SPROUT_USE_BUILTIN_BIT_OPERATION 1
#else // #ifndef SPROUT_CONFIG_DISABLE_BUILTIN_BIT_OPERATION
# define SPROUT_USE_BUILTIN_BIT_OPERATION 0
#endif // #ifndef SPROUT_CONFIG_DISABLE_BUILTIN_BIT_OPERATION
#ifndef SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION
# define SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION 1
#else // #ifndef SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION
# ifdef SPROUT_CONFIG_SUPPORT_TEMPORARY_CONTAINER_ITERATION
# error config conflict: SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION, SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION
# endif // #ifndef SPROUT_CONFIG_SUPPORT_TEMPORARY_CONTAINER_ITERATION
# define SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION 0
#endif // #ifndef SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION
#endif // #ifndef SPROUT_CONFIG_CONFIG_HPP #endif // #ifndef SPROUT_CONFIG_CONFIG_HPP

83
sprout/config/suffix.hpp Normal file
View file

@ -0,0 +1,83 @@
#ifndef SPROUT_CONFIG_SUFFIX_HPP
#define SPROUT_CONFIG_SUFFIX_HPP
#ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
# define SPROUT_CONSTEXPR constexpr
# define SPROUT_CONSTEXPR_OR_CONST constexpr
# define SPROUT_STATIC_CONSTEXPR static constexpr
#
# define SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_INNER(EXPR) = EXPR
# define SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_OUTER(EXPR)
#else // #ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
# define SPROUT_CONSTEXPR
# define SPROUT_CONSTEXPR_OR_CONST const
# define SPROUT_STATIC_CONSTEXPR static const
#
# define SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_INNER(EXPR)
# define SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_OUTER(EXPR) = EXPR
#endif // #ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
#ifndef SPROUT_CONFIG_DISABLE_NOEXCEPT
# define SPROUT_NOEXCEPT noexcept
# define SPROUT_NOEXCEPT_EXPR(EXPR) noexcept(EXPR)
# define SPROUT_NOEXCEPT_EXPR_OR_DEFAULT(EXPR, C) noexcept(EXPR)
#else // #ifndef SPROUT_CONFIG_DISABLE_NOEXCEPT
# define SPROUT_NOEXCEPT
# define SPROUT_NOEXCEPT_EXPR(EXPR)
# define SPROUT_NOEXCEPT_EXPR_OR_DEFAULT(EXPR, C) C
#endif // #ifndef SPROUT_CONFIG_DISABLE_NOEXCEPT
#ifndef SPROUT_CONFIG_DISABLE_TEMPLATE_ALIASES
# define SPROUT_USE_TEMPLATE_ALIASES 1
#else // #ifndef SPROUT_CONFIG_DISABLE_TEMPLATE_ALIASES
# define SPROUT_USE_TEMPLATE_ALIASES 0
#endif // #ifndef SPROUT_CONFIG_DISABLE_TEMPLATE_ALIASES
#ifndef SPROUT_CONFIG_DISABLE_USER_DEFINED_LITERALS
# define SPROUT_USE_USER_DEFINED_LITERALS 1
#else // #ifndef SPROUT_CONFIG_DISABLE_USER_DEFINED_LITERALS
# define SPROUT_USE_USER_DEFINED_LITERALS 0
#endif // #ifndef SPROUT_CONFIG_DISABLE_USER_DEFINED_LITERALS
#ifndef SPROUT_CONFIG_DISABLE_DELEGATING_CONSTRUCTORS
# define SPROUT_USE_DELEGATING_CONSTRUCTORS 1
#else // #ifndef SPROUT_CONFIG_DISABLE_DELEGATING_CONSTRUCTORS
# define SPROUT_USE_DELEGATING_CONSTRUCTORS 0
#endif // #ifndef SPROUT_CONFIG_DISABLE_DELEGATING_CONSTRUCTORS
#ifndef SPROUT_CONFIG_USE_SSCRISK_CEL
# define HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT <sprout/functional/functor.hpp>
# define HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT <sprout/algorithm/non_modifying.hpp>
# define HDR_NUMERIC_SSCRISK_CEL_OR_SPROUT <sprout/numeric/non_modifying.hpp>
# define HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT <sprout/iterator/operation.hpp>
# define NS_SSCRISK_CEL_OR_SPROUT sprout
#else // #ifndef SPROUT_CONFIG_USE_SSCRISK_CEL
# define HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT <sscrisk/cel/functional.hpp>
# define HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT <sscrisk/cel/algorithm.hpp>
# define HDR_NUMERIC_SSCRISK_CEL_OR_SPROUT <sscrisk/cel/numeric.hpp>
# define HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT <sscrisk/cel/iterator.hpp>
# define NS_SSCRISK_CEL_OR_SPROUT sscrisk::cel
#endif // #ifndef SPROUT_CONFIG_USE_SSCRISK_CEL
#ifndef SPROUT_CONFIG_DISABLE_BUILTIN_CMATH_FUNCTION
# define SPROUT_USE_BUILTIN_CMATH_FUNCTION 1
#else // #ifndef SPROUT_CONFIG_DISABLE_BUILTIN_CMATH_FUNCTION
# define SPROUT_USE_BUILTIN_CMATH_FUNCTION 0
#endif // #ifndef SPROUT_CONFIG_DISABLE_BUILTIN_CMATH_FUNCTION
#ifndef SPROUT_CONFIG_DISABLE_BUILTIN_BIT_OPERATION
# define SPROUT_USE_BUILTIN_BIT_OPERATION 1
#else // #ifndef SPROUT_CONFIG_DISABLE_BUILTIN_BIT_OPERATION
# define SPROUT_USE_BUILTIN_BIT_OPERATION 0
#endif // #ifndef SPROUT_CONFIG_DISABLE_BUILTIN_BIT_OPERATION
#ifndef SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION
# define SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION 1
#else // #ifndef SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION
# ifdef SPROUT_CONFIG_SUPPORT_TEMPORARY_CONTAINER_ITERATION
# error config conflict: SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION, SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION
# endif // #ifndef SPROUT_CONFIG_SUPPORT_TEMPORARY_CONTAINER_ITERATION
# define SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION 0
#endif // #ifndef SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION
#endif // #ifndef SPROUT_CONFIG_SUFFIX_HPP

17
sprout/none.hpp Normal file
View file

@ -0,0 +1,17 @@
#ifndef SPROUT_NONE_HPP
#define SPROUT_NONE_HPP
#include <sprout/config.hpp>
namespace sprout {
//
// none_t
// none
//
struct none_t {};
namespace {
SPROUT_STATIC_CONSTEXPR none_t none{};
} // anonymous-namespace
} // namespace sprout
#endif // #ifndef SPROUT_NONE_HPP

11
sprout/optional.hpp Normal file
View file

@ -0,0 +1,11 @@
#ifndef SPROUT_OPTIONAL_HPP
#define SPROUT_OPTIONAL_HPP
#include <sprout/config.hpp>
#include <sprout/optional/optional.hpp>
#include <sprout/optional/comparison.hpp>
#include <sprout/optional/io.hpp>
#include <sprout/optional/make_optional.hpp>
#include <sprout/optional/get.hpp>
#endif // #ifndef SPROUT_OPTIONAL_HPP

View file

@ -0,0 +1,49 @@
#ifndef SPROUT_OPTIONAL_COMPARISON_HPP
#define SPROUT_OPTIONAL_COMPARISON_HPP
#include <sprout/config.hpp>
#include <sprout/optional/optional.hpp>
#include <sprout/utility/compare_pointees.hpp>
namespace sprout {
//
// operator==
// operator!=
// operator<
// operator>
// operator<=
// operator>=
//
template<typename T>
inline SPROUT_CONSTEXPR bool
operator==(sprout::optional<T> const& lhs, sprout::optional<T> const& rhs) {
return sprout::equal_pointees(lhs, rhs);
}
template<typename T>
inline SPROUT_CONSTEXPR bool
operator!=(sprout::optional<T> const& lhs, sprout::optional<T> const& rhs) {
return !(lhs == rhs);
}
template<typename T>
inline SPROUT_CONSTEXPR bool
operator<(sprout::optional<T> const& lhs, sprout::optional<T> const& rhs) {
return sprout::less_pointees(lhs, rhs);
}
template<typename T>
inline SPROUT_CONSTEXPR bool
operator>(sprout::optional<T> const& lhs, sprout::optional<T> const& rhs) {
return rhs < lhs;
}
template<typename T>
inline SPROUT_CONSTEXPR bool
operator<=(sprout::optional<T> const& lhs, sprout::optional<T> const& rhs) {
return !(rhs < lhs);
}
template<typename T>
inline SPROUT_CONSTEXPR bool
operator>=(sprout::optional<T> const& lhs, sprout::optional<T> const& rhs) {
return !(lhs < rhs);
}
} // namespace sprout
#endif // #ifndef SPROUT_OPTIONAL_COMPARISON_HPP

61
sprout/optional/get.hpp Normal file
View file

@ -0,0 +1,61 @@
#ifndef SPROUT_OPTIONAL_GET_HPP
#define SPROUT_OPTIONAL_GET_HPP
#include <sprout/config.hpp>
#include <sprout/optional/optional.hpp>
namespace sprout {
//
// get
//
template<typename T>
inline SPROUT_CONSTEXPR typename sprout::optional<T>::reference_const_type
get(sprout::optional<T> const& x) {
return x.get();
}
template<typename T>
inline typename sprout::optional<T>::reference_type
get(sprout::optional<T>& x) {
return x.get();
}
template<typename T>
inline SPROUT_CONSTEXPR typename sprout::optional<T>::pointer_const_type
get(sprout::optional<T> const* x) {
return x->get_ptr();
}
template<typename T>
inline typename sprout::optional<T>::pointer_type
get(sprout::optional<T>* x) {
return x->get_ptr();
}
//
// get_pointer
//
template<typename T>
inline SPROUT_CONSTEXPR typename sprout::optional<T>::pointer_const_type
get_pointer(sprout::optional<T> const& x) {
return x.get_pointer();
}
template<typename T>
inline typename sprout::optional<T>::pointer_type
get_pointer(sprout::optional<T>& x) {
return x.get_pointer();
}
//
// get_optional_value_or
//
template<typename T>
inline SPROUT_CONSTEXPR typename sprout::optional<T>::reference_const_type
get_optional_value_or(sprout::optional<T> const& x, typename sprout::optional<T>::reference_const_type v) {
return x.get_value_or(v);
}
template<typename T>
inline typename sprout::optional<T>::reference_type
get_optional_value_or(sprout::optional<T>& x, typename sprout::optional<T>::reference_type v) {
return x.get_value_or(v);
}
} // namespace sprout
#endif // #ifndef SPROUT_OPTIONAL_GET_HPP

53
sprout/optional/io.hpp Normal file
View file

@ -0,0 +1,53 @@
#ifndef SPROUT_OPTIONAL_IO_HPP
#define SPROUT_OPTIONAL_IO_HPP
#include <iosfwd>
#include <ios>
#include <sprout/config.hpp>
#include <sprout/optional/optional.hpp>
#include <sprout/none.hpp>
namespace sprout {
//
// operator>>
//
template<typename Elem, typename Traits, typename T>
inline std::basic_istream<Elem, Traits>&
operator<<(std::basic_istream<Elem, Traits>& lhs, sprout::optional<T>& rhs) {
if (lhs.good()) {
int d = lhs.get();
if (d == ' ') {
T x;
lhs >> x;
rhs = x;
} else {
if (d == '-') {
d = lhs.get();
if (d == '-') {
rhs = sprout::none;
return lhs;
}
}
lhs.setstate(std::ios::failbit);
}
}
return lhs;
}
//
// operator<<
//
template<typename Elem, typename Traits, typename T>
inline std::basic_ostream<Elem, Traits>&
operator<<(std::basic_ostream<Elem, Traits>& lhs, sprout::optional<T> const& rhs) {
if (lhs.good()) {
if (!rhs) {
lhs << "--";
} else {
lhs << ' ' << *rhs;
}
}
return lhs;
}
} // namespace sprout
#endif // #ifndef SPROUT_OPTIONAL_IO_HPP

View file

@ -0,0 +1,23 @@
#ifndef SPROUT_OPTIONAL_MAKE_OPTIONAL_HPP
#define SPROUT_OPTIONAL_MAKE_OPTIONAL_HPP
#include <sprout/config.hpp>
#include <sprout/optional/optional.hpp>
namespace sprout {
//
// make_optional
//
template<typename T>
inline SPROUT_CONSTEXPR sprout::optional<T>
make_optional(T const& v) {
return optional<T>(v);
}
template<typename T>
inline SPROUT_CONSTEXPR sprout::optional<T>
make_optional(bool cond, T const& v) {
return optional<T>(cond, v);
}
} // namespace sprout
#endif // #ifndef SPROUT_OPTIONAL_MAKE_OPTIONAL_HPP

View file

@ -0,0 +1,187 @@
#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>
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:
bool initialized_;
holder_type storage_;
private:
void destroy() {
initialized_ = false;
}
public:
SPROUT_CONSTEXPR optional()
: initialized_(false)
{}
SPROUT_CONSTEXPR optional(sprout::none_t)
: initialized_(false)
{}
SPROUT_CONSTEXPR optional(argument_type v)
: initialized_(true)
, storage_(v)
{}
SPROUT_CONSTEXPR optional(bool cond, argument_type v)
: initialized_(cond)
, storage_(cond ? holder_type(v) : holder_type())
{}
SPROUT_CONSTEXPR optional(optional const& v)
: initialized_(v.initialized_)
, storage_(v.storage_)
{}
template<class U>
explicit SPROUT_CONSTEXPR optional(optional<U> const& v)
: initialized_(v.initialized_)
, storage_(v.storage_.get())
{}
optional& operator=(sprout::none_t v) {
assign(v);
return *this;
}
optional& operator=(argument_type v) {
assign(v);
return *this;
}
optional& operator=(optional const& v) {
assign(v);
return *this;
}
template<class U>
optional& operator=(optional<U> const& v) {
assign(v);
return *this;
}
void assign(sprout::none_t) {
destroy();
}
void assign(argument_type v) {
optional temp(v);
temp.swap(v);
}
void assign(optional const& v) {
optional temp(v);
temp.swap(v);
}
template<class U>
void assign(optional<U> const& v) {
optional temp(v);
temp.swap(v);
}
void reset() {
destroy();
}
void reset(sprout::none_t v) {
assign(v);
}
void reset(argument_type v) {
assign(v);
}
void swap(optional& other)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout::swap(storage_, other.storage_)))
{
sprout::swap(storage_, other.storage_);
}
SPROUT_CONSTEXPR reference_const_type get() const {
return is_initialized() ? storage_.get()
: throw std::domain_error("optional: value not initialized")
;
}
reference_type get() {
return is_initialized() ? storage_.get()
: throw std::domain_error("optional: value not initialized")
;
}
SPROUT_CONSTEXPR reference_const_type get_value_or(reference_const_type& v) const {
return is_initialized() ? storage_.get()
: v
;
}
reference_type get_value_or(reference_type& v) {
return is_initialized() ? storage_.get()
: v
;
}
SPROUT_CONSTEXPR pointer_const_type operator->() const {
return is_initialized() ? storage_.get_pointer()
: throw std::domain_error("optional: value not initialized")
;
}
pointer_type operator->() {
return is_initialized() ? storage_.get_pointer()
: throw std::domain_error("optional: value not initialized")
;
}
SPROUT_CONSTEXPR pointer_const_type get_pointer() const {
return is_initialized() ? storage_.get_pointer()
: throw std::domain_error("optional: value not initialized")
;
}
pointer_type get_pointer() {
return is_initialized() ? storage_.get_pointer()
: 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();
}
SPROUT_CONSTEXPR operator bool() const {
return is_initialized();
}
SPROUT_CONSTEXPR bool operator!() const {
return !is_initialized();
}
SPROUT_CONSTEXPR bool is_initialized() const {
return initialized_;
}
};
//
// 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
#include <boost/none.hpp>

View file

@ -6,8 +6,9 @@
#include <sprout/utility/rel_ops.hpp> #include <sprout/utility/rel_ops.hpp>
#include <sprout/utility/operation.hpp> #include <sprout/utility/operation.hpp>
#include <sprout/utility/operation_ext.hpp> #include <sprout/utility/operation_ext.hpp>
#include <sprout/utility/enabler_if.hpp> #include <sprout/utility/compare_pointees.hpp>
#include <sprout/utility/noncopyable.hpp> #include <sprout/utility/noncopyable.hpp>
#include <sprout/utility/enabler_if.hpp>
#include <sprout/utility/pack.hpp> #include <sprout/utility/pack.hpp>
#include <sprout/utility/value_holder.hpp> #include <sprout/utility/value_holder.hpp>

View file

@ -0,0 +1,51 @@
#ifndef SPROUT_UTILITY_COMPARE_POINTEES_HPP
#define SPROUT_UTILITY_COMPARE_POINTEES_HPP
#include <sprout/config.hpp>
#include <sprout/functional/base.hpp>
namespace sprout {
//
// equal_pointees
//
template<class OptionalPointee>
inline SPROUT_CONSTEXPR bool
equal_pointees(OptionalPointee const& lhs, OptionalPointee const& rhs) {
return (!lhs) != (!rhs) ? false : (!lhs ? true : (*lhs) == (*rhs));
}
//
// less_pointees
//
template<class OptionalPointee>
inline SPROUT_CONSTEXPR bool
less_pointees(OptionalPointee const& lhs, OptionalPointee const& rhs) {
return !rhs ? false : (!lhs ? true : (*lhs) < (*rhs));
}
//
// equal_pointees_t
//
template<class OptionalPointee>
struct equal_pointees_t
: public sprout::binary_function<OptionalPointee, OptionalPointee, bool>
{
SPROUT_CONSTEXPR bool
operator()(OptionalPointee const& lhs, OptionalPointee const& rhs) const {
return sprout::equal_pointees(lhs, rhs) ;
}
} ;
//
// less_pointees_t
//
template<class OptionalPointee>
struct less_pointees_t
: public sprout::binary_function<OptionalPointee, OptionalPointee, bool>
{
SPROUT_CONSTEXPR bool
operator()(OptionalPointee const& lhs, OptionalPointee const& rhs) const {
return sprout::less_pointees(lhs, rhs) ;
}
} ;
} // namespace sprout
#endif // #ifndef SPROUT_UTILITY_COMPARE_POINTEES_HPP

View file

@ -129,6 +129,11 @@ namespace sprout {
typedef typename helper_type::const_pointer const_pointer; typedef typename helper_type::const_pointer const_pointer;
typedef typename helper_type::mutable_or_const_pointer mutable_or_const_pointer; typedef typename helper_type::mutable_or_const_pointer mutable_or_const_pointer;
typedef typename helper_type::param_type param_type; typedef typename helper_type::param_type param_type;
typedef reference reference_type;
typedef const_reference reference_const_type;
typedef pointer pointer_type;
typedef const_pointer pointer_const_type;
typedef param_type argument_type;
private: private:
holder_type holder_; holder_type holder_;
public: public:
@ -154,6 +159,12 @@ namespace sprout {
SPROUT_CONSTEXPR mutable_or_const_pointer get_pointer() const { SPROUT_CONSTEXPR mutable_or_const_pointer get_pointer() const {
return helper_type::ptr(holder_); return helper_type::ptr(holder_);
} }
pointer get_ptr() {
return get_pointer();
}
SPROUT_CONSTEXPR mutable_or_const_pointer get_ptr() const {
return get_pointer();
}
}; };
} // namespace sprout } // namespace sprout