|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r56165 - in sandbox/explore: boost/explore libs/explore/test
From: jeff_at_[hidden]
Date: 2009-09-12 20:30:42
Author: jefffaust
Date: 2009-09-12 20:30:41 EDT (Sat, 12 Sep 2009)
New Revision: 56165
URL: http://svn.boost.org/trac/boost/changeset/56165
Log:
simplify and rename depth_guard. Make pair streaming work with item_width. Make stream_associative_value work with item_width. Improve quote_strings test. Make item_width work with maps and pairs.
Text files modified:
sandbox/explore/boost/explore/container_stream_state.hpp | 14 ++++++++++----
sandbox/explore/boost/explore/manipulators.hpp | 12 ++++++------
sandbox/explore/boost/explore/pair.hpp | 22 +++++++++++++++++-----
sandbox/explore/boost/explore/stream_container.hpp | 7 ++++---
sandbox/explore/boost/explore/stream_value.hpp | 22 ++++++++++++++++------
sandbox/explore/libs/explore/test/quote_strings.cpp | 12 +++++++-----
sandbox/explore/libs/explore/test/std_map.cpp | 27 +++++++++++++++++++++------
sandbox/explore/libs/explore/test/std_pair.cpp | 25 +++++++++++++++++++------
8 files changed, 100 insertions(+), 41 deletions(-)
Modified: sandbox/explore/boost/explore/container_stream_state.hpp
==============================================================================
--- sandbox/explore/boost/explore/container_stream_state.hpp (original)
+++ sandbox/explore/boost/explore/container_stream_state.hpp 2009-09-12 20:30:41 EDT (Sat, 12 Sep 2009)
@@ -1,7 +1,7 @@
//
// container_stream_state.hpp - stream state for containers
//
-// Copyright (C) 2007, Jeffrey Faust
+// Copyright (C) 2007-2009, Jeffrey Faust
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -32,7 +32,7 @@
BOOST_EXPLORE_INIT_STRING(assoc_item_start, "")
BOOST_EXPLORE_INIT_STRING(assoc_item_end, "")
- struct depth_guard;
+ struct increment_depth;
// read
template<typename T>
@@ -63,7 +63,13 @@
{
}
- void set_level(size_t l) { m_level = l; }
+ size_t set_level(size_t l)
+ {
+ size_t prev = m_level;
+ m_level = l;
+ return prev;
+ }
+
void level_up() { ++m_level; }
void level_down() { --m_level; }
size_t level() const { return m_level; }
@@ -80,7 +86,7 @@
void set_quote_strings(bool qs) { m_quotestrings = qs; }
private:
- friend struct detail::depth_guard;
+ friend struct detail::increment_depth;
typedef std::vector<std::size_t, std::allocator<std::size_t> > size_cont_typ;
Modified: sandbox/explore/boost/explore/manipulators.hpp
==============================================================================
--- sandbox/explore/boost/explore/manipulators.hpp (original)
+++ sandbox/explore/boost/explore/manipulators.hpp 2009-09-12 20:30:41 EDT (Sat, 12 Sep 2009)
@@ -1,7 +1,7 @@
//
// stream_container.hpp - container streaming.
//
-// Copyright (C) 2007, Jeffrey Faust
+// Copyright (C) 2007-2009, Jeffrey Faust
// Copyright (C) 2008-2009, Jared McIntyre
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
@@ -25,15 +25,15 @@
{
namespace detail
{
- struct depth_guard
+ struct increment_depth
{
- depth_guard(container_common_stream_state* state)
- : m_state(state), m_prev_level(state->level())
+ increment_depth(container_common_stream_state* state)
+ : m_state(state)
{
- ++m_state->m_depth;
+ m_prev_level = m_state->set_level(m_state->m_depth++);
}
- ~depth_guard()
+ ~increment_depth()
{
--m_state->m_depth;
m_state->set_level(m_prev_level);
Modified: sandbox/explore/boost/explore/pair.hpp
==============================================================================
--- sandbox/explore/boost/explore/pair.hpp (original)
+++ sandbox/explore/boost/explore/pair.hpp 2009-09-12 20:30:41 EDT (Sat, 12 Sep 2009)
@@ -1,7 +1,7 @@
//
// pair.hpp - container streaming.
//
-// Copyright (C) 2007, Jeffrey Faust
+// Copyright (C) 2007-2009, Jeffrey Faust
// Copyright (C) 2009, Jared McIntyre
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
@@ -20,12 +20,24 @@
std::basic_ostream<Elem, Tr>& operator<<(std::basic_ostream<Elem, Tr>& ostr, const std::pair<T1, T2>& p)
{
using namespace boost::explore;
+
container_common_stream_state* common_state = get_stream_state<container_common_stream_state>(ostr);
- detail::depth_guard guard(common_state);
- common_state->set_level(common_state->depth()-1);
container_stream_state<Elem>* state = get_stream_state<container_stream_state<Elem> >(ostr);
- return ostr << state->start() << p.first << state->separator() << p.second << state->end();
+
+ // set the level based on the current recursive depth
+ detail::increment_depth guard(common_state);
+
+ 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 << state->start() << p.first << state->separator() << p.second << state->end();
+ }
+
+ ostr.width(common_state->itemwidth());
+ return ostr << sstream.str();
}
}
-#endif
\ No newline at end of file
+#endif
Modified: sandbox/explore/boost/explore/stream_container.hpp
==============================================================================
--- sandbox/explore/boost/explore/stream_container.hpp (original)
+++ sandbox/explore/boost/explore/stream_container.hpp 2009-09-12 20:30:41 EDT (Sat, 12 Sep 2009)
@@ -1,7 +1,7 @@
//
// stream_container.hpp - container streaming.
//
-// Copyright (C) 2007, Jeffrey Faust
+// Copyright (C) 2007-2009, Jeffrey Faust
// Copyright (C) 2009, Jared McIntyre
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
@@ -25,8 +25,9 @@
// grab the extra data embedded in the stream object.
container_stream_state<Elem>* state = explore::get_stream_state<container_stream_state<Elem> >(ostr);
container_common_stream_state* common_state = explore::get_stream_state<container_common_stream_state>(ostr);
- detail::depth_guard guard(common_state);
- common_state->set_level(common_state->depth()-1);
+
+ // set the level based on the current recursive depth
+ detail::increment_depth guard(common_state);
// starting delimiter
ostr << state->start();
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 20:30:41 EDT (Sat, 12 Sep 2009)
@@ -1,7 +1,7 @@
//
// stream_value.hpp - streaming function objects for different value types
//
-// Copyright (C) 2007, Jeffrey Faust
+// Copyright (C) 2007-2009, Jeffrey Faust
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -121,11 +121,21 @@
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();
- 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();
+ 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());
+ 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)
+ << state->assoc_item_end();
+ }
+
+ // now width will correctly apply to the entire value
+ ostr.width(common_state->itemwidth());
+ ostr << sstream.str();
}
};
}}
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 20:30:41 EDT (Sat, 12 Sep 2009)
@@ -24,16 +24,17 @@
reset(str_out);
- mis.insert(std::make_pair(1, str_to<C>("first")));
+ mis[1] = str_to<C>("first");
+ mis[2] = str_to<C>("second");
str_out << mis;
- BOOST_CHECK_EQUAL(output(str_out), "[1:first]");
+ BOOST_CHECK_EQUAL(output(str_out), "[1:first, 2:second]");
reset(str_out);
str_out << quote_strings() << mis;
- BOOST_CHECK_EQUAL(output(str_out), "[1:\"first\"]");
+ BOOST_CHECK_EQUAL(output(str_out), "[1:\"first\", 2:\"second\"]");
str_out << no_quote_strings() << mis;
- BOOST_CHECK_EQUAL(output(str_out), "[1:\"first\"][1:first]");
+ BOOST_CHECK_EQUAL(output(str_out), "[1:\"first\", 2:\"second\"][1:first, 2:second]");
}
BOOST_AUTO_TEST_CASE_TEMPLATE( strings_with_item_width, C, test_types )
@@ -45,7 +46,8 @@
std::vector<string_type> vs;
vs.push_back(str_to<C>("1234"));
+ vs.push_back(str_to<C>("5678"));
str_out << quote_strings() << item_width(7) << vs;
- BOOST_CHECK_EQUAL(output(str_out), "[ \"1234\"]");
+ BOOST_CHECK_EQUAL(output(str_out), "[ \"1234\", \"5678\"]");
}
Modified: sandbox/explore/libs/explore/test/std_map.cpp
==============================================================================
--- sandbox/explore/libs/explore/test/std_map.cpp (original)
+++ sandbox/explore/libs/explore/test/std_map.cpp 2009-09-12 20:30:41 EDT (Sat, 12 Sep 2009)
@@ -1,10 +1,11 @@
// Boost.Explore library
-
-// Copyright Jared McIntyre 2007. Use, modification and
-// distribution is subject to the Boost Software License, Version
-// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
-// http://www.boost.org/LICENSE_1_0.txt)
-
+//
+// Copyright (C) 2007, Jared McIntyre
+// Copyright (C) 2009, Jeffrey Faust
+//
+// Use, modification and distribution is subject to the Boost Software License, Version
+// 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
// For more information, see http://www.boost.org
#define BOOST_TEST_MODULE PrintLib
@@ -107,3 +108,17 @@
str_out << boost::explore::make_iterator_range(mivi.begin(), ++(++mivi.begin()));
BOOST_CHECK_EQUAL(output(str_out), "[1:[1, 2, 3], 2:[1, 2, 3]]");
}
+
+BOOST_AUTO_TEST_CASE_TEMPLATE( map_with_item_width, C, test_types )
+{
+ using namespace boost::explore;
+
+ typename test_traits<C>::stream_type str_out;
+
+ std::map<int, int> mii;
+ mii[12] = 34;
+ mii[56] = 78;
+ str_out << item_width(10) << separator(str_to<C>("")) << mii;
+
+ BOOST_CHECK_EQUAL(output(str_out), "[ 12:34 56:78]");
+}
Modified: sandbox/explore/libs/explore/test/std_pair.cpp
==============================================================================
--- sandbox/explore/libs/explore/test/std_pair.cpp (original)
+++ sandbox/explore/libs/explore/test/std_pair.cpp 2009-09-12 20:30:41 EDT (Sat, 12 Sep 2009)
@@ -1,10 +1,11 @@
// Boost.Explore library
-
-// Copyright Jared McIntyre 2007. Use, modification and
-// distribution is subject to the Boost Software License, Version
-// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
-// http://www.boost.org/LICENSE_1_0.txt)
-
+//
+// Copyright (C) 2007, Jared McIntyre
+// Copyright (C) 2009, Jeffrey Faust
+//
+// Use, modification and distribution is subject to the Boost Software License, Version
+// 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
// For more information, see http://www.boost.org
#define BOOST_TEST_MODULE PrintLib
@@ -51,3 +52,15 @@
str_out << boost::explore::make_iterator_range(vpi.begin(), ++(++vpi.begin()));
BOOST_CHECK_EQUAL(output(str_out), "[[1, 2], [1, 2]]");
}
+
+BOOST_AUTO_TEST_CASE_TEMPLATE( pair_with_item_width, C, test_types )
+{
+ using namespace boost::explore;
+ using namespace std;
+ typename test_traits<C>::stream_type str_out;
+
+ pair<int,int> pi(3, 33);
+
+ str_out << item_width(10) << pi;
+ BOOST_CHECK_EQUAL(output(str_out), " [3, 33]");
+}
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