Improvement to "cout << none" implementation

This commit is contained in:
Andrzej Krzemienski
2014-11-24 22:53:59 +01:00
parent f8bbb9fabb
commit 0d06d66f5c
2 changed files with 36 additions and 11 deletions

View File

@ -28,7 +28,11 @@ inline
std::basic_ostream<CharType, CharTrait>& std::basic_ostream<CharType, CharTrait>&
operator<<(std::basic_ostream<CharType, CharTrait>& out, none_t const&) operator<<(std::basic_ostream<CharType, CharTrait>& out, none_t const&)
{ {
if (out.good())
{
out << "--"; out << "--";
}
return out; return out;
} }
@ -37,9 +41,9 @@ inline
std::basic_ostream<CharType, CharTrait>& std::basic_ostream<CharType, CharTrait>&
operator<<(std::basic_ostream<CharType, CharTrait>& out, optional<T> const& v) operator<<(std::basic_ostream<CharType, CharTrait>& out, optional<T> const& v)
{ {
if ( out.good() ) if (out.good())
{ {
if ( !v ) if (!v)
out << "--" ; out << "--" ;
else out << ' ' << *v ; else out << ' ' << *v ;
} }

View File

@ -61,8 +61,8 @@ void test2( Opt o, Opt buff )
s << o << " " << markv ; s << o << " " << markv ;
s >> buff >> mark ; s >> buff >> mark ;
BOOST_ASSERT( buff == o ) ; BOOST_CHECK( buff == o ) ;
BOOST_ASSERT( mark == markv ) ; BOOST_CHECK( mark == markv ) ;
} }
@ -75,12 +75,32 @@ void test( T v, T w )
test2( optional<T> () , make_optional(w)); test2( optional<T> () , make_optional(w));
} }
void
test() template <class T>
void subtest_tag_none_reversibility_with_optional(optional<T> ov)
{ {
stringstream s ; stringstream s;
s << boost::none; s << boost::none;
BOOST_ASSERT(s.str() == "--"); s >> ov;
BOOST_CHECK(!ov);
}
template <class T>
void subtest_tag_none_equivalence_with_optional()
{
stringstream s, r;
optional<T> ov;
s << boost::none;
r << ov;
BOOST_CHECK(s.str() == r.str());
}
template <class T>
void test_tag_none(T v)
{
subtest_tag_none_reversibility_with_optional(optional<T>(v));
subtest_tag_none_reversibility_with_optional(optional<T>());
subtest_tag_none_equivalence_with_optional<T>();
} }
@ -90,7 +110,8 @@ int test_main( int, char* [] )
{ {
test(1,2); test(1,2);
test(string("hello"),string("buffer")); test(string("hello"),string("buffer"));
test(); test_tag_none(10);
test_tag_none(string("text"));
} }
catch ( ... ) catch ( ... )
{ {