From 72cd2231306097e299b320d76b677af93916c8d4 Mon Sep 17 00:00:00 2001 From: Steven Watanabe Date: Tue, 19 Apr 2011 20:10:43 +0000 Subject: [PATCH] Merge [67751] from the trunk. [SVN r71382] --- include/boost/tuple/tuple_io.hpp | 39 ++++++++++++++++++++++++++++++++ test/io_test.cpp | 6 +++++ 2 files changed, 45 insertions(+) diff --git a/include/boost/tuple/tuple_io.hpp b/include/boost/tuple/tuple_io.hpp index 06a2339..c549716 100644 --- a/include/boost/tuple/tuple_io.hpp +++ b/include/boost/tuple/tuple_io.hpp @@ -29,6 +29,8 @@ #include #endif +#include + #include "boost/tuple/tuple.hpp" // This is ugly: one should be using twoargument isspace since whitspace can @@ -244,6 +246,22 @@ print(std::ostream& o, const cons& t) { } +template +inline bool handle_width(std::ostream& o, const T& t) { + std::streamsize width = o.width(); + if(width == 0) return false; + + std::ostringstream ss; + + ss.copyfmt(o); + ss.tie(0); + ss.width(0); + + ss << t; + o << ss.str(); + + return true; +} #else @@ -280,6 +298,23 @@ print(std::basic_ostream& o, const cons& t) { return print(o, t.tail); } +template +inline bool handle_width(std::basic_ostream& o, const T& t) { + std::streamsize width = o.width(); + if(width == 0) return false; + + std::basic_ostringstream ss; + + ss.copyfmt(o); + ss.tie(0); + ss.width(0); + + ss << t; + o << ss.str(); + + return true; +} + #endif // BOOST_NO_TEMPLATED_STREAMS } // namespace detail @@ -288,6 +323,7 @@ print(std::basic_ostream& o, const cons& t) { inline std::ostream& operator<<(std::ostream& o, const null_type& t) { if (!o.good() ) return o; + if (detail::handle_width(o, t)) return o; const char l = detail::format_info::get_manipulator(o, detail::format_info::open); @@ -303,6 +339,7 @@ inline std::ostream& operator<<(std::ostream& o, const null_type& t) { template inline std::ostream& operator<<(std::ostream& o, const cons& t) { if (!o.good() ) return o; + if (detail::handle_width(o, t)) return o; const char l = detail::format_info::get_manipulator(o, detail::format_info::open); @@ -325,6 +362,7 @@ inline std::basic_ostream& operator<<(std::basic_ostream& o, const null_type& t) { if (!o.good() ) return o; + if (detail::handle_width(o, t)) return o; const CharType l = detail::format_info::get_manipulator(o, detail::format_info::open); @@ -342,6 +380,7 @@ inline std::basic_ostream& operator<<(std::basic_ostream& o, const cons& t) { if (!o.good() ) return o; + if (detail::handle_width(o, t)) return o; const CharType l = detail::format_info::get_manipulator(o, detail::format_info::open); diff --git a/test/io_test.cpp b/test/io_test.cpp index 95babe3..382f8cc 100644 --- a/test/io_test.cpp +++ b/test/io_test.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #if defined BOOST_NO_STRINGSTREAM #include @@ -77,6 +78,11 @@ int test_main(int argc, char * argv[] ) { os3 << set_close(']'); os3 << make_tuple(); BOOST_CHECK (os3.str() == std::string("()[]") ); + + // check width + useThisOStringStream os4; + os4 << std::setw(10) << make_tuple(1, 2, 3); + BOOST_CHECK (os4.str() == std::string(" (1 2 3)") ); std::ofstream tmp("temp.tmp");