williamr@2: // Copyright (C) 2003, Fernando Luis Cacciola Carballal. williamr@2: // williamr@2: // Use, modification, and distribution is subject to the Boost Software williamr@2: // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at williamr@2: // http://www.boost.org/LICENSE_1_0.txt) williamr@2: // williamr@2: // See http://www.boost.org/lib/optional for documentation. williamr@2: // williamr@2: // You are welcome to contact the author at: williamr@2: // fernando_cacciola@hotmail.com williamr@2: // williamr@2: #ifndef BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP williamr@2: #define BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP williamr@2: williamr@2: #include williamr@2: #include williamr@2: williamr@2: #include "boost/config.hpp" williamr@2: #include "boost/assert.hpp" williamr@2: #include "boost/type.hpp" williamr@2: #include "boost/type_traits/alignment_of.hpp" williamr@2: #include "boost/type_traits/type_with_alignment.hpp" williamr@2: #include "boost/type_traits/remove_reference.hpp" williamr@2: #include "boost/type_traits/is_reference.hpp" williamr@2: #include "boost/mpl/if.hpp" williamr@2: #include "boost/mpl/bool.hpp" williamr@2: #include "boost/mpl/not.hpp" williamr@2: #include "boost/detail/reference_content.hpp" williamr@2: #include "boost/none.hpp" williamr@2: #include "boost/utility/compare_pointees.hpp" williamr@2: williamr@2: #include "boost/optional/optional_fwd.hpp" williamr@2: williamr@2: #if BOOST_WORKAROUND(BOOST_MSVC, == 1200) williamr@2: // VC6.0 has the following bug: williamr@2: // When a templated assignment operator exist, an implicit conversion williamr@2: // constructing an optional is used when assigment of the form: williamr@2: // optional opt ; opt = T(...); williamr@2: // is compiled. williamr@2: // However, optional's ctor is _explicit_ and the assignemt shouldn't compile. williamr@2: // Therefore, for VC6.0 templated assignment is disabled. williamr@2: // williamr@2: #define BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT williamr@2: #endif williamr@2: williamr@2: #if BOOST_WORKAROUND(BOOST_MSVC, == 1300) williamr@2: // VC7.0 has the following bug: williamr@2: // When both a non-template and a template copy-ctor exist williamr@2: // and the templated version is made 'explicit', the explicit is also williamr@2: // given to the non-templated version, making the class non-implicitely-copyable. williamr@2: // williamr@2: #define BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR williamr@2: #endif williamr@2: williamr@2: #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) || BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION,<=700) williamr@2: // AFAICT only VC7.1 correctly resolves the overload set williamr@2: // that includes the in-place factory taking functions, williamr@2: // so for the other VC versions, in-place factory support williamr@2: // is disabled williamr@2: #define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT williamr@2: #endif williamr@2: williamr@2: #if BOOST_WORKAROUND(__BORLANDC__, <= 0x551) williamr@2: // BCB (5.5.1) cannot parse the nested template struct in an inplace factory. williamr@2: #define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT williamr@2: #endif williamr@2: williamr@2: #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) \ williamr@2: && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581) ) williamr@2: // BCB (up to 5.64) has the following bug: williamr@2: // If there is a member function/operator template of the form williamr@2: // template mfunc( Expr expr ) ; williamr@2: // some calls are resolved to this even if there are other better matches. williamr@2: // The effect of this bug is that calls to converting ctors and assignments williamr@2: // are incrorrectly sink to this general catch-all member function template as shown above. williamr@2: #define BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION williamr@2: #endif williamr@2: williamr@2: // Daniel Wallin discovered that bind/apply.hpp badly interacts with the apply<> williamr@2: // member template of a factory as used in the optional<> implementation. williamr@2: // He proposed this simple fix which is to move the call to apply<> outside williamr@2: // namespace boost. williamr@2: namespace boost_optional_detail williamr@2: { williamr@2: template williamr@2: void construct(Factory const& factory, void* address) williamr@2: { williamr@2: factory.BOOST_NESTED_TEMPLATE apply(address); williamr@2: } williamr@2: } williamr@2: williamr@2: williamr@2: namespace boost { williamr@2: williamr@2: class in_place_factory_base ; williamr@2: class typed_in_place_factory_base ; williamr@2: williamr@2: namespace optional_detail { williamr@2: williamr@2: // This local class is used instead of that in "aligned_storage.hpp" williamr@2: // because I've found the 'official' class to ICE BCB5.5 williamr@2: // when some types are used with optional<> williamr@2: // (due to sizeof() passed down as a non-type template parameter) williamr@2: template williamr@2: class aligned_storage williamr@2: { williamr@2: // Borland ICEs if unnamed unions are used for this! williamr@2: union dummy_u williamr@2: { williamr@2: char data[ sizeof(T) ]; williamr@2: BOOST_DEDUCED_TYPENAME type_with_alignment< williamr@2: ::boost::alignment_of::value >::type aligner_; williamr@2: } dummy_ ; williamr@2: williamr@2: public: williamr@2: williamr@2: void const* address() const { return &dummy_.data[0]; } williamr@2: void * address() { return &dummy_.data[0]; } williamr@2: } ; williamr@2: williamr@2: template williamr@2: struct types_when_isnt_ref williamr@2: { williamr@2: typedef T const& reference_const_type ; williamr@2: typedef T & reference_type ; williamr@2: typedef T const* pointer_const_type ; williamr@2: typedef T * pointer_type ; williamr@2: typedef T const& argument_type ; williamr@2: } ; williamr@2: template williamr@2: struct types_when_is_ref williamr@2: { williamr@2: typedef BOOST_DEDUCED_TYPENAME remove_reference::type raw_type ; williamr@2: williamr@2: typedef raw_type& reference_const_type ; williamr@2: typedef raw_type& reference_type ; williamr@2: typedef raw_type* pointer_const_type ; williamr@2: typedef raw_type* pointer_type ; williamr@2: typedef raw_type& argument_type ; williamr@2: } ; williamr@2: williamr@2: struct optional_tag {} ; williamr@2: williamr@2: template williamr@2: class optional_base : public optional_tag williamr@2: { williamr@2: private : williamr@2: williamr@2: typedef williamr@2: #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) williamr@2: BOOST_DEDUCED_TYPENAME williamr@2: #endif williamr@2: ::boost::detail::make_reference_content::type internal_type ; williamr@2: williamr@2: typedef aligned_storage storage_type ; williamr@2: williamr@2: typedef types_when_isnt_ref types_when_not_ref ; williamr@2: typedef types_when_is_ref types_when_ref ; williamr@2: williamr@2: typedef optional_base this_type ; williamr@2: williamr@2: protected : williamr@2: williamr@2: typedef T value_type ; williamr@2: williamr@2: typedef mpl::true_ is_reference_tag ; williamr@2: typedef mpl::false_ is_not_reference_tag ; williamr@2: williamr@2: typedef BOOST_DEDUCED_TYPENAME is_reference::type is_reference_predicate ; williamr@2: williamr@2: typedef BOOST_DEDUCED_TYPENAME mpl::if_::type types ; williamr@2: williamr@2: typedef bool (this_type::*unspecified_bool_type)() const; williamr@2: williamr@2: typedef BOOST_DEDUCED_TYPENAME types::reference_type reference_type ; williamr@2: typedef BOOST_DEDUCED_TYPENAME types::reference_const_type reference_const_type ; williamr@2: typedef BOOST_DEDUCED_TYPENAME types::pointer_type pointer_type ; williamr@2: typedef BOOST_DEDUCED_TYPENAME types::pointer_const_type pointer_const_type ; williamr@2: typedef BOOST_DEDUCED_TYPENAME types::argument_type argument_type ; williamr@2: williamr@2: // Creates an optional uninitialized. williamr@2: // No-throw williamr@2: optional_base() williamr@2: : williamr@2: m_initialized(false) {} williamr@2: williamr@2: // Creates an optional uninitialized. williamr@2: // No-throw williamr@2: optional_base ( none_t ) williamr@2: : williamr@2: m_initialized(false) {} williamr@2: williamr@2: // Creates an optional initialized with 'val'. williamr@2: // Can throw if T::T(T const&) does williamr@2: optional_base ( argument_type val ) williamr@2: : williamr@2: m_initialized(false) williamr@2: { williamr@2: construct(val); williamr@2: } williamr@2: williamr@2: // Creates an optional initialized with 'val' IFF cond is true, otherwise creates an uninitialzed optional. williamr@2: // Can throw if T::T(T const&) does williamr@2: optional_base ( bool cond, argument_type val ) williamr@2: : williamr@2: m_initialized(false) williamr@2: { williamr@2: if ( cond ) williamr@2: construct(val); williamr@2: } williamr@2: williamr@2: // Creates a deep copy of another optional williamr@2: // Can throw if T::T(T const&) does williamr@2: optional_base ( optional_base const& rhs ) williamr@2: : williamr@2: m_initialized(false) williamr@2: { williamr@2: if ( rhs.is_initialized() ) williamr@2: construct(rhs.get_impl()); williamr@2: } williamr@2: williamr@2: williamr@2: // This is used for both converting and in-place constructions. williamr@2: // Derived classes use the 'tag' to select the appropriate williamr@2: // implementation (the correct 'construct()' overload) williamr@2: template williamr@2: explicit optional_base ( Expr const& expr, Expr const* tag ) williamr@2: : williamr@2: m_initialized(false) williamr@2: { williamr@2: construct(expr,tag); williamr@2: } williamr@2: williamr@2: williamr@2: williamr@2: // No-throw (assuming T::~T() doesn't) williamr@2: ~optional_base() { destroy() ; } williamr@2: williamr@2: // Assigns from another optional (deep-copies the rhs value) williamr@2: void assign ( optional_base const& rhs ) williamr@2: { williamr@2: if (is_initialized()) williamr@2: { williamr@2: if ( rhs.is_initialized() ) williamr@2: assign_value(rhs.get_impl(), is_reference_predicate() ); williamr@2: else destroy(); williamr@2: } williamr@2: else williamr@2: { williamr@2: if ( rhs.is_initialized() ) williamr@2: construct(rhs.get_impl()); williamr@2: } williamr@2: } williamr@2: williamr@2: // Assigns from another _convertible_ optional (deep-copies the rhs value) williamr@2: template williamr@2: void assign ( optional const& rhs ) williamr@2: { williamr@2: if (is_initialized()) williamr@2: { williamr@2: if ( rhs.is_initialized() ) williamr@2: assign_value(static_cast(rhs.get()), is_reference_predicate() ); williamr@2: else destroy(); williamr@2: } williamr@2: else williamr@2: { williamr@2: if ( rhs.is_initialized() ) williamr@2: construct(static_cast(rhs.get())); williamr@2: } williamr@2: } williamr@2: williamr@2: // Assigns from a T (deep-copies the rhs value) williamr@2: void assign ( argument_type val ) williamr@2: { williamr@2: if (is_initialized()) williamr@2: assign_value(val, is_reference_predicate() ); williamr@2: else construct(val); williamr@2: } williamr@2: williamr@2: // Assigns from "none", destroying the current value, if any, leaving this UNINITIALIZED williamr@2: // No-throw (assuming T::~T() doesn't) williamr@2: void assign ( none_t ) { destroy(); } williamr@2: williamr@2: #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT williamr@2: template williamr@2: void assign_expr ( Expr const& expr, Expr const* tag ) williamr@2: { williamr@2: if (is_initialized()) williamr@2: assign_expr_to_initialized(expr,tag); williamr@2: else construct(expr,tag); williamr@2: } williamr@2: #endif williamr@2: williamr@2: public : williamr@2: williamr@2: // Destroys the current value, if any, leaving this UNINITIALIZED williamr@2: // No-throw (assuming T::~T() doesn't) williamr@2: void reset() { destroy(); } williamr@2: williamr@2: // Replaces the current value -if any- with 'val' williamr@2: void reset ( argument_type val ) { assign(val); } williamr@2: williamr@2: // Returns a pointer to the value if this is initialized, otherwise, williamr@2: // returns NULL. williamr@2: // No-throw williamr@2: pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; } williamr@2: pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; } williamr@2: williamr@2: bool is_initialized() const { return m_initialized ; } williamr@2: williamr@2: protected : williamr@2: williamr@2: void construct ( argument_type val ) williamr@2: { williamr@2: new (m_storage.address()) internal_type(val) ; williamr@2: m_initialized = true ; williamr@2: } williamr@2: williamr@2: #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT williamr@2: // Constructs in-place using the given factory williamr@2: template williamr@2: void construct ( Expr const& factory, in_place_factory_base const* ) williamr@2: { williamr@2: BOOST_STATIC_ASSERT ( ::boost::mpl::not_::value ) ; williamr@2: boost_optional_detail::construct(factory, m_storage.address()); williamr@2: m_initialized = true ; williamr@2: } williamr@2: williamr@2: // Constructs in-place using the given typed factory williamr@2: template williamr@2: void construct ( Expr const& factory, typed_in_place_factory_base const* ) williamr@2: { williamr@2: BOOST_STATIC_ASSERT ( ::boost::mpl::not_::value ) ; williamr@2: factory.apply(m_storage.address()) ; williamr@2: m_initialized = true ; williamr@2: } williamr@2: williamr@2: template williamr@2: void assign_expr_to_initialized ( Expr const& factory, in_place_factory_base const* tag ) williamr@2: { williamr@2: destroy(); williamr@2: construct(factory,tag); williamr@2: } williamr@2: williamr@2: // Constructs in-place using the given typed factory williamr@2: template williamr@2: void assign_expr_to_initialized ( Expr const& factory, typed_in_place_factory_base const* tag ) williamr@2: { williamr@2: destroy(); williamr@2: construct(factory,tag); williamr@2: } williamr@2: #endif williamr@2: williamr@2: // Constructs using any expression implicitely convertible to the single argument williamr@2: // of a one-argument T constructor. williamr@2: // Converting constructions of optional from optional uses this function with williamr@2: // 'Expr' being of type 'U' and relying on a converting constructor of T from U. williamr@2: template williamr@2: void construct ( Expr const& expr, void const* ) williamr@2: { williamr@2: new (m_storage.address()) internal_type(expr) ; williamr@2: m_initialized = true ; williamr@2: } williamr@2: williamr@2: // Assigns using a form any expression implicitely convertible to the single argument williamr@2: // of a T's assignment operator. williamr@2: // Converting assignments of optional from optional uses this function with williamr@2: // 'Expr' being of type 'U' and relying on a converting assignment of T from U. williamr@2: template williamr@2: void assign_expr_to_initialized ( Expr const& expr, void const* ) williamr@2: { williamr@2: assign_value(expr, is_reference_predicate()); williamr@2: } williamr@2: williamr@2: #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION williamr@2: // BCB5.64 (and probably lower versions) workaround. williamr@2: // The in-place factories are supported by means of catch-all constructors williamr@2: // and assignment operators (the functions are parameterized in terms of williamr@2: // an arbitrary 'Expr' type) williamr@2: // This compiler incorrectly resolves the overload set and sinks optional and optional williamr@2: // to the 'Expr'-taking functions even though explicit overloads are present for them. williamr@2: // Thus, the following overload is needed to properly handle the case when the 'lhs' williamr@2: // is another optional. williamr@2: // williamr@2: // For VC<=70 compilers this workaround dosen't work becasue the comnpiler issues and error williamr@2: // instead of choosing the wrong overload williamr@2: // williamr@2: // Notice that 'Expr' will be optional or optional (but not optional_base<..>) williamr@2: template williamr@2: void construct ( Expr const& expr, optional_tag const* ) williamr@2: { williamr@2: if ( expr.is_initialized() ) williamr@2: { williamr@2: // An exception can be thrown here. williamr@2: // It it happens, THIS will be left uninitialized. williamr@2: new (m_storage.address()) internal_type(expr.get()) ; williamr@2: m_initialized = true ; williamr@2: } williamr@2: } williamr@2: #endif williamr@2: williamr@2: void assign_value ( argument_type val, is_not_reference_tag ) { get_impl() = val; } williamr@2: void assign_value ( argument_type val, is_reference_tag ) { construct(val); } williamr@2: williamr@2: void destroy() williamr@2: { williamr@2: if ( m_initialized ) williamr@2: destroy_impl(is_reference_predicate()) ; williamr@2: } williamr@2: williamr@2: unspecified_bool_type safe_bool() const { return m_initialized ? &this_type::is_initialized : 0 ; } williamr@2: williamr@2: reference_const_type get_impl() const { return dereference(get_object(), is_reference_predicate() ) ; } williamr@2: reference_type get_impl() { return dereference(get_object(), is_reference_predicate() ) ; } williamr@2: williamr@2: pointer_const_type get_ptr_impl() const { return cast_ptr(get_object(), is_reference_predicate() ) ; } williamr@2: pointer_type get_ptr_impl() { return cast_ptr(get_object(), is_reference_predicate() ) ; } williamr@2: williamr@2: private : williamr@2: williamr@2: // internal_type can be either T or reference_content williamr@2: internal_type const* get_object() const { return static_cast(m_storage.address()); } williamr@2: internal_type * get_object() { return static_cast (m_storage.address()); } williamr@2: williamr@2: // reference_content lacks an implicit conversion to T&, so the following is needed to obtain a proper reference. williamr@2: reference_const_type dereference( internal_type const* p, is_not_reference_tag ) const { return *p ; } williamr@2: reference_type dereference( internal_type* p, is_not_reference_tag ) { return *p ; } williamr@2: reference_const_type dereference( internal_type const* p, is_reference_tag ) const { return p->get() ; } williamr@2: reference_type dereference( internal_type* p, is_reference_tag ) { return p->get() ; } williamr@2: williamr@2: #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581)) williamr@2: void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->internal_type::~internal_type() ; m_initialized = false ; } williamr@2: #else williamr@2: void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->T::~T() ; m_initialized = false ; } williamr@2: #endif williamr@2: williamr@2: void destroy_impl ( is_reference_tag ) { m_initialized = false ; } williamr@2: williamr@2: // If T is of reference type, trying to get a pointer to the held value must result in a compile-time error. williamr@2: // Decent compilers should disallow conversions from reference_content* to T*, but just in case, williamr@2: // the following olverloads are used to filter out the case and guarantee an error in case of T being a reference. williamr@2: pointer_const_type cast_ptr( internal_type const* p, is_not_reference_tag ) const { return p ; } williamr@2: pointer_type cast_ptr( internal_type * p, is_not_reference_tag ) { return p ; } williamr@2: pointer_const_type cast_ptr( internal_type const* p, is_reference_tag ) const { return &p->get() ; } williamr@2: pointer_type cast_ptr( internal_type * p, is_reference_tag ) { return &p->get() ; } williamr@2: williamr@2: bool m_initialized ; williamr@2: storage_type m_storage ; williamr@2: } ; williamr@2: williamr@2: } // namespace optional_detail williamr@2: williamr@2: template williamr@2: class optional : public optional_detail::optional_base williamr@2: { williamr@2: typedef optional_detail::optional_base base ; williamr@2: williamr@2: typedef BOOST_DEDUCED_TYPENAME base::unspecified_bool_type unspecified_bool_type ; williamr@2: williamr@2: public : williamr@2: williamr@2: typedef optional this_type ; williamr@2: williamr@2: typedef BOOST_DEDUCED_TYPENAME base::value_type value_type ; williamr@2: typedef BOOST_DEDUCED_TYPENAME base::reference_type reference_type ; williamr@2: typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ; williamr@2: typedef BOOST_DEDUCED_TYPENAME base::pointer_type pointer_type ; williamr@2: typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type pointer_const_type ; williamr@2: typedef BOOST_DEDUCED_TYPENAME base::argument_type argument_type ; williamr@2: williamr@2: // Creates an optional uninitialized. williamr@2: // No-throw williamr@2: optional() : base() {} williamr@2: williamr@2: // Creates an optional uninitialized. williamr@2: // No-throw williamr@2: optional( none_t none_ ) : base(none_) {} williamr@2: williamr@2: // Creates an optional initialized with 'val'. williamr@2: // Can throw if T::T(T const&) does williamr@2: optional ( argument_type val ) : base(val) {} williamr@2: williamr@2: // Creates an optional initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional. williamr@2: // Can throw if T::T(T const&) does williamr@2: optional ( bool cond, argument_type val ) : base(cond,val) {} williamr@2: williamr@2: #ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR williamr@2: // NOTE: MSVC needs templated versions first williamr@2: williamr@2: // Creates a deep copy of another convertible optional williamr@2: // Requires a valid conversion from U to T. williamr@2: // Can throw if T::T(U const&) does williamr@2: template williamr@2: explicit optional ( optional const& rhs ) williamr@2: : williamr@2: base() williamr@2: { williamr@2: if ( rhs.is_initialized() ) williamr@2: this->construct(rhs.get()); williamr@2: } williamr@2: #endif williamr@2: williamr@2: #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT williamr@2: // Creates an optional with an expression which can be either williamr@2: // (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n); williamr@2: // (b) An instance of TypedInPlaceFactory ( i.e. in_place(a,b,...,n); williamr@2: // (c) Any expression implicitely convertible to the single type williamr@2: // of a one-argument T's constructor. williamr@2: // (d*) Weak compilers (BCB) might also resolved Expr as optional and optional williamr@2: // even though explicit overloads are present for these. williamr@2: // Depending on the above some T ctor is called. williamr@2: // Can throw is the resolved T ctor throws. williamr@2: template williamr@2: explicit optional ( Expr const& expr ) : base(expr,&expr) {} williamr@2: #endif williamr@2: williamr@2: // Creates a deep copy of another optional williamr@2: // Can throw if T::T(T const&) does williamr@2: optional ( optional const& rhs ) : base(rhs) {} williamr@2: williamr@2: // No-throw (assuming T::~T() doesn't) williamr@2: ~optional() {} williamr@2: williamr@2: #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION) williamr@2: // Assigns from an expression. See corresponding constructor. williamr@2: // Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED williamr@2: template williamr@2: optional& operator= ( Expr expr ) williamr@2: { williamr@2: this->assign_expr(expr,&expr); williamr@2: return *this ; williamr@2: } williamr@2: #endif williamr@2: williamr@2: williamr@2: #ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT williamr@2: // Assigns from another convertible optional (converts && deep-copies the rhs value) williamr@2: // Requires a valid conversion from U to T. williamr@2: // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED williamr@2: template williamr@2: optional& operator= ( optional const& rhs ) williamr@2: { williamr@2: this->assign(rhs); williamr@2: return *this ; williamr@2: } williamr@2: #endif williamr@2: williamr@2: // Assigns from another optional (deep-copies the rhs value) williamr@2: // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED williamr@2: // (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw) williamr@2: optional& operator= ( optional const& rhs ) williamr@2: { williamr@2: this->assign( rhs ) ; williamr@2: return *this ; williamr@2: } williamr@2: williamr@2: // Assigns from a T (deep-copies the rhs value) williamr@2: // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED williamr@2: optional& operator= ( argument_type val ) williamr@2: { williamr@2: this->assign( val ) ; williamr@2: return *this ; williamr@2: } williamr@2: williamr@2: // Assigns from a "none" williamr@2: // Which destroys the current value, if any, leaving this UNINITIALIZED williamr@2: // No-throw (assuming T::~T() doesn't) williamr@2: optional& operator= ( none_t none_ ) williamr@2: { williamr@2: this->assign( none_ ) ; williamr@2: return *this ; williamr@2: } williamr@2: williamr@2: // Returns a reference to the value if this is initialized, otherwise, williamr@2: // the behaviour is UNDEFINED williamr@2: // No-throw williamr@2: reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); } williamr@2: reference_type get() { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); } williamr@2: williamr@2: // Returns a copy of the value if this is initialized, 'v' otherwise williamr@2: reference_const_type get_value_or ( reference_const_type v ) const { return this->is_initialized() ? get() : v ; } williamr@2: reference_type get_value_or ( reference_type v ) { return this->is_initialized() ? get() : v ; } williamr@2: williamr@2: // Returns a pointer to the value if this is initialized, otherwise, williamr@2: // the behaviour is UNDEFINED williamr@2: // No-throw williamr@2: pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; } williamr@2: pointer_type operator->() { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; } williamr@2: williamr@2: // Returns a reference to the value if this is initialized, otherwise, williamr@2: // the behaviour is UNDEFINED williamr@2: // No-throw williamr@2: reference_const_type operator *() const { return this->get() ; } williamr@2: reference_type operator *() { return this->get() ; } williamr@2: williamr@2: // implicit conversion to "bool" williamr@2: // No-throw williamr@2: operator unspecified_bool_type() const { return this->safe_bool() ; } williamr@2: williamr@2: // This is provided for those compilers which don't like the conversion to bool williamr@2: // on some contexts. williamr@2: bool operator!() const { return !this->is_initialized() ; } williamr@2: } ; williamr@2: williamr@2: // Returns optional(v) williamr@2: template williamr@2: inline williamr@2: optional make_optional ( T const& v ) williamr@2: { williamr@2: return optional(v); williamr@2: } williamr@2: williamr@2: // Returns optional(cond,v) williamr@2: template williamr@2: inline williamr@2: optional make_optional ( bool cond, T const& v ) williamr@2: { williamr@2: return optional(cond,v); williamr@2: } williamr@2: williamr@2: // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED. williamr@2: // No-throw williamr@2: template williamr@2: inline williamr@2: BOOST_DEDUCED_TYPENAME optional::reference_const_type williamr@2: get ( optional const& opt ) williamr@2: { williamr@2: return opt.get() ; williamr@2: } williamr@2: williamr@2: template williamr@2: inline williamr@2: BOOST_DEDUCED_TYPENAME optional::reference_type williamr@2: get ( optional& opt ) williamr@2: { williamr@2: return opt.get() ; williamr@2: } williamr@2: williamr@2: // Returns a pointer to the value if this is initialized, otherwise, returns NULL. williamr@2: // No-throw williamr@2: template williamr@2: inline williamr@2: BOOST_DEDUCED_TYPENAME optional::pointer_const_type williamr@2: get ( optional const* opt ) williamr@2: { williamr@2: return opt->get_ptr() ; williamr@2: } williamr@2: williamr@2: template williamr@2: inline williamr@2: BOOST_DEDUCED_TYPENAME optional::pointer_type williamr@2: get ( optional* opt ) williamr@2: { williamr@2: return opt->get_ptr() ; williamr@2: } williamr@2: williamr@2: // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED. williamr@2: // No-throw williamr@2: template williamr@2: inline williamr@2: BOOST_DEDUCED_TYPENAME optional::reference_const_type williamr@2: get_optional_value_or ( optional const& opt, BOOST_DEDUCED_TYPENAME optional::reference_const_type v ) williamr@2: { williamr@2: return opt.get_value_or(v) ; williamr@2: } williamr@2: williamr@2: template williamr@2: inline williamr@2: BOOST_DEDUCED_TYPENAME optional::reference_type williamr@2: get_optional_value_or ( optional& opt, BOOST_DEDUCED_TYPENAME optional::reference_type v ) williamr@2: { williamr@2: return opt.get_value_or(v) ; williamr@2: } williamr@2: williamr@2: // Returns a pointer to the value if this is initialized, otherwise, returns NULL. williamr@2: // No-throw williamr@2: template williamr@2: inline williamr@2: BOOST_DEDUCED_TYPENAME optional::pointer_const_type williamr@2: get_pointer ( optional const& opt ) williamr@2: { williamr@2: return opt.get_ptr() ; williamr@2: } williamr@2: williamr@2: template williamr@2: inline williamr@2: BOOST_DEDUCED_TYPENAME optional::pointer_type williamr@2: get_pointer ( optional& opt ) williamr@2: { williamr@2: return opt.get_ptr() ; williamr@2: } williamr@2: williamr@2: // optional's relational operators ( ==, !=, <, >, <=, >= ) have deep-semantics (compare values). williamr@2: // WARNING: This is UNLIKE pointers. Use equal_pointees()/less_pointess() in generic code instead. williamr@2: williamr@2: williamr@2: // williamr@2: // optional vs optional cases williamr@2: // williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator == ( optional const& x, optional const& y ) williamr@2: { return equal_pointees(x,y); } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator < ( optional const& x, optional const& y ) williamr@2: { return less_pointees(x,y); } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator != ( optional const& x, optional const& y ) williamr@2: { return !( x == y ) ; } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator > ( optional const& x, optional const& y ) williamr@2: { return y < x ; } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator <= ( optional const& x, optional const& y ) williamr@2: { return !( y < x ) ; } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator >= ( optional const& x, optional const& y ) williamr@2: { return !( x < y ) ; } williamr@2: williamr@2: williamr@2: // williamr@2: // optional vs T cases williamr@2: // williamr@2: template williamr@2: inline williamr@2: bool operator == ( optional const& x, T const& y ) williamr@2: { return equal_pointees(x, optional(y)); } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator < ( optional const& x, T const& y ) williamr@2: { return less_pointees(x, optional(y)); } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator != ( optional const& x, T const& y ) williamr@2: { return !( x == y ) ; } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator > ( optional const& x, T const& y ) williamr@2: { return y < x ; } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator <= ( optional const& x, T const& y ) williamr@2: { return !( y < x ) ; } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator >= ( optional const& x, T const& y ) williamr@2: { return !( x < y ) ; } williamr@2: williamr@2: // williamr@2: // T vs optional cases williamr@2: // williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator == ( T const& x, optional const& y ) williamr@2: { return equal_pointees( optional(x), y ); } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator < ( T const& x, optional const& y ) williamr@2: { return less_pointees( optional(x), y ); } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator != ( T const& x, optional const& y ) williamr@2: { return !( x == y ) ; } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator > ( T const& x, optional const& y ) williamr@2: { return y < x ; } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator <= ( T const& x, optional const& y ) williamr@2: { return !( y < x ) ; } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator >= ( T const& x, optional const& y ) williamr@2: { return !( x < y ) ; } williamr@2: williamr@2: williamr@2: // williamr@2: // optional vs none cases williamr@2: // williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator == ( optional const& x, none_t ) williamr@2: { return equal_pointees(x, optional() ); } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator < ( optional const& x, none_t ) williamr@2: { return less_pointees(x,optional() ); } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator != ( optional const& x, none_t y ) williamr@2: { return !( x == y ) ; } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator > ( optional const& x, none_t y ) williamr@2: { return y < x ; } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator <= ( optional const& x, none_t y ) williamr@2: { return !( y < x ) ; } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator >= ( optional const& x, none_t y ) williamr@2: { return !( x < y ) ; } williamr@2: williamr@2: // williamr@2: // none vs optional cases williamr@2: // williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator == ( none_t x, optional const& y ) williamr@2: { return equal_pointees(optional() ,y); } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator < ( none_t x, optional const& y ) williamr@2: { return less_pointees(optional() ,y); } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator != ( none_t x, optional const& y ) williamr@2: { return !( x == y ) ; } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator > ( none_t x, optional const& y ) williamr@2: { return y < x ; } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator <= ( none_t x, optional const& y ) williamr@2: { return !( y < x ) ; } williamr@2: williamr@2: template williamr@2: inline williamr@2: bool operator >= ( none_t x, optional const& y ) williamr@2: { return !( x < y ) ; } williamr@2: williamr@2: // williamr@2: // The following swap implementation follows the GCC workaround as found in williamr@2: // "boost/detail/compressed_pair.hpp" williamr@2: // williamr@2: namespace optional_detail { williamr@2: williamr@2: // GCC < 3.2 gets the using declaration at namespace scope (FLC, DWA) williamr@2: #if BOOST_WORKAROUND(__GNUC__, < 3) \ williamr@2: || BOOST_WORKAROUND(__GNUC__, == 3) && __GNUC_MINOR__ <= 2 williamr@2: using std::swap; williamr@2: #define BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE williamr@2: #endif williamr@2: williamr@2: // optional's swap: williamr@2: // If both are initialized, calls swap(T&, T&). If this swap throws, both will remain initialized but their values are now unspecified. williamr@2: // If only one is initialized, calls U.reset(*I), THEN I.reset(). williamr@2: // If U.reset(*I) throws, both are left UNCHANGED (U is kept uinitialized and I is never reset) williamr@2: // If both are uninitialized, do nothing (no-throw) williamr@2: template williamr@2: inline williamr@2: void optional_swap ( optional& x, optional& y ) williamr@2: { williamr@2: if ( !x && !!y ) williamr@2: { williamr@2: x.reset(*y); williamr@2: y.reset(); williamr@2: } williamr@2: else if ( !!x && !y ) williamr@2: { williamr@2: y.reset(*x); williamr@2: x.reset(); williamr@2: } williamr@2: else if ( !!x && !!y ) williamr@2: { williamr@2: // GCC > 3.2 and all other compilers have the using declaration at function scope (FLC) williamr@2: #ifndef BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE williamr@2: // allow for Koenig lookup williamr@2: using std::swap ; williamr@2: #endif williamr@2: swap(*x,*y); williamr@2: } williamr@2: } williamr@2: williamr@2: } // namespace optional_detail williamr@2: williamr@2: template inline void swap ( optional& x, optional& y ) williamr@2: { williamr@2: optional_detail::optional_swap(x,y); williamr@2: } williamr@2: williamr@2: williamr@2: } // namespace boost williamr@2: williamr@2: #endif williamr@2: