diff --git a/include/boost/optional/optional.hpp b/include/boost/optional/optional.hpp index ff39dad..01cb2bd 100644 --- a/include/boost/optional/optional.hpp +++ b/include/boost/optional/optional.hpp @@ -60,19 +60,34 @@ #include #include +namespace boost { namespace optional_detail { + +template +struct optional_value_type +{ +}; + +template +struct optional_value_type< ::boost::optional > +{ + typedef T type; +}; + +}} // namespace boost::optional_detail + #ifdef BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL #include #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 @@ -82,7 +97,7 @@ struct in_place_init_if_t 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; @@ -252,7 +267,7 @@ class optional_base : public optional_tag construct(rhs.get_impl()); } } - + #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES // Assigns from another optional (deep-moves the rhs value) void assign ( optional_base&& rhs ) @@ -269,7 +284,7 @@ class optional_base : public optional_tag construct(boost::move(rhs.get_impl())); } } -#endif +#endif // Assigns from another _convertible_ optional (deep-copies the rhs value) template @@ -283,7 +298,7 @@ class optional_base : public optional_tag #else assign_value( static_cast(rhs.get()) ); #endif - + else destroy(); } else @@ -316,7 +331,7 @@ class optional_base : public optional_tag } } #endif - + // Assigns from a T (deep-copies the rhs value) void assign ( argument_type val ) { @@ -324,7 +339,7 @@ class optional_base : public optional_tag 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 ) @@ -385,7 +400,7 @@ class optional_base : public optional_tag ::new (m_storage.address()) value_type(val) ; m_initialized = true ; } - + #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES void construct ( rval_reference_type val ) { @@ -411,7 +426,7 @@ class optional_base : public optional_tag destroy(); construct(in_place_init, boost::forward(args)...); } - + template 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)...); } - + template 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) ); m_initialized = true ; } - + void construct ( in_place_init_t ) { ::new (m_storage.address()) value_type(); m_initialized = true ; } - + template void emplace_assign ( Arg&& arg ) { destroy(); construct(in_place_init, boost::forward(arg)) ; } - + void emplace_assign () { destroy(); construct(in_place_init) ; } - + template 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)); } - + explicit optional_base ( in_place_init_t ) : m_initialized(false) { construct(in_place_init); } - + template explicit optional_base ( in_place_init_if_t, bool cond, Arg&& arg ) : @@ -478,7 +493,7 @@ class optional_base : public optional_tag if ( cond ) construct(in_place_init, boost::forward(arg)); } - + explicit optional_base ( in_place_init_if_t, bool cond ) : m_initialized(false) @@ -488,21 +503,21 @@ class optional_base : public optional_tag } #else - + template void construct ( in_place_init_t, const Arg& arg ) { ::new (m_storage.address()) value_type( arg ); m_initialized = true ; } - + template 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(); @@ -515,20 +530,20 @@ class optional_base : public optional_tag destroy(); construct(in_place_init, arg); } - + template void emplace_assign ( Arg& arg ) { destroy(); construct(in_place_init, arg); } - + void emplace_assign () { destroy(); construct(in_place_init); } - + template explicit optional_base ( in_place_init_t, const Arg& arg ) : m_initialized(false) @@ -542,13 +557,13 @@ class optional_base : public optional_tag { construct(in_place_init, arg); } - + explicit optional_base ( in_place_init_t ) : m_initialized(false) { construct(in_place_init); } - + template explicit optional_base ( in_place_init_if_t, bool cond, const Arg& arg ) : m_initialized(false) @@ -556,15 +571,15 @@ class optional_base : public optional_tag if ( cond ) construct(in_place_init, arg); } - + template 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) { @@ -758,17 +773,6 @@ class optional_base : public optional_tag storage_type m_storage ; } ; -template -struct optional_value_type -{ -}; - -template -struct optional_value_type< ::boost::optional > -{ - typedef T type; -}; - #include // 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) - + template struct is_convertible_to_T_or_factory : boost::conditional< boost::is_base_of::type>::value @@ -871,7 +875,7 @@ class optional #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES // Creates an optional initialized with 'move(val)'. // Can throw if T::T(T &&) does - optional ( rval_reference_type val ) : base( boost::forward(val) ) + optional ( rval_reference_type val ) : base( boost::forward(val) ) {} #endif @@ -882,7 +886,7 @@ class optional #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES /// Creates an optional 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(val) ) + optional ( bool cond, rval_reference_type val ) : base( cond, boost::forward(val) ) {} #endif @@ -899,11 +903,11 @@ class optional ) : base() - { + { if ( rhs.is_initialized() ) this->construct(rhs.get()); } - + #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES // Creates a deep move of another convertible optional // Requires a valid conversion from U to T. @@ -936,10 +940,10 @@ class optional template - explicit optional ( Expr&& expr, - BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_val_init_candidate, bool>::type = true - ) - : base(boost::forward(expr),boost::addressof(expr)) + explicit optional ( Expr&& expr, + BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_val_init_candidate, bool>::type = true + ) + : base(boost::forward(expr),boost::addressof(expr)) {} #else @@ -983,7 +987,7 @@ class optional #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES template - BOOST_DEDUCED_TYPENAME boost::enable_if, optional&>::type + BOOST_DEDUCED_TYPENAME boost::enable_if, optional&>::type operator= ( Expr&& expr ) { this->assign_expr(boost::forward(expr),boost::addressof(expr)); @@ -1009,7 +1013,7 @@ class optional this->assign(rhs); return *this ; } - + #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES // Move-assigns from another convertible optional (converts && deep-moves the rhs value) // Requires a valid conversion from U to T. @@ -1040,14 +1044,14 @@ class optional #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::value && ::boost::is_nothrow_move_assignable::value) { this->assign( static_cast(rhs) ) ; return *this ; } -#endif - +#endif + #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES #ifndef BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX @@ -1060,7 +1064,7 @@ class optional this->assign( boost::forward(val) ) ; return *this ; } - + #else // Assigns from a T (deep-copies the rhs value) @@ -1070,7 +1074,7 @@ class optional 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 ) @@ -1079,9 +1083,9 @@ class optional 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) @@ -1090,7 +1094,7 @@ class optional 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 @@ -1099,29 +1103,29 @@ class optional { this->emplace_assign( boost::forward(args)... ); } - + template explicit optional ( in_place_init_t, Args&&... args ) : base( in_place_init, boost::forward(args)... ) {} - + template explicit optional ( in_place_init_if_t, bool cond, Args&&... args ) : base( in_place_init_if, cond, boost::forward(args)... ) {} - + #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) template void emplace ( Arg&& arg ) { this->emplace_assign( boost::forward(arg) ); } - + void emplace () { this->emplace_assign(); } - + template explicit optional ( in_place_init_t, Args&& args ) : base( in_place_init, boost::forward(args) ) @@ -1130,12 +1134,12 @@ class optional explicit optional ( in_place_init_t ) : base( in_place_init ) {} - + template explicit optional ( in_place_init_if_t, bool cond, Args&& args ) : base( in_place_init_if, cond, boost::forward(args) ) {} - + explicit optional ( in_place_init_if_t, bool cond ) : base( in_place_init_if, cond ) {} @@ -1145,23 +1149,23 @@ class optional { this->emplace_assign( arg ); } - + template void emplace ( Arg& arg ) { this->emplace_assign( arg ); } - + void emplace () { this->emplace_assign(); } - + template explicit optional ( in_place_init_t, const Arg& arg ) : base( in_place_init, arg ) {} - + template explicit optional ( in_place_init_t, Arg& arg ) : base( in_place_init, arg ) @@ -1170,17 +1174,17 @@ class optional explicit optional ( in_place_init_t ) : base( in_place_init ) {} - + template explicit optional ( in_place_init_if_t, bool cond, const Arg& arg ) : base( in_place_init_if, cond, arg ) {} - + template 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 ) {} @@ -1213,7 +1217,7 @@ class optional // 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()) ; } @@ -1222,42 +1226,42 @@ class optional 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 @@ -1269,16 +1273,16 @@ class optional #ifndef BOOST_NO_CXX11_REF_QUALIFIERS template value_type value_or ( U&& v ) const& - { + { if (this->is_initialized()) return get(); else return boost::forward(v); } - + template - value_type value_or ( U&& v ) && - { + value_type value_or ( U&& v ) && + { if (this->is_initialized()) return boost::move(get()); else @@ -1286,7 +1290,7 @@ class optional } #elif !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES template - value_type value_or ( U&& v ) const + value_type value_or ( U&& v ) const { if (this->is_initialized()) return get(); @@ -1295,17 +1299,17 @@ class optional } #else template - 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 - value_type value_or ( U& v ) const - { + value_type value_or ( U& v ) const + { if (this->is_initialized()) return get(); else @@ -1314,7 +1318,7 @@ class optional #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 value_type value_or_eval ( F f ) const& { @@ -1323,7 +1327,7 @@ class optional else return f(); } - + template value_type value_or_eval ( F f ) && { @@ -1350,7 +1354,7 @@ class optional else return none; } - + template optional::type> map(F f) && { @@ -1359,7 +1363,7 @@ class optional else return none; } - + template optional::type>::type> flat_map(F f) & { @@ -1368,7 +1372,7 @@ class optional else return none; } - + template optional::type>::type> flat_map(F f) const& { @@ -1377,7 +1381,7 @@ class optional else return none; } - + template optional::type>::type> flat_map(F f) && { @@ -1386,7 +1390,7 @@ class optional else return none; } - + #else template value_type value_or_eval ( F f ) const @@ -1423,7 +1427,7 @@ class optional else return none; } - + template optional::type>::type> flat_map(F f) const { @@ -1432,13 +1436,13 @@ class optional 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() } ; @@ -1447,7 +1451,7 @@ class optional #endif // BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL namespace boost { - + #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES template class optional @@ -1576,14 +1580,14 @@ get_pointer ( optional& opt ) } // 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 std::basic_ostream& operator<<(std::basic_ostream& os, optional_detail::optional_tag const&) { BOOST_STATIC_ASSERT_MSG(sizeof(CharType) == 0, "If you want to output boost::optional, include header "); - return os; + return os; } } // namespace boost diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 4a758da..9000c2a 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -40,6 +40,7 @@ import testing ; [ run optional_test_io.cpp ] [ run optional_test_move.cpp ] [ run optional_test_noexcept_move.cpp ] + [ run optional_test_old_impl.cpp ] [ run optional_test_equals_none.cpp ] [ run optional_test_value_access.cpp ] [ run optional_test_emplace.cpp ] diff --git a/test/optional_test_old_impl.cpp b/test/optional_test_old_impl.cpp new file mode 100644 index 0000000..994d989 --- /dev/null +++ b/test/optional_test_old_impl.cpp @@ -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 +void test_function_value_or_for() +{ + optional oM0; + const optional oC0; + optional oM1(1); + const optional 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 +void test_function_value_for() +{ + optional o0; + optional o1(1); + const optional 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(); + test_function_value_for(); + test_function_value_for(); +} + +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(); + test_function_value_or_for(); + test_function_value_or_for(); + + optional 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 o1 = 1; + optional 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 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 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(); +}