Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r67751 - in trunk: boost/tuple libs/tuple/test
From: steven_at_[hidden]
Date: 2011-01-07 10:22:17


Author: steven_watanabe
Date: 2011-01-07 10:22:13 EST (Fri, 07 Jan 2011)
New Revision: 67751
URL: http://svn.boost.org/trac/boost/changeset/67751

Log:
Handle width correctly. Fixes #5046.
Text files modified:
   trunk/boost/tuple/tuple_io.hpp | 39 +++++++++++++++++++++++++++++++++++++++
   trunk/libs/tuple/test/io_test.cpp | 6 ++++++
   2 files changed, 45 insertions(+), 0 deletions(-)

Modified: trunk/boost/tuple/tuple_io.hpp
==============================================================================
--- trunk/boost/tuple/tuple_io.hpp (original)
+++ trunk/boost/tuple/tuple_io.hpp 2011-01-07 10:22:13 EST (Fri, 07 Jan 2011)
@@ -29,6 +29,8 @@
 #include <ostream>
 #endif
 
+#include <sstream>
+
 #include "boost/tuple/tuple.hpp"
 
 // This is ugly: one should be using twoargument isspace since whitspace can
@@ -244,6 +246,22 @@
 
 }
 
+template<class T>
+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 @@
   return print(o, t.tail);
 }
 
+template<class CharT, class Traits, class T>
+inline bool handle_width(std::basic_ostream<CharT, Traits>& o, const T& t) {
+ std::streamsize width = o.width();
+ if(width == 0) return false;
+
+ std::basic_ostringstream<CharT, Traits> 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 @@
 
 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 @@
 template<class T1, class T2>
 inline std::ostream& operator<<(std::ostream& o, const cons<T1, T2>& 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 @@
 operator<<(std::basic_ostream<CharType, CharTrait>& 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 @@
 operator<<(std::basic_ostream<CharType, CharTrait>& o,
            const cons<T1, T2>& 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);

Modified: trunk/libs/tuple/test/io_test.cpp
==============================================================================
--- trunk/libs/tuple/test/io_test.cpp (original)
+++ trunk/libs/tuple/test/io_test.cpp 2011-01-07 10:22:13 EST (Fri, 07 Jan 2011)
@@ -20,6 +20,7 @@
 #include <iterator>
 #include <algorithm>
 #include <string>
+#include <iomanip>
 
 #if defined BOOST_NO_STRINGSTREAM
 #include <strstream>
@@ -77,6 +78,11 @@
   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");
 


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