|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r67184 - in trunk: boost/optional libs/optional/test
From: andrey.semashev_at_[hidden]
Date: 2010-12-12 06:35:19
Author: andysem
Date: 2010-12-12 06:35:19 EST (Sun, 12 Dec 2010)
New Revision: 67184
URL: http://svn.boost.org/trac/boost/changeset/67184
Log:
operator>> behavior changed slightly so that the stream is not accessed when unrecognized character sequence is detected.
Text files modified:
trunk/boost/optional/optional_io.hpp | 36 +++++++-----
trunk/libs/optional/test/optional_test.cpp | 108 +++++++++++++++++++++++++--------------
2 files changed, 88 insertions(+), 56 deletions(-)
Modified: trunk/boost/optional/optional_io.hpp
==============================================================================
--- trunk/boost/optional/optional_io.hpp (original)
+++ trunk/boost/optional/optional_io.hpp 2010-12-12 06:35:19 EST (Sun, 12 Dec 2010)
@@ -13,18 +13,19 @@
#define BOOST_OPTIONAL_OPTIONAL_IO_FLC_19NOV2002_HPP
#if defined __GNUC__
-# if (__GNUC__ == 2 && __GNUC_MINOR__ <= 97)
+# if (__GNUC__ == 2 && __GNUC_MINOR__ <= 97)
# define BOOST_OPTIONAL_NO_TEMPLATED_STREAMS
# endif
#endif // __GNUC__
#if defined BOOST_OPTIONAL_NO_TEMPLATED_STREAMS
# include <iostream>
-#else
+#else
# include <istream>
# include <ostream>
-#endif
+#endif
+#include <boost/none.hpp>
#include <boost/assert.hpp>
#include "boost/optional/optional.hpp"
#include "boost/utility/value_init.hpp"
@@ -62,26 +63,29 @@
operator>>(std::basic_istream<CharType, CharTrait>& in, optional<T>& v)
#endif
{
- if ( in.good() )
+ if (in.good())
{
int d = in.get();
- if ( d == ' ' )
+ if (d == ' ')
{
- T x ;
+ T x;
in >> x;
- v = x ;
+ v = x;
}
else
{
- if ( d != '-')
- in.setstate( std::ios::failbit );
-
- d = in.get();
-
- if ( d != '-')
- in.setstate( std::ios::failbit );
-
- v = optional<T>() ;
+ if (d == '-')
+ {
+ d = in.get();
+
+ if (d == '-')
+ {
+ v = none;
+ return in;
+ }
+ }
+
+ in.setstate( std::ios::failbit );
}
}
Modified: trunk/libs/optional/test/optional_test.cpp
==============================================================================
--- trunk/libs/optional/test/optional_test.cpp (original)
+++ trunk/libs/optional/test/optional_test.cpp 2010-12-12 06:35:19 EST (Sun, 12 Dec 2010)
@@ -158,27 +158,27 @@
ob.reset();
check_is_pending_dtor( ARG(T) ) ;
check_uninitialized(ob);
-
+
}
template<class T>
void test_conditional_ctor_and_get_valur_or ( T const* )
{
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
-
+
T a(321);
-
+
T z(123);
-
+
optional<T> const cdef0(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
check_uninitialized(def0);
check_uninitialized(def1);
optional<T> const co0(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
@@ -186,65 +186,65 @@
check_initialized(o1);
check_value(o0,a,z);
check_value(o1,a,z);
-
+
T b = def0.get_value_or(z);
BOOST_CHECK( b == z ) ;
-
+
b = get_optional_value_or(def0,z);
BOOST_CHECK( b == z ) ;
-
+
b = o0.get_value_or(z);
BOOST_CHECK( b == a ) ;
b = get_optional_value_or(o0,z);
BOOST_CHECK( b == a ) ;
-
-
+
+
T const& crz = z ;
T& rz = z ;
-
+
T const& crzz = def0.get_value_or(crz);
BOOST_CHECK( crzz == crz ) ;
-
+
T& rzz = def0.get_value_or(rz);
BOOST_CHECK( rzz == rz ) ;
-
+
T const& crzzz = get_optional_value_or(cdef0,crz);
BOOST_CHECK( crzzz == crz ) ;
-
+
T& rzzz = get_optional_value_or(def0,rz);
BOOST_CHECK( rzzz == rz ) ;
-
+
T const& crb = o0.get_value_or(crz);
BOOST_CHECK( crb == a ) ;
-
+
T& rb = o0.get_value_or(rz);
BOOST_CHECK( rb == b ) ;
-
+
T const& crbb = get_optional_value_or(co0,crz);
BOOST_CHECK( crbb == b ) ;
-
+
T const& crbbb = get_optional_value_or(o0,crz);
BOOST_CHECK( crbbb == b ) ;
-
+
T& rbb = get_optional_value_or(o0,rz);
BOOST_CHECK( rbb == b ) ;
-
+
T& ra = a ;
-
+
optional<T&> defref(false,ra);
BOOST_CHECK(!defref);
-
+
optional<T&> ref(true,ra);
BOOST_CHECK(!!ref);
-
+
a = T(432);
-
+
BOOST_CHECK( *ref == a ) ;
-
+
T& r1 = defref.get_value_or(z);
BOOST_CHECK( r1 == z ) ;
-
+
T& r2 = ref.get_value_or(z);
BOOST_CHECK( r2 == a ) ;
}
@@ -718,7 +718,7 @@
optional<T> opt0(v0);
optional<T> opt1(v1);
optional<T> opt2(v2);
-
+
// Check identity
BOOST_CHECK ( def0 == def0 ) ;
BOOST_CHECK ( opt0 == opt0 ) ;
@@ -756,7 +756,7 @@
BOOST_CHECK ( opt1 > opt0 ) ;
BOOST_CHECK ( opt1 <= opt2 ) ;
BOOST_CHECK ( opt1 >= opt0 ) ;
-
+
// Compare against a value directly
BOOST_CHECK ( opt0 == v0 ) ;
BOOST_CHECK ( opt0 != v1 ) ;
@@ -799,7 +799,7 @@
BOOST_CHECK ( def0 == none ) ;
BOOST_CHECK ( non_def != none ) ;
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 ) ;
@@ -819,11 +819,11 @@
optional<T> oa(a) ;
optional<T> const coa(a) ;
-
+
BOOST_CHECK ( coa->V() == 1234 ) ;
-
+
oa->V() = 4321 ;
-
+
BOOST_CHECK ( a.V() = 1234 ) ;
BOOST_CHECK ( (*oa).V() = 4321 ) ;
}
@@ -1086,7 +1086,7 @@
x = boost::in_place('\0');
else if ( !hasY )
y = boost::in_place('\0');
-
+
optional_swap_test::swap(*x,*y);
if( !hasX )
@@ -1118,7 +1118,7 @@
//
// 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)
{
optional_swap_test::swap(x, y);
@@ -1133,25 +1133,25 @@
// 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)
{
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)
{
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)
{
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)
{
optional_swap_test::swap(x, y);
@@ -1204,7 +1204,7 @@
//
// 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.
//
template <class T>
@@ -1263,6 +1263,33 @@
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* [] )
{
try
@@ -1273,6 +1300,7 @@
test_conversions1();
test_conversions2();
test_swap_tweaking();
+ test_custom_addressof_operator();
}
catch ( ... )
{
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk