Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r56239 - in sandbox/explore: boost/explore libs/explore/test
From: jeff_at_[hidden]
Date: 2009-09-15 20:33:02


Author: jefffaust
Date: 2009-09-15 20:33:01 EDT (Tue, 15 Sep 2009)
New Revision: 56239
URL: http://svn.boost.org/trac/boost/changeset/56239

Log:
Add state_saver, a stack object that restores container stream state.
Added:
   sandbox/explore/boost/explore/state_saver.hpp (contents, props changed)
   sandbox/explore/libs/explore/test/state_saver.cpp (contents, props changed)
Text files modified:
   sandbox/explore/boost/explore/container_stream_state.hpp | 40 ++++++++++++++++++++++++++++++++++++++++
   sandbox/explore/libs/explore/test/Jamfile.v2 | 1 +
   2 files changed, 41 insertions(+), 0 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-15 20:33:01 EDT (Tue, 15 Sep 2009)
@@ -85,6 +85,24 @@
         bool quote_strings() const { return m_quotestrings; }
         void set_quote_strings(bool qs) { m_quotestrings = qs; }
 
+ void swap(container_common_stream_state& other)
+ {
+ std::swap(m_level, other.m_level);
+ std::swap(m_depth, other.m_depth);
+ std::swap(m_rows, other.m_rows);
+ std::swap(m_itemwidth, other.m_itemwidth);
+ std::swap(m_quotestrings, other.m_quotestrings);
+ }
+
+ bool operator==(const container_common_stream_state& other) const
+ {
+ return other.m_level == m_level &&
+ other.m_depth == m_depth &&
+ other.m_rows == m_rows &&
+ other.m_itemwidth == m_itemwidth &&
+ other.m_quotestrings == m_quotestrings;
+ }
+
    private:
         friend struct detail::increment_depth;
 
@@ -158,6 +176,28 @@
         void set_assoc_item_start(const str_typ& str) { at(m_assoc_item_start) = str; }
         void set_assoc_item_end(const str_typ& str) { at(m_assoc_item_end) = str; }
 
+ void swap(container_stream_state<Elem>& other)
+ {
+ assert(m_stream == other.m_stream);
+ m_separator.swap(other.m_separator);
+ m_start.swap(other.m_start);
+ m_end.swap(other.m_end);
+ m_assoc_item_separator.swap(other.m_assoc_item_separator);
+ m_assoc_item_start.swap(other.m_assoc_item_start);
+ m_assoc_item_end.swap(other.m_assoc_item_end);
+ }
+
+ bool operator==(const container_stream_state<Elem>& other) const
+ {
+ assert(m_stream == other.m_stream);
+ return m_separator == other.m_separator &&
+ m_start == other.m_start &&
+ m_end == other.m_end &&
+ m_assoc_item_separator == other.m_assoc_item_separator &&
+ m_assoc_item_start == other.m_assoc_item_start &&
+ m_assoc_item_end == other.m_assoc_item_end;
+ }
+
     private:
         container_common_stream_state* common()
         {

Added: sandbox/explore/boost/explore/state_saver.hpp
==============================================================================
--- (empty file)
+++ sandbox/explore/boost/explore/state_saver.hpp 2009-09-15 20:33:01 EDT (Tue, 15 Sep 2009)
@@ -0,0 +1,61 @@
+//
+// state_saver.hpp - save/restore container state with a scoped object
+//
+// Copyright (c) 2009 Jeff 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)
+//
+// state_saver works like the boost I/O boost stream state saver, but on container
+// state values.
+//
+
+#ifndef BOOST_EXPLORE_STATE_SAVER_H
+#define BOOST_EXPLORE_STATE_SAVER_H
+
+#include <boost/explore/container_stream_state.hpp>
+
+namespace boost
+{
+ namespace explore
+ {
+ template<typename Elem>
+ class state_saver
+ {
+ public:
+ explicit state_saver(std::ios_base& ostr)
+ : m_state(get_stream_state<container_stream_state<Elem> >(ostr)),
+ m_save_state(*m_state),
+ m_common_state(get_stream_state<container_common_stream_state>(ostr)),
+ m_save_common_state(*m_common_state), m_restored(false)
+ {
+ }
+
+ ~state_saver()
+ {
+ restore();
+ }
+
+ void restore()
+ {
+ if( !m_restored ) // don't double-swap
+ {
+ m_state->swap(m_save_state);
+ m_common_state->swap(m_save_common_state);
+ m_restored = true;
+ }
+ }
+
+ private:
+ container_stream_state<Elem>* m_state;
+ container_stream_state<Elem> m_save_state;
+
+ container_common_stream_state* m_common_state;
+ container_common_stream_state m_save_common_state;
+
+ bool m_restored;
+ };
+ }
+}
+
+#endif

Modified: sandbox/explore/libs/explore/test/Jamfile.v2
==============================================================================
--- sandbox/explore/libs/explore/test/Jamfile.v2 (original)
+++ sandbox/explore/libs/explore/test/Jamfile.v2 2009-09-15 20:33:01 EDT (Tue, 15 Sep 2009)
@@ -40,5 +40,6 @@
 
   [ run columnated.cpp ]
   [ run quote_strings.cpp ]
+ [ run state_saver.cpp ]
  ;
 }

Added: sandbox/explore/libs/explore/test/state_saver.cpp
==============================================================================
--- (empty file)
+++ sandbox/explore/libs/explore/test/state_saver.cpp 2009-09-15 20:33:01 EDT (Tue, 15 Sep 2009)
@@ -0,0 +1,43 @@
+// Boost.Explore library
+//
+// Copyright (C) 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)
+//
+
+#define BOOST_TEST_MODULE PrintLib
+#include <boost/test/unit_test.hpp>
+
+#include <boost/explore/vector.hpp>
+#include <boost/explore/state_saver.hpp>
+#include <iostream>
+
+#include "boost_explore_test_tools.hpp"
+
+BOOST_AUTO_TEST_CASE_TEMPLATE( save_state, C, test_types )
+{
+ typename test_traits<C>::stream_type str_out;
+
+ using namespace boost::explore;
+
+ std::vector<unsigned int> vui;
+ vui.push_back('A');
+ vui.push_back('B');
+
+ container_stream_state<C> state_before = *get_stream_state<container_stream_state<C> >(str_out);
+ container_common_stream_state common_state_before = *get_stream_state<container_common_stream_state>(str_out);
+
+ {
+ state_saver<C> ss(str_out);
+ str_out << item_width(10) << delimiters(str_to<C>("\\"), str_to<C>(";"), str_to<C>("/"));
+ str_out << vui;
+ BOOST_CHECK_EQUAL(output(str_out), "\\ 65; 66/");
+ }
+
+ str_out << vui;
+ BOOST_CHECK_EQUAL(output(str_out), "\\ 65; 66/[65, 66]");
+
+ BOOST_CHECK(state_before == *get_stream_state<container_stream_state<C> >(str_out));
+ BOOST_CHECK(common_state_before == *get_stream_state<container_common_stream_state>(str_out));
+}


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