From f258713788d43b9377a857882420fe2cfa10c2f1 Mon Sep 17 00:00:00 2001 From: Fernando Cacciola Date: Thu, 1 Mar 2007 21:39:25 +0000 Subject: [PATCH] Fixed operator->() for optional references. [SVN r37124] --- include/boost/optional/optional.hpp | 57 +++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/include/boost/optional/optional.hpp b/include/boost/optional/optional.hpp index 4421f4a..2135a59 100644 --- a/include/boost/optional/optional.hpp +++ b/include/boost/optional/optional.hpp @@ -185,6 +185,16 @@ class optional_base : public optional_tag { construct(val); } + + // Creates an optional initialized with 'val' IFF cond is true, otherwise creates an uninitialzed optional. + // Can throw if T::T(T const&) does + optional_base ( bool cond, argument_type val ) + : + m_initialized(false) + { + if ( cond ) + construct(val); + } // Creates a deep copy of another optional // Can throw if T::T(T const&) does @@ -418,6 +428,8 @@ class optional_base : public optional_tag // the following olverloads are used to filter out the case and guarantee an error in case of T being a reference. pointer_const_type cast_ptr( internal_type const* p, is_not_reference_tag ) const { return p ; } pointer_type cast_ptr( internal_type * p, is_not_reference_tag ) { return p ; } + pointer_const_type cast_ptr( internal_type const* p, is_reference_tag ) const { return &p->get() ; } + pointer_type cast_ptr( internal_type * p, is_reference_tag ) { return &p->get() ; } bool m_initialized ; storage_type m_storage ; @@ -455,6 +467,9 @@ class optional : public optional_detail::optional_base // Can throw if T::T(T const&) does optional ( argument_type val ) : base(val) {} + // Creates an optional initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional. + // Can throw if T::T(T const&) does + optional ( bool cond, argument_type val ) : base(cond,val) {} #ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR // NOTE: MSVC needs templated versions first @@ -549,6 +564,10 @@ class optional : public optional_detail::optional_base reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); } reference_type get() { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); } + // Returns a copy of the value if this is initialized, 'v' otherwise + reference_const_type get_value_or ( reference_const_type v ) const { return this->is_initialized() ? get() : v ; } + reference_type get_value_or ( reference_type v ) { return this->is_initialized() ? get() : v ; } + // Returns a pointer to the value if this is initialized, otherwise, // the behaviour is UNDEFINED // No-throw @@ -570,6 +589,22 @@ class optional : public optional_detail::optional_base bool operator!() const { return !this->is_initialized() ; } } ; +// Returns optional(v) +template +inline +optional make_optional ( T const& v ) +{ + return optional(v); +} + +// Returns optional(cond,v) +template +inline +optional make_optional ( bool cond, T const& v ) +{ + return optional(cond,v); +} + // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED. // No-throw template @@ -606,6 +641,24 @@ get ( optional* opt ) return opt->get_ptr() ; } +// Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED. +// No-throw +template +inline +BOOST_DEDUCED_TYPENAME optional::reference_const_type +get_optional_value_or ( optional const& opt, BOOST_DEDUCED_TYPENAME optional::reference_const_type v ) +{ + return opt.get_value_or(v) ; +} + +template +inline +BOOST_DEDUCED_TYPENAME optional::reference_type +get_optional_value_or ( optional& opt, BOOST_DEDUCED_TYPENAME optional::reference_type v ) +{ + return opt.get_value_or(v) ; +} + // Returns a pointer to the value if this is initialized, otherwise, returns NULL. // No-throw template @@ -767,10 +820,6 @@ template inline void swap ( optional& x, optional& y ) optional_detail::optional_swap(x,y); } -template inline optional make_optional ( T const& v ) -{ - return optional(v); -} } // namespace boost