Compare commits

..

5 Commits

Author SHA1 Message Date
d645adc070 This commit was manufactured by cvs2svn to create tag
'Version_1_30_0'.

[SVN r18026]
2003-03-20 02:53:48 +00:00
929847570c Merged to RC
[SVN r17957]
2003-03-17 13:42:06 +00:00
63a928a510 Merged typename fix from trunk to branch.
[SVN r17913]
2003-03-14 13:00:11 +00:00
650b4a89b3 Alignment bug fixed
[SVN r17892]
2003-03-13 15:38:04 +00:00
f8fb254fff This commit was manufactured by cvs2svn to create branch 'RC_1_30_0'.
[SVN r17693]
2003-03-01 19:43:06 +00:00
29 changed files with 815 additions and 2684 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,56 +0,0 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#ifndef BOOST_UTILITY_INPLACE_FACTORY_25AGO2003_HPP
#define BOOST_UTILITY_INPLACE_FACTORY_25AGO2003_HPP
#include <boost/detail/in_place_factory_prefix.hpp>
#include <boost/type.hpp>
namespace boost {
class InPlaceFactoryBase {} ;
#define BOOST_DEFINE_INPLACE_FACTORY_CLASS(z,n,_) \
template< BOOST_PP_ENUM_PARAMS(BOOST_PP_INC(n),class A) > \
class BOOST_PP_CAT(InPlaceFactory, BOOST_PP_INC(n) ) : public InPlaceFactoryBase \
{ \
public: \
\
BOOST_PP_CAT(InPlaceFactory, BOOST_PP_INC(n) ) ( BOOST_PP_ENUM_BINARY_PARAMS(BOOST_PP_INC(n),A,const& a) ) \
: \
BOOST_PP_ENUM( BOOST_PP_INC(n), BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_INIT, _ ) \
{} \
\
template<class T> \
void apply ( void* address BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(T) ) const \
{ \
new ( address ) T ( BOOST_PP_ENUM_PARAMS( BOOST_PP_INC(n), m_a ) ) ; \
} \
\
BOOST_PP_REPEAT( BOOST_PP_INC(n), BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_DECL, _) \
} ; \
\
template< BOOST_PP_ENUM_PARAMS(BOOST_PP_INC(n),class A) > \
BOOST_PP_CAT(InPlaceFactory, BOOST_PP_INC(n) ) < BOOST_PP_ENUM_PARAMS( BOOST_PP_INC(n), A ) > \
in_place ( BOOST_PP_ENUM_BINARY_PARAMS(BOOST_PP_INC(n),A, const& a) ) \
{ \
return BOOST_PP_CAT(InPlaceFactory, BOOST_PP_INC(n) ) < BOOST_PP_ENUM_PARAMS( BOOST_PP_INC(n), A ) > \
( BOOST_PP_ENUM_PARAMS( BOOST_PP_INC(n), a ) ) ; \
} ; \
BOOST_PP_REPEAT( BOOST_MAX_INPLACE_FACTORY_ARITY, BOOST_DEFINE_INPLACE_FACTORY_CLASS, BOOST_PP_EMPTY() )
} // namespace boost
#endif

View File

@ -1,33 +0,0 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#ifndef BOOST_UTILITY_INPLACE_FACTORY_PREFIX_25AGO2003_HPP
#define BOOST_UTILITY_INPLACE_FACTORY_PREFIX_25AGO2003_HPP
#include <boost/config.hpp>
#include <boost/preprocessor/repetition/enum.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>
#include <boost/preprocessor/repetition/enum_binary_params.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/arithmetic/inc.hpp>
#include <boost/preprocessor/punctuation/paren.hpp>
#include <boost/preprocessor/facilities/empty.hpp>
#define BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_INIT(z,n,_) BOOST_PP_CAT(m_a,n) BOOST_PP_LPAREN() BOOST_PP_CAT(a,n) BOOST_PP_RPAREN()
#define BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_DECL(z,n,_) BOOST_PP_CAT(A,n) const& BOOST_PP_CAT(m_a,n);
#define BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_ARG(z,n,_) BOOST_PP_CAT(m_a,n)
#define BOOST_MAX_INPLACE_FACTORY_ARITY 10
#undef BOOST_UTILITY_INPLACE_FACTORY_SUFFIX_25AGO2003_HPP
#endif

View File

@ -1,23 +0,0 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#ifndef BOOST_UTILITY_INPLACE_FACTORY_SUFFIX_25AGO2003_HPP
#define BOOST_UTILITY_INPLACE_FACTORY_SUFFIX_25AGO2003_HPP
#undef BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_INIT
#undef BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_DECL
#undef BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_ARG
#undef BOOST_MAX_INPLACE_FACTORY_ARITY
#undef BOOST_UTILITY_INPLACE_FACTORY_PREFIX_25AGO2003_HPP
#endif

View File

@ -1,31 +0,0 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#ifndef BOOST_UTILITY_NONE_17SEP2003_HPP
#define BOOST_UTILITY_NONE_17SEP2003_HPP
#include "boost/detail/none_t.hpp"
// NOTE: Borland users have to include this header outside any precompiled headers
// (bcc<=5.64 cannot include instance data in a precompiled header)
namespace boost {
namespace {
detail::none_t const none = ((detail::none_t)0) ;
}
} // namespace boost
#endif

View File

@ -1,28 +0,0 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#ifndef BOOST_DETAIL_NONE_T_17SEP2003_HPP
#define BOOST_DETAIL_NONE_T_17SEP2003_HPP
namespace boost {
namespace detail {
struct none_helper{};
typedef int none_helper::*none_t ;
} // namespace detail
} // namespace boost
#endif

View File

@ -1,57 +0,0 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#ifndef BOOST_UTILITY_TYPED_INPLACE_FACTORY_25AGO2003_HPP
#define BOOST_UTILITY_TYPED_INPLACE_FACTORY_25AGO2003_HPP
#include <boost/detail/in_place_factory_prefix.hpp>
namespace boost {
class TypedInPlaceFactoryBase {} ;
#define BOOST_DEFINE_TYPED_INPLACE_FACTORY_CLASS(z,n,_) \
template< class T, BOOST_PP_ENUM_PARAMS(BOOST_PP_INC(n),class A) > \
class BOOST_PP_CAT(TypedInPlaceFactory, BOOST_PP_INC(n) ) : public TypedInPlaceFactoryBase \
{ \
public: \
\
typedef T value_type ; \
\
BOOST_PP_CAT(TypedInPlaceFactory, BOOST_PP_INC(n) ) ( BOOST_PP_ENUM_BINARY_PARAMS(BOOST_PP_INC(n),A,const& a) ) \
: \
BOOST_PP_ENUM( BOOST_PP_INC(n), BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_INIT, _ ) \
{} \
\
void apply ( void* address ) const \
{ \
new ( address ) T ( BOOST_PP_ENUM_PARAMS( BOOST_PP_INC(n), m_a ) ) ; \
} \
\
BOOST_PP_REPEAT( BOOST_PP_INC(n), BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_DECL, _) \
} ; \
\
template< class T, BOOST_PP_ENUM_PARAMS(BOOST_PP_INC(n),class A) > \
BOOST_PP_CAT(TypedInPlaceFactory, BOOST_PP_INC(n) ) < T , BOOST_PP_ENUM_PARAMS( BOOST_PP_INC(n), A ) > \
in_place ( BOOST_PP_ENUM_BINARY_PARAMS(BOOST_PP_INC(n),A, const& a) ) \
{ \
return BOOST_PP_CAT(TypedInPlaceFactory, BOOST_PP_INC(n) ) < T, BOOST_PP_ENUM_PARAMS( BOOST_PP_INC(n), A ) > \
( BOOST_PP_ENUM_PARAMS( BOOST_PP_INC(n), a ) ) ; \
} ; \
BOOST_PP_REPEAT( BOOST_MAX_INPLACE_FACTORY_ARITY, BOOST_DEFINE_TYPED_INPLACE_FACTORY_CLASS, BOOST_PP_EMPTY() )
} // namespace boost
#include <boost/detail/in_place_factory_suffix.hpp>
#endif

View File

