// Copyright (C) 2003, 2008 Fernando Luis Cacciola Carballal.
-// Copyright (C) 2014 - 2017 Andrzej Krzemienski.
+// Copyright (C) 2014 - 2018 Andrzej Krzemienski.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
#include <boost/move/utility.hpp>
#include <boost/none.hpp>
#include <boost/utility/compare_pointees.hpp>
+#include <boost/utility/result_of.hpp>
#include <boost/optional/optional_fwd.hpp>
#include <boost/optional/detail/optional_config.hpp>
#include <boost/optional/detail/optional_factory_support.hpp>
#include <boost/optional/detail/optional_aligned_storage.hpp>
+namespace boost { namespace optional_detail {
+
+template <typename T>
+struct optional_value_type
+{
+};
+
+template <typename T>
+struct optional_value_type< ::boost::optional<T> >
+{
+ typedef T type;
+};
+
+}} // namespace boost::optional_detail
+
#ifdef BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL
#include <boost/optional/detail/old_optional_implementation.hpp>
#else
namespace boost {
-
+
namespace optional_ns {
-
+
// a tag for in-place initialization of contained value
struct in_place_init_t
{
struct init_tag{};
explicit in_place_init_t(init_tag){}
-};
+};
const in_place_init_t in_place_init ((in_place_init_t::init_tag()));
// a tag for conditional in-place initialization of contained value
explicit in_place_init_if_t(init_tag){}
};
const in_place_init_if_t in_place_init_if ((in_place_init_if_t::init_tag()));
-
+
} // namespace optional_ns
using optional_ns::in_place_init_t;
namespace optional_detail {
-struct optional_tag {} ;
+struct init_value_tag {};
+
+struct optional_tag {};
template<class T>
// Creates an optional<T> initialized with 'val'.
// Can throw if T::T(T const&) does
- optional_base ( argument_type val )
+ optional_base ( init_value_tag, argument_type val )
:
m_initialized(false)
{
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
// move-construct an optional<T> initialized from an rvalue-ref to 'val'.
// Can throw if T::T(T&&) does
- optional_base ( rval_reference_type val )
+ optional_base ( init_value_tag, rval_reference_type val )
:
m_initialized(false)
{
construct(rhs.get_impl());
}
}
-
+
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
// Assigns from another optional<T> (deep-moves the rhs value)
void assign ( optional_base&& rhs )
construct(boost::move(rhs.get_impl()));
}
}
-#endif
+#endif
// Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
template<class U>
#else
assign_value( static_cast<value_type>(rhs.get()) );
#endif
-
+
else destroy();
}
else
}
}
#endif
-
+
// Assigns from a T (deep-copies the rhs value)
void assign ( argument_type val )
{
assign_value(val);
else construct(val);
}
-
+
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
// Assigns from a T (deep-moves the rhs value)
void assign ( rval_reference_type val )
public :
- // **DEPPRECATED** Destroys the current value, if any, leaving this UNINITIALIZED
+ // Destroys the current value, if any, leaving this UNINITIALIZED
// No-throw (assuming T::~T() doesn't)
void reset() BOOST_NOEXCEPT { destroy(); }
pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; }
- bool is_initialized() const { return m_initialized ; }
+ bool is_initialized() const BOOST_NOEXCEPT { return m_initialized ; }
protected :
::new (m_storage.address()) value_type(val) ;
m_initialized = true ;
}
-
+
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
void construct ( rval_reference_type val )
{
destroy();
construct(in_place_init, boost::forward<Args>(args)...);
}
-
+
template<class... Args>
explicit optional_base ( in_place_init_t, Args&&... args )
:
{
construct(in_place_init, boost::forward<Args>(args)...);
}
-
+
template<class... Args>
explicit optional_base ( in_place_init_if_t, bool cond, Args&&... args )
:
::new (m_storage.address()) value_type( boost::forward<Arg>(arg) );
m_initialized = true ;
}
-
+
void construct ( in_place_init_t )
{
::new (m_storage.address()) value_type();
m_initialized = true ;
}
-
+
template<class Arg>
void emplace_assign ( Arg&& arg )
{
destroy();
construct(in_place_init, boost::forward<Arg>(arg)) ;
}
-
+
void emplace_assign ()
{
destroy();
construct(in_place_init) ;
}
-
+
template<class Arg>
explicit optional_base ( in_place_init_t, Arg&& arg )
:
{
construct(in_place_init, boost::forward<Arg>(arg));
}
-
+
explicit optional_base ( in_place_init_t )
:
m_initialized(false)
{
construct(in_place_init);
}
-
+
template<class Arg>
explicit optional_base ( in_place_init_if_t, bool cond, Arg&& arg )
:
if ( cond )
construct(in_place_init, boost::forward<Arg>(arg));
}
-
+
explicit optional_base ( in_place_init_if_t, bool cond )
:
m_initialized(false)
}
#else
-
+
template<class Arg>
void construct ( in_place_init_t, const Arg& arg )
{
::new (m_storage.address()) value_type( arg );
m_initialized = true ;
}
-
+
template<class Arg>
void construct ( in_place_init_t, Arg& arg )
{
::new (m_storage.address()) value_type( arg );
m_initialized = true ;
}
-
+
void construct ( in_place_init_t )
{
::new (m_storage.address()) value_type();
destroy();
construct(in_place_init, arg);
}
-
+
template<class Arg>
void emplace_assign ( Arg& arg )
{
destroy();
construct(in_place_init, arg);
}
-
+
void emplace_assign ()
{
destroy();
construct(in_place_init);
}
-
+
template<class Arg>
explicit optional_base ( in_place_init_t, const Arg& arg )
: m_initialized(false)
{
construct(in_place_init, arg);
}
-
+
explicit optional_base ( in_place_init_t )
: m_initialized(false)
{
construct(in_place_init);
}
-
+
template<class Arg>
explicit optional_base ( in_place_init_if_t, bool cond, const Arg& arg )
: m_initialized(false)
if ( cond )
construct(in_place_init, arg);
}
-
+
template<class Arg>
explicit optional_base ( in_place_init_if_t, bool cond, Arg& arg )
: m_initialized(false)
{
if ( cond )
construct(in_place_init, arg);
- }
-
+ }
+
explicit optional_base ( in_place_init_if_t, bool cond )
: m_initialized(false)
{
storage_type m_storage ;
} ;
-
-
#include <boost/optional/detail/optional_trivially_copyable_base.hpp>
// definition of metafunciton is_optional_val_init_candidate
{};
#if !defined(BOOST_OPTIONAL_DETAIL_NO_IS_CONSTRUCTIBLE_TRAIT)
-
+
template <typename T, typename U>
struct is_convertible_to_T_or_factory
: boost::conditional< boost::is_base_of<boost::in_place_factory_base, BOOST_DEDUCED_TYPENAME boost::decay<U>::type>::value
// Creates an optional<T> initialized with 'val'.
// Can throw if T::T(T const&) does
- optional ( argument_type val ) : base(val) {}
+ optional ( argument_type val ) : base(optional_detail::init_value_tag(), val) {}
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
// Creates an optional<T> initialized with 'move(val)'.
// Can throw if T::T(T &&) does
- optional ( rval_reference_type val ) : base( boost::forward<T>(val) )
+ optional ( rval_reference_type val ) : base(optional_detail::init_value_tag(), boost::forward<T>(val))
{}
#endif
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
/// Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
// Can throw if T::T(T &&) does
- optional ( bool cond, rval_reference_type val ) : base( cond, boost::forward<T>(val) )
+ optional ( bool cond, rval_reference_type val ) : base( cond, boost::forward<T>(val) )
{}
#endif
)
:
base()
- {
+ {
if ( rhs.is_initialized() )
this->construct(rhs.get());
}
-
+
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
// Creates a deep move of another convertible optional<U>
// Requires a valid conversion from U to T.
template<class Expr>
- explicit optional ( Expr&& expr,
- BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_val_init_candidate<T, Expr>, bool>::type = true
- )
- : base(boost::forward<Expr>(expr),boost::addressof(expr))
+ explicit optional ( Expr&& expr,
+ BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_val_init_candidate<T, Expr>, bool>::type = true
+ )
+ : base(boost::forward<Expr>(expr),boost::addressof(expr))
{}
#else
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
template<class Expr>
- BOOST_DEDUCED_TYPENAME boost::enable_if<optional_detail::is_optional_val_init_candidate<T, Expr>, optional&>::type
+ BOOST_DEDUCED_TYPENAME boost::enable_if<optional_detail::is_optional_val_init_candidate<T, Expr>, optional&>::type
operator= ( Expr&& expr )
{
this->assign_expr(boost::forward<Expr>(expr),boost::addressof(expr));
this->assign(rhs);
return *this ;
}
-
+
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
// Move-assigns from another convertible optional<U> (converts && deep-moves the rhs value)
// Requires a valid conversion from U to T.
#ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
optional& operator= ( optional && ) = default;
#else
- optional& operator= ( optional && rhs )
+ optional& operator= ( optional && rhs )
BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
{
this->assign( static_cast<base &&>(rhs) ) ;
return *this ;
}
-#endif
-
+#endif
+
#endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
#ifndef BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
this->assign( boost::forward<T_>(val) ) ;
return *this ;
}
-
+
#else
// Assigns from a T (deep-copies the rhs value)
this->assign( val ) ;
return *this ;
}
-
+
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
// Assigns from a T (deep-moves the rhs value)
optional& operator= ( rval_reference_type val )
return *this ;
}
#endif
-
+
#endif // BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
-
+
// Assigns from a "none"
// Which destroys the current value, if any, leaving this UNINITIALIZED
// No-throw (assuming T::~T() doesn't)
this->assign( none_ ) ;
return *this ;
}
-
+
#if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
// Constructs in-place
// upon exception *this is always uninitialized
{
this->emplace_assign( boost::forward<Args>(args)... );
}
-
+
template<class... Args>
explicit optional ( in_place_init_t, Args&&... args )
: base( in_place_init, boost::forward<Args>(args)... )
{}
-
+
template<class... Args>
explicit optional ( in_place_init_if_t, bool cond, Args&&... args )
: base( in_place_init_if, cond, boost::forward<Args>(args)... )
{}
-
+
#elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
template<class Arg>
void emplace ( Arg&& arg )
{
this->emplace_assign( boost::forward<Arg>(arg) );
}
-
+
void emplace ()
{
this->emplace_assign();
}
-
+
template<class Args>
explicit optional ( in_place_init_t, Args&& args )
: base( in_place_init, boost::forward<Args>(args) )
explicit optional ( in_place_init_t )
: base( in_place_init )
{}
-
+
template<class Args>
explicit optional ( in_place_init_if_t, bool cond, Args&& args )
: base( in_place_init_if, cond, boost::forward<Args>(args) )
{}
-
+
explicit optional ( in_place_init_if_t, bool cond )
: base( in_place_init_if, cond )
{}
{
this->emplace_assign( arg );
}
-
+
template<class Arg>
void emplace ( Arg& arg )
{
this->emplace_assign( arg );
}
-
+
void emplace ()
{
this->emplace_assign();
}
-
+
template<class Arg>
explicit optional ( in_place_init_t, const Arg& arg )
: base( in_place_init, arg )
{}
-
+
template<class Arg>
explicit optional ( in_place_init_t, Arg& arg )
: base( in_place_init, arg )
explicit optional ( in_place_init_t )
: base( in_place_init )
{}
-
+
template<class Arg>
explicit optional ( in_place_init_if_t, bool cond, const Arg& arg )
: base( in_place_init_if, cond, arg )
{}
-
+
template<class Arg>
explicit optional ( in_place_init_if_t, bool cond, Arg& arg )
: base( in_place_init_if, cond, arg )
- {}
-
+ {}
+
explicit optional ( in_place_init_if_t, bool cond )
: base( in_place_init_if, cond )
{}
// Returns a reference to the value if this is initialized, otherwise,
// the behaviour is UNDEFINED
// No-throw
-#if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
+#if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
reference_const_type operator *() const& { return this->get() ; }
reference_type operator *() & { return this->get() ; }
reference_type_of_temporary_wrapper operator *() && { return boost::move(this->get()) ; }
reference_type operator *() { return this->get() ; }
#endif // !defined BOOST_NO_CXX11_REF_QUALIFIERS
-#if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
+#if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
reference_const_type value() const&
- {
+ {
if (this->is_initialized())
return this->get() ;
else
throw_exception(bad_optional_access());
}
-
+
reference_type value() &
- {
+ {
if (this->is_initialized())
return this->get() ;
else
throw_exception(bad_optional_access());
}
-
+
reference_type_of_temporary_wrapper value() &&
- {
+ {
if (this->is_initialized())
return boost::move(this->get()) ;
else
throw_exception(bad_optional_access());
}
-#else
+#else
reference_const_type value() const
- {
+ {
if (this->is_initialized())
return this->get() ;
else
throw_exception(bad_optional_access());
}
-
+
reference_type value()
- {
+ {
if (this->is_initialized())
return this->get() ;
else
#ifndef BOOST_NO_CXX11_REF_QUALIFIERS
template <class U>
value_type value_or ( U&& v ) const&
- {
+ {
if (this->is_initialized())
return get();
else
return boost::forward<U>(v);
}
-
+
template <class U>
- value_type value_or ( U&& v ) &&
- {
+ value_type value_or ( U&& v ) &&
+ {
if (this->is_initialized())
return boost::move(get());
else
}
#elif !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
template <class U>
- value_type value_or ( U&& v ) const
+ value_type value_or ( U&& v ) const
{
if (this->is_initialized())
return get();
}
#else
template <class U>
- value_type value_or ( U const& v ) const
- {
+ value_type value_or ( U const& v ) const
+ {
if (this->is_initialized())
return get();
else
return v;
}
-
+
template <class U>
- value_type value_or ( U& v ) const
- {
+ value_type value_or ( U& v ) const
+ {
if (this->is_initialized())
return get();
else
#endif
-#ifndef BOOST_NO_CXX11_REF_QUALIFIERS
+#if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
template <typename F>
value_type value_or_eval ( F f ) const&
{
else
return f();
}
-
+
template <typename F>
value_type value_or_eval ( F f ) &&
{
else
return f();
}
+
+ template <typename F>
+ optional<typename boost::result_of<F(reference_type)>::type> map(F f) &
+ {
+ if (this->has_value())
+ return f(get());
+ else
+ return none;
+ }
+
+ template <typename F>
+ optional<typename boost::result_of<F(reference_const_type)>::type> map(F f) const&
+ {
+ if (this->has_value())
+ return f(get());
+ else
+ return none;
+ }
+
+ template <typename F>
+ optional<typename boost::result_of<F(reference_type_of_temporary_wrapper)>::type> map(F f) &&
+ {
+ if (this->has_value())
+ return f(boost::move(this->get()));
+ else
+ return none;
+ }
+
+ template <typename F>
+ optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_type)>::type>::type> flat_map(F f) &
+ {
+ if (this->has_value())
+ return f(get());
+ else
+ return none;
+ }
+
+ template <typename F>
+ optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_const_type)>::type>::type> flat_map(F f) const&
+ {
+ if (this->has_value())
+ return f(get());
+ else
+ return none;
+ }
+
+ template <typename F>
+ optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_type_of_temporary_wrapper)>::type>::type> flat_map(F f) &&
+ {
+ if (this->has_value())
+ return f(boost::move(get()));
+ else
+ return none;
+ }
+
#else
template <typename F>
value_type value_or_eval ( F f ) const
else
return f();
}
+
+ template <typename F>
+ optional<typename boost::result_of<F(reference_type)>::type> map(F f)
+ {
+ if (this->has_value())
+ return f(get());
+ else
+ return none;
+ }
+
+ template <typename F>
+ optional<typename boost::result_of<F(reference_const_type)>::type> map(F f) const
+ {
+ if (this->has_value())
+ return f(get());
+ else
+ return none;
+ }
+
+ template <typename F>
+ optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_type)>::type>::type> flat_map(F f)
+ {
+ if (this->has_value())
+ return f(get());
+ else
+ return none;
+ }
+
+ template <typename F>
+ optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_const_type)>::type>::type> flat_map(F f) const
+ {
+ if (this->has_value())
+ return f(get());
+ else
+ return none;
+ }
+
#endif
-
+
+ bool has_value() const BOOST_NOEXCEPT { return this->is_initialized() ; }
+
bool operator!() const BOOST_NOEXCEPT { return !this->is_initialized() ; }
-
+
BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
} ;
#endif // BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL
namespace boost {
-
+
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
template<class T>
class optional<T&&>
} // namespace boost
namespace boost {
-
+
// The following declaration prevents a bug where operator safe-bool is used upon streaming optional object if you forget the IO header.
template<class CharType, class CharTrait>
std::basic_ostream<CharType, CharTrait>&
operator<<(std::basic_ostream<CharType, CharTrait>& os, optional_detail::optional_tag const&)
{
BOOST_STATIC_ASSERT_MSG(sizeof(CharType) == 0, "If you want to output boost::optional, include header <boost/optional/optional_io.hpp>");
- return os;
+ return os;
}
} // namespace boost