fixed old implementation (issue #57)

This commit is contained in:
Andrzej Krzemienski
2018-07-12 00:30:47 +02:00
parent d0b87d2f35
commit 228b20df82
3 changed files with 339 additions and 111 deletions

View File

@ -60,19 +60,34 @@
#include <boost/optional/detail/optional_factory_support.hpp> #include <boost/optional/detail/optional_factory_support.hpp>
#include <boost/optional/detail/optional_aligned_storage.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 #ifdef BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL
#include <boost/optional/detail/old_optional_implementation.hpp> #include <boost/optional/detail/old_optional_implementation.hpp>
#else #else
namespace boost { namespace boost {
namespace optional_ns { namespace optional_ns {
// a tag for in-place initialization of contained value // a tag for in-place initialization of contained value
struct in_place_init_t struct in_place_init_t
{ {
struct init_tag{}; struct init_tag{};
explicit in_place_init_t(init_tag){} explicit in_place_init_t(init_tag){}
}; };
const in_place_init_t in_place_init ((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 // a tag for conditional in-place initialization of contained value
@ -82,7 +97,7 @@ struct in_place_init_if_t
explicit in_place_init_if_t(init_tag){} 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())); const in_place_init_if_t in_place_init_if ((in_place_init_if_t::init_tag()));
} // namespace optional_ns } // namespace optional_ns
using optional_ns::in_place_init_t; using optional_ns::in_place_init_t;
@ -252,7 +267,7 @@ class optional_base : public optional_tag
construct(rhs.get_impl()); construct(rhs.get_impl());
} }
} }
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
// Assigns from another optional<T> (deep-moves the rhs value) // Assigns from another optional<T> (deep-moves the rhs value)
void assign ( optional_base&& rhs ) void assign ( optional_base&& rhs )
@ -269,7 +284,7 @@ class optional_base : public optional_tag
construct(boost::move(rhs.get_impl())); construct(boost::move(rhs.get_impl()));
} }
} }
#endif #endif
// Assigns from another _convertible_ optional<U> (deep-copies the rhs value) // Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
template<class U> template<class U>
@ -283,7 +298,7 @@ class optional_base : public optional_tag
#else #else
assign_value( static_cast<value_type>(rhs.get()) ); assign_value( static_cast<value_type>(rhs.get()) );
#endif #endif
else destroy(); else destroy();
} }
else else
@ -316,7 +331,7 @@ class optional_base : public optional_tag
} }
} }
#endif #endif
// Assigns from a T (deep-copies the rhs value) // Assigns from a T (deep-copies the rhs value)
void assign ( argument_type val ) void assign ( argument_type val )
{ {
@ -324,7 +339,7 @@ class optional_base : public optional_tag
assign_value(val); assign_value(val);
else construct(val); else construct(val);
} }
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
// Assigns from a T (deep-moves the rhs value) // Assigns from a T (deep-moves the rhs value)
void assign ( rval_reference_type val ) void assign ( rval_reference_type val )
@ -385,7 +400,7 @@ class optional_base : public optional_tag
::new (m_storage.address()) value_type(val) ; ::new (m_storage.address()) value_type(val) ;
m_initialized = true ; m_initialized = true ;
} }
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
void construct ( rval_reference_type val ) void construct ( rval_reference_type val )
{ {
@ -411,7 +426,7 @@ class optional_base : public optional_tag
destroy(); destroy();
construct(in_place_init, boost::forward<Args>(args)...); construct(in_place_init, boost::forward<Args>(args)...);
} }
template<class... Args> template<class... Args>
explicit optional_base ( in_place_init_t, Args&&... args ) explicit optional_base ( in_place_init_t, Args&&... args )
: :
@ -419,7 +434,7 @@ class optional_base : public optional_tag
{ {
construct(in_place_init, boost::forward<Args>(args)...); construct(in_place_init, boost::forward<Args>(args)...);
} }
template<class... Args> template<class... Args>
explicit optional_base ( in_place_init_if_t, bool cond, Args&&... args ) explicit optional_base ( in_place_init_if_t, bool cond, Args&&... args )
: :
@ -435,26 +450,26 @@ class optional_base : public optional_tag
::new (m_storage.address()) value_type( boost::forward<Arg>(arg) ); ::new (m_storage.address()) value_type( boost::forward<Arg>(arg) );
m_initialized = true ; m_initialized = true ;
} }
void construct ( in_place_init_t ) void construct ( in_place_init_t )
{ {
::new (m_storage.address()) value_type(); ::new (m_storage.address()) value_type();
m_initialized = true ; m_initialized = true ;
} }
template<class Arg> template<class Arg>
void emplace_assign ( Arg&& arg ) void emplace_assign ( Arg&& arg )
{ {
destroy(); destroy();
construct(in_place_init, boost::forward<Arg>(arg)) ; construct(in_place_init, boost::forward<Arg>(arg)) ;
} }
void emplace_assign () void emplace_assign ()
{ {
destroy(); destroy();
construct(in_place_init) ; construct(in_place_init) ;
} }
template<class Arg> template<class Arg>
explicit optional_base ( in_place_init_t, Arg&& arg ) explicit optional_base ( in_place_init_t, Arg&& arg )
: :
@ -462,14 +477,14 @@ class optional_base : public optional_tag
{ {
construct(in_place_init, boost::forward<Arg>(arg)); construct(in_place_init, boost::forward<Arg>(arg));
} }
explicit optional_base ( in_place_init_t ) explicit optional_base ( in_place_init_t )
: :
m_initialized(false) m_initialized(false)
{ {
construct(in_place_init); construct(in_place_init);
} }
template<class Arg> template<class Arg>
explicit optional_base ( in_place_init_if_t, bool cond, Arg&& arg ) explicit optional_base ( in_place_init_if_t, bool cond, Arg&& arg )
: :
@ -478,7 +493,7 @@ class optional_base : public optional_tag
if ( cond ) if ( cond )
construct(in_place_init, boost::forward<Arg>(arg)); construct(in_place_init, boost::forward<Arg>(arg));
} }
explicit optional_base ( in_place_init_if_t, bool cond ) explicit optional_base ( in_place_init_if_t, bool cond )
: :
m_initialized(false) m_initialized(false)
@ -488,21 +503,21 @@ class optional_base : public optional_tag
} }
#else #else
template<class Arg> template<class Arg>
void construct ( in_place_init_t, const Arg& arg ) void construct ( in_place_init_t, const Arg& arg )
{ {
::new (m_storage.address()) value_type( arg ); ::new (m_storage.address()) value_type( arg );
m_initialized = true ; m_initialized = true ;
} }
template<class Arg> template<class Arg>
void construct ( in_place_init_t, Arg& arg ) void construct ( in_place_init_t, Arg& arg )
{ {
::new (m_storage.address()) value_type( arg ); ::new (m_storage.address()) value_type( arg );
m_initialized = true ; m_initialized = true ;
} }
void construct ( in_place_init_t ) void construct ( in_place_init_t )
{ {
::new (m_storage.address()) value_type(); ::new (m_storage.address()) value_type();
@ -515,20 +530,20 @@ class optional_base : public optional_tag
destroy(); destroy();
construct(in_place_init, arg); construct(in_place_init, arg);
} }
template<class Arg> template<class Arg>
void emplace_assign ( Arg& arg ) void emplace_assign ( Arg& arg )
{ {
destroy(); destroy();
construct(in_place_init, arg); construct(in_place_init, arg);
} }
void emplace_assign () void emplace_assign ()
{ {
destroy(); destroy();
construct(in_place_init); construct(in_place_init);
} }
template<class Arg> template<class Arg>
explicit optional_base ( in_place_init_t, const Arg& arg ) explicit optional_base ( in_place_init_t, const Arg& arg )
: m_initialized(false) : m_initialized(false)
@ -542,13 +557,13 @@ class optional_base : public optional_tag
{ {
construct(in_place_init, arg); construct(in_place_init, arg);
} }
explicit optional_base ( in_place_init_t ) explicit optional_base ( in_place_init_t )
: m_initialized(false) : m_initialized(false)
{ {
construct(in_place_init); construct(in_place_init);
} }
template<class Arg> template<class Arg>
explicit optional_base ( in_place_init_if_t, bool cond, const Arg& arg ) explicit optional_base ( in_place_init_if_t, bool cond, const Arg& arg )
: m_initialized(false) : m_initialized(false)
@ -556,15 +571,15 @@ class optional_base : public optional_tag
if ( cond ) if ( cond )
construct(in_place_init, arg); construct(in_place_init, arg);
} }
template<class Arg> template<class Arg>
explicit optional_base ( in_place_init_if_t, bool cond, Arg& arg ) explicit optional_base ( in_place_init_if_t, bool cond, Arg& arg )
: m_initialized(false) : m_initialized(false)
{ {
if ( cond ) if ( cond )
construct(in_place_init, arg); construct(in_place_init, arg);
} }
explicit optional_base ( in_place_init_if_t, bool cond ) explicit optional_base ( in_place_init_if_t, bool cond )
: m_initialized(false) : m_initialized(false)
{ {
@ -758,17 +773,6 @@ class optional_base : public optional_tag
storage_type m_storage ; storage_type m_storage ;
} ; } ;
template <typename T>
struct optional_value_type
{
};
template <typename T>
struct optional_value_type< ::boost::optional<T> >
{
typedef T type;
};
#include <boost/optional/detail/optional_trivially_copyable_base.hpp> #include <boost/optional/detail/optional_trivially_copyable_base.hpp>
// definition of metafunciton is_optional_val_init_candidate // definition of metafunciton is_optional_val_init_candidate
@ -782,7 +786,7 @@ struct is_optional_related
{}; {};
#if !defined(BOOST_OPTIONAL_DETAIL_NO_IS_CONSTRUCTIBLE_TRAIT) #if !defined(BOOST_OPTIONAL_DETAIL_NO_IS_CONSTRUCTIBLE_TRAIT)
template <typename T, typename U> template <typename T, typename U>
struct is_convertible_to_T_or_factory 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 : boost::conditional< boost::is_base_of<boost::in_place_factory_base, BOOST_DEDUCED_TYPENAME boost::decay<U>::type>::value
@ -871,7 +875,7 @@ class optional
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
// Creates an optional<T> initialized with 'move(val)'. // Creates an optional<T> initialized with 'move(val)'.
// Can throw if T::T(T &&) does // Can throw if T::T(T &&) does
optional ( rval_reference_type val ) : base( boost::forward<T>(val) ) optional ( rval_reference_type val ) : base( boost::forward<T>(val) )
{} {}
#endif #endif
@ -882,7 +886,7 @@ class optional
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
/// Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional. /// Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
// Can throw if T::T(T &&) does // 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 #endif
@ -899,11 +903,11 @@ class optional
) )
: :
base() base()
{ {
if ( rhs.is_initialized() ) if ( rhs.is_initialized() )
this->construct(rhs.get()); this->construct(rhs.get());
} }
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
// Creates a deep move of another convertible optional<U> // Creates a deep move of another convertible optional<U>
// Requires a valid conversion from U to T. // Requires a valid conversion from U to T.
@ -936,10 +940,10 @@ class optional
template<class Expr> template<class Expr>
explicit optional ( Expr&& expr, explicit optional ( Expr&& expr,
BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_val_init_candidate<T, Expr>, bool>::type = true 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)) : base(boost::forward<Expr>(expr),boost::addressof(expr))
{} {}
#else #else
@ -983,7 +987,7 @@ class optional
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
template<class Expr> 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 ) operator= ( Expr&& expr )
{ {
this->assign_expr(boost::forward<Expr>(expr),boost::addressof(expr)); this->assign_expr(boost::forward<Expr>(expr),boost::addressof(expr));
@ -1009,7 +1013,7 @@ class optional
this->assign(rhs); this->assign(rhs);
return *this ; return *this ;
} }
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
// Move-assigns from another convertible optional<U> (converts && deep-moves the rhs value) // Move-assigns from another convertible optional<U> (converts && deep-moves the rhs value)
// Requires a valid conversion from U to T. // Requires a valid conversion from U to T.
@ -1040,14 +1044,14 @@ class optional
#ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
optional& operator= ( optional && ) = default; optional& operator= ( optional && ) = default;
#else #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) BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
{ {
this->assign( static_cast<base &&>(rhs) ) ; this->assign( static_cast<base &&>(rhs) ) ;
return *this ; return *this ;
} }
#endif #endif
#endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
#ifndef BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX #ifndef BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
@ -1060,7 +1064,7 @@ class optional
this->assign( boost::forward<T_>(val) ) ; this->assign( boost::forward<T_>(val) ) ;
return *this ; return *this ;
} }
#else #else
// Assigns from a T (deep-copies the rhs value) // Assigns from a T (deep-copies the rhs value)
@ -1070,7 +1074,7 @@ class optional
this->assign( val ) ; this->assign( val ) ;
return *this ; return *this ;
} }
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
// Assigns from a T (deep-moves the rhs value) // Assigns from a T (deep-moves the rhs value)
optional& operator= ( rval_reference_type val ) optional& operator= ( rval_reference_type val )
@ -1079,9 +1083,9 @@ class optional
return *this ; return *this ;
} }
#endif #endif
#endif // BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX #endif // BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
// Assigns from a "none" // Assigns from a "none"
// Which destroys the current value, if any, leaving this UNINITIALIZED // Which destroys the current value, if any, leaving this UNINITIALIZED
// No-throw (assuming T::~T() doesn't) // No-throw (assuming T::~T() doesn't)
@ -1090,7 +1094,7 @@ class optional
this->assign( none_ ) ; this->assign( none_ ) ;
return *this ; return *this ;
} }
#if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES) #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
// Constructs in-place // Constructs in-place
// upon exception *this is always uninitialized // upon exception *this is always uninitialized
@ -1099,29 +1103,29 @@ class optional
{ {
this->emplace_assign( boost::forward<Args>(args)... ); this->emplace_assign( boost::forward<Args>(args)... );
} }
template<class... Args> template<class... Args>
explicit optional ( in_place_init_t, Args&&... args ) explicit optional ( in_place_init_t, Args&&... args )
: base( in_place_init, boost::forward<Args>(args)... ) : base( in_place_init, boost::forward<Args>(args)... )
{} {}
template<class... Args> template<class... Args>
explicit optional ( in_place_init_if_t, bool cond, Args&&... args ) explicit optional ( in_place_init_if_t, bool cond, Args&&... args )
: base( in_place_init_if, cond, boost::forward<Args>(args)... ) : base( in_place_init_if, cond, boost::forward<Args>(args)... )
{} {}
#elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
template<class Arg> template<class Arg>
void emplace ( Arg&& arg ) void emplace ( Arg&& arg )
{ {
this->emplace_assign( boost::forward<Arg>(arg) ); this->emplace_assign( boost::forward<Arg>(arg) );
} }
void emplace () void emplace ()
{ {
this->emplace_assign(); this->emplace_assign();
} }
template<class Args> template<class Args>
explicit optional ( in_place_init_t, Args&& args ) explicit optional ( in_place_init_t, Args&& args )
: base( in_place_init, boost::forward<Args>(args) ) : base( in_place_init, boost::forward<Args>(args) )
@ -1130,12 +1134,12 @@ class optional
explicit optional ( in_place_init_t ) explicit optional ( in_place_init_t )
: base( in_place_init ) : base( in_place_init )
{} {}
template<class Args> template<class Args>
explicit optional ( in_place_init_if_t, bool cond, Args&& args ) explicit optional ( in_place_init_if_t, bool cond, Args&& args )
: base( in_place_init_if, cond, boost::forward<Args>(args) ) : base( in_place_init_if, cond, boost::forward<Args>(args) )
{} {}
explicit optional ( in_place_init_if_t, bool cond ) explicit optional ( in_place_init_if_t, bool cond )
: base( in_place_init_if, cond ) : base( in_place_init_if, cond )
{} {}
@ -1145,23 +1149,23 @@ class optional
{ {
this->emplace_assign( arg ); this->emplace_assign( arg );
} }
template<class Arg> template<class Arg>
void emplace ( Arg& arg ) void emplace ( Arg& arg )
{ {
this->emplace_assign( arg ); this->emplace_assign( arg );
} }
void emplace () void emplace ()
{ {
this->emplace_assign(); this->emplace_assign();
} }
template<class Arg> template<class Arg>
explicit optional ( in_place_init_t, const Arg& arg ) explicit optional ( in_place_init_t, const Arg& arg )
: base( in_place_init, arg ) : base( in_place_init, arg )
{} {}
template<class Arg> template<class Arg>
explicit optional ( in_place_init_t, Arg& arg ) explicit optional ( in_place_init_t, Arg& arg )
: base( in_place_init, arg ) : base( in_place_init, arg )
@ -1170,17 +1174,17 @@ class optional
explicit optional ( in_place_init_t ) explicit optional ( in_place_init_t )
: base( in_place_init ) : base( in_place_init )
{} {}
template<class Arg> template<class Arg>
explicit optional ( in_place_init_if_t, bool cond, const Arg& arg ) explicit optional ( in_place_init_if_t, bool cond, const Arg& arg )
: base( in_place_init_if, cond, arg ) : base( in_place_init_if, cond, arg )
{} {}
template<class Arg> template<class Arg>
explicit optional ( in_place_init_if_t, bool cond, Arg& arg ) explicit optional ( in_place_init_if_t, bool cond, Arg& arg )
: base( in_place_init_if, cond, arg ) : base( in_place_init_if, cond, arg )
{} {}
explicit optional ( in_place_init_if_t, bool cond ) explicit optional ( in_place_init_if_t, bool cond )
: base( in_place_init_if, cond ) : base( in_place_init_if, cond )
{} {}
@ -1213,7 +1217,7 @@ class optional
// Returns a reference to the value if this is initialized, otherwise, // Returns a reference to the value if this is initialized, otherwise,
// the behaviour is UNDEFINED // the behaviour is UNDEFINED
// No-throw // 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_const_type operator *() const& { return this->get() ; }
reference_type operator *() & { return this->get() ; } reference_type operator *() & { return this->get() ; }
reference_type_of_temporary_wrapper operator *() && { return boost::move(this->get()) ; } reference_type_of_temporary_wrapper operator *() && { return boost::move(this->get()) ; }
@ -1222,42 +1226,42 @@ class optional
reference_type operator *() { return this->get() ; } reference_type operator *() { return this->get() ; }
#endif // !defined BOOST_NO_CXX11_REF_QUALIFIERS #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& reference_const_type value() const&
{ {
if (this->is_initialized()) if (this->is_initialized())
return this->get() ; return this->get() ;
else else
throw_exception(bad_optional_access()); throw_exception(bad_optional_access());
} }
reference_type value() & reference_type value() &
{ {
if (this->is_initialized()) if (this->is_initialized())
return this->get() ; return this->get() ;
else else
throw_exception(bad_optional_access()); throw_exception(bad_optional_access());
} }
reference_type_of_temporary_wrapper value() && reference_type_of_temporary_wrapper value() &&
{ {
if (this->is_initialized()) if (this->is_initialized())
return boost::move(this->get()) ; return boost::move(this->get()) ;
else else
throw_exception(bad_optional_access()); throw_exception(bad_optional_access());
} }
#else #else
reference_const_type value() const reference_const_type value() const
{ {
if (this->is_initialized()) if (this->is_initialized())
return this->get() ; return this->get() ;
else else
throw_exception(bad_optional_access()); throw_exception(bad_optional_access());
} }
reference_type value() reference_type value()
{ {
if (this->is_initialized()) if (this->is_initialized())
return this->get() ; return this->get() ;
else else
@ -1269,16 +1273,16 @@ class optional
#ifndef BOOST_NO_CXX11_REF_QUALIFIERS #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
template <class U> template <class U>
value_type value_or ( U&& v ) const& value_type value_or ( U&& v ) const&
{ {
if (this->is_initialized()) if (this->is_initialized())
return get(); return get();
else else
return boost::forward<U>(v); return boost::forward<U>(v);
} }
template <class U> template <class U>
value_type value_or ( U&& v ) && value_type value_or ( U&& v ) &&
{ {
if (this->is_initialized()) if (this->is_initialized())
return boost::move(get()); return boost::move(get());
else else
@ -1286,7 +1290,7 @@ class optional
} }
#elif !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES #elif !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
template <class U> template <class U>
value_type value_or ( U&& v ) const value_type value_or ( U&& v ) const
{ {
if (this->is_initialized()) if (this->is_initialized())
return get(); return get();
@ -1295,17 +1299,17 @@ class optional
} }
#else #else
template <class U> template <class U>
value_type value_or ( U const& v ) const value_type value_or ( U const& v ) const
{ {
if (this->is_initialized()) if (this->is_initialized())
return get(); return get();
else else
return v; return v;
} }
template <class U> template <class U>
value_type value_or ( U& v ) const value_type value_or ( U& v ) const
{ {
if (this->is_initialized()) if (this->is_initialized())
return get(); return get();
else else
@ -1314,7 +1318,7 @@ class optional
#endif #endif
#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)
template <typename F> template <typename F>
value_type value_or_eval ( F f ) const& value_type value_or_eval ( F f ) const&
{ {
@ -1323,7 +1327,7 @@ class optional
else else
return f(); return f();
} }
template <typename F> template <typename F>
value_type value_or_eval ( F f ) && value_type value_or_eval ( F f ) &&
{ {
@ -1350,7 +1354,7 @@ class optional
else else
return none; return none;
} }
template <typename F> template <typename F>
optional<typename boost::result_of<F(reference_type_of_temporary_wrapper)>::type> map(F f) && optional<typename boost::result_of<F(reference_type_of_temporary_wrapper)>::type> map(F f) &&
{ {
@ -1359,7 +1363,7 @@ class optional
else else
return none; return none;
} }
template <typename F> template <typename F>
optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_type)>::type>::type> flat_map(F f) & optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_type)>::type>::type> flat_map(F f) &
{ {
@ -1368,7 +1372,7 @@ class optional
else else
return none; return none;
} }
template <typename F> 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& optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_const_type)>::type>::type> flat_map(F f) const&
{ {
@ -1377,7 +1381,7 @@ class optional
else else
return none; return none;
} }
template <typename F> 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) && optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_type_of_temporary_wrapper)>::type>::type> flat_map(F f) &&
{ {
@ -1386,7 +1390,7 @@ class optional
else else
return none; return none;
} }
#else #else
template <typename F> template <typename F>
value_type value_or_eval ( F f ) const value_type value_or_eval ( F f ) const
@ -1423,7 +1427,7 @@ class optional
else else
return none; return none;
} }
template <typename F> 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 optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_const_type)>::type>::type> flat_map(F f) const
{ {
@ -1432,13 +1436,13 @@ class optional
else else
return none; return none;
} }
#endif #endif
bool has_value() const BOOST_NOEXCEPT { return this->is_initialized() ; } bool has_value() const BOOST_NOEXCEPT { return this->is_initialized() ; }
bool operator!() const BOOST_NOEXCEPT { return !this->is_initialized() ; } bool operator!() const BOOST_NOEXCEPT { return !this->is_initialized() ; }
BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT() BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
} ; } ;
@ -1447,7 +1451,7 @@ class optional
#endif // BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL #endif // BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL
namespace boost { namespace boost {
#ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
template<class T> template<class T>
class optional<T&&> class optional<T&&>
@ -1576,14 +1580,14 @@ get_pointer ( optional<T>& opt )
} // namespace boost } // namespace boost
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. // 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> template<class CharType, class CharTrait>
std::basic_ostream<CharType, CharTrait>& std::basic_ostream<CharType, CharTrait>&
operator<<(std::basic_ostream<CharType, CharTrait>& os, optional_detail::optional_tag const&) 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>"); 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 } // namespace boost

View File

@ -40,6 +40,7 @@ import testing ;
[ run optional_test_io.cpp ] [ run optional_test_io.cpp ]
[ run optional_test_move.cpp ] [ run optional_test_move.cpp ]
[ run optional_test_noexcept_move.cpp ] [ run optional_test_noexcept_move.cpp ]
[ run optional_test_old_impl.cpp ]
[ run optional_test_equals_none.cpp ] [ run optional_test_equals_none.cpp ]
[ run optional_test_value_access.cpp ] [ run optional_test_value_access.cpp ]
[ run optional_test_emplace.cpp ] [ run optional_test_emplace.cpp ]

View File

@ -0,0 +1,223 @@
// 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
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// akrzemi1@gmail.com
#define BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL // does old implementation still work for basic usage?
#include "boost/optional/optional.hpp"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "boost/core/ignore_unused.hpp"
#include "boost/core/lightweight_test.hpp"
using boost::optional;
struct IntWrapper
{
int _i;
IntWrapper(int i) : _i(i) {}
bool operator==(IntWrapper const& rhs) const { return _i == rhs._i; }
};
template <typename T>
void test_function_value_or_for()
{
optional<T> oM0;
const optional<T> oC0;
optional<T> oM1(1);
const optional<T> oC2(2);
BOOST_TEST(oM0.value_or(5) == 5);
BOOST_TEST(oC0.value_or(5) == 5);
BOOST_TEST(oM1.value_or(5) == 1);
BOOST_TEST(oC2.value_or(5) == 2);
}
template <typename T>
void test_function_value_for()
{
optional<T> o0;
optional<T> o1(1);
const optional<T> oC(2);
try
{
T& v = o1.value();
BOOST_TEST(v == 1);
}
catch(...)
{
BOOST_TEST(false);
}
try
{
T const& v = oC.value();
BOOST_TEST(v == 2);
}
catch(...)
{
BOOST_TEST(false);
}
BOOST_TEST_THROWS(o0.value(), boost::bad_optional_access);
}
void test_function_value()
{
test_function_value_for<int>();
test_function_value_for<double>();
test_function_value_for<IntWrapper>();
}
struct FatToIntConverter
{
static int conversions;
int _val;
FatToIntConverter(int val) : _val(val) {}
operator int() const { conversions += 1; return _val; }
};
int FatToIntConverter::conversions = 0;
void test_function_value_or()
{
test_function_value_or_for<int>();
test_function_value_or_for<double>();
test_function_value_or_for<IntWrapper>();
optional<int> oi(1);
BOOST_TEST(oi.value_or(FatToIntConverter(2)) == 1);
BOOST_TEST(FatToIntConverter::conversions == 0);
oi = boost::none;
BOOST_TEST(oi.value_or(FatToIntConverter(2)) == 2);
BOOST_TEST(FatToIntConverter::conversions == 1);
}
struct FunM
{
int operator()() { return 5; }
};
struct FunC
{
int operator()() const { return 6; }
};
int funP ()
{
return 7;
}
int throw_()
{
throw int();
}
void test_function_value_or_eval()
{
optional<int> o1 = 1;
optional<int> oN;
FunM funM;
FunC funC;
BOOST_TEST_EQ(o1.value_or_eval(funM), 1);
BOOST_TEST_EQ(oN.value_or_eval(funM), 5);
BOOST_TEST_EQ(o1.value_or_eval(FunM()), 1);
BOOST_TEST_EQ(oN.value_or_eval(FunM()), 5);
BOOST_TEST_EQ(o1.value_or_eval(funC), 1);
BOOST_TEST_EQ(oN.value_or_eval(funC), 6);
BOOST_TEST_EQ(o1.value_or_eval(FunC()), 1);
BOOST_TEST_EQ(oN.value_or_eval(FunC()), 6);
BOOST_TEST_EQ(o1.value_or_eval(funP), 1);
BOOST_TEST_EQ(oN.value_or_eval(funP), 7);
#ifndef BOOST_NO_CXX11_LAMBDAS
BOOST_TEST_EQ(o1.value_or_eval([](){return 8;}), 1);
BOOST_TEST_EQ(oN.value_or_eval([](){return 8;}), 8);
#endif
try
{
BOOST_TEST_EQ(o1.value_or_eval(throw_), 1);
}
catch(...)
{
BOOST_TEST(false);
}
BOOST_TEST_THROWS(oN.value_or_eval(throw_), int);
}
const optional<std::string> makeConstOptVal()
{
return std::string("something");
}
void test_const_move()
{
std::string s5 = *makeConstOptVal();
std::string s6 = makeConstOptVal().value();
boost::ignore_unused(s5);
boost::ignore_unused(s6);
}
#if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
struct MoveOnly
{
explicit MoveOnly(int){}
MoveOnly(MoveOnly &&){}
void operator=(MoveOnly &&);
private:
MoveOnly(MoveOnly const&);
void operator=(MoveOnly const&);
};
optional<MoveOnly> makeMoveOnly()
{
return MoveOnly(1);
}
MoveOnly moveOnlyDefault()
{
return MoveOnly(1);
}
// compile-time test
void test_move_only_getters()
{
MoveOnly m1 = *makeMoveOnly();
MoveOnly m2 = makeMoveOnly().value();
MoveOnly m3 = makeMoveOnly().value_or(MoveOnly(1));
MoveOnly m4 = makeMoveOnly().value_or_eval(moveOnlyDefault);
boost::ignore_unused(m1);
boost::ignore_unused(m2);
boost::ignore_unused(m3);
boost::ignore_unused(m4);
}
#endif // !defined BOOST_NO_CXX11_REF_QUALIFIERS
int main()
{
test_function_value();
test_function_value_or();
test_function_value_or_eval();
test_const_move();
return boost::report_errors();
}