@ -1,8 +1,13 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
// (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// This material is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
// Permission to use or copy this software for any purpose is hereby granted
// without fee, provided the above notices are retained on all copies.
// Permission to modify the code and to distribute modified code is granted,
// provided the above notices are retained, and a notice that the code was
// modified is included with the above copyright notice.
//
// See http://www.boost.org/lib/optional for documentation.
//
@ -17,17 +22,8 @@
#include "boost/config.hpp"
#include "boost/assert.hpp"
#include "boost/type.hpp"
#include "boost/type_traits/alignment_of.hpp"
#include "boost/type_traits/type_with_alignment.hpp"
#include "boost/type_traits/remove_reference.hpp"
#include "boost/type_traits/is_reference.hpp"
#include "boost/mpl/if.hpp"
#include "boost/mpl/bool.hpp"
#include "boost/mpl/not.hpp"
#include "boost/detail/reference_content.hpp"
#include "boost/detail/none_t.hpp"
#include "boost/utility/compare_pointees.hpp"
#if BOOST_WORKAROUND(BOOST_MSVC, == 1200)
// VC6.0 has the following bug:
@ -50,345 +46,56 @@
#define BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
#endif
#if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) || BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION,<=700)
// AFAICT only VC7.1 correctly resolves the overload set
// that includes the in-place factory taking functions,
// so for the other VC versions, in-place factory support
// is disabled
#define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
#endif
#if BOOST_WORKAROUND(__BORLANDC__, <= 0x551)
// BCB (5.5.1) cannot parse the nested template struct in an inplace factory.
#define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
#endif
#if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) \
&& BOOST_WORKAROUND(__BORLANDC__, <= 0x564)
// BCB (up to 5.64) has the following bug:
// If there is a member function/operator template of the form
// template<class Expr> mfunc( Expr expr ) ;
// some calls are resolved to this even if there are other better matches.
// The effect of this bug is that calls to converting ctors and assignments
// are incrorrectly sink to this general catch-all member function template as shown above.
#define BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
#endif
namespace boost {
class InPlaceFactoryBase ;
class TypedInPlaceFactoryBase ;
namespace optional_detail {
// This local class is used instead of that in "aligned_storage.hpp"
// because I've found the 'official' class to ICE BCB5.5
// when some types are used with optional<>
// (due to sizeof() passed down as a non-type template parameter)
template <class T>
class aligned_storage
namespace boost
{
// Borland ICEs if unnamed unions are used for this!
union dummy_u
namespace optional_detail
{
template <class T>
class aligned_storage
{
char data[ sizeof(T) ];
BOOST_DEDUCED_TYPENAME type_with_alignment<
::boost::alignment_of<T>::value >::type aligner_;
} dummy_ ;
// Borland ICEs if unnamed unions are used for this!
union dummy_u
{
char data[ sizeof(T) ];
BOOST_DEDUCED_TYPENAME type_with_alignment<
::boost::alignment_of<T>::value >::type aligner_;
} dummy_ ;
public:
public:
void const* address() const { return &dummy_.data[0]; }
void * address() { return &dummy_.data[0]; }
} ;
void const* address() const { return &dummy_.data[0]; }
void * address() { return &dummy_.data[0]; }
} ;
}
template<class T>
struct types_when_isnt_ref
class optional
{
typedef T const& reference_const_type ;
typedef T & reference_type ;
typedef T const* pointer_const_type ;
typedef T * pointer_type ;
typedef T const& argument_type ;
} ;
template<class T>
struct types_when_is_ref
{
typedef BOOST_DEDUCED_TYPENAME remove_reference<T>::type raw_type ;
typedef optional<T> this_type ;
typedef raw_type& reference_const_type ;
typedef raw_type& reference_type ;
typedef raw_type* pointer_const_type ;
typedef raw_type* pointer_type ;
typedef raw_type& argument_type ;
} ;
typedef optional_detail::aligned_storage<T> storage_type ;
struct optional_tag {} ;
template<class T>
class optional_base : public optional_tag
{
private :
typedef BOOST_DEDUCED_TYPENAME detail::make_reference_content<T>::type internal_type ;
typedef aligned_storage<internal_type> storage_type ;
typedef types_when_isnt_ref<T> types_when_not_ref ;
typedef types_when_is_ref<T> types_when_ref ;
typedef optional_base<T> this_type ;
protected :
typedef void (this_type::*unspecified_bool_type)();
public :
typedef T value_type ;
typedef mpl::true_ is_reference_tag ;
typedef mpl::false_ is_not_reference_tag ;
typedef BOOST_DEDUCED_TYPENAME is_reference<T>::type is_reference_predicate ;
typedef BOOST_DEDUCED_TYPENAME mpl::if_<is_reference_predicate,types_when_ref,types_when_not_ref>::type types ;
typedef bool (this_type::*unspecified_bool_type)() const;
typedef BOOST_DEDUCED_TYPENAME types::reference_type reference_type ;
typedef BOOST_DEDUCED_TYPENAME types::reference_const_type reference_const_type ;
typedef BOOST_DEDUCED_TYPENAME types::pointer_type pointer_type ;
typedef BOOST_DEDUCED_TYPENAME types::pointer_const_type pointer_const_type ;
typedef BOOST_DEDUCED_TYPENAME types::argument_type argument_type ;
// Creates an optional<T> uninitialized.
// No-throw
optional_base()
:
m_initialized(false) {}
// Creates an optional<T> uninitialized.
// No-throw
optional_base ( detail::none_t const& )
optional ()
:
m_initialized(false) {}
// Creates an optional<T> initialized with 'val'.
// Can throw if T::T(T const&) does
optional_base ( argument_type val )
explicit optional ( T const& val )
:
m_initialized(false)
{
construct(val);
}
// Creates a deep copy of another optional<T>
// Can throw if T::T(T const&) does
optional_base ( optional_base const& rhs )
:
m_initialized(false)
{
if ( rhs.is_initialized() )
construct(rhs.get_impl());
}
// This is used for both converting and in-place constructions.
// Derived classes use the 'tag' to select the appropriate
// implementation (the correct 'construct()' overload)
template<class Expr>
explicit optional_base ( Expr const& expr, Expr const* tag )
:
m_initialized(false)
{
construct(expr,tag);
}
// No-throw (assuming T::~T() doesn't)
~optional_base() { destroy() ; }
// Assigns from another optional<T> (deep-copies the rhs value)
// Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
void assign ( optional_base const& rhs )
{
destroy();
if ( rhs.is_initialized() )
construct(rhs.get_impl());
}
// Assigns from a T (deep-copies the rhs value)
// Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED
void assign ( argument_type val )
{
destroy();
construct(val);
}
// Assigns from "none", destroying the current value, if any, leaving this UNINITIALIZED
// No-throw (assuming T::~T() doesn't)
void assign ( detail::none_t const& ) { destroy(); }
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
template<class Expr>
void assign_expr ( Expr const& expr, Expr const* tag )
{
destroy();
construct(expr,tag);
}
#endif
public :
// Destroys the current value, if any, leaving this UNINITIALIZED
// No-throw (assuming T::~T() doesn't)
void reset() { destroy(); }
// Replaces the current value -if any- with 'val'
// Basic Guarantee: If T::T( T const& ) throws this is left UNINITIALIZED.
void reset ( argument_type val ) { assign(val); }
// Returns a pointer to the value if this is initialized, otherwise,
// returns NULL.
// No-throw
pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; }
bool is_initialized() const { return m_initialized ; }
protected :
void construct ( argument_type val )
{
new (m_storage.address()) internal_type(val) ;
m_initialized = true ;
}
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
// Constructs in-place using the given factory
template<class Expr>
void construct ( Expr const& factory, InPlaceFactoryBase const* )
{
BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
factory.BOOST_NESTED_TEMPLATE apply<value_type>(m_storage.address()) ;
m_initialized = true ;
}
// Constructs in-place using the given typed factory
template<class Expr>
void construct ( Expr const& factory, TypedInPlaceFactoryBase const* )
{
BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
factory.apply(m_storage.address()) ;
m_initialized = true ;
}
#endif
// Constructs using any expression implicitely convertible to the single argument
// of a one-argument T constructor.
// Converting constructions of optional<T> from optional<U> uses this function with
// 'Expr' being of type 'U' and relying on a converting constructor of T from U.
template<class Expr>
void construct ( Expr const& expr, void const* )
{
new (m_storage.address()) internal_type(expr) ;
m_initialized = true ;
}
#ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
// BCB5.64 (and probably lower versions) workaround.
// The in-place factories are supported by means of catch-all constructors
// and assignment operators (the functions are parameterized in terms of
// an arbitrary 'Expr' type)
// This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
// to the 'Expr'-taking functions even though explicit overloads are present for them.
// Thus, the following overload is needed to properly handle the case when the 'lhs'
// is another optional.
//
// For VC<=70 compilers this workaround dosen't work becasue the comnpiler issues and error
// instead of choosing the wrong overload
//
// Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
template<class Expr>
void construct ( Expr const& expr, optional_tag const* )
{
if ( expr.is_initialized() )
{
// An exception can be thrown here.
// It it happens, THIS will be left uninitialized.
new (m_storage.address()) internal_type(expr.get()) ;
m_initialized = true ;
}
}
#endif
void destroy()
{
if ( m_initialized )
destroy_impl(is_reference_predicate()) ;
}
unspecified_bool_type safe_bool() const { return m_initialized ? &this_type::is_initialized : 0 ; }
reference_const_type get_impl() const { return dereference(get_object(), is_reference_predicate() ) ; }
reference_type get_impl() { return dereference(get_object(), is_reference_predicate() ) ; }
pointer_const_type get_ptr_impl() const { return cast_ptr(get_object(), is_reference_predicate() ) ; }
pointer_type get_ptr_impl() { return cast_ptr(get_object(), is_reference_predicate() ) ; }
private :
// internal_type can be either T or reference_content<T>
internal_type const* get_object() const { return static_cast<internal_type const*>(m_storage.address()); }
internal_type * get_object() { return static_cast<internal_type *> (m_storage.address()); }
// reference_content<T> lacks an implicit conversion to T&, so the following is needed to obtain a proper reference.
reference_const_type dereference( internal_type const* p, is_not_reference_tag ) const { return *p ; }
reference_type dereference( internal_type* p, is_not_reference_tag ) { return *p ; }
reference_const_type dereference( internal_type const* p, is_reference_tag ) const { return p->get() ; }
reference_type dereference( internal_type* p, is_reference_tag ) { return p->get() ; }
void destroy_impl ( is_not_reference_tag ) { get_impl().~T() ; m_initialized = false ; }
void destroy_impl ( is_reference_tag ) { m_initialized = false ; }
// If T is of reference type, trying to get a pointer to the held value must result in a compile-time error.
// Decent compilers should disallow conversions from reference_content<T>* to T*, but just in case,
// 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 ; }
bool m_initialized ;
storage_type m_storage ;
} ;
} // namespace optional_detail
template<class T>
class optional : public optional_detail::optional_base<T>
{
typedef optional_detail::optional_base<T> base ;
typedef BOOST_DEDUCED_TYPENAME base::unspecified_bool_type unspecified_bool_type ;
public :
typedef optional<T> this_type ;
typedef BOOST_DEDUCED_TYPENAME base::value_type value_type ;
typedef BOOST_DEDUCED_TYPENAME base::reference_type reference_type ;
typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ;
typedef BOOST_DEDUCED_TYPENAME base::pointer_type pointer_type ;
typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type pointer_const_type ;
typedef BOOST_DEDUCED_TYPENAME base::argument_type argument_type ;
// Creates an optional<T> uninitialized.
// No-throw
optional() : base() {}
// Creates an optional<T> uninitialized.
// No-throw
optional( detail::none_t const& none_ ) : base(none_) {}
// Creates an optional<T> initialized with 'val'.
// Can throw if T::T(T const&) does
optional ( argument_type val ) : base(val) {}
#ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
// NOTE: MSVC needs templated versions first
@ -398,44 +105,25 @@ class optional : public optional_detail::optional_base<T>
template<class U>
explicit optional ( optional<U> const& rhs )
:
base()
m_initialized(false)
{
if ( rhs.is_initialized() )
this->construct(rhs.get());
if ( rhs )
construct(*rhs);
}
#endif
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
// Creates an optional<T> with an expression which can be either
// (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n);
// (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n);
// (c) Any expression implicitely convertible to the single type
// of a one-argument T's constructor.
// (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U>
// even though explicit overloads are present for these.
// Depending on the above some T ctor is called.
// Can throw is the resolved T ctor throws.
template<class Expr>
explicit optional ( Expr const& expr ) : base(expr,&expr) {}
#endif
// Creates a deep copy of another optional<T>
// Can throw if T::T(T const&) does
optional ( optional const& rhs ) : base(rhs) {}
optional ( optional const& rhs )
:
m_initialized(false)
{
if ( rhs )
construct(*rhs);
}
// No-throw (assuming T::~T() doesn't)
~optional() {}
#if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
// Assigns from an expression. See corresponding constructor.
// Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED
template<class Expr>
optional& operator= ( Expr expr )
{
this->assign_expr(expr,&expr);
return *this ;
}
#endif
~optional() { destroy() ; }
#ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
// Assigns from another convertible optional<U> (converts && deep-copies the rhs value)
@ -444,13 +132,13 @@ class optional : public optional_detail::optional_base<T>
template<class U>
optional& operator= ( optional<U> const& rhs )
{
this->destroy(); // no-throw
destroy(); // no-throw
if ( rhs.is_initialized() )
if ( rhs )
{
// An exception can be thrown here.
// It it happens, THIS will be left uninitialized.
this->assign(rhs.get());
construct(*rhs);
}
return *this ;
}
@ -458,203 +146,124 @@ class optional : public optional_detail::optional_base<T>
// Assigns from another optional<T> (deep-copies the rhs value)
// Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
// (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw)
optional& operator= ( optional const& rhs )
{
this->assign( rhs ) ;
destroy(); // no-throw
if ( rhs )
{
// An exception can be thrown here.
// It it happens, THIS will be left uninitialized.
construct(*rhs);
}
return *this ;
}
// Assigns from a T (deep-copies the rhs value)
// Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED
optional& operator= ( argument_type val )
{
this->assign( val ) ;
return *this ;
}
// Assigns from a "none"
// Which destroys the current value, if any, leaving this UNINITIALIZED
// Destroys the current value, if any, leaving this UNINITIALIZED
// No-throw (assuming T::~T() doesn't)
optional& operator= ( detail::none_t const& none_ )
void reset()
{
this->assign( none_ ) ;
return *this ;
destroy();
}
// Returns a reference to the value if this is initialized, otherwise,
// the behaviour is UNDEFINED
// Replaces the current value -if any- with 'val'
// Basic Guarantee: If T::T( T const& ) throws this is left UNINITIALIZED.
void reset ( T const& val )
{
destroy();
construct(val);
}
// Returns a pointer to the value if this is initialized, otherwise,
// returns NULL.
// No-throw
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(); }
T const* get() const { return m_initialized ? static_cast<T const*>(m_storage.address()) : 0 ; }
T* get() { return m_initialized ? static_cast<T*> (m_storage.address()) : 0 ; }
// Returns a pointer to the value if this is initialized, otherwise,
// the behaviour is UNDEFINED
// No-throw
pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
pointer_type operator->() { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
T const* operator->() const { BOOST_ASSERT(m_initialized) ; return static_cast<T const*>(m_storage.address()) ; }
T* operator->() { BOOST_ASSERT(m_initialized) ; return static_cast<T*> (m_storage.address()) ; }
// Returns a reference to the value if this is initialized, otherwise,
// the behaviour is UNDEFINED
// No-throw
reference_const_type operator *() const { return this->get() ; }
reference_type operator *() { return this->get() ; }
T const& operator *() const { BOOST_ASSERT(m_initialized) ; return *static_cast<T const*>(m_storage.address()) ; }
T& operator *() { BOOST_ASSERT(m_initialized) ; return *static_cast<T*> (m_storage.address()) ; }
// implicit conversion to "bool"
// No-throw
operator unspecified_bool_type() const { return this->safe_bool() ; }
operator unspecified_bool_type() const { return m_initialized ? &this_type::destroy : 0 ; }
// This is provided for those compilers which don't like the conversion to bool
// on some contexts.
bool operator!() const { return !this->is_initialized() ; }
bool operator!() const { return !m_initialized ; }
private :
void construct ( T const& val )
{
new (m_storage.address()) T(val) ;
m_initialized = true ;
}
void destroy()
{
if ( m_initialized )
{
get()->~T() ;
m_initialized = false ;
}
}
bool m_initialized ;
storage_type m_storage ;
} ;
// Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
// Returns a pointer to the value if this is initialized, otherwise, returns NULL.
// No-throw
template<class T>
inline
BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
get ( optional<T> const& opt )
T const* get_pointer ( optional<T> const& opt )
{
return opt.get() ;
}
template<class T>
inline
BOOST_DEDUCED_TYPENAME optional<T>::reference_type
get ( optional<T>& opt )
T* get_pointer ( optional<T>& opt )
{
return opt.get() ;
}
// Returns a pointer to the value if this is initialized, otherwise, returns NULL.
// template<class OP> bool equal_pointees(OP const& x, OP const& y);
//
// Being OP a model of OptionalPointee (either a pointer or an optional):
//
// If both x and y have valid pointees, returns the result of (*x == *y)
// If only one has a valid pointee, returns false.
// If none have valid pointees, returns true.
// No-throw
template<class T>
template<class OptionalPointee>
inline
BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
get ( optional<T> const* opt )
bool equal_pointees ( OptionalPointee const& x, OptionalPointee const& y )
{
return opt->get_ptr() ;
return (!x) != (!y) ? false : ( !x ? true : (*x) == (*y) ) ;
}
template<class T>
inline
BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
get ( optional<T>* opt )
{
return opt->get_ptr() ;
}
// Returns a pointer to the value if this is initialized, otherwise, returns NULL.
// No-throw
template<class T>
inline
BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
get_pointer ( optional<T> const& opt )
{
return opt.get_ptr() ;
}
template<class T>
inline
BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
get_pointer ( optional<T>& opt )
{
return opt.get_ptr() ;
}
// optional's relational operators ( ==, !=, <, >, <=, >= ) have deep-semantics (compare values).
// WARNING: This is UNLIKE pointers. Use equal_pointees()/less_pointess() in generic code instead.
// optional's operator == and != have deep-semantics (compare values).
// WARNING: This is UNLIKE pointers. Use equal_pointees() in generic code instead.
template<class T>
inline
bool operator == ( optional<T> const& x, optional<T> const& y )
{ return equal_pointees(x,y); }
template<class T>
inline
bool operator < ( optional<T> const& x, optional<T> const& y )
{ return less_pointees(x,y); }
template<class T>
inline
bool operator != ( optional<T> const& x, optional<T> const& y )
{ return !( x == y ) ; }
template<class T>
inline
bool operator > ( optional<T> const& x, optional<T> const& y )
{ return y < x ; }
template<class T>
inline
bool operator <= ( optional<T> const& x, optional<T> const& y )
{ return !( y < x ) ; }
template<class T>
inline
bool operator >= ( optional<T> const& x, optional<T> const& y )
{ return !( x < y ) ; }
template<class T>
inline
bool operator == ( optional<T> const& x, detail::none_t const& )
{ return equal_pointees(x, optional<T>() ); }
template<class T>
inline
bool operator < ( optional<T> const& x, detail::none_t const& )
{ return less_pointees(x,optional<T>() ); }
template<class T>
inline
bool operator != ( optional<T> const& x, detail::none_t const& y )
{ return !( x == y ) ; }
template<class T>
inline
bool operator > ( optional<T> const& x, detail::none_t const& y )
{ return y < x ; }
template<class T>
inline
bool operator <= ( optional<T> const& x, detail::none_t const& y )
{ return !( y < x ) ; }
template<class T>
inline
bool operator >= ( optional<T> const& x, detail::none_t const& y )
{ return !( x < y ) ; }
template<class T>
inline
bool operator == ( detail::none_t const& x, optional<T> const& y )
{ return equal_pointees(optional<T>() ,y); }
template<class T>
inline
bool operator < ( detail::none_t const& x, optional<T> const& y )
{ return less_pointees(optional<T>() ,y); }
template<class T>
inline
bool operator != ( detail::none_t const& x, optional<T> const& y )
{ return !( x == y ) ; }
template<class T>
inline
bool operator > ( detail::none_t const& x, optional<T> const& y )
{ return y < x ; }
template<class T>
inline
bool operator <= ( detail::none_t const& x, optional<T> const& y )
{ return !( y < x ) ; }
template<class T>
inline
bool operator >= ( detail::none_t const& x, optional<T> const& y )
{ return !( x < y ) ; }
//
// The following swap implementation follows the GCC workaround as found in
@ -662,11 +271,9 @@ bool operator >= ( detail::none_t const& x, optional<T> const& y )
//
namespace optional_detail {
// GCC < 3.2 gets the using declaration at namespace scope (FLC, DWA)
#if BOOST_WORKAROUND(__GNUC__, < 3) \
|| BOOST_WORKAROUND(__GNUC__, == 3) && __GNUC_MINOR__ <= 2
#ifdef __GNUC__
// workaround for GCC (JM):
using std::swap;
#define BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
#endif
// optional's swap:
@ -689,9 +296,7 @@ void optional_swap ( optional<T>& x, optional<T>& y )
}
else if ( !!x && !!y )
{
// GCC > 3.2 and all other compilers have the using declaration at function scope (FLC)
#ifndef BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
// allow for Koenig lookup
#ifndef __GNUC__
using std::swap ;
#endif
swap(*x,*y);
@ -705,6 +310,7 @@ template<class T> inline void swap ( optional<T>& x, optional<T>& y )
optional_detail::optional_swap(x,y);
}
} // namespace boost
#endif

View File

@ -1,10 +1,15 @@
# Boost.Optional Library test Jamfile
# Boost.Optional Library test Jamfile
#
# Copyright (C) 2003, Fernando Luis Cacciola Carballal.
# Copyright (C) 2003, Fernando Luis Cacciola Carballal.
#
# Use, modification, and distribution is subject to the Boost Software
# License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
# This material is provided "as is", with absolutely no warranty expressed
# or implied. Any use is at your own risk.
#
# Permission to use or copy this software for any purpose is hereby granted
# without fee, provided the above notices are retained on all copies.
# Permission to modify the code and to distribute modified code is granted,
# provided the above notices are retained, and a notice that the code was
# modified is included with the above copyright notice.
#
subproject libs/optional/test ;
@ -17,20 +22,16 @@ include testing.jam ;
DEPENDS all : test ;
{
# look in BOOST_ROOT for sources first, just in this Jamfile
local SEARCH_SOURCE = $(BOOST_ROOT) $(SEARCH_SOURCE) ;
test-suite optional :
[ run optional_test.cpp ]
[ run optional_test_tie.cpp ]
[ run optional_test_ref.cpp ]
[ run optional_test_inplace.cpp ]
[ compile-fail optional_test_fail1.cpp ]
[ compile-fail optional_test_fail2.cpp ]
[ compile-fail optional_test_fail3a.cpp ]
[ compile-fail optional_test_fail3b.cpp ]
[ compile-fail optional_test_ref_fail1.cpp ]
[ compile-fail optional_test_ref_fail2.cpp ]
[ compile-fail optional_test_ref_fail3.cpp ]
[ compile-fail optional_test_ref_fail4.cpp ]
[ compile-fail optional_test_inplace_fail.cpp ]
[ compile-fail optional_test_inplace_fail2.cpp ]
[ run libs/optional/test/optional_test.cpp ]
[ compile-fail libs/optional/test/optional_test_fail1.cpp ]
[ compile-fail libs/optional/test/optional_test_fail2.cpp ]
[ compile-fail libs/optional/test/optional_test_fail3.cpp ]
[ compile-fail libs/optional/test/optional_test_fail4.cpp ]
[ compile-fail libs/optional/test/optional_test_fail5a.cpp ]
[ compile-fail libs/optional/test/optional_test_fail5b.cpp ]
;
}

View File

@ -1,34 +0,0 @@
# Boost.Optional Library test Jamfile
#
# Copyright (C) 2003, Fernando Luis Cacciola Carballal.
#
# This material is provided "as is", with absolutely no warranty expressed
# or implied. Any use is at your own risk.
#
# Permission to use or copy this software for any purpose is hereby granted
# without fee, provided the above notices are retained on all copies.
# Permission to modify the code and to distribute modified code is granted,
# provided the above notices are retained, and a notice that the code was
# modified is included with the above copyright notice.
#
import testing ;
{
test-suite optional :
[ run optional_test.cpp ]
[ run optional_test_tie.cpp ]
[ run optional_test_ref.cpp ]
[ run optional_test_inplace.cpp ]
[ compile-fail optional_test_fail1.cpp ]
[ compile-fail optional_test_fail2.cpp ]
[ compile-fail optional_test_fail3a.cpp ]
[ compile-fail optional_test_fail3b.cpp ]
[ compile-fail optional_test_ref_fail1.cpp ]
[ compile-fail optional_test_ref_fail2.cpp ]
[ compile-fail optional_test_ref_fail3.cpp ]
[ compile-fail optional_test_ref_fail4.cpp ]
[ compile-fail optional_test_inplace_fail.cpp ]
[ compile-fail optional_test_inplace_fail2.cpp ]
;
}

View File

