Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r56157 - in sandbox/explore: boost/explore libs/explore/test
From: jeff_at_[hidden]
Date: 2009-09-12 16:00:26


Author: jefffaust
Date: 2009-09-12 16:00:25 EDT (Sat, 12 Sep 2009)
New Revision: 56157
URL: http://svn.boost.org/trac/boost/changeset/56157

Log:
Make quote_strings() and item_width work together.
Text files modified:
   sandbox/explore/boost/explore/stream_value.hpp | 36 ++++++++++++++++++++++++++++++++----
   sandbox/explore/libs/explore/test/quote_strings.cpp | 15 +++++++++++++++
   2 files changed, 47 insertions(+), 4 deletions(-)

Modified: sandbox/explore/boost/explore/stream_value.hpp
==============================================================================
--- sandbox/explore/boost/explore/stream_value.hpp (original)
+++ sandbox/explore/boost/explore/stream_value.hpp 2009-09-12 16:00:25 EDT (Sat, 12 Sep 2009)
@@ -13,6 +13,7 @@
 #include "container_stream_state.hpp"
 
 #include <ostream>
+#include <sstream>
 
 // should be the last #include
 #include <boost/type_traits/detail/bool_trait_def.hpp>
@@ -75,6 +76,24 @@
         {
             explicit value(const T& value, bool quotestrings) : value_holder<T, is_string<T>::value>(value, quotestrings) {}
         };
+
+ template<typename Elem, typename Tr>
+ struct rdbuf_guard
+ {
+ explicit rdbuf_guard(std::basic_ostream<Elem, Tr>& ostr)
+ : m_ostr(ostr), m_prev(ostr.rdbuf())
+ {
+ }
+
+ ~rdbuf_guard()
+ {
+ m_ostr.rdbuf(m_prev);
+ }
+
+ private:
+ std::basic_ostream<Elem, Tr>& m_ostr;
+ std::basic_streambuf<Elem, Tr>* m_prev;
+ };
     }
 
     struct stream_normal_value
@@ -82,9 +101,17 @@
         template<typename Elem, typename Tr, typename T>
         void operator()(std::basic_ostream<Elem, Tr>& ostr, const T& val, container_stream_state<Elem>* state, container_common_stream_state* common_state)
         {
- const bool qs = common_state->quote_strings();
+ std::basic_stringstream<Elem, Tr> sstream;
+
+ { // redirect output to a string stream so we can correctly set width()
+ detail::rdbuf_guard<Elem, Tr> guard(ostr);
+ ostr.rdbuf(sstream.rdbuf());
+ ostr << detail::value<T>(val, common_state->quote_strings());
+ }
+
+ // now width will correctly apply to the entire value
             ostr.width(common_state->itemwidth());
- ostr << detail::value<T>(val, qs);
+ ostr << sstream.str();
         }
     };
 
@@ -95,8 +122,9 @@
         void operator()(std::basic_ostream<Elem, Tr>& ostr, const T& val, container_stream_state<Elem>* state, container_common_stream_state* common_state)
         {
             const bool qs = common_state->quote_strings();
- ostr << state->assoc_item_start() << detail::value<typename T::first_type>(val.first, qs)
- << state->assoc_item_separator() << detail::value<typename T::second_type>(val.second, qs)
+ ostr << state->assoc_item_start()
+ << detail::value<typename T::first_type>(val.first, qs) << state->assoc_item_separator()
+ << detail::value<typename T::second_type>(val.second, qs)
                  << state->assoc_item_end();
         }
     };

Modified: sandbox/explore/libs/explore/test/quote_strings.cpp
==============================================================================
--- sandbox/explore/libs/explore/test/quote_strings.cpp (original)
+++ sandbox/explore/libs/explore/test/quote_strings.cpp 2009-09-12 16:00:25 EDT (Sat, 12 Sep 2009)
@@ -10,6 +10,7 @@
 #define BOOST_TEST_MODULE PrintLib
 #include <boost/test/unit_test.hpp>
 #include <boost/explore/map.hpp>
+#include <boost/explore/vector.hpp>
 #include "boost_explore_test_tools.hpp"
 
 BOOST_AUTO_TEST_CASE_TEMPLATE( strings_in_map, C, test_types )
@@ -34,3 +35,17 @@
     str_out << no_quote_strings() << mis;
     BOOST_CHECK_EQUAL(output(str_out), "[1:\"first\"][1:first]");
 }
+
+BOOST_AUTO_TEST_CASE_TEMPLATE( strings_with_item_width, C, test_types )
+{
+ using namespace boost::explore;
+ typedef typename test_traits<C>::string_type string_type;
+
+ typename test_traits<C>::stream_type str_out;
+
+ std::vector<string_type> vs;
+ vs.push_back(str_to<C>("1234"));
+ str_out << quote_strings() << item_width(7) << vs;
+
+ BOOST_CHECK_EQUAL(output(str_out), "[ \"1234\"]");
+}


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