Boost logo

Boost-Commit :

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


Author: jefffaust
Date: 2009-09-09 00:12:42 EDT (Wed, 09 Sep 2009)
New Revision: 56118
URL: http://svn.boost.org/trac/boost/changeset/56118

Log:
Allow strings to be quoted.
Added:
   sandbox/explore/libs/explore/test/quote_strings.cpp (contents, props changed)
Text files modified:
   sandbox/explore/boost/explore/container_stream_state.hpp | 31 +++++++++--------
   sandbox/explore/boost/explore/manipulators.hpp | 50 +++++++++++++++++++---------
   sandbox/explore/boost/explore/stream_value.hpp | 69 ++++++++++++++++++++++++++++++++++++++-
   sandbox/explore/libs/explore/test/Jamfile.v2 | 1
   4 files changed, 118 insertions(+), 33 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-09 00:12:42 EDT (Wed, 09 Sep 2009)
@@ -59,23 +59,26 @@
     struct container_common_stream_state
     {
         container_common_stream_state(const std::ios_base* stream)
- : m_level(0), m_depth(0), m_rows(1), m_itemwidth(1)
+ : m_level(0), m_depth(0), m_rows(1), m_itemwidth(1), m_quotestrings(false)
         {
         }
 
         void set_level(size_t l) { m_level = l; }
         void level_up() { ++m_level; }
         void level_down() { --m_level; }
+ size_t level() const { return m_level; }
 
- size_t get_level() const { return m_level; }
         std::size_t depth() const { return m_depth; }
 
         std::size_t rows() const { return at(m_rows); }
- std::size_t itemwidth() const { return at(m_itemwidth); }
-
         void set_rows(std::size_t rows) { at(m_rows) = rows; }
+
+ std::size_t itemwidth() const { return at(m_itemwidth); }
         void set_itemwidth(std::size_t iw) { at(m_itemwidth) = iw; }
 
+ bool quote_strings() const { return m_quotestrings; }
+ void set_quote_strings(bool qs) { m_quotestrings = qs; }
+
    private:
         friend struct detail::depth_guard;
 
@@ -86,16 +89,15 @@
         const T& at(const std::vector<T>& c) const
         {
             // return the highest item if it does not exist at the given index
- return detail::value_at(c, get_level());
+ return detail::value_at(c, level());
         }
 
         // write
         template<typename T>
         T& at(std::vector<T>& c)
         {
- size_t level = get_level();
- assert(depth() <= level);
- return detail::value_at(c, get_level());
+ assert(depth() <= level());
+ return detail::value_at(c, level());
         }
 
         std::size_t m_level;
@@ -103,6 +105,8 @@
 
         size_cont_typ m_rows;
         size_cont_typ m_itemwidth;
+
+ bool m_quotestrings;
     };
 
     // A simple collection of additional stream state
@@ -159,9 +163,9 @@
             return get_stream_state<container_common_stream_state>(*m_stream);
         }
 
- size_t get_level() const
+ size_t level() const
         {
- return common()->get_level();
+ return common()->level();
         }
 
         str_cont_typ m_separator;
@@ -185,16 +189,15 @@
         const T& at(const std::vector<T>& c) const
         {
             // return the highest item if it does not exist at the given index
- return detail::value_at(c, get_level());
+ return detail::value_at(c, level());
         }
 
         // write
         template<typename T>
         T& at(std::vector<T>& c)
         {
- size_t level = get_level();
- assert(common()->depth() <= level);
- return detail::value_at(c, get_level());
+ assert(common()->depth() <= level());
+ return detail::value_at(c, level());
         }
     };
 }}

Modified: sandbox/explore/boost/explore/manipulators.hpp
==============================================================================
--- sandbox/explore/boost/explore/manipulators.hpp (original)
+++ sandbox/explore/boost/explore/manipulators.hpp 2009-09-09 00:12:42 EDT (Wed, 09 Sep 2009)
@@ -28,7 +28,7 @@
         struct depth_guard
         {
             depth_guard(container_common_stream_state* state)
- : m_state(state), m_prev_level(state->get_level())
+ : m_state(state), m_prev_level(state->level())
             {
                 ++m_state->m_depth;
             }
@@ -80,6 +80,13 @@
             (*manip.pfun)(ostr, manip.arg);
             return ostr;
         }
+
+ template<typename Elem, typename Tr>
+ std::basic_ostream<Elem, Tr>& operator<<(std::basic_ostream<Elem, Tr>& ostr, const manipfunc<bool>& manip)
+ {
+ (*manip.pfun)(ostr, manip.arg);
+ return ostr;
+ }
 
         struct handle_custom_stream
         {
@@ -170,9 +177,13 @@
         {
             explore::get_stream_state<container_common_stream_state>(ostr)->set_itemwidth(sz);
         }
+
+ void quotestringsFn(std::ios_base& ostr, bool qs)
+ {
+ explore::get_stream_state<container_common_stream_state>(ostr)->set_quote_strings(qs);
+ }
     }
     
- // manipulator
     template<typename Elem>
     detail::manipfunc<const Elem*> separator(const Elem* sep)
     {
@@ -185,7 +196,6 @@
         return explore::get_stream_state<container_stream_state<Elem> >(ostr)->separator();
     }
     
- // manipulator
     template<typename Elem>
     detail::manipfunc<const Elem*> start(const Elem* s)
     {
@@ -198,7 +208,6 @@
         return explore::get_stream_state<container_stream_state<Elem> >(ostr)->start();
     }
     
- // manipulator
     template<typename Elem>
     detail::manipfunc<const Elem*> end(const Elem* e)
     {
@@ -211,7 +220,6 @@
         return explore::get_stream_state<container_stream_state<Elem> >(ostr)->end();
     }
     
- // manipulator
     template<typename Elem>
     detail::manipfunc<const Elem*> assoc_item_separator(const Elem* sep)
     {
@@ -224,7 +232,6 @@
         return explore::get_stream_state<container_stream_state<Elem> >(ostr)->assoc_item_separator();
     }
     
- // manipulator
     template<typename Elem>
     detail::manipfunc<const Elem*> assoc_item_start(const Elem* start)
     {
@@ -237,7 +244,6 @@
         return explore::get_stream_state<container_stream_state<Elem> >(ostr)->assoc_item_start();
     }
     
- // manipulator
     template<typename Elem>
     detail::manipfunc<const Elem*> assoc_item_end(const Elem* end)
     {
@@ -250,7 +256,6 @@
         return explore::get_stream_state<container_stream_state<Elem> >(ostr)->assoc_item_end();
     }
     
- // manipulator
     detail::manipfunc<std::size_t> level(std::size_t l)
     {
         return detail::manipfunc<std::size_t>(&detail::levelFn, l);
@@ -259,10 +264,9 @@
     template<typename Elem, typename Tr>
     std::size_t get_level(std::basic_ostream<Elem, Tr>& ostr)
     {
- return explore::get_stream_state<container_common_stream_state>(ostr)->get_level();
+ return explore::get_stream_state<container_common_stream_state>(ostr)->level();
     }
 
- // manipulator
     detail::manipfunc<std::size_t> rows(std::size_t sz)
     {
         return detail::manipfunc<std::size_t>(detail::setrowsFn, sz);
@@ -271,10 +275,9 @@
     template<typename Elem, typename Tr>
     std::size_t get_rows(std::basic_ostream<Elem, Tr>& ostr)
     {
- return explore::get_stream_state<container_common_stream_state>(ostr)->get_level();
+ return explore::get_stream_state<container_common_stream_state>(ostr)->get_rows();
     }
     
- // manipulator
     detail::manipfunc<std::size_t> item_width(std::size_t sz)
     {
         return detail::manipfunc<std::size_t>(detail::setitemwidthFn, sz);
@@ -283,10 +286,25 @@
     template<typename Elem, typename Tr>
     std::size_t get_item_width(std::basic_ostream<Elem, Tr>& ostr)
     {
- return explore::get_stream_state<container_common_stream_state>(ostr)->get_level();
- }
+ return explore::get_stream_state<container_common_stream_state>(ostr)->itemwidth();
+ }
+
+ detail::manipfunc<bool> quote_strings()
+ {
+ return detail::manipfunc<bool>(detail::quotestringsFn, true);
+ }
+
+ detail::manipfunc<bool> no_quote_strings()
+ {
+ return detail::manipfunc<bool>(detail::quotestringsFn, false);
+ }
+
+ template<typename Elem, typename Tr>
+ bool get_quote_strings(std::basic_ostream<Elem, Tr>& ostr)
+ {
+ return explore::get_stream_state<container_common_stream_state>(ostr)->quote_strings();
+ }
      
- // manipulator
     template<typename Elem, typename Tr>
     std::basic_ostream<Elem, Tr>& format_normal(std::basic_ostream<Elem, Tr>& ostr)
     {
@@ -349,8 +367,6 @@
         return detail::begin_end_manipulator<detail::assoc_tag,const Elem*>(start, end);
     }
     
-
- // manipulator
     namespace detail
     {
         template<typename Tag, typename T>

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-09 00:12:42 EDT (Wed, 09 Sep 2009)
@@ -14,15 +14,77 @@
 
 #include <ostream>
 
+// should be the last #include
+#include <boost/type_traits/detail/bool_trait_def.hpp>
+
 namespace boost { namespace explore
 {
+ namespace detail
+ {
+ BOOST_TT_AUX_BOOL_TRAIT_DEF1(is_string, T, false)
+
+ BOOST_TT_AUX_BOOL_TRAIT_CV_SPEC1(is_string, char*, true)
+ BOOST_TT_AUX_BOOL_TRAIT_CV_SPEC1(is_string, const char*, true)
+ BOOST_TT_AUX_BOOL_TRAIT_CV_SPEC1(is_string, wchar_t*, true)
+ BOOST_TT_AUX_BOOL_TRAIT_CV_SPEC1(is_string, const wchar_t*, true)
+ BOOST_TT_AUX_BOOL_TRAIT_CV_SPEC1(is_string, std::string, true)
+ BOOST_TT_AUX_BOOL_TRAIT_CV_SPEC1(is_string, std::wstring, true)
+
+ template<typename T>
+ struct value_holder_base
+ {
+ explicit value_holder_base(const T& t, bool quotestrings) : m_value(t), m_quotestrings(quotestrings) {}
+ const T& m_value;
+ bool m_quotestrings;
+ };
+
+ template<typename T, int S>
+ struct value_holder;
+
+ template<typename T>
+ struct value_holder<T, boost::false_type::value> : public value_holder_base<T>
+ {
+ explicit value_holder(const T& value, bool quotestrings) : value_holder_base<T>(value, quotestrings) {}
+
+ template<typename Elem, typename Tr>
+ friend std::basic_ostream<Elem, Tr>& operator<<(std::basic_ostream<Elem, Tr>& ostr, const value_holder_base<T>& v)
+ {
+ return ostr << v.m_value;
+ }
+ };
+
+ template<typename T>
+ struct value_holder<T, boost::true_type::value> : public value_holder_base<T>
+ {
+ explicit value_holder(const T& value, bool quotestrings) : value_holder_base<T>(value, quotestrings) {}
+
+ template<typename Elem, typename Tr>
+ friend std::basic_ostream<Elem, Tr>& operator<<(std::basic_ostream<Elem, Tr>& ostr, const value_holder_base<T>& v)
+ {
+ if( v.m_quotestrings )
+ {
+ return ostr << "\"" << v.m_value << "\"";
+ }
+
+ return ostr << v.m_value;
+ }
+ };
+
+ template<typename T>
+ struct value : public value_holder<T, is_string<T>::value>
+ {
+ explicit value(const T& value, bool quotestrings) : value_holder<T, is_string<T>::value>(value, quotestrings) {}
+ };
+ }
+
     struct stream_normal_value
     {
         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.width(common_state->itemwidth());
- ostr << val;
+ ostr << detail::value<T>(val, qs);
         }
     };
 
@@ -32,7 +94,10 @@
         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)
         {
- ostr << state->assoc_item_start() << val.first << state->assoc_item_separator() << val.second << state->assoc_item_end();
+ 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();
         }
     };
 }}

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-09 00:12:42 EDT (Wed, 09 Sep 2009)
@@ -39,5 +39,6 @@
   [ run multi_dim_test.cpp ]
 
   [ run columnated.cpp ]
+ [ run quote_strings.cpp ]
  ;
 }

Added: sandbox/explore/libs/explore/test/quote_strings.cpp
==============================================================================
--- (empty file)
+++ sandbox/explore/libs/explore/test/quote_strings.cpp 2009-09-09 00:12:42 EDT (Wed, 09 Sep 2009)
@@ -0,0 +1,36 @@
+// Boost.Explore library
+
+// Copyright Jeffrey Faust 2009. 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
+#include <boost/test/unit_test.hpp>
+#include <boost/explore/map.hpp>
+#include "boost_explore_test_tools.hpp"
+
+BOOST_AUTO_TEST_CASE_TEMPLATE( strings_in_map, C, test_types )
+{
+ using namespace boost::explore;
+ typename test_traits<C>::stream_type str_out;
+
+ std::map<int, typename test_traits<C>::string_type> mis;
+ str_out << mis;
+ BOOST_CHECK_EQUAL(output(str_out), "[]");
+
+ reset(str_out);
+
+ mis.insert(std::make_pair(1, str_to<C>("first")));
+ str_out << mis;
+ BOOST_CHECK_EQUAL(output(str_out), "[1:first]");
+
+ reset(str_out);
+
+ str_out << quote_strings() << mis;
+ BOOST_CHECK_EQUAL(output(str_out), "[1:\"first\"]");
+ str_out << no_quote_strings() << mis;
+ BOOST_CHECK_EQUAL(output(str_out), "[1:\"first\"][1:first]");
+}


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