@ -1,10 +1,13 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
// (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// This material is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
// See http://www.boost.org/lib/optional for documentation.
// Permission to use or copy this software for any purpose is hereby granted
// without fee, provided the above notices are retained on all copies.
// Permission to modify the code and to distribute modified code is granted,
// provided the above notices are retained, and a notice that the code was
// modified is included with the above copyright notice.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
@ -21,32 +24,235 @@
#pragma hdrstop
#endif
#include "boost/detail/none.hpp"
#include "boost/test/minimal.hpp"
#include "optional_test_common.cpp"
#ifdef ENABLE_TRACE
#define TRACE(msg) std::cout << msg << std::endl ;
#else
#define TRACE(msg)
#endif
void test_implicit_construction ( optional<double> opt, double v, double z )
namespace boost {
void assertion_failed (char const * expr, char const * func, char const * file, long )
{
check_value(opt,v,z);
using std::string ;
string msg = string("Boost assertion failure for \"")
+ string(expr)
+ string("\" at file \"")
+ string(file)
+ string("\" function \"")
+ string(func)
+ string("\"") ;
TRACE(msg);
throw std::logic_error(msg);
}
void test_implicit_construction ( optional<X> opt, X const& v, X const& z )
{
check_value(opt,v,z);
}
void test_default_implicit_construction ( double, optional<double> opt )
using boost::optional ;
template<class T> inline void unused_variable ( T const& ) {}
#ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
using boost::swap ;
using boost::get_pointer ;
#endif
// MSVC6.0 does not support comparisons of optional against a literal null pointer value (0)
// via the safe_bool operator.
#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1300) ) // 1300 == VC++ 7.1
#define BOOST_OPTIONAL_NO_NULL_COMPARE
#endif
#define ARG(T) (static_cast< T const* >(0))
//
// Helper class used to verify the lifetime managment of the values held by optional
//
class X
{
BOOST_CHECK(!opt);
public :
X ( int av ) : v(av)
{
++ count ;
TRACE ( "X::X(" << av << "). this=" << this ) ;
}
X ( X const& rhs ) : v(rhs.v)
{
pending_copy = false ;
TRACE ( "X::X( X const& rhs). this=" << this << " rhs.v=" << rhs.v ) ;
if ( throw_on_copy )
{
TRACE ( "throwing exception in X's copy ctor" ) ;
throw 0 ;
}
++ count ;
}
~X()
{
pending_dtor = false ;
-- count ;
TRACE ( "X::~X(). v=" << v << " this=" << this );
}
X& operator= ( X const& rhs )
{
v = rhs.v ;
TRACE ( "X::operator =( X const& rhs). this=" << this << " rhs.v=" << rhs.v ) ;
return *this ;
}
friend bool operator == ( X const& a, X const& b )
{ return a.v == b.v ; }
friend bool operator != ( X const& a, X const& b )
{ return a.v != b.v ; }
friend bool operator < ( X const& a, X const& b )
{ return a.v < b.v ; }
int V() const { return v ; }
int& V() { return v ; }
static int count ;
static bool pending_copy ;
static bool pending_dtor ;
static bool throw_on_copy ;
private :
int v ;
private :
X() ;
} ;
int X::count = 0 ;
bool X::pending_copy = false ;
bool X::pending_dtor = false ;
bool X::throw_on_copy = false ;
inline void set_pending_copy ( X const* x ) { X::pending_copy = true ; }
inline void set_pending_dtor ( X const* x ) { X::pending_dtor = true ; }
inline void set_throw_on_copy ( X const* x ) { X::throw_on_copy = true ; }
inline void reset_throw_on_copy ( X const* x ) { X::throw_on_copy = false ; }
inline void check_is_pending_copy ( X const* x ) { BOOST_CHECK( X::pending_copy ) ; }
inline void check_is_pending_dtor ( X const* x ) { BOOST_CHECK( X::pending_dtor ) ; }
inline void check_is_not_pending_copy( X const* x ) { BOOST_CHECK( !X::pending_copy ) ; }
inline void check_is_not_pending_dtor( X const* x ) { BOOST_CHECK( !X::pending_dtor ) ; }
inline void check_instance_count ( int c, X const* x ) { BOOST_CHECK( X::count == c ) ; }
inline int get_instance_count ( X const* x ) { return X::count ; }
inline void set_pending_copy (...) {}
inline void set_pending_dtor (...) {}
inline void set_throw_on_copy (...) {}
inline void reset_throw_on_copy (...) {}
inline void check_is_pending_copy (...) {}
inline void check_is_pending_dtor (...) {}
inline void check_is_not_pending_copy(...) {}
inline void check_is_not_pending_dtor(...) {}
inline void check_instance_count (...) {}
inline int get_instance_count (...) { return 0 ; }
template<class T>
inline void check_uninitialized_const ( optional<T> const& opt )
{
#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE
BOOST_CHECK( opt == 0 ) ;
#endif
BOOST_CHECK( !opt ) ;
BOOST_CHECK( !get_pointer(opt) ) ;
BOOST_CHECK( !opt.get() ) ;
}
template<class T>
inline void check_uninitialized ( optional<T>& opt )
{
#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE
BOOST_CHECK( opt == 0 ) ;
#endif
BOOST_CHECK( !opt ) ;
BOOST_CHECK( !get_pointer(opt) ) ;
BOOST_CHECK( !opt.get() ) ;
check_uninitialized_const(opt);
}
void test_default_implicit_construction ( X const&, optional<X> opt )
template<class T>
inline void check_initialized_const ( optional<T> const& opt )
{
BOOST_CHECK(!opt);
BOOST_CHECK( opt ) ;
#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE
BOOST_CHECK( opt != 0 ) ;
#endif
BOOST_CHECK ( !!opt ) ;
BOOST_CHECK ( get_pointer(opt) ) ;
BOOST_CHECK ( opt.get() ) ;
}
template<class T>
inline void check_initialized ( optional<T>& opt )
{
BOOST_CHECK( opt ) ;
#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE
BOOST_CHECK( opt != 0 ) ;
#endif
BOOST_CHECK ( !!opt ) ;
BOOST_CHECK ( get_pointer(opt) ) ;
BOOST_CHECK ( opt.get() ) ;
check_initialized_const(opt);
}
template<class T>
inline void check_value_const ( optional<T> const& opt, T const& v, T const& z )
{
BOOST_CHECK( *opt == v ) ;
BOOST_CHECK( *opt != z ) ;
BOOST_CHECK( (*(opt.operator->()) == v) ) ;
BOOST_CHECK( *get_pointer(opt) == v ) ;
BOOST_CHECK( *opt.get() == v ) ;
}
template<class T>
inline void check_value ( optional<T>& opt, T const& v, T const& z )
{
#if BOOST_WORKAROUND(BOOST_MSVC, <= 1200) // 1200 == VC++ 6.0
// For some reason, VC6.0 is creating a temporary while evaluating (*opt == v),
// so we need to turn throw on copy off first.
reset_throw_on_copy( ARG(T) ) ;
#endif
BOOST_CHECK( *opt == v ) ;
BOOST_CHECK( *opt != z ) ;
BOOST_CHECK( (*(opt.operator->()) == v) ) ;
BOOST_CHECK( *get_pointer(opt) == v ) ;
BOOST_CHECK( *opt.get() == v ) ;
check_value_const(opt,v,z);
}
//
// Basic test.
// Check ordinary functionality:
@ -67,10 +273,6 @@ void test_basics( T const* )
optional<T> def ;
check_uninitialized(def);
// Implicit construction
// The first parameter is implicitely converted to optional<T>(a);
test_implicit_construction(a,a,z);
// Direct initialization.
// 'oa' state is Initialized with 'a'
// T::T( T const& x ) is used.
@ -80,6 +282,7 @@ void test_basics( T const* )
check_initialized(oa);
check_value(oa,a,z);
T b(2);
optional<T> ob ;
@ -87,18 +290,19 @@ void test_basics( T const* )
// Value-Assignment upon Uninitialized optional.
// T::T ( T const& x ) is used.
set_pending_copy( ARG(T) ) ;
ob = a ;
ob.reset(a) ;
check_is_not_pending_copy( ARG(T) ) ;
check_initialized(ob);
check_value(ob,a,z);
// Value-Assignment upon Initialized optional.
// T::T ( T const& x ) is used
// This uses T::operator= ( T const& x ) directly
// on the reference returned by operator*()
set_pending_dtor( ARG(T) ) ;
set_pending_copy( ARG(T) ) ;
ob = b ;
check_is_not_pending_dtor( ARG(T) ) ;
check_is_not_pending_copy( ARG(T) ) ;
*ob = b ;
check_is_pending_dtor( ARG(T) ) ;
check_is_pending_copy( ARG(T) ) ;
check_initialized(ob);
check_value(ob,b,z);
@ -167,9 +371,6 @@ void test_direct_value_manip( T const* )
optional<T> const c_opt0(x) ;
optional<T> opt0(x);
BOOST_CHECK( c_opt0.get().V() == x.V() ) ;
BOOST_CHECK( opt0.get().V() == x.V() ) ;
BOOST_CHECK( c_opt0->V() == x.V() ) ;
BOOST_CHECK( opt0->V() == x.V() ) ;
@ -177,8 +378,10 @@ void test_direct_value_manip( T const* )
BOOST_CHECK( (* opt0).V() == x.V() ) ;
T y(4);
opt0 = y ;
BOOST_CHECK( get(opt0).V() == y.V() ) ;
*opt0 = y ;
BOOST_CHECK( (*opt0).V() == y.V() ) ;
BOOST_CHECK( x < (*opt0) ) ;
}
//
@ -193,17 +396,6 @@ void test_uninitialized_access( T const* )
bool passed = false ;
try
{
// This should throw because 'def' is uninitialized
T const& n = def.get() ;
unused_variable(n);
passed = true ;
}
catch (...) {}
BOOST_CHECK(!passed);
passed = false ;
try
{
// This should throw because 'def' is uninitialized
T const& n = *def ;
@ -212,7 +404,7 @@ void test_uninitialized_access( T const* )
}
catch (...) {}
BOOST_CHECK(!passed);
passed = false ;
try
{
@ -475,7 +667,7 @@ void test_throwing_assign_to_initialized( T const* )
{
// This should:
// Attempt to copy construct 'opt1.value()' into opt0 and throw.
// opt0 should be left unmodified or uninitialized
// opt0 should be left uninitialized (even though it was initialized)
set_pending_dtor( ARG(T) ) ;
set_pending_copy( ARG(T) ) ;
opt0 = opt1 ;
@ -485,8 +677,8 @@ void test_throwing_assign_to_initialized( T const* )
BOOST_CHECK(!passed);
// opt0 was left uninitialized
-- count ;
check_is_not_pending_dtor( ARG(T) );
check_is_not_pending_copy( ARG(T) );
check_instance_count(count, ARG(T) );
@ -514,6 +706,8 @@ void test_no_throwing_swap( T const* )
int count = get_instance_count( ARG(T) ) ;
using boost::swap ;
swap(def0,def1);
check_uninitialized(def0);
check_uninitialized(def1);
@ -625,58 +819,17 @@ void test_relops( T const* )
BOOST_CHECK ( !(def0 != def0) ) ;
BOOST_CHECK ( !(opt0 != opt0) ) ;
// Check when both are uininitalized.
BOOST_CHECK ( def0 == def1 ) ; // both uninitialized compare equal
BOOST_CHECK ( !(def0 < def1) ) ; // uninitialized is never less than uninitialized
BOOST_CHECK ( !(def0 > def1) ) ; // uninitialized is never greater than uninitialized
// If both are uininitalized they compare equal
BOOST_CHECK ( def0 == def1 ) ;
BOOST_CHECK ( !(def0 != def1) ) ;
BOOST_CHECK ( def0 <= def1 ) ;
BOOST_CHECK ( def0 >= def1 ) ;
// Check when only lhs is uninitialized.
BOOST_CHECK ( def0 != opt0 ) ; // uninitialized is never equal to initialized
BOOST_CHECK ( !(def0 == opt0) ) ;
BOOST_CHECK ( def0 < opt0 ) ; // uninitialized is always less than initialized
BOOST_CHECK ( !(def0 > opt0) ) ;
BOOST_CHECK ( def0 <= opt0 ) ;
BOOST_CHECK ( !(def0 >= opt0) ) ;
// Check when only rhs is uninitialized.
BOOST_CHECK ( opt0 != def0 ) ; // initialized is never equal to uninitialized
BOOST_CHECK ( !(opt0 == def0) ) ;
BOOST_CHECK ( !(opt0 < def0) ) ; // initialized is never less than uninitialized
BOOST_CHECK ( opt0 > def0 ) ;
BOOST_CHECK ( !(opt0 <= def0) ) ;
BOOST_CHECK ( opt0 >= opt0 ) ;
// If only one is initialized they compare unequal
BOOST_CHECK ( def0 != opt0 ) ;
BOOST_CHECK ( !(def1 == opt1) ) ;
// If both are initialized, values are compared
BOOST_CHECK ( opt0 != opt1 ) ;
BOOST_CHECK ( opt1 == opt2 ) ;
BOOST_CHECK ( opt0 < opt1 ) ;
BOOST_CHECK ( opt1 > opt0 ) ;
BOOST_CHECK ( opt1 <= opt2 ) ;
BOOST_CHECK ( opt1 >= opt0 ) ;
}
template<class T>
void test_none( T const* )
{
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
using boost::none ;
optional<T> def0 ;
optional<T> def1(none) ;
optional<T> non_def( T(1234) ) ;
BOOST_CHECK ( def0 == none ) ;
BOOST_CHECK ( non_def != none ) ;
BOOST_CHECK ( !def1 ) ;
non_def = none ;
BOOST_CHECK ( !non_def ) ;
test_default_implicit_construction(T(1),none);
}
void test_with_builtin_types()
@ -687,7 +840,6 @@ void test_with_builtin_types()
test_uninitialized_access( ARG(double) );
test_no_throwing_swap( ARG(double) );
test_relops( ARG(double) ) ;
test_none( ARG(double) ) ;
}
void test_with_class_type()
@ -706,11 +858,9 @@ void test_with_class_type()
test_no_throwing_swap( ARG(X) );
test_throwing_swap( ARG(X) );
test_relops( ARG(X) ) ;
test_none( ARG(X) ) ;
BOOST_CHECK ( X::count == 0 ) ;
}
int eat ( bool ) { return 1 ; }
int eat ( char ) { return 1 ; }
int eat ( int ) { return 1 ; }
int eat ( void const* ) { return 1 ; }
@ -733,19 +883,17 @@ void test_no_implicit_conversions()
{
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
bool b = false ;
char c = 0 ;
int i = 0 ;
void const* p = 0 ;
test_no_implicit_conversions_impl(b);
test_no_implicit_conversions_impl(c);
test_no_implicit_conversions_impl(i);
test_no_implicit_conversions_impl(p);
}
struct A {} ;
void test_conversions1()
void test_conversions()
{
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
@ -766,20 +914,6 @@ void test_conversions1()
#endif
}
void test_conversions2()
{
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
char c = 20 ;
optional<int> opt(c);
BOOST_CHECK( get(opt) == static_cast<int>(c));
float f = 21.22f ;
optional<double> opt1;
opt1 = f ;
BOOST_CHECK(*get(&opt1) == static_cast<double>(f));
}
int test_main( int, char* [] )
{
try
@ -787,8 +921,7 @@ int test_main( int, char* [] )
test_with_class_type();
test_with_builtin_types();
test_no_implicit_conversions();
test_conversions1();
test_conversions2();
test_conversions();
}
catch ( ... )
{

View File

@ -1,241 +0,0 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#ifdef ENABLE_TRACE
#define TRACE(msg) std::cout << msg << std::endl ;
#else
#define TRACE(msg)
#endif
namespace boost {
void assertion_failed (char const * expr, char const * func, char const * file, long )
{
using std::string ;
string msg = string("Boost assertion failure for \"")
+ string(expr)
+ string("\" at file \"")
+ string(file)
+ string("\" function \"")
+ string(func)
+ string("\"") ;
TRACE(msg);
throw std::logic_error(msg);
}
}
using boost::optional ;
template<class T> inline void unused_variable ( T ) {}
#ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
using boost::swap ;
using boost::get ;
using boost::get_pointer ;
#endif
// MSVC6.0 does not support comparisons of optional against a literal null pointer value (0)
// via the safe_bool operator.
#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1300) ) // 1300 == VC++ 7.1
#define BOOST_OPTIONAL_NO_NULL_COMPARE
#endif
#define ARG(T) (static_cast< T const* >(0))
//
// Helper class used to verify the lifetime managment of the values held by optional
//
class X
{
public :
X ( int av ) : v(av)
{
++ count ;
TRACE ( "X::X(" << av << "). this=" << this ) ;
}
X ( X const& rhs ) : v(rhs.v)
{
pending_copy = false ;
TRACE ( "X::X( X const& rhs). this=" << this << " rhs.v=" << rhs.v ) ;
if ( throw_on_copy )
{
TRACE ( "throwing exception in X's copy ctor" ) ;
throw 0 ;
}
++ count ;
}
~X()
{
pending_dtor = false ;
-- count ;
TRACE ( "X::~X(). v=" << v << " this=" << this );
}
X& operator= ( X const& rhs )
{
v = rhs.v ;
TRACE ( "X::operator =( X const& rhs). this=" << this << " rhs.v=" << rhs.v ) ;
return *this ;
}
friend bool operator == ( X const& a, X const& b )
{ return a.v == b.v ; }
friend bool operator != ( X const& a, X const& b )
{ return a.v != b.v ; }
friend bool operator < ( X const& a, X const& b )
{ return a.v < b.v ; }
int V() const { return v ; }
int& V() { return v ; }
static int count ;
static bool pending_copy ;
static bool pending_dtor ;
static bool throw_on_copy ;
private :
int v ;
private :
X() ;
} ;
int X::count = 0 ;
bool X::pending_copy = false ;
bool X::pending_dtor = false ;
bool X::throw_on_copy = false ;
inline void set_pending_copy ( X const* x ) { X::pending_copy = true ; }
inline void set_pending_dtor ( X const* x ) { X::pending_dtor = true ; }
inline void set_throw_on_copy ( X const* x ) { X::throw_on_copy = true ; }
inline void reset_throw_on_copy ( X const* x ) { X::throw_on_copy = false ; }
inline void check_is_pending_copy ( X const* x ) { BOOST_CHECK( X::pending_copy ) ; }
inline void check_is_pending_dtor ( X const* x ) { BOOST_CHECK( X::pending_dtor ) ; }
inline void check_is_not_pending_copy( X const* x ) { BOOST_CHECK( !X::pending_copy ) ; }
inline void check_is_not_pending_dtor( X const* x ) { BOOST_CHECK( !X::pending_dtor ) ; }
inline void check_instance_count ( int c, X const* x ) { BOOST_CHECK( X::count == c ) ; }
inline int get_instance_count ( X const* x ) { return X::count ; }
inline void set_pending_copy (...) {}
inline void set_pending_dtor (...) {}
inline void set_throw_on_copy (...) {}
inline void reset_throw_on_copy (...) {}
inline void check_is_pending_copy (...) {}
inline void check_is_pending_dtor (...) {}
inline void check_is_not_pending_copy(...) {}
inline void check_is_not_pending_dtor(...) {}
inline void check_instance_count (...) {}
inline int get_instance_count (...) { return 0 ; }
template<class T>
inline void check_uninitialized_const ( optional<T> const& opt )
{
#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE
BOOST_CHECK( opt == 0 ) ;
#endif
BOOST_CHECK( !opt ) ;
BOOST_CHECK( !get_pointer(opt) ) ;
BOOST_CHECK( !opt.get_ptr() ) ;
}
template<class T>
inline void check_uninitialized ( optional<T>& opt )
{
#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE
BOOST_CHECK( opt == 0 ) ;
#endif
BOOST_CHECK( !opt ) ;
BOOST_CHECK( !get_pointer(opt) ) ;
BOOST_CHECK( !opt.get_ptr() ) ;
check_uninitialized_const(opt);
}
template<class T>
inline void check_initialized_const ( optional<T> const& opt )
{
BOOST_CHECK( opt ) ;
#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE
BOOST_CHECK( opt != 0 ) ;
#endif
BOOST_CHECK ( !!opt ) ;
BOOST_CHECK ( get_pointer(opt) ) ;
BOOST_CHECK ( opt.get_ptr() ) ;
}
template<class T>
inline void check_initialized ( optional<T>& opt )
{
BOOST_CHECK( opt ) ;
#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE
BOOST_CHECK( opt != 0 ) ;
#endif
BOOST_CHECK ( !!opt ) ;
BOOST_CHECK ( get_pointer(opt) ) ;
BOOST_CHECK ( opt.get_ptr() ) ;
check_initialized_const(opt);
}
template<class T>
inline void check_value_const ( optional<T> const& opt, T const& v, T const& z )
{
BOOST_CHECK( *opt == v ) ;
BOOST_CHECK( *opt != z ) ;
BOOST_CHECK( opt.get() == v ) ;
BOOST_CHECK( opt.get() != z ) ;
BOOST_CHECK( (*(opt.operator->()) == v) ) ;
BOOST_CHECK( *get_pointer(opt) == v ) ;
}
template<class T>
inline void check_value ( optional<T>& opt, T const& v, T const& z )
{
#if BOOST_WORKAROUND(BOOST_MSVC, <= 1200) // 1200 == VC++ 6.0
// For some reason, VC6.0 is creating a temporary while evaluating (*opt == v),
// so we need to turn throw on copy off first.
reset_throw_on_copy( ARG(T) ) ;
#endif
BOOST_CHECK( *opt == v ) ;
BOOST_CHECK( *opt != z ) ;
BOOST_CHECK( opt.get() == v ) ;
BOOST_CHECK( opt.get() != z ) ;
BOOST_CHECK( (*(opt.operator->()) == v) ) ;
BOOST_CHECK( *get_pointer(opt) == v ) ;
check_value_const(opt,v,z);
}

View File

@ -1,10 +1,13 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
// (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// This material is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
// See http://www.boost.org/lib/optional for documentation.
// Permission to use or copy this software for any purpose is hereby granted
// without fee, provided the above notices are retained on all copies.
// Permission to modify the code and to distribute modified code is granted,
// provided the above notices are retained, and a notice that the code was
// modified is included with the above copyright notice.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com

View File

@ -1,10 +1,13 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
// (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// This material is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
// See http://www.boost.org/lib/optional for documentation.
// Permission to use or copy this software for any purpose is hereby granted
// without fee, provided the above notices are retained on all copies.
// Permission to modify the code and to distribute modified code is granted,
// provided the above notices are retained, and a notice that the code was
// modified is included with the above copyright notice.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
@ -14,13 +17,10 @@
//
// THIS TEST SHOULD FAIL TO COMPILE
//
void test_no_implicit_conversion()
void test_no_direct_value_assignment()
{
boost::optional<int> opt(1) ;
// You can compare against 0 or against another optional<>,
// but not against another value
if ( opt == 1 ) ;
boost::optional<int> opt(3) ;
opt = 4 ; // Cannot assign "int" to "optional<int>"
}

View File

@ -1,10 +1,13 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
// (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// This material is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
// See http://www.boost.org/lib/optional for documentation.
// Permission to use or copy this software for any purpose is hereby granted
// without fee, provided the above notices are retained on all copies.
// Permission to modify the code and to distribute modified code is granted,
// provided the above notices are retained, and a notice that the code was
// modified is included with the above copyright notice.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com

View File

@ -1,25 +0,0 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#include<string>
#include "boost/optional.hpp"
//
// THIS TEST SHOULD FAIL TO COMPILE
//
void test_no_unsupported_conversion()
{
boost::optional<int> opt1(1) ;
boost::optional< std::string > opt2( opt1 ) ; // Cannot convert from "int" to "std::string"
}

View File

@ -1,26 +0,0 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#include<string>
#include "boost/optional.hpp"
//
// THIS TEST SHOULD FAIL TO COMPILE
//
void test_no_unsupported_conversion()
{
boost::optional<int> opt1(1) ;
boost::optional< std::string > opt2 ;
opt2 = opt1 ; // Cannot convert from "int" to "std::string"
}

View File

@ -0,0 +1,29 @@
// (C) 2003, Fernando Luis Cacciola Carballal.
//
// This material is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
// Permission to use or copy this software for any purpose is hereby granted
// without fee, provided the above notices are retained on all copies.
// Permission to modify the code and to distribute modified code is granted,
// provided the above notices are retained, and a notice that the code was
// modified is included with the above copyright notice.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#include "boost/optional.hpp"
//
// THIS TEST SHOULD FAIL TO COMPILE
//
void test_no_implicit_conversion()
{
boost::optional<int> opt(1) ;
// You can compare against 0 or against another optional<>,
// but not against another value
if ( opt == 1 ) ;
}

View File

@ -0,0 +1,28 @@
// (C) 2003, Fernando Luis Cacciola Carballal.
//
// This material is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
// Permission to use or copy this software for any purpose is hereby granted
// without fee, provided the above notices are retained on all copies.
// Permission to modify the code and to distribute modified code is granted,
// provided the above notices are retained, and a notice that the code was
// modified is included with the above copyright notice.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#include<string>
#include "boost/optional.hpp"
//
// THIS TEST SHOULD FAIL TO COMPILE
//
void test_no_unsupported_conversion()
{
boost::optional<int> opt1(1) ;
boost::optional< std::string > opt2( opt1 ) ; // Cannot convert from "int" to "std::string"
}

View File

@ -0,0 +1,29 @@
// (C) 2003, Fernando Luis Cacciola Carballal.
//
// This material is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
// Permission to use or copy this software for any purpose is hereby granted
// without fee, provided the above notices are retained on all copies.
// Permission to modify the code and to distribute modified code is granted,
// provided the above notices are retained, and a notice that the code was
// modified is included with the above copyright notice.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#include<string>
#include "boost/optional.hpp"
//
// THIS TEST SHOULD FAIL TO COMPILE
//
void test_no_unsupported_conversion()
{
boost::optional<int> opt1(1) ;
boost::optional< std::string > opt2 ;
opt2 = opt1 ; // Cannot convert from "int" to "std::string"
}

View File

@ -1,80 +0,0 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#include<iostream>
#include<stdexcept>
#include<string>
#define BOOST_ENABLE_ASSERT_HANDLER
#include "boost/optional.hpp"
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
#include "boost/detail/in_place_factory.hpp"
#include "boost/detail/typed_in_place_factory.hpp"
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "boost/test/minimal.hpp"
#include "optional_test_common.cpp"
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
struct A
{
A ( double a0, std::string a1 ) : m_a0(a0), m_a1(a1) {}
friend bool operator == ( A const& x, A const& y )
{ return x.m_a0 == y.m_a0 && x.m_a1 == y.m_a1 ; }
double m_a0 ;
std::string m_a1 ;
} ;
int test_main( int, char* [] )
{
double a00 = 3.14, a10 = 6.02e-23;
std::string a01("pi"), a11("mol");
A a0(a00,a01);
A a1(a10,a11);
boost::optional<A> opt1(a0);
boost::optional<A> opt2 ( boost::in_place(a00,a01) ) ;
boost::optional<A> opt3 ( boost::in_place<A>(a00,a01) ) ;
BOOST_CHECK( opt1 == opt2 ) ;
BOOST_CHECK( opt2 == opt2 ) ;
BOOST_CHECK( *opt2 == a0 ) ;
opt2 = boost::in_place(a10,a11);
BOOST_CHECK( *opt2 == a1 ) ;
opt3 = boost::in_place<A>(a10,a11);
BOOST_CHECK( *opt3 == a1 ) ;
return 0;
}
#else
int test_main( int, char* [] )
{
// If in-place factories are not supported there is nothing to test
return 0 ;
}
#endif

View File

@ -1,60 +0,0 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#include<iostream>
#include<stdexcept>
#include<string>
#define BOOST_ENABLE_ASSERT_HANDLER
#include "boost/optional.hpp"
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
#include "boost/detail/in_place_factory.hpp"
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "boost/test/minimal.hpp"
#include "optional_test_common.cpp"
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
struct A
{
A ( double a0, std::string a1 ) : m_a0(a0), m_a1(a1) {}
friend bool operator == ( A const& x, A const& y )
{ return x.m_a0 == y.m_a0 && x.m_a1 == y.m_a1 ; }
double m_a0 ;
std::string m_a1 ;
} ;
int test_main( int, char* [] )
{
int invalid_extra_parameter ;
boost::optional<A> opt2 ( boost::in_place(3.14,"pi",invalid_extra_parameter) ) ;
return 0;
}
#else
int test_main( int, char* [] )
{
int invalid_extra_parameter ;
boost::optional<A> opt2 ( A(3.14,"pi",invalid_extra_parameter) ) ;
return 0;
}
#endif

View File

@ -1,62 +0,0 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#include<iostream>
#include<stdexcept>
#include<string>
#define BOOST_ENABLE_ASSERT_HANDLER
#include "boost/optional.hpp"
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
#include "boost/detail/typed_in_place_factory.hpp"
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "boost/test/minimal.hpp"
#include "optional_test_common.cpp"
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
struct A
{
A ( double a0, std::string a1 ) : m_a0(a0), m_a1(a1) {}
friend bool operator == ( A const& x, A const& y )
{ return x.m_a0 == y.m_a0 && x.m_a1 == y.m_a1 ; }
double m_a0 ;
std::string m_a1 ;
} ;
int test_main( int, char* [] )
{
// This must fail to compile.
// The first template argument to in_place<> is the target-type,
// not the first constructor parameter type.
boost::optional<A> opt2 ( boost::in_place<int>(3.14,"pi") ) ;
return 0;
}
#else
int test_main( int, char* [] )
{
boost::optional<A> opt2 ( int(3.14) ) ;
return 0;
}
#endif

View File

@ -1,323 +0,0 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#include<iostream>
#include<stdexcept>
#include<string>
#define BOOST_ENABLE_ASSERT_HANDLER
#include "boost/optional.hpp"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "boost/detail/none.hpp"
#include "boost/test/minimal.hpp"
#include "optional_test_common.cpp"
template<class T>
inline void check_ref_uninitialized_const ( optional<T&> const& opt )
{
#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE
BOOST_CHECK( opt == 0 ) ;
#endif
BOOST_CHECK( !opt ) ;
}
template<class T>
inline void check_ref_uninitialized ( optional<T&>& opt )
{
#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE
BOOST_CHECK( opt == 0 ) ;
#endif
BOOST_CHECK( !opt ) ;
check_ref_uninitialized_const(opt);
}
template<class T>
inline void check_ref_initialized_const ( optional<T&> const& opt )
{
BOOST_CHECK( opt ) ;
#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE
BOOST_CHECK( opt != 0 ) ;
#endif
BOOST_CHECK ( !!opt ) ;
}
template<class T>
inline void check_ref_initialized ( optional<T&>& opt )
{
BOOST_CHECK( opt ) ;
#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE
BOOST_CHECK( opt != 0 ) ;
#endif
BOOST_CHECK ( !!opt ) ;
check_ref_initialized_const(opt);
}
template<class T>
inline void check_ref_value_const ( optional<T&> const& opt, T const& v, T const& z )
{
BOOST_CHECK( *opt == v ) ;
BOOST_CHECK( *opt != z ) ;
BOOST_CHECK( opt.get() == v ) ;
BOOST_CHECK( opt.get() != z ) ;
}
template<class T>
inline void check_ref_value ( optional<T&>& opt, T const& v, T const& z )
{
BOOST_CHECK( *opt == v ) ;
BOOST_CHECK( *opt != z ) ;
BOOST_CHECK( opt.get() == v ) ;
BOOST_CHECK( opt.get() != z ) ;
check_ref_value_const(opt,v,z);
}
//
// Basic test.
// Check ordinary functionality:
// Initialization, assignment, comparison and value-accessing.
//
template<class T>
void test_basics( T const* )
{
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
T z(0);
T a(1);
T& aref = a ;
// Default construction.
// 'def' state is Uninitialized.
// T::T() is not called
optional<T&> def ;
check_ref_uninitialized(def);
// Direct initialization.
// 'oa' state is Initialized with 'a'
// T::T( T const& x ) is NOT used becasue the optional holds a reference.
set_pending_copy( ARG(T) ) ;
optional<T&> oa ( aref ) ;
check_is_pending_copy( ARG(T) );
check_ref_initialized(oa);
check_ref_value(oa,a,z);
// Copy initialization.
// T::T ( T const& x ) is NOT used becasue the optional holds a reference.
set_pending_copy( ARG(T) ) ;
optional<T&> const oa2 ( oa ) ;
check_is_pending_copy( ARG(T) ) ;
check_ref_initialized_const(oa2);
check_ref_value_const(oa2,a,z);
T b(2);
optional<T&> ob ;
// Value-Assignment upon Uninitialized optional.
// T::T ( T const& x ) is NOT used becasue the optional holds a reference.
set_pending_copy( ARG(T) ) ;
ob = a ;
check_is_pending_copy( ARG(T) ) ;
check_ref_initialized(ob);
check_ref_value(ob,a,z);
// Value-Assignment upon Initialized optional.
// T::T ( T const& x ) is NOT used becasue the optional holds a reference.
set_pending_dtor( ARG(T) ) ;
set_pending_copy( ARG(T) ) ;
ob = b ;
check_is_pending_dtor( ARG(T) ) ;
check_is_pending_copy( ARG(T) ) ;
check_ref_initialized(ob);
check_ref_value(ob,b,z);
// Assignment initialization.
// T::T ( T const& x ) is NOT used becasue the optional holds a reference.
set_pending_copy( ARG(T) ) ;
optional<T&> const oa3 = b ;
check_is_pending_copy( ARG(T) ) ;
check_ref_initialized_const(oa3);
check_ref_value_const(oa3,b,z);
// Assignment
// T::~T() is used to destroy previous value in ob.
// T::T ( T const& x ) is NOT used becasue the optional holds a reference.
set_pending_dtor( ARG(T) ) ;
set_pending_copy( ARG(T) ) ;
oa = ob ;
check_is_pending_dtor( ARG(T) ) ;
check_is_pending_copy( ARG(T) ) ;
check_ref_initialized(oa);
check_ref_value(oa,b,z);
// Uninitializing Assignment upon Initialized Optional
// T::~T() is NOT used becasue the optional holds a reference.
set_pending_dtor( ARG(T) ) ;
set_pending_copy( ARG(T) ) ;
oa = def ;
check_is_pending_dtor( ARG(T) ) ;
check_is_pending_copy( ARG(T) ) ;
check_ref_uninitialized(oa);
// Uninitializing Assignment upon Uninitialized Optional
// (Dtor is not called this time)
set_pending_dtor( ARG(T) ) ;
set_pending_copy( ARG(T) ) ;
oa = def ;
check_is_pending_dtor( ARG(T) ) ;
check_is_pending_copy( ARG(T) ) ;
check_ref_uninitialized(oa);
// Deinitialization of Initialized Optional
// T::~T() is NOT used becasue the optional holds a reference.
set_pending_dtor( ARG(T) ) ;
ob.reset();
check_is_pending_dtor( ARG(T) ) ;
check_ref_uninitialized(ob);
// Deinitialization of Uninitialized Optional
// T::~T() is not called this time
set_pending_dtor( ARG(T) ) ;
ob.reset();
check_is_pending_dtor( ARG(T) ) ;
check_ref_uninitialized(ob);
}
//
// This verifies relational operators.
//
template<class T>
void test_relops( T const* )
{
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
reset_throw_on_copy( ARG(T) ) ;
T v0(18);
T v1(19);
T v2(19);
optional<T&> def0 ;
optional<T&> def1 ;
optional<T&> opt0(v0);
optional<T&> opt1(v1);
optional<T&> opt2(v2);
// Check identity
BOOST_CHECK ( def0 == def0 ) ;
BOOST_CHECK ( opt0 == opt0 ) ;
BOOST_CHECK ( !(def0 != def0) ) ;
BOOST_CHECK ( !(opt0 != opt0) ) ;
// Check when both are uininitalized.
BOOST_CHECK ( def0 == def1 ) ; // both uninitialized compare equal
BOOST_CHECK ( !(def0 < def1) ) ; // uninitialized is never less than uninitialized
BOOST_CHECK ( !(def0 > def1) ) ; // uninitialized is never greater than uninitialized
BOOST_CHECK ( !(def0 != def1) ) ;
BOOST_CHECK ( def0 <= def1 ) ;
BOOST_CHECK ( def0 >= def1 ) ;
// Check when only lhs is uninitialized.
BOOST_CHECK ( def0 != opt0 ) ; // uninitialized is never equal to initialized
BOOST_CHECK ( !(def0 == opt0) ) ;
BOOST_CHECK ( def0 < opt0 ) ; // uninitialized is always less than initialized
BOOST_CHECK ( !(def0 > opt0) ) ;
BOOST_CHECK ( def0 <= opt0 ) ;
BOOST_CHECK ( !(def0 >= opt0) ) ;
// Check when only rhs is uninitialized.
BOOST_CHECK ( opt0 != def0 ) ; // initialized is never equal to uninitialized
BOOST_CHECK ( !(opt0 == def0) ) ;
BOOST_CHECK ( !(opt0 < def0) ) ; // initialized is never less than uninitialized
BOOST_CHECK ( opt0 > def0 ) ;
BOOST_CHECK ( !(opt0 <= def0) ) ;
BOOST_CHECK ( opt0 >= opt0 ) ;
// If both are initialized, values are compared
BOOST_CHECK ( opt0 != opt1 ) ;
BOOST_CHECK ( opt1 == opt2 ) ;
BOOST_CHECK ( opt0 < opt1 ) ;
BOOST_CHECK ( opt1 > opt0 ) ;
BOOST_CHECK ( opt1 <= opt2 ) ;
BOOST_CHECK ( opt1 >= opt0 ) ;
}
template<class T>
void test_none( T const* )
{
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
using boost::none ;
T a(1234);
optional<T&> def0 ;
optional<T&> def1(none) ;
optional<T&> non_def(a) ;
BOOST_CHECK ( def0 == none ) ;
BOOST_CHECK ( non_def != none ) ;
BOOST_CHECK ( !def1 ) ;
non_def = none ;
BOOST_CHECK ( !non_def ) ;
}
void test_with_builtin_types()
{
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
test_basics( ARG(double) );
test_relops( ARG(double) ) ;
test_none ( ARG(double) ) ;
}
void test_with_class_type()
{
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
test_basics( ARG(X) );
test_relops( ARG(X) ) ;
test_none ( ARG(X) ) ;
BOOST_CHECK ( X::count == 0 ) ;
}
int test_main( int, char* [] )
{
try
{
test_with_class_type();
test_with_builtin_types();
}
catch ( ... )
{
BOOST_ERROR("Unexpected Exception caught!");
}
return 0;
}

View File

@ -1,25 +0,0 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#include "boost/optional.hpp"
//
// THIS TEST SHOULD FAIL TO COMPILE
//
void optional_reference__test_no_converting_assignment()
{
boost::optional<int&> opt ;
short v = 1 ;
short& r = v ;
opt = r ;
}

View File

@ -1,23 +0,0 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#include "boost/optional.hpp"
//
// THIS TEST SHOULD FAIL TO COMPILE
//
void optional_reference__test_no_ptr_access()
{
boost::optional<int&> opt ;
opt.get_ptr();
}

View File

@ -1,23 +0,0 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#include "boost/optional.hpp"
//
// THIS TEST SHOULD FAIL TO COMPILE
//
void optional_reference__test_no_converting_ctor()
{
boost::optional<short&> opt1 ;
boost::optional<int&> opt2 = opt1 ;
}

View File

@ -1,24 +0,0 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#include "boost/optional.hpp"
//
// THIS TEST SHOULD FAIL TO COMPILE
//
void optional_reference__test_no_converting_initialization()
{
short v = 1 ;
short& r = v;
boost::optional<int&> opt(r) ;
}

View File

@ -1,64 +0,0 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#include<iostream>
#include<stdexcept>
#include<string>
#define BOOST_ENABLE_ASSERT_HANDLER
#include "boost/optional.hpp"
#include "boost/tuple/tuple.hpp"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "boost/test/minimal.hpp"
#include "optional_test_common.cpp"
// Test boost::tie() interoperabiliy.
int test_main( int, char* [] )
{
typedef X T ;
try
{
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
T z(0);
T a(1);
T b(2);
optional<T> oa, ob ;
// T::T( T const& x ) is used
set_pending_dtor( ARG(T) ) ;
set_pending_copy( ARG(T) ) ;
boost::tie(oa,ob) = std::make_pair(a,b) ;
check_is_not_pending_dtor( ARG(T) ) ;
check_is_not_pending_copy( ARG(T) ) ;
check_initialized(oa);
check_initialized(ob);
check_value(oa,a,z);
check_value(ob,b,z);
}
catch ( ... )
{
BOOST_ERROR("Unexpected Exception caught!");
}
return 0;
}