Chris@16: // Copyright (C) 2003, 2008 Fernando Luis Cacciola Carballal. Chris@101: // Copyright (C) 2014, 2015 Andrzej Krzemienski. Chris@16: // Chris@16: // Use, modification, and distribution is subject to the Boost Software Chris@16: // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/optional for documentation. Chris@16: // Chris@16: // You are welcome to contact the author at: Chris@16: // fernando_cacciola@hotmail.com Chris@16: // Chris@16: // Revisions: Chris@16: // 27 Apr 2008 (improved swap) Fernando Cacciola, Niels Dekker, Thorsten Ottosen Chris@101: // 05 May 2014 (Added move semantics) Andrzej Krzemienski Chris@16: // Chris@16: #ifndef BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP Chris@16: #define BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: Chris@16: #include Chris@16: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@16: #include Chris@101: #include Chris@101: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@101: #include Chris@101: Chris@101: Chris@16: Chris@16: #include Chris@16: Chris@101: #if (defined BOOST_NO_CXX11_RVALUE_REFERENCES) || (defined BOOST_OPTIONAL_CONFIG_NO_RVALUE_REFERENCES) Chris@101: #define BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@16: #endif Chris@16: Chris@101: #if BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION,<=700) Chris@101: // AFAICT only Intel 7 correctly resolves the overload set Chris@16: // that includes the in-place factory taking functions, Chris@101: // so for the other icc versions, in-place factory support Chris@16: // is disabled Chris@16: #define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT Chris@16: #endif Chris@16: Chris@16: #if BOOST_WORKAROUND(__BORLANDC__, <= 0x551) Chris@16: // BCB (5.5.1) cannot parse the nested template struct in an inplace factory. Chris@16: #define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT Chris@16: #endif Chris@16: Chris@16: #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) \ Chris@16: && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581) ) Chris@16: // BCB (up to 5.64) has the following bug: Chris@16: // If there is a member function/operator template of the form Chris@16: // template mfunc( Expr expr ) ; Chris@16: // some calls are resolved to this even if there are other better matches. Chris@16: // The effect of this bug is that calls to converting ctors and assignments Chris@16: // are incrorrectly sink to this general catch-all member function template as shown above. Chris@16: #define BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION Chris@16: #endif Chris@16: Chris@101: #if defined(__GNUC__) && !defined(__INTEL_COMPILER) Chris@16: // GCC since 3.3 has may_alias attribute that helps to alleviate optimizer issues with Chris@16: // regard to violation of the strict aliasing rules. The optional< T > storage type is marked Chris@16: // with this attribute in order to let the compiler know that it will alias objects of type T Chris@16: // and silence compilation warnings. Chris@16: #define BOOST_OPTIONAL_DETAIL_USE_ATTRIBUTE_MAY_ALIAS Chris@16: #endif Chris@16: Chris@16: // Daniel Wallin discovered that bind/apply.hpp badly interacts with the apply<> Chris@16: // member template of a factory as used in the optional<> implementation. Chris@16: // He proposed this simple fix which is to move the call to apply<> outside Chris@16: // namespace boost. Chris@16: namespace boost_optional_detail Chris@16: { Chris@16: template Chris@16: inline void construct(Factory const& factory, void* address) Chris@16: { Chris@16: factory.BOOST_NESTED_TEMPLATE apply(address); Chris@16: } Chris@16: } Chris@16: Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: class in_place_factory_base ; Chris@16: class typed_in_place_factory_base ; Chris@16: Chris@16: // This forward is needed to refer to namespace scope swap from the member swap Chris@16: template void swap ( optional& x, optional& y ); Chris@16: Chris@16: namespace optional_detail { Chris@16: // This local class is used instead of that in "aligned_storage.hpp" Chris@16: // because I've found the 'official' class to ICE BCB5.5 Chris@16: // when some types are used with optional<> Chris@16: // (due to sizeof() passed down as a non-type template parameter) Chris@16: template Chris@16: class aligned_storage Chris@16: { Chris@16: // Borland ICEs if unnamed unions are used for this! Chris@16: union Chris@16: // This works around GCC warnings about breaking strict aliasing rules when casting storage address to T* Chris@16: #if defined(BOOST_OPTIONAL_DETAIL_USE_ATTRIBUTE_MAY_ALIAS) Chris@101: __attribute__((__may_alias__)) Chris@16: #endif Chris@16: dummy_u Chris@16: { Chris@16: char data[ sizeof(T) ]; Chris@16: BOOST_DEDUCED_TYPENAME type_with_alignment< Chris@16: ::boost::alignment_of::value >::type aligner_; Chris@16: } dummy_ ; Chris@16: Chris@16: public: Chris@16: Chris@16: #if defined(BOOST_OPTIONAL_DETAIL_USE_ATTRIBUTE_MAY_ALIAS) Chris@16: void const* address() const { return &dummy_; } Chris@16: void * address() { return &dummy_; } Chris@16: #else Chris@16: void const* address() const { return dummy_.data; } Chris@16: void * address() { return dummy_.data; } Chris@16: #endif Chris@16: } ; Chris@16: Chris@16: template Chris@16: struct types_when_isnt_ref Chris@16: { Chris@16: typedef T const& reference_const_type ; Chris@16: typedef T & reference_type ; Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: typedef T && rval_reference_type ; Chris@101: typedef T && reference_type_of_temporary_wrapper; Chris@101: #ifdef BOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES Chris@101: // GCC 4.4 has support for an early draft of rvalue references. The conforming version below Chris@101: // causes warnings about returning references to a temporary. Chris@101: static T&& move(T&& r) { return r; } Chris@101: #else Chris@101: static rval_reference_type move(reference_type r) { return boost::move(r); } Chris@101: #endif Chris@101: #endif Chris@16: typedef T const* pointer_const_type ; Chris@16: typedef T * pointer_type ; Chris@16: typedef T const& argument_type ; Chris@16: } ; Chris@101: Chris@16: template Chris@16: struct types_when_is_ref Chris@16: { Chris@16: typedef BOOST_DEDUCED_TYPENAME remove_reference::type raw_type ; Chris@16: Chris@101: typedef raw_type& reference_const_type ; Chris@101: typedef raw_type& reference_type ; Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: typedef BOOST_DEDUCED_TYPENAME remove_const::type&& rval_reference_type ; Chris@101: typedef raw_type& reference_type_of_temporary_wrapper; Chris@101: static reference_type move(reference_type r) { return r; } Chris@101: #endif Chris@101: typedef raw_type* pointer_const_type ; Chris@101: typedef raw_type* pointer_type ; Chris@101: typedef raw_type& argument_type ; Chris@16: } ; Chris@16: Chris@101: template Chris@101: void prevent_binding_rvalue_ref_to_optional_lvalue_ref() Chris@101: { Chris@101: #ifndef BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES Chris@101: BOOST_STATIC_ASSERT_MSG( Chris@101: !boost::is_lvalue_reference::value || !boost::is_rvalue_reference::value, Chris@101: "binding rvalue references to optional lvalue references is disallowed"); Chris@101: #endif Chris@101: } Chris@101: Chris@16: struct optional_tag {} ; Chris@16: Chris@16: template Chris@16: class optional_base : public optional_tag Chris@16: { Chris@16: private : Chris@16: Chris@16: typedef Chris@16: #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) Chris@16: BOOST_DEDUCED_TYPENAME Chris@16: #endif Chris@16: ::boost::detail::make_reference_content::type internal_type ; Chris@16: Chris@16: typedef aligned_storage storage_type ; Chris@16: Chris@16: typedef types_when_isnt_ref types_when_not_ref ; Chris@16: typedef types_when_is_ref types_when_ref ; Chris@16: Chris@16: typedef optional_base this_type ; Chris@16: Chris@16: protected : Chris@16: Chris@16: typedef T value_type ; Chris@16: Chris@16: typedef mpl::true_ is_reference_tag ; Chris@16: typedef mpl::false_ is_not_reference_tag ; Chris@16: Chris@16: typedef BOOST_DEDUCED_TYPENAME is_reference::type is_reference_predicate ; Chris@16: Chris@16: public: Chris@16: typedef BOOST_DEDUCED_TYPENAME mpl::if_::type types ; Chris@16: Chris@16: protected: Chris@16: typedef BOOST_DEDUCED_TYPENAME types::reference_type reference_type ; Chris@16: typedef BOOST_DEDUCED_TYPENAME types::reference_const_type reference_const_type ; Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: typedef BOOST_DEDUCED_TYPENAME types::rval_reference_type rval_reference_type ; Chris@101: typedef BOOST_DEDUCED_TYPENAME types::reference_type_of_temporary_wrapper reference_type_of_temporary_wrapper ; Chris@101: #endif Chris@16: typedef BOOST_DEDUCED_TYPENAME types::pointer_type pointer_type ; Chris@16: typedef BOOST_DEDUCED_TYPENAME types::pointer_const_type pointer_const_type ; Chris@16: typedef BOOST_DEDUCED_TYPENAME types::argument_type argument_type ; Chris@16: Chris@16: // Creates an optional uninitialized. Chris@16: // No-throw Chris@16: optional_base() Chris@16: : Chris@16: m_initialized(false) {} Chris@16: Chris@16: // Creates an optional uninitialized. Chris@16: // No-throw Chris@16: optional_base ( none_t ) Chris@16: : Chris@16: m_initialized(false) {} Chris@16: Chris@16: // Creates an optional initialized with 'val'. Chris@16: // Can throw if T::T(T const&) does Chris@16: optional_base ( argument_type val ) Chris@16: : Chris@16: m_initialized(false) Chris@16: { Chris@16: construct(val); Chris@16: } Chris@16: Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: // move-construct an optional initialized from an rvalue-ref to 'val'. Chris@101: // Can throw if T::T(T&&) does Chris@101: optional_base ( rval_reference_type val ) Chris@101: : Chris@101: m_initialized(false) Chris@101: { Chris@101: construct( boost::move(val) ); Chris@101: } Chris@101: #endif Chris@101: Chris@16: // Creates an optional initialized with 'val' IFF cond is true, otherwise creates an uninitialzed optional. Chris@16: // Can throw if T::T(T const&) does Chris@16: optional_base ( bool cond, argument_type val ) Chris@16: : Chris@16: m_initialized(false) Chris@16: { Chris@16: if ( cond ) Chris@16: construct(val); Chris@16: } Chris@16: Chris@16: // Creates a deep copy of another optional Chris@16: // Can throw if T::T(T const&) does Chris@16: optional_base ( optional_base const& rhs ) Chris@16: : Chris@16: m_initialized(false) Chris@16: { Chris@16: if ( rhs.is_initialized() ) Chris@16: construct(rhs.get_impl()); Chris@16: } Chris@16: Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: // Creates a deep move of another optional Chris@101: // Can throw if T::T(T&&) does Chris@101: optional_base ( optional_base&& rhs ) Chris@101: : Chris@101: m_initialized(false) Chris@101: { Chris@101: if ( rhs.is_initialized() ) Chris@101: construct( boost::move(rhs.get_impl()) ); Chris@101: } Chris@101: #endif Chris@16: Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: Chris@101: template Chris@101: explicit optional_base ( Expr&& expr, PtrExpr const* tag ) Chris@101: : Chris@101: m_initialized(false) Chris@101: { Chris@101: construct(boost::forward(expr),tag); Chris@101: } Chris@101: Chris@101: #else Chris@16: // This is used for both converting and in-place constructions. Chris@16: // Derived classes use the 'tag' to select the appropriate Chris@16: // implementation (the correct 'construct()' overload) Chris@16: template Chris@16: explicit optional_base ( Expr const& expr, Expr const* tag ) Chris@16: : Chris@16: m_initialized(false) Chris@16: { Chris@16: construct(expr,tag); Chris@16: } Chris@16: Chris@101: #endif Chris@16: Chris@16: Chris@16: // No-throw (assuming T::~T() doesn't) Chris@16: ~optional_base() { destroy() ; } Chris@16: Chris@16: // Assigns from another optional (deep-copies the rhs value) Chris@16: void assign ( optional_base const& rhs ) Chris@16: { Chris@16: if (is_initialized()) Chris@16: { Chris@16: if ( rhs.is_initialized() ) Chris@16: assign_value(rhs.get_impl(), is_reference_predicate() ); Chris@16: else destroy(); Chris@16: } Chris@16: else Chris@16: { Chris@16: if ( rhs.is_initialized() ) Chris@16: construct(rhs.get_impl()); Chris@16: } Chris@16: } Chris@101: Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: // Assigns from another optional (deep-moves the rhs value) Chris@101: void assign ( optional_base&& rhs ) Chris@101: { Chris@101: if (is_initialized()) Chris@101: { Chris@101: if ( rhs.is_initialized() ) Chris@101: assign_value(boost::move(rhs.get_impl()), is_reference_predicate() ); Chris@101: else destroy(); Chris@101: } Chris@101: else Chris@101: { Chris@101: if ( rhs.is_initialized() ) Chris@101: construct(boost::move(rhs.get_impl())); Chris@101: } Chris@101: } Chris@101: #endif Chris@16: Chris@16: // Assigns from another _convertible_ optional (deep-copies the rhs value) Chris@16: template Chris@16: void assign ( optional const& rhs ) Chris@16: { Chris@16: if (is_initialized()) Chris@16: { Chris@16: if ( rhs.is_initialized() ) Chris@101: #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES Chris@101: assign_value(rhs.get(), is_reference_predicate() ); Chris@101: #else Chris@101: assign_value(static_cast(rhs.get()), is_reference_predicate() ); Chris@101: #endif Chris@101: Chris@16: else destroy(); Chris@16: } Chris@16: else Chris@16: { Chris@16: if ( rhs.is_initialized() ) Chris@101: #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES Chris@101: construct(rhs.get()); Chris@101: #else Chris@16: construct(static_cast(rhs.get())); Chris@101: #endif Chris@16: } Chris@16: } Chris@16: Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: // move-assigns from another _convertible_ optional (deep-moves from the rhs value) Chris@101: template Chris@101: void assign ( optional&& rhs ) Chris@101: { Chris@101: typedef BOOST_DEDUCED_TYPENAME optional::rval_reference_type ref_type; Chris@101: if (is_initialized()) Chris@101: { Chris@101: if ( rhs.is_initialized() ) Chris@101: assign_value(static_cast(rhs.get()), is_reference_predicate() ); Chris@101: else destroy(); Chris@101: } Chris@101: else Chris@101: { Chris@101: if ( rhs.is_initialized() ) Chris@101: construct(static_cast(rhs.get())); Chris@101: } Chris@101: } Chris@101: #endif Chris@101: Chris@16: // Assigns from a T (deep-copies the rhs value) Chris@16: void assign ( argument_type val ) Chris@16: { Chris@16: if (is_initialized()) Chris@16: assign_value(val, is_reference_predicate() ); Chris@16: else construct(val); Chris@16: } Chris@101: Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: // Assigns from a T (deep-moves the rhs value) Chris@101: void assign ( rval_reference_type val ) Chris@101: { Chris@101: if (is_initialized()) Chris@101: assign_value( boost::move(val), is_reference_predicate() ); Chris@101: else construct( boost::move(val) ); Chris@101: } Chris@101: #endif Chris@16: Chris@16: // Assigns from "none", destroying the current value, if any, leaving this UNINITIALIZED Chris@16: // No-throw (assuming T::~T() doesn't) Chris@101: void assign ( none_t ) BOOST_NOEXCEPT { destroy(); } Chris@16: Chris@16: #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT Chris@101: Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: template Chris@101: void assign_expr ( Expr&& expr, ExprPtr const* tag ) Chris@101: { Chris@101: if (is_initialized()) Chris@101: assign_expr_to_initialized(boost::forward(expr),tag); Chris@101: else construct(boost::forward(expr),tag); Chris@101: } Chris@101: #else Chris@16: template Chris@16: void assign_expr ( Expr const& expr, Expr const* tag ) Chris@101: { Chris@101: if (is_initialized()) Chris@101: assign_expr_to_initialized(expr,tag); Chris@101: else construct(expr,tag); Chris@101: } Chris@101: #endif Chris@101: Chris@16: #endif Chris@16: Chris@16: public : Chris@16: Chris@101: // **DEPPRECATED** Destroys the current value, if any, leaving this UNINITIALIZED Chris@16: // No-throw (assuming T::~T() doesn't) Chris@101: void reset() BOOST_NOEXCEPT { destroy(); } Chris@16: Chris@101: // **DEPPRECATED** Replaces the current value -if any- with 'val' Chris@16: void reset ( argument_type val ) { assign(val); } Chris@16: Chris@16: // Returns a pointer to the value if this is initialized, otherwise, Chris@16: // returns NULL. Chris@16: // No-throw Chris@16: pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; } Chris@16: pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; } Chris@16: Chris@16: bool is_initialized() const { return m_initialized ; } Chris@16: Chris@16: protected : Chris@16: Chris@16: void construct ( argument_type val ) Chris@16: { Chris@101: ::new (m_storage.address()) internal_type(val) ; Chris@101: m_initialized = true ; Chris@101: } Chris@101: Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: void construct ( rval_reference_type val ) Chris@101: { Chris@101: ::new (m_storage.address()) internal_type( types::move(val) ) ; Chris@101: m_initialized = true ; Chris@101: } Chris@101: #endif Chris@101: Chris@101: Chris@101: #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES) Chris@101: // Constructs in-place Chris@101: // upon exception *this is always uninitialized Chris@101: template Chris@101: void emplace_assign ( Args&&... args ) Chris@101: { Chris@101: destroy(); Chris@101: ::new (m_storage.address()) internal_type( boost::forward(args)... ); Chris@101: m_initialized = true ; Chris@101: } Chris@101: #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) Chris@101: template Chris@101: void emplace_assign ( Arg&& arg ) Chris@101: { Chris@101: destroy(); Chris@101: ::new (m_storage.address()) internal_type( boost::forward(arg) ); Chris@101: m_initialized = true ; Chris@101: } Chris@101: #else Chris@101: template Chris@101: void emplace_assign ( const Arg& arg ) Chris@101: { Chris@101: destroy(); Chris@101: ::new (m_storage.address()) internal_type( arg ); Chris@101: m_initialized = true ; Chris@101: } Chris@101: Chris@101: template Chris@101: void emplace_assign ( Arg& arg ) Chris@101: { Chris@101: destroy(); Chris@101: ::new (m_storage.address()) internal_type( arg ); Chris@101: m_initialized = true ; Chris@101: } Chris@101: #endif Chris@101: Chris@101: #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT Chris@101: Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: // Constructs in-place using the given factory Chris@101: template Chris@101: void construct ( Expr&& factory, in_place_factory_base const* ) Chris@101: { Chris@101: BOOST_STATIC_ASSERT ( ::boost::mpl::not_::value ) ; Chris@101: boost_optional_detail::construct(factory, m_storage.address()); Chris@16: m_initialized = true ; Chris@16: } Chris@16: Chris@101: // Constructs in-place using the given typed factory Chris@101: template Chris@101: void construct ( Expr&& factory, typed_in_place_factory_base const* ) Chris@101: { Chris@101: BOOST_STATIC_ASSERT ( ::boost::mpl::not_::value ) ; Chris@101: factory.apply(m_storage.address()) ; Chris@101: m_initialized = true ; Chris@101: } Chris@101: Chris@101: template Chris@101: void assign_expr_to_initialized ( Expr&& factory, in_place_factory_base const* tag ) Chris@101: { Chris@101: destroy(); Chris@101: construct(factory,tag); Chris@101: } Chris@101: Chris@101: // Constructs in-place using the given typed factory Chris@101: template Chris@101: void assign_expr_to_initialized ( Expr&& factory, typed_in_place_factory_base const* tag ) Chris@101: { Chris@101: destroy(); Chris@101: construct(factory,tag); Chris@101: } Chris@101: Chris@101: #else Chris@16: // Constructs in-place using the given factory Chris@16: template Chris@16: void construct ( Expr const& factory, in_place_factory_base const* ) Chris@16: { Chris@16: BOOST_STATIC_ASSERT ( ::boost::mpl::not_::value ) ; Chris@16: boost_optional_detail::construct(factory, m_storage.address()); Chris@16: m_initialized = true ; Chris@16: } Chris@16: Chris@16: // Constructs in-place using the given typed factory Chris@16: template Chris@16: void construct ( Expr const& factory, typed_in_place_factory_base const* ) Chris@16: { Chris@16: BOOST_STATIC_ASSERT ( ::boost::mpl::not_::value ) ; Chris@16: factory.apply(m_storage.address()) ; Chris@16: m_initialized = true ; Chris@16: } Chris@16: Chris@16: template Chris@16: void assign_expr_to_initialized ( Expr const& factory, in_place_factory_base const* tag ) Chris@16: { Chris@16: destroy(); Chris@16: construct(factory,tag); Chris@16: } Chris@16: Chris@16: // Constructs in-place using the given typed factory Chris@16: template Chris@16: void assign_expr_to_initialized ( Expr const& factory, typed_in_place_factory_base const* tag ) Chris@16: { Chris@16: destroy(); Chris@16: construct(factory,tag); Chris@16: } Chris@16: #endif Chris@16: Chris@101: #endif Chris@101: Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: // Constructs using any expression implicitly convertible to the single argument Chris@101: // of a one-argument T constructor. Chris@101: // Converting constructions of optional from optional uses this function with Chris@101: // 'Expr' being of type 'U' and relying on a converting constructor of T from U. Chris@101: template Chris@101: void construct ( Expr&& expr, void const* ) Chris@101: { Chris@101: new (m_storage.address()) internal_type(boost::forward(expr)) ; Chris@101: m_initialized = true ; Chris@101: } Chris@101: Chris@101: // Assigns using a form any expression implicitly convertible to the single argument Chris@101: // of a T's assignment operator. Chris@101: // Converting assignments of optional from optional uses this function with Chris@101: // 'Expr' being of type 'U' and relying on a converting assignment of T from U. Chris@101: template Chris@101: void assign_expr_to_initialized ( Expr&& expr, void const* ) Chris@101: { Chris@101: assign_value(boost::forward(expr), is_reference_predicate()); Chris@101: } Chris@101: #else Chris@101: // Constructs using any expression implicitly convertible to the single argument Chris@16: // of a one-argument T constructor. Chris@16: // Converting constructions of optional from optional uses this function with Chris@16: // 'Expr' being of type 'U' and relying on a converting constructor of T from U. Chris@16: template Chris@16: void construct ( Expr const& expr, void const* ) Chris@16: { Chris@16: new (m_storage.address()) internal_type(expr) ; Chris@16: m_initialized = true ; Chris@16: } Chris@16: Chris@101: // Assigns using a form any expression implicitly convertible to the single argument Chris@16: // of a T's assignment operator. Chris@16: // Converting assignments of optional from optional uses this function with Chris@16: // 'Expr' being of type 'U' and relying on a converting assignment of T from U. Chris@16: template Chris@16: void assign_expr_to_initialized ( Expr const& expr, void const* ) Chris@16: { Chris@16: assign_value(expr, is_reference_predicate()); Chris@16: } Chris@16: Chris@101: #endif Chris@101: Chris@16: #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION Chris@16: // BCB5.64 (and probably lower versions) workaround. Chris@16: // The in-place factories are supported by means of catch-all constructors Chris@16: // and assignment operators (the functions are parameterized in terms of Chris@16: // an arbitrary 'Expr' type) Chris@16: // This compiler incorrectly resolves the overload set and sinks optional and optional Chris@16: // to the 'Expr'-taking functions even though explicit overloads are present for them. Chris@16: // Thus, the following overload is needed to properly handle the case when the 'lhs' Chris@16: // is another optional. Chris@16: // Chris@16: // For VC<=70 compilers this workaround dosen't work becasue the comnpiler issues and error Chris@16: // instead of choosing the wrong overload Chris@16: // Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: // Notice that 'Expr' will be optional or optional (but not optional_base<..>) Chris@101: template Chris@101: void construct ( Expr&& expr, optional_tag const* ) Chris@101: { Chris@101: if ( expr.is_initialized() ) Chris@101: { Chris@101: // An exception can be thrown here. Chris@101: // It it happens, THIS will be left uninitialized. Chris@101: new (m_storage.address()) internal_type(types::move(expr.get())) ; Chris@101: m_initialized = true ; Chris@101: } Chris@101: } Chris@101: #else Chris@16: // Notice that 'Expr' will be optional or optional (but not optional_base<..>) Chris@16: template Chris@16: void construct ( Expr const& expr, optional_tag const* ) Chris@16: { Chris@16: if ( expr.is_initialized() ) Chris@16: { Chris@16: // An exception can be thrown here. Chris@16: // It it happens, THIS will be left uninitialized. Chris@16: new (m_storage.address()) internal_type(expr.get()) ; Chris@16: m_initialized = true ; Chris@16: } Chris@16: } Chris@16: #endif Chris@101: #endif // defined BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION Chris@16: Chris@16: void assign_value ( argument_type val, is_not_reference_tag ) { get_impl() = val; } Chris@16: void assign_value ( argument_type val, is_reference_tag ) { construct(val); } Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: void assign_value ( rval_reference_type val, is_not_reference_tag ) { get_impl() = static_cast(val); } Chris@101: void assign_value ( rval_reference_type val, is_reference_tag ) { construct( static_cast(val) ); } Chris@101: #endif Chris@16: Chris@16: void destroy() Chris@16: { Chris@16: if ( m_initialized ) Chris@16: destroy_impl(is_reference_predicate()) ; Chris@16: } Chris@16: Chris@16: reference_const_type get_impl() const { return dereference(get_object(), is_reference_predicate() ) ; } Chris@16: reference_type get_impl() { return dereference(get_object(), is_reference_predicate() ) ; } Chris@16: Chris@16: pointer_const_type get_ptr_impl() const { return cast_ptr(get_object(), is_reference_predicate() ) ; } Chris@16: pointer_type get_ptr_impl() { return cast_ptr(get_object(), is_reference_predicate() ) ; } Chris@16: Chris@16: private : Chris@16: Chris@16: // internal_type can be either T or reference_content Chris@16: #if defined(BOOST_OPTIONAL_DETAIL_USE_ATTRIBUTE_MAY_ALIAS) Chris@16: // This workaround is supposed to silence GCC warnings about broken strict aliasing rules Chris@16: internal_type const* get_object() const Chris@16: { Chris@16: union { void const* ap_pvoid; internal_type const* as_ptype; } caster = { m_storage.address() }; Chris@16: return caster.as_ptype; Chris@16: } Chris@16: internal_type * get_object() Chris@16: { Chris@16: union { void* ap_pvoid; internal_type* as_ptype; } caster = { m_storage.address() }; Chris@16: return caster.as_ptype; Chris@16: } Chris@16: #else Chris@16: internal_type const* get_object() const { return static_cast(m_storage.address()); } Chris@16: internal_type * get_object() { return static_cast (m_storage.address()); } Chris@16: #endif Chris@16: Chris@16: // reference_content lacks an implicit conversion to T&, so the following is needed to obtain a proper reference. Chris@16: reference_const_type dereference( internal_type const* p, is_not_reference_tag ) const { return *p ; } Chris@16: reference_type dereference( internal_type* p, is_not_reference_tag ) { return *p ; } Chris@16: reference_const_type dereference( internal_type const* p, is_reference_tag ) const { return p->get() ; } Chris@16: reference_type dereference( internal_type* p, is_reference_tag ) { return p->get() ; } Chris@16: Chris@16: #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581)) Chris@16: void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->internal_type::~internal_type() ; m_initialized = false ; } Chris@16: #else Chris@101: void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->~T() ; m_initialized = false ; } Chris@16: #endif Chris@16: Chris@16: void destroy_impl ( is_reference_tag ) { m_initialized = false ; } Chris@16: Chris@16: // If T is of reference type, trying to get a pointer to the held value must result in a compile-time error. Chris@16: // Decent compilers should disallow conversions from reference_content* to T*, but just in case, Chris@16: // the following olverloads are used to filter out the case and guarantee an error in case of T being a reference. Chris@16: pointer_const_type cast_ptr( internal_type const* p, is_not_reference_tag ) const { return p ; } Chris@16: pointer_type cast_ptr( internal_type * p, is_not_reference_tag ) { return p ; } Chris@16: pointer_const_type cast_ptr( internal_type const* p, is_reference_tag ) const { return &p->get() ; } Chris@16: pointer_type cast_ptr( internal_type * p, is_reference_tag ) { return &p->get() ; } Chris@16: Chris@16: bool m_initialized ; Chris@16: storage_type m_storage ; Chris@16: } ; Chris@16: Chris@16: } // namespace optional_detail Chris@16: Chris@16: template Chris@16: class optional : public optional_detail::optional_base Chris@16: { Chris@16: typedef optional_detail::optional_base base ; Chris@16: Chris@16: public : Chris@16: Chris@16: typedef optional this_type ; Chris@16: Chris@16: typedef BOOST_DEDUCED_TYPENAME base::value_type value_type ; Chris@16: typedef BOOST_DEDUCED_TYPENAME base::reference_type reference_type ; Chris@16: typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ; Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: typedef BOOST_DEDUCED_TYPENAME base::rval_reference_type rval_reference_type ; Chris@101: typedef BOOST_DEDUCED_TYPENAME base::reference_type_of_temporary_wrapper reference_type_of_temporary_wrapper ; Chris@101: #endif Chris@16: typedef BOOST_DEDUCED_TYPENAME base::pointer_type pointer_type ; Chris@16: typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type pointer_const_type ; Chris@16: typedef BOOST_DEDUCED_TYPENAME base::argument_type argument_type ; Chris@16: Chris@16: // Creates an optional uninitialized. Chris@16: // No-throw Chris@101: optional() BOOST_NOEXCEPT : base() {} Chris@16: Chris@16: // Creates an optional uninitialized. Chris@16: // No-throw Chris@101: optional( none_t none_ ) BOOST_NOEXCEPT : base(none_) {} Chris@16: Chris@16: // Creates an optional initialized with 'val'. Chris@16: // Can throw if T::T(T const&) does Chris@16: optional ( argument_type val ) : base(val) {} Chris@16: Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: // Creates an optional initialized with 'move(val)'. Chris@101: // Can throw if T::T(T &&) does Chris@101: optional ( rval_reference_type val ) : base( boost::forward(val) ) Chris@101: {optional_detail::prevent_binding_rvalue_ref_to_optional_lvalue_ref();} Chris@101: #endif Chris@101: Chris@16: // Creates an optional initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional. Chris@16: // Can throw if T::T(T const&) does Chris@16: optional ( bool cond, argument_type val ) : base(cond,val) {} Chris@16: Chris@16: // NOTE: MSVC needs templated versions first Chris@16: Chris@16: // Creates a deep copy of another convertible optional Chris@16: // Requires a valid conversion from U to T. Chris@16: // Can throw if T::T(U const&) does Chris@16: template Chris@16: explicit optional ( optional const& rhs ) Chris@16: : Chris@16: base() Chris@16: { Chris@16: if ( rhs.is_initialized() ) Chris@16: this->construct(rhs.get()); Chris@16: } Chris@101: Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: // Creates a deep move of another convertible optional Chris@101: // Requires a valid conversion from U to T. Chris@101: // Can throw if T::T(U&&) does Chris@101: template Chris@101: explicit optional ( optional && rhs ) Chris@101: : Chris@101: base() Chris@101: { Chris@101: if ( rhs.is_initialized() ) Chris@101: this->construct( boost::move(rhs.get()) ); Chris@101: } Chris@16: #endif Chris@16: Chris@16: #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT Chris@16: // Creates an optional with an expression which can be either Chris@16: // (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n); Chris@16: // (b) An instance of TypedInPlaceFactory ( i.e. in_place(a,b,...,n); Chris@101: // (c) Any expression implicitly convertible to the single type Chris@16: // of a one-argument T's constructor. Chris@16: // (d*) Weak compilers (BCB) might also resolved Expr as optional and optional Chris@16: // even though explicit overloads are present for these. Chris@16: // Depending on the above some T ctor is called. Chris@101: // Can throw if the resolved T ctor throws. Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: Chris@101: Chris@101: template Chris@101: explicit optional ( Expr&& expr, Chris@101: BOOST_DEDUCED_TYPENAME boost::disable_if_c< Chris@101: (boost::is_base_of::type>::value) || Chris@101: boost::is_same::type, none_t>::value >::type* = 0 Chris@101: ) Chris@101: : base(boost::forward(expr),boost::addressof(expr)) Chris@101: {optional_detail::prevent_binding_rvalue_ref_to_optional_lvalue_ref();} Chris@101: Chris@101: #else Chris@16: template Chris@16: explicit optional ( Expr const& expr ) : base(expr,boost::addressof(expr)) {} Chris@101: #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: #endif // !defined BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT Chris@16: Chris@16: // Creates a deep copy of another optional Chris@16: // Can throw if T::T(T const&) does Chris@16: optional ( optional const& rhs ) : base( static_cast(rhs) ) {} Chris@16: Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: // Creates a deep move of another optional Chris@101: // Can throw if T::T(T&&) does Chris@101: optional ( optional && rhs ) Chris@101: BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible::value) Chris@101: : base( boost::move(rhs) ) Chris@101: {} Chris@101: Chris@101: #endif Chris@16: // No-throw (assuming T::~T() doesn't) Chris@16: ~optional() {} Chris@16: Chris@16: #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION) Chris@16: // Assigns from an expression. See corresponding constructor. Chris@16: // Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: Chris@101: template Chris@101: BOOST_DEDUCED_TYPENAME boost::disable_if_c< Chris@101: boost::is_base_of::type>::value || Chris@101: boost::is_same::type, none_t>::value, Chris@101: optional& Chris@101: >::type Chris@101: operator= ( Expr&& expr ) Chris@101: { Chris@101: optional_detail::prevent_binding_rvalue_ref_to_optional_lvalue_ref(); Chris@101: this->assign_expr(boost::forward(expr),boost::addressof(expr)); Chris@101: return *this ; Chris@101: } Chris@101: Chris@101: #else Chris@16: template Chris@16: optional& operator= ( Expr const& expr ) Chris@16: { Chris@16: this->assign_expr(expr,boost::addressof(expr)); Chris@16: return *this ; Chris@16: } Chris@101: #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: #endif // !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION) Chris@16: Chris@101: // Copy-assigns from another convertible optional (converts && deep-copies the rhs value) Chris@16: // Requires a valid conversion from U to T. Chris@16: // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED Chris@16: template Chris@16: optional& operator= ( optional const& rhs ) Chris@16: { Chris@16: this->assign(rhs); Chris@16: return *this ; Chris@16: } Chris@101: Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: // Move-assigns from another convertible optional (converts && deep-moves the rhs value) Chris@101: // Requires a valid conversion from U to T. Chris@101: // Basic Guarantee: If T::T( U && ) throws, this is left UNINITIALIZED Chris@101: template Chris@101: optional& operator= ( optional && rhs ) Chris@101: { Chris@101: this->assign(boost::move(rhs)); Chris@101: return *this ; Chris@101: } Chris@16: #endif Chris@16: Chris@16: // Assigns from another optional (deep-copies the rhs value) Chris@16: // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED Chris@16: // (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw) Chris@16: optional& operator= ( optional const& rhs ) Chris@16: { Chris@16: this->assign( static_cast(rhs) ) ; Chris@16: return *this ; Chris@16: } Chris@16: Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: // Assigns from another optional (deep-moves the rhs value) Chris@101: optional& operator= ( optional && rhs ) Chris@101: BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible::value && ::boost::is_nothrow_move_assignable::value) Chris@101: { Chris@101: this->assign( static_cast(rhs) ) ; Chris@101: return *this ; Chris@101: } Chris@101: #endif Chris@101: Chris@16: // Assigns from a T (deep-copies the rhs value) Chris@16: // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED Chris@16: optional& operator= ( argument_type val ) Chris@16: { Chris@16: this->assign( val ) ; Chris@16: return *this ; Chris@16: } Chris@16: Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: // Assigns from a T (deep-moves the rhs value) Chris@101: optional& operator= ( rval_reference_type val ) Chris@101: { Chris@101: optional_detail::prevent_binding_rvalue_ref_to_optional_lvalue_ref(); Chris@101: this->assign( boost::move(val) ) ; Chris@101: return *this ; Chris@101: } Chris@101: #endif Chris@101: Chris@16: // Assigns from a "none" Chris@16: // Which destroys the current value, if any, leaving this UNINITIALIZED Chris@16: // No-throw (assuming T::~T() doesn't) Chris@101: optional& operator= ( none_t none_ ) BOOST_NOEXCEPT Chris@16: { Chris@16: this->assign( none_ ) ; Chris@16: return *this ; Chris@16: } Chris@101: Chris@101: #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES) Chris@101: // Constructs in-place Chris@101: // upon exception *this is always uninitialized Chris@101: template Chris@101: void emplace ( Args&&... args ) Chris@101: { Chris@101: this->emplace_assign( boost::forward(args)... ); Chris@101: } Chris@101: #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) Chris@101: template Chris@101: void emplace ( Arg&& arg ) Chris@101: { Chris@101: this->emplace_assign( boost::forward(arg) ); Chris@101: } Chris@101: #else Chris@101: template Chris@101: void emplace ( const Arg& arg ) Chris@101: { Chris@101: this->emplace_assign( arg ); Chris@101: } Chris@101: Chris@101: template Chris@101: void emplace ( Arg& arg ) Chris@101: { Chris@101: this->emplace_assign( arg ); Chris@101: } Chris@101: #endif Chris@16: Chris@16: void swap( optional & arg ) Chris@101: BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible::value && ::boost::is_nothrow_move_assignable::value) Chris@16: { Chris@16: // allow for Koenig lookup Chris@101: boost::swap(*this, arg); Chris@16: } Chris@16: Chris@16: Chris@16: // Returns a reference to the value if this is initialized, otherwise, Chris@16: // the behaviour is UNDEFINED Chris@16: // No-throw Chris@16: reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); } Chris@16: reference_type get() { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); } Chris@16: Chris@16: // Returns a copy of the value if this is initialized, 'v' otherwise Chris@16: reference_const_type get_value_or ( reference_const_type v ) const { return this->is_initialized() ? get() : v ; } Chris@16: reference_type get_value_or ( reference_type v ) { return this->is_initialized() ? get() : v ; } Chris@16: Chris@16: // Returns a pointer to the value if this is initialized, otherwise, Chris@16: // the behaviour is UNDEFINED Chris@16: // No-throw Chris@16: pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; } Chris@16: pointer_type operator->() { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; } Chris@16: Chris@16: // Returns a reference to the value if this is initialized, otherwise, Chris@16: // the behaviour is UNDEFINED Chris@16: // No-throw Chris@101: #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) Chris@101: reference_const_type operator *() const& { return this->get() ; } Chris@101: reference_type operator *() & { return this->get() ; } Chris@101: reference_type_of_temporary_wrapper operator *() && { return base::types::move(this->get()) ; } Chris@101: #else Chris@16: reference_const_type operator *() const { return this->get() ; } Chris@16: reference_type operator *() { return this->get() ; } Chris@101: #endif // !defined BOOST_NO_CXX11_REF_QUALIFIERS Chris@16: Chris@101: #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) Chris@101: reference_const_type value() const& Chris@101: { Chris@101: if (this->is_initialized()) Chris@101: return this->get() ; Chris@101: else Chris@101: throw_exception(bad_optional_access()); Chris@101: } Chris@101: Chris@101: reference_type value() & Chris@101: { Chris@101: if (this->is_initialized()) Chris@101: return this->get() ; Chris@101: else Chris@101: throw_exception(bad_optional_access()); Chris@101: } Chris@101: Chris@101: reference_type_of_temporary_wrapper value() && Chris@101: { Chris@101: if (this->is_initialized()) Chris@101: return base::types::move(this->get()) ; Chris@101: else Chris@101: throw_exception(bad_optional_access()); Chris@101: } Chris@16: Chris@101: #else Chris@101: reference_const_type value() const Chris@101: { Chris@101: if (this->is_initialized()) Chris@101: return this->get() ; Chris@101: else Chris@101: throw_exception(bad_optional_access()); Chris@101: } Chris@101: Chris@101: reference_type value() Chris@101: { Chris@101: if (this->is_initialized()) Chris@101: return this->get() ; Chris@101: else Chris@101: throw_exception(bad_optional_access()); Chris@101: } Chris@101: #endif Chris@101: Chris@101: Chris@101: #ifndef BOOST_NO_CXX11_REF_QUALIFIERS Chris@101: template Chris@101: value_type value_or ( U&& v ) const& Chris@101: { Chris@101: if (this->is_initialized()) Chris@101: return get(); Chris@101: else Chris@101: return boost::forward(v); Chris@101: } Chris@101: Chris@101: template Chris@101: value_type value_or ( U&& v ) && Chris@101: { Chris@101: if (this->is_initialized()) Chris@101: return base::types::move(get()); Chris@101: else Chris@101: return boost::forward(v); Chris@101: } Chris@101: #elif !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: template Chris@101: value_type value_or ( U&& v ) const Chris@101: { Chris@101: if (this->is_initialized()) Chris@101: return get(); Chris@101: else Chris@101: return boost::forward(v); Chris@101: } Chris@101: #else Chris@101: template Chris@101: value_type value_or ( U const& v ) const Chris@101: { Chris@101: if (this->is_initialized()) Chris@101: return get(); Chris@101: else Chris@101: return v; Chris@101: } Chris@101: Chris@101: template Chris@101: value_type value_or ( U& v ) const Chris@101: { Chris@101: if (this->is_initialized()) Chris@101: return get(); Chris@101: else Chris@101: return v; Chris@101: } Chris@101: #endif Chris@101: Chris@101: Chris@101: #ifndef BOOST_NO_CXX11_REF_QUALIFIERS Chris@101: template Chris@101: value_type value_or_eval ( F f ) const& Chris@101: { Chris@101: if (this->is_initialized()) Chris@101: return get(); Chris@101: else Chris@101: return f(); Chris@101: } Chris@101: Chris@101: template Chris@101: value_type value_or_eval ( F f ) && Chris@101: { Chris@101: if (this->is_initialized()) Chris@101: return base::types::move(get()); Chris@101: else Chris@101: return f(); Chris@101: } Chris@101: #else Chris@101: template Chris@101: value_type value_or_eval ( F f ) const Chris@101: { Chris@101: if (this->is_initialized()) Chris@101: return get(); Chris@101: else Chris@101: return f(); Chris@101: } Chris@101: #endif Chris@101: Chris@101: bool operator!() const BOOST_NOEXCEPT { return !this->is_initialized() ; } Chris@101: Chris@101: BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT() Chris@16: } ; Chris@16: Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: template Chris@101: class optional Chris@101: { Chris@101: BOOST_STATIC_ASSERT_MSG(sizeof(T) == 0, "Optional rvalue references are illegal."); Chris@101: } ; Chris@101: #endif Chris@101: Chris@16: // Returns optional(v) Chris@16: template Chris@16: inline Chris@16: optional make_optional ( T const& v ) Chris@16: { Chris@16: return optional(v); Chris@16: } Chris@16: Chris@16: // Returns optional(cond,v) Chris@16: template Chris@16: inline Chris@16: optional make_optional ( bool cond, T const& v ) Chris@16: { Chris@16: return optional(cond,v); Chris@16: } Chris@16: Chris@16: // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED. Chris@16: // No-throw Chris@16: template Chris@16: inline Chris@16: BOOST_DEDUCED_TYPENAME optional::reference_const_type Chris@16: get ( optional const& opt ) Chris@16: { Chris@16: return opt.get() ; Chris@16: } Chris@16: Chris@16: template Chris@16: inline Chris@16: BOOST_DEDUCED_TYPENAME optional::reference_type Chris@16: get ( optional& opt ) Chris@16: { Chris@16: return opt.get() ; Chris@16: } Chris@16: Chris@16: // Returns a pointer to the value if this is initialized, otherwise, returns NULL. Chris@16: // No-throw Chris@16: template Chris@16: inline Chris@16: BOOST_DEDUCED_TYPENAME optional::pointer_const_type Chris@16: get ( optional const* opt ) Chris@16: { Chris@16: return opt->get_ptr() ; Chris@16: } Chris@16: Chris@16: template Chris@16: inline Chris@16: BOOST_DEDUCED_TYPENAME optional::pointer_type Chris@16: get ( optional* opt ) Chris@16: { Chris@16: return opt->get_ptr() ; Chris@16: } Chris@16: Chris@16: // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED. Chris@16: // No-throw Chris@16: template Chris@16: inline Chris@16: BOOST_DEDUCED_TYPENAME optional::reference_const_type Chris@16: get_optional_value_or ( optional const& opt, BOOST_DEDUCED_TYPENAME optional::reference_const_type v ) Chris@16: { Chris@16: return opt.get_value_or(v) ; Chris@16: } Chris@16: Chris@16: template Chris@16: inline Chris@16: BOOST_DEDUCED_TYPENAME optional::reference_type Chris@16: get_optional_value_or ( optional& opt, BOOST_DEDUCED_TYPENAME optional::reference_type v ) Chris@16: { Chris@16: return opt.get_value_or(v) ; Chris@16: } Chris@16: Chris@16: // Returns a pointer to the value if this is initialized, otherwise, returns NULL. Chris@16: // No-throw Chris@16: template Chris@16: inline Chris@16: BOOST_DEDUCED_TYPENAME optional::pointer_const_type Chris@16: get_pointer ( optional const& opt ) Chris@16: { Chris@16: return opt.get_ptr() ; Chris@16: } Chris@16: Chris@16: template Chris@16: inline Chris@16: BOOST_DEDUCED_TYPENAME optional::pointer_type Chris@16: get_pointer ( optional& opt ) Chris@16: { Chris@16: return opt.get_ptr() ; Chris@16: } Chris@16: Chris@101: // The following declaration prevents a bug where operator safe-bool is used upon streaming optional object if you forget the IO header. Chris@101: template Chris@101: std::basic_ostream& Chris@101: operator<<(std::basic_ostream& out, optional_detail::optional_tag const& v) Chris@101: { Chris@101: BOOST_STATIC_ASSERT_MSG(sizeof(CharType) == 0, "If you want to output boost::optional, include header "); Chris@101: } Chris@101: Chris@16: // optional's relational operators ( ==, !=, <, >, <=, >= ) have deep-semantics (compare values). Chris@16: // WARNING: This is UNLIKE pointers. Use equal_pointees()/less_pointess() in generic code instead. Chris@16: Chris@16: Chris@16: // Chris@16: // optional vs optional cases Chris@16: // Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator == ( optional const& x, optional const& y ) Chris@16: { return equal_pointees(x,y); } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator < ( optional const& x, optional const& y ) Chris@16: { return less_pointees(x,y); } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator != ( optional const& x, optional const& y ) Chris@16: { return !( x == y ) ; } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator > ( optional const& x, optional const& y ) Chris@16: { return y < x ; } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator <= ( optional const& x, optional const& y ) Chris@16: { return !( y < x ) ; } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator >= ( optional const& x, optional const& y ) Chris@16: { return !( x < y ) ; } Chris@16: Chris@16: Chris@16: // Chris@16: // optional vs T cases Chris@16: // Chris@16: template Chris@16: inline Chris@16: bool operator == ( optional const& x, T const& y ) Chris@16: { return equal_pointees(x, optional(y)); } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator < ( optional const& x, T const& y ) Chris@16: { return less_pointees(x, optional(y)); } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator != ( optional const& x, T const& y ) Chris@16: { return !( x == y ) ; } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator > ( optional const& x, T const& y ) Chris@16: { return y < x ; } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator <= ( optional const& x, T const& y ) Chris@16: { return !( y < x ) ; } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator >= ( optional const& x, T const& y ) Chris@16: { return !( x < y ) ; } Chris@16: Chris@16: // Chris@16: // T vs optional cases Chris@16: // Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator == ( T const& x, optional const& y ) Chris@16: { return equal_pointees( optional(x), y ); } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator < ( T const& x, optional const& y ) Chris@16: { return less_pointees( optional(x), y ); } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator != ( T const& x, optional const& y ) Chris@16: { return !( x == y ) ; } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator > ( T const& x, optional const& y ) Chris@16: { return y < x ; } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator <= ( T const& x, optional const& y ) Chris@16: { return !( y < x ) ; } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator >= ( T const& x, optional const& y ) Chris@16: { return !( x < y ) ; } Chris@16: Chris@16: Chris@16: // Chris@16: // optional vs none cases Chris@16: // Chris@16: Chris@16: template Chris@16: inline Chris@101: bool operator == ( optional const& x, none_t ) BOOST_NOEXCEPT Chris@101: { return !x; } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator < ( optional const& x, none_t ) Chris@16: { return less_pointees(x,optional() ); } Chris@16: Chris@16: template Chris@16: inline Chris@101: bool operator != ( optional const& x, none_t ) BOOST_NOEXCEPT Chris@101: { return bool(x); } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator > ( optional const& x, none_t y ) Chris@16: { return y < x ; } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator <= ( optional const& x, none_t y ) Chris@16: { return !( y < x ) ; } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator >= ( optional const& x, none_t y ) Chris@16: { return !( x < y ) ; } Chris@16: Chris@16: // Chris@16: // none vs optional cases Chris@16: // Chris@16: Chris@16: template Chris@16: inline Chris@101: bool operator == ( none_t , optional const& y ) BOOST_NOEXCEPT Chris@101: { return !y; } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator < ( none_t , optional const& y ) Chris@16: { return less_pointees(optional() ,y); } Chris@16: Chris@16: template Chris@16: inline Chris@101: bool operator != ( none_t, optional const& y ) BOOST_NOEXCEPT Chris@101: { return bool(y); } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator > ( none_t x, optional const& y ) Chris@16: { return y < x ; } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator <= ( none_t x, optional const& y ) Chris@16: { return !( y < x ) ; } Chris@16: Chris@16: template Chris@16: inline Chris@16: bool operator >= ( none_t x, optional const& y ) Chris@16: { return !( x < y ) ; } Chris@16: Chris@16: namespace optional_detail { Chris@16: Chris@16: template struct swap_selector; Chris@16: Chris@16: template<> Chris@16: struct swap_selector Chris@16: { Chris@16: template Chris@16: static void optional_swap ( optional& x, optional& y ) Chris@16: { Chris@16: const bool hasX = !!x; Chris@16: const bool hasY = !!y; Chris@16: Chris@16: if ( !hasX && !hasY ) Chris@16: return; Chris@16: Chris@16: if( !hasX ) Chris@16: x = boost::in_place(); Chris@16: else if ( !hasY ) Chris@16: y = boost::in_place(); Chris@16: Chris@16: // Boost.Utility.Swap will take care of ADL and workarounds for broken compilers Chris@16: boost::swap(x.get(),y.get()); Chris@16: Chris@16: if( !hasX ) Chris@16: y = boost::none ; Chris@16: else if( !hasY ) Chris@16: x = boost::none ; Chris@16: } Chris@16: }; Chris@16: Chris@101: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@101: template<> Chris@101: struct swap_selector Chris@101: { Chris@101: template Chris@101: static void optional_swap ( optional& x, optional& y ) Chris@101: //BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible::value && BOOST_NOEXCEPT_EXPR(boost::swap(*x, *y))) Chris@101: { Chris@101: if(x) Chris@101: { Chris@101: if (y) Chris@101: { Chris@101: boost::swap(*x, *y); Chris@101: } Chris@101: else Chris@101: { Chris@101: y = boost::move(*x); Chris@101: x = boost::none; Chris@101: } Chris@101: } Chris@101: else Chris@101: { Chris@101: if (y) Chris@101: { Chris@101: x = boost::move(*y); Chris@101: y = boost::none; Chris@101: } Chris@101: } Chris@101: } Chris@101: }; Chris@101: #else Chris@16: template<> Chris@16: struct swap_selector Chris@16: { Chris@16: template Chris@16: static void optional_swap ( optional& x, optional& y ) Chris@16: { Chris@16: const bool hasX = !!x; Chris@16: const bool hasY = !!y; Chris@16: Chris@16: if ( !hasX && hasY ) Chris@16: { Chris@16: x = y.get(); Chris@16: y = boost::none ; Chris@16: } Chris@16: else if ( hasX && !hasY ) Chris@16: { Chris@16: y = x.get(); Chris@16: x = boost::none ; Chris@16: } Chris@16: else if ( hasX && hasY ) Chris@16: { Chris@16: // Boost.Utility.Swap will take care of ADL and workarounds for broken compilers Chris@16: boost::swap(x.get(),y.get()); Chris@16: } Chris@16: } Chris@16: }; Chris@101: #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES Chris@16: Chris@16: } // namespace optional_detail Chris@16: Chris@16: template Chris@16: struct optional_swap_should_use_default_constructor : has_nothrow_default_constructor {} ; Chris@16: Chris@16: template inline void swap ( optional& x, optional& y ) Chris@101: //BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible::value && BOOST_NOEXCEPT_EXPR(boost::swap(*x, *y))) Chris@16: { Chris@16: optional_detail::swap_selector::value>::optional_swap(x, y); Chris@16: } Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif