mirror of
https://github.com/boostorg/optional.git
synced 2025-07-23 09:07:17 +02:00
operator>> behavior changed slightly so that the stream is not accessed when unrecognized character sequence is detected.
[SVN r67184]
This commit is contained in:
@ -13,18 +13,19 @@
|
|||||||
#define BOOST_OPTIONAL_OPTIONAL_IO_FLC_19NOV2002_HPP
|
#define BOOST_OPTIONAL_OPTIONAL_IO_FLC_19NOV2002_HPP
|
||||||
|
|
||||||
#if defined __GNUC__
|
#if defined __GNUC__
|
||||||
# if (__GNUC__ == 2 && __GNUC_MINOR__ <= 97)
|
# if (__GNUC__ == 2 && __GNUC_MINOR__ <= 97)
|
||||||
# define BOOST_OPTIONAL_NO_TEMPLATED_STREAMS
|
# define BOOST_OPTIONAL_NO_TEMPLATED_STREAMS
|
||||||
# endif
|
# endif
|
||||||
#endif // __GNUC__
|
#endif // __GNUC__
|
||||||
|
|
||||||
#if defined BOOST_OPTIONAL_NO_TEMPLATED_STREAMS
|
#if defined BOOST_OPTIONAL_NO_TEMPLATED_STREAMS
|
||||||
# include <iostream>
|
# include <iostream>
|
||||||
#else
|
#else
|
||||||
# include <istream>
|
# include <istream>
|
||||||
# include <ostream>
|
# include <ostream>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <boost/none.hpp>
|
||||||
#include <boost/assert.hpp>
|
#include <boost/assert.hpp>
|
||||||
#include "boost/optional/optional.hpp"
|
#include "boost/optional/optional.hpp"
|
||||||
#include "boost/utility/value_init.hpp"
|
#include "boost/utility/value_init.hpp"
|
||||||
@ -62,26 +63,29 @@ std::basic_istream<CharType, CharTrait>&
|
|||||||
operator>>(std::basic_istream<CharType, CharTrait>& in, optional<T>& v)
|
operator>>(std::basic_istream<CharType, CharTrait>& in, optional<T>& v)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
if ( in.good() )
|
if (in.good())
|
||||||
{
|
{
|
||||||
int d = in.get();
|
int d = in.get();
|
||||||
if ( d == ' ' )
|
if (d == ' ')
|
||||||
{
|
{
|
||||||
T x ;
|
T x;
|
||||||
in >> x;
|
in >> x;
|
||||||
v = x ;
|
v = x;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if ( d != '-')
|
if (d == '-')
|
||||||
in.setstate( std::ios::failbit );
|
{
|
||||||
|
d = in.get();
|
||||||
d = in.get();
|
|
||||||
|
if (d == '-')
|
||||||
if ( d != '-')
|
{
|
||||||
in.setstate( std::ios::failbit );
|
v = none;
|
||||||
|
return in;
|
||||||
v = optional<T>() ;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
in.setstate( std::ios::failbit );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,27 +158,27 @@ void test_basics( T const* )
|
|||||||
ob.reset();
|
ob.reset();
|
||||||
check_is_pending_dtor( ARG(T) ) ;
|
check_is_pending_dtor( ARG(T) ) ;
|
||||||
check_uninitialized(ob);
|
check_uninitialized(ob);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
void test_conditional_ctor_and_get_valur_or ( T const* )
|
void test_conditional_ctor_and_get_valur_or ( T const* )
|
||||||
{
|
{
|
||||||
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
|
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
|
||||||
|
|
||||||
T a(321);
|
T a(321);
|
||||||
|
|
||||||
T z(123);
|
T z(123);
|
||||||
|
|
||||||
optional<T> const cdef0(false,a);
|
optional<T> const cdef0(false,a);
|
||||||
|
|
||||||
optional<T> def0(false,a);
|
optional<T> def0(false,a);
|
||||||
optional<T> def1 = boost::make_optional(false,a); // T is not within boost so ADL won't find make_optional unqualified
|
optional<T> def1 = boost::make_optional(false,a); // T is not within boost so ADL won't find make_optional unqualified
|
||||||
check_uninitialized(def0);
|
check_uninitialized(def0);
|
||||||
check_uninitialized(def1);
|
check_uninitialized(def1);
|
||||||
|
|
||||||
optional<T> const co0(true,a);
|
optional<T> const co0(true,a);
|
||||||
|
|
||||||
optional<T> o0(true,a);
|
optional<T> o0(true,a);
|
||||||
optional<T> o1 = boost::make_optional(true,a); // T is not within boost so ADL won't find make_optional unqualified
|
optional<T> o1 = boost::make_optional(true,a); // T is not within boost so ADL won't find make_optional unqualified
|
||||||
|
|
||||||
@ -186,65 +186,65 @@ void test_conditional_ctor_and_get_valur_or ( T const* )
|
|||||||
check_initialized(o1);
|
check_initialized(o1);
|
||||||
check_value(o0,a,z);
|
check_value(o0,a,z);
|
||||||
check_value(o1,a,z);
|
check_value(o1,a,z);
|
||||||
|
|
||||||
T b = def0.get_value_or(z);
|
T b = def0.get_value_or(z);
|
||||||
BOOST_CHECK( b == z ) ;
|
BOOST_CHECK( b == z ) ;
|
||||||
|
|
||||||
b = get_optional_value_or(def0,z);
|
b = get_optional_value_or(def0,z);
|
||||||
BOOST_CHECK( b == z ) ;
|
BOOST_CHECK( b == z ) ;
|
||||||
|
|
||||||
b = o0.get_value_or(z);
|
b = o0.get_value_or(z);
|
||||||
BOOST_CHECK( b == a ) ;
|
BOOST_CHECK( b == a ) ;
|
||||||
|
|
||||||
b = get_optional_value_or(o0,z);
|
b = get_optional_value_or(o0,z);
|
||||||
BOOST_CHECK( b == a ) ;
|
BOOST_CHECK( b == a ) ;
|
||||||
|
|
||||||
|
|
||||||
T const& crz = z ;
|
T const& crz = z ;
|
||||||
T& rz = z ;
|
T& rz = z ;
|
||||||
|
|
||||||
T const& crzz = def0.get_value_or(crz);
|
T const& crzz = def0.get_value_or(crz);
|
||||||
BOOST_CHECK( crzz == crz ) ;
|
BOOST_CHECK( crzz == crz ) ;
|
||||||
|
|
||||||
T& rzz = def0.get_value_or(rz);
|
T& rzz = def0.get_value_or(rz);
|
||||||
BOOST_CHECK( rzz == rz ) ;
|
BOOST_CHECK( rzz == rz ) ;
|
||||||
|
|
||||||
T const& crzzz = get_optional_value_or(cdef0,crz);
|
T const& crzzz = get_optional_value_or(cdef0,crz);
|
||||||
BOOST_CHECK( crzzz == crz ) ;
|
BOOST_CHECK( crzzz == crz ) ;
|
||||||
|
|
||||||
T& rzzz = get_optional_value_or(def0,rz);
|
T& rzzz = get_optional_value_or(def0,rz);
|
||||||
BOOST_CHECK( rzzz == rz ) ;
|
BOOST_CHECK( rzzz == rz ) ;
|
||||||
|
|
||||||
T const& crb = o0.get_value_or(crz);
|
T const& crb = o0.get_value_or(crz);
|
||||||
BOOST_CHECK( crb == a ) ;
|
BOOST_CHECK( crb == a ) ;
|
||||||
|
|
||||||
T& rb = o0.get_value_or(rz);
|
T& rb = o0.get_value_or(rz);
|
||||||
BOOST_CHECK( rb == b ) ;
|
BOOST_CHECK( rb == b ) ;
|
||||||
|
|
||||||
T const& crbb = get_optional_value_or(co0,crz);
|
T const& crbb = get_optional_value_or(co0,crz);
|
||||||
BOOST_CHECK( crbb == b ) ;
|
BOOST_CHECK( crbb == b ) ;
|
||||||
|
|
||||||
T const& crbbb = get_optional_value_or(o0,crz);
|
T const& crbbb = get_optional_value_or(o0,crz);
|
||||||
BOOST_CHECK( crbbb == b ) ;
|
BOOST_CHECK( crbbb == b ) ;
|
||||||
|
|
||||||
T& rbb = get_optional_value_or(o0,rz);
|
T& rbb = get_optional_value_or(o0,rz);
|
||||||
BOOST_CHECK( rbb == b ) ;
|
BOOST_CHECK( rbb == b ) ;
|
||||||
|
|
||||||
T& ra = a ;
|
T& ra = a ;
|
||||||
|
|
||||||
optional<T&> defref(false,ra);
|
optional<T&> defref(false,ra);
|
||||||
BOOST_CHECK(!defref);
|
BOOST_CHECK(!defref);
|
||||||
|
|
||||||
optional<T&> ref(true,ra);
|
optional<T&> ref(true,ra);
|
||||||
BOOST_CHECK(!!ref);
|
BOOST_CHECK(!!ref);
|
||||||
|
|
||||||
a = T(432);
|
a = T(432);
|
||||||
|
|
||||||
BOOST_CHECK( *ref == a ) ;
|
BOOST_CHECK( *ref == a ) ;
|
||||||
|
|
||||||
T& r1 = defref.get_value_or(z);
|
T& r1 = defref.get_value_or(z);
|
||||||
BOOST_CHECK( r1 == z ) ;
|
BOOST_CHECK( r1 == z ) ;
|
||||||
|
|
||||||
T& r2 = ref.get_value_or(z);
|
T& r2 = ref.get_value_or(z);
|
||||||
BOOST_CHECK( r2 == a ) ;
|
BOOST_CHECK( r2 == a ) ;
|
||||||
}
|
}
|
||||||
@ -718,7 +718,7 @@ void test_relops( T const* )
|
|||||||
optional<T> opt0(v0);
|
optional<T> opt0(v0);
|
||||||
optional<T> opt1(v1);
|
optional<T> opt1(v1);
|
||||||
optional<T> opt2(v2);
|
optional<T> opt2(v2);
|
||||||
|
|
||||||
// Check identity
|
// Check identity
|
||||||
BOOST_CHECK ( def0 == def0 ) ;
|
BOOST_CHECK ( def0 == def0 ) ;
|
||||||
BOOST_CHECK ( opt0 == opt0 ) ;
|
BOOST_CHECK ( opt0 == opt0 ) ;
|
||||||
@ -756,7 +756,7 @@ void test_relops( T const* )
|
|||||||
BOOST_CHECK ( opt1 > opt0 ) ;
|
BOOST_CHECK ( opt1 > opt0 ) ;
|
||||||
BOOST_CHECK ( opt1 <= opt2 ) ;
|
BOOST_CHECK ( opt1 <= opt2 ) ;
|
||||||
BOOST_CHECK ( opt1 >= opt0 ) ;
|
BOOST_CHECK ( opt1 >= opt0 ) ;
|
||||||
|
|
||||||
// Compare against a value directly
|
// Compare against a value directly
|
||||||
BOOST_CHECK ( opt0 == v0 ) ;
|
BOOST_CHECK ( opt0 == v0 ) ;
|
||||||
BOOST_CHECK ( opt0 != v1 ) ;
|
BOOST_CHECK ( opt0 != v1 ) ;
|
||||||
@ -799,7 +799,7 @@ void test_none( T const* )
|
|||||||
BOOST_CHECK ( def0 == none ) ;
|
BOOST_CHECK ( def0 == none ) ;
|
||||||
BOOST_CHECK ( non_def != none ) ;
|
BOOST_CHECK ( non_def != none ) ;
|
||||||
BOOST_CHECK ( !def1 ) ;
|
BOOST_CHECK ( !def1 ) ;
|
||||||
BOOST_CHECK ( !(non_def < none) ) ;
|
BOOST_CHECK ( !(non_def < none) ) ;
|
||||||
BOOST_CHECK ( non_def > none ) ;
|
BOOST_CHECK ( non_def > none ) ;
|
||||||
BOOST_CHECK ( !(non_def <= none) ) ;
|
BOOST_CHECK ( !(non_def <= none) ) ;
|
||||||
BOOST_CHECK ( non_def >= none ) ;
|
BOOST_CHECK ( non_def >= none ) ;
|
||||||
@ -819,11 +819,11 @@ void test_arrow( T const* )
|
|||||||
|
|
||||||
optional<T> oa(a) ;
|
optional<T> oa(a) ;
|
||||||
optional<T> const coa(a) ;
|
optional<T> const coa(a) ;
|
||||||
|
|
||||||
BOOST_CHECK ( coa->V() == 1234 ) ;
|
BOOST_CHECK ( coa->V() == 1234 ) ;
|
||||||
|
|
||||||
oa->V() = 4321 ;
|
oa->V() = 4321 ;
|
||||||
|
|
||||||
BOOST_CHECK ( a.V() = 1234 ) ;
|
BOOST_CHECK ( a.V() = 1234 ) ;
|
||||||
BOOST_CHECK ( (*oa).V() = 4321 ) ;
|
BOOST_CHECK ( (*oa).V() = 4321 ) ;
|
||||||
}
|
}
|
||||||
@ -1086,7 +1086,7 @@ namespace optional_swap_test
|
|||||||
x = boost::in_place('\0');
|
x = boost::in_place('\0');
|
||||||
else if ( !hasY )
|
else if ( !hasY )
|
||||||
y = boost::in_place('\0');
|
y = boost::in_place('\0');
|
||||||
|
|
||||||
optional_swap_test::swap(*x,*y);
|
optional_swap_test::swap(*x,*y);
|
||||||
|
|
||||||
if( !hasX )
|
if( !hasX )
|
||||||
@ -1118,7 +1118,7 @@ template <class T> struct optional_swap_should_use_default_constructor<
|
|||||||
//
|
//
|
||||||
// Specialization of boost::swap:
|
// Specialization of boost::swap:
|
||||||
//
|
//
|
||||||
template <>
|
template <>
|
||||||
void swap(optional<optional_swap_test::class_whose_explicit_ctor_should_be_used> & x, optional<optional_swap_test::class_whose_explicit_ctor_should_be_used> & y)
|
void swap(optional<optional_swap_test::class_whose_explicit_ctor_should_be_used> & x, optional<optional_swap_test::class_whose_explicit_ctor_should_be_used> & y)
|
||||||
{
|
{
|
||||||
optional_swap_test::swap(x, y);
|
optional_swap_test::swap(x, y);
|
||||||
@ -1133,25 +1133,25 @@ namespace std {
|
|||||||
// Specializations of std::swap:
|
// Specializations of std::swap:
|
||||||
//
|
//
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void swap(optional_swap_test::class_whose_default_ctor_should_be_used & x, optional_swap_test::class_whose_default_ctor_should_be_used & y)
|
void swap(optional_swap_test::class_whose_default_ctor_should_be_used & x, optional_swap_test::class_whose_default_ctor_should_be_used & y)
|
||||||
{
|
{
|
||||||
optional_swap_test::swap(x, y);
|
optional_swap_test::swap(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void swap(optional_swap_test::class_whose_default_ctor_should_not_be_used & x, optional_swap_test::class_whose_default_ctor_should_not_be_used & y)
|
void swap(optional_swap_test::class_whose_default_ctor_should_not_be_used & x, optional_swap_test::class_whose_default_ctor_should_not_be_used & y)
|
||||||
{
|
{
|
||||||
optional_swap_test::swap(x, y);
|
optional_swap_test::swap(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void swap(optional_swap_test::class_without_default_ctor & x, optional_swap_test::class_without_default_ctor & y)
|
void swap(optional_swap_test::class_without_default_ctor & x, optional_swap_test::class_without_default_ctor & y)
|
||||||
{
|
{
|
||||||
optional_swap_test::swap(x, y);
|
optional_swap_test::swap(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void swap(optional_swap_test::class_whose_explicit_ctor_should_be_used & x, optional_swap_test::class_whose_explicit_ctor_should_be_used & y)
|
void swap(optional_swap_test::class_whose_explicit_ctor_should_be_used & x, optional_swap_test::class_whose_explicit_ctor_should_be_used & y)
|
||||||
{
|
{
|
||||||
optional_swap_test::swap(x, y);
|
optional_swap_test::swap(x, y);
|
||||||
@ -1204,7 +1204,7 @@ bool test_swap_function( T const* )
|
|||||||
|
|
||||||
//
|
//
|
||||||
// Tests whether the optional<T>::swap member function works properly.
|
// Tests whether the optional<T>::swap member function works properly.
|
||||||
// Assumes that T has one data member, of type char.
|
// Assumes that T has one data member, of type char.
|
||||||
// Returns true iff the test is passed.
|
// Returns true iff the test is passed.
|
||||||
//
|
//
|
||||||
template <class T>
|
template <class T>
|
||||||
@ -1263,6 +1263,33 @@ void test_swap_tweaking()
|
|||||||
BOOST_CHECK( test_swap_member_function( ARG(optional_swap_test::template_whose_default_ctor_should_be_used<char>) ) );
|
BOOST_CHECK( test_swap_member_function( ARG(optional_swap_test::template_whose_default_ctor_should_be_used<char>) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test for support for classes with overridden operator&
|
||||||
|
class CustomAddressOfClass
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
|
||||||
|
public:
|
||||||
|
CustomAddressOfClass() : n(0) {}
|
||||||
|
CustomAddressOfClass(CustomAddressOfClass const& that) : n(that.n) {}
|
||||||
|
explicit CustomAddressOfClass(int m) : n(m) {}
|
||||||
|
int* operator& () { return &n; }
|
||||||
|
bool operator== (CustomAddressOfClass const& that) const { return n == that.n; }
|
||||||
|
};
|
||||||
|
|
||||||
|
void test_custom_addressof_operator()
|
||||||
|
{
|
||||||
|
boost::optional< CustomAddressOfClass > o1(CustomAddressOfClass(10));
|
||||||
|
BOOST_CHECK(!!o1);
|
||||||
|
BOOST_CHECK(o1.get() == CustomAddressOfClass(10));
|
||||||
|
|
||||||
|
o1 = CustomAddressOfClass(20);
|
||||||
|
BOOST_CHECK(!!o1);
|
||||||
|
BOOST_CHECK(o1.get() == CustomAddressOfClass(20));
|
||||||
|
|
||||||
|
o1 = boost::none;
|
||||||
|
BOOST_CHECK(!o1);
|
||||||
|
}
|
||||||
|
|
||||||
int test_main( int, char* [] )
|
int test_main( int, char* [] )
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -1273,6 +1300,7 @@ int test_main( int, char* [] )
|
|||||||
test_conversions1();
|
test_conversions1();
|
||||||
test_conversions2();
|
test_conversions2();
|
||||||
test_swap_tweaking();
|
test_swap_tweaking();
|
||||||
|
test_custom_addressof_operator();
|
||||||
}
|
}
|
||||||
catch ( ... )
|
catch ( ... )
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user