Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r60455 - trunk/libs/spirit/example/scheme
From: joel_at_[hidden]
Date: 2010-03-10 22:01:36


Author: djowel
Date: 2010-03-10 22:01:35 EST (Wed, 10 Mar 2010)
New Revision: 60455
URL: http://svn.boost.org/trac/boost/changeset/60455

Log:
better simple_print
Text files modified:
   trunk/libs/spirit/example/scheme/sexpr_test.cpp | 12 ---------
   trunk/libs/spirit/example/scheme/simple_print.hpp | 45 +++++++++++++++++++--------------------
   trunk/libs/spirit/example/scheme/utree.hpp | 2 +
   trunk/libs/spirit/example/scheme/utree_test.cpp | 36 ++++++++++++++++----------------
   4 files changed, 43 insertions(+), 52 deletions(-)

Modified: trunk/libs/spirit/example/scheme/sexpr_test.cpp
==============================================================================
--- trunk/libs/spirit/example/scheme/sexpr_test.cpp (original)
+++ trunk/libs/spirit/example/scheme/sexpr_test.cpp 2010-03-10 22:01:35 EST (Wed, 10 Mar 2010)
@@ -9,16 +9,6 @@
 #include <iostream>
 #include <fstream>
 
-namespace scheme
-{
- inline std::ostream& operator<<(std::ostream& out, utree const& x)
- {
- using ::detail::println;
- println(x);
- return out;
- }
-}
-
 ///////////////////////////////////////////////////////////////////////////////
 // Main program
 ///////////////////////////////////////////////////////////////////////////////
@@ -79,7 +69,7 @@
     if (phrase_parse(first, last, p, ws, result))
     {
         std::cout << "success: ";
- println(result);
+ println(std::cout, result);
         std::cout << std::endl;
     }
     else

Modified: trunk/libs/spirit/example/scheme/simple_print.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/simple_print.hpp (original)
+++ trunk/libs/spirit/example/scheme/simple_print.hpp 2010-03-10 22:01:35 EST (Wed, 10 Mar 2010)
@@ -9,43 +9,46 @@
     ///////////////////////////////////////////////////////////////////////////
     // simple utree printing facility prints the utree in a single line
     ///////////////////////////////////////////////////////////////////////////
- void print(char ch);
- void print(scheme::utree const& val);
- void println(scheme::utree const& val);
+
+ std::ostream& print(std::ostream& out, scheme::utree const& val);
+ std::ostream& println(std::ostream& out, scheme::utree const& val);
 
     // simple_print visitor
     struct simple_print
     {
         typedef void result_type;
 
+ std::ostream& out;
+ simple_print(std::ostream& out) : out(out) {}
+
         void operator()(scheme::utree::nil) const
         {
- std::cout << "nil";
+ out << "nil";
         }
 
         template <typename T>
         void operator()(T val) const
         {
- std::cout << val;
+ out << val;
         }
 
         void operator()(bool b) const
         {
- std::cout << (b ? "true" : "false");
+ out << (b ? "true" : "false");
         }
 
         template <typename Range> // for lists
         void print_string_or_list(Range range, boost::mpl::false_) const
         {
             typedef typename Range::const_iterator iterator;
- print('(');
+ (*this)('(');
             for (iterator i = range.begin(); i != range.end(); ++i)
             {
                 if (i != range.begin())
- detail::print(' ');
- detail::print(*i);
+ (*this)(' ');
+ scheme::utree::visit(*i, *this);
             }
- detail::print(')');
+ (*this)(')');
         }
 
         template <typename Range> // for strings
@@ -55,13 +58,13 @@
             iterator i = range.begin();
             bool const is_symbol = *i == '\0'; // a 0 byte at the beginning signifies a symbol
             if (!is_symbol)
- detail::print('"');
+ (*this)('"');
             else
                 ++i;
             for (; i != range.end(); ++i)
- detail::print(*i);
+ (*this)(*i);
             if (!is_symbol)
- detail::print('"');
+ (*this)('"');
         }
 
         template <typename Iterator>
@@ -72,20 +75,16 @@
         }
     };
 
- inline void print(char ch)
- {
- std::cout << ch;
- }
-
- inline void print(scheme::utree const& val)
+ inline std::ostream& print(std::ostream& out, scheme::utree const& val)
     {
- scheme::utree::visit(val, simple_print());
+ scheme::utree::visit(val, simple_print(out));
+ return out;
     }
 
- inline void println(scheme::utree const& val)
+ inline std::ostream& println(std::ostream& out, scheme::utree const& val)
     {
- detail::print(val);
- std::cout << std::endl;
+ out << detail::print(out, val) << std::endl;
+ return out;
     }
 }
 

Modified: trunk/libs/spirit/example/scheme/utree.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/utree.hpp (original)
+++ trunk/libs/spirit/example/scheme/utree.hpp 2010-03-10 22:01:35 EST (Wed, 10 Mar 2010)
@@ -144,6 +144,7 @@
     // - a double
     // - a string (textual or binary)
     // - a (doubly linked) list of utree
+ // - a reference to a utree
     //
     // The utree has minimal memory footprint. The data structure size is
     // 16 bytes on a 32-bit platform. Being a container of itself, it can
@@ -173,6 +174,7 @@
         explicit utree(std::string const& str);
 
         utree(utree const& other);
+ utree(utree const& other);
         ~utree();
 
         utree& operator=(utree const& other);

Modified: trunk/libs/spirit/example/scheme/utree_test.cpp
==============================================================================
--- trunk/libs/spirit/example/scheme/utree_test.cpp (original)
+++ trunk/libs/spirit/example/scheme/utree_test.cpp 2010-03-10 22:01:35 EST (Wed, 10 Mar 2010)
@@ -16,33 +16,33 @@
 
     {
         utree val;
- println(val);
+ println(std::cout, val);
     }
 
     {
         utree val(true);
- println(val);
+ println(std::cout, val);
     }
 
     {
         utree val(123);
- println(val);
+ println(std::cout, val);
     }
 
     {
         utree val(123.456);
- println(val);
+ println(std::cout, val);
     }
 
     {
         utree val("Hello, World");
- println(val);
+ println(std::cout, val);
         utree val2;
         val2 = val;
- println(val2);
+ println(std::cout, val2);
         utree val3("Hello, World. Chuckie is back!!!");
         val = val3;
- println(val);
+ println(std::cout, val);
 
         utree val4("Apple");
         utree val5("Apple");
@@ -60,32 +60,32 @@
         val2.push_back(123.456);
         val2.push_back("Mah Doggie");
         val.push_back(val2);
- println(val);
- println(val.front());
+ println(std::cout, val);
+ println(std::cout, val.front());
 
         utree val3;
         val3.swap(val);
- println(val);
+ println(std::cout, val);
         val3.swap(val);
- println(val);
+ println(std::cout, val);
         val.push_back("Ba Ba Black Sheep");
- println(val);
+ println(std::cout, val);
         val.pop_front();
- println(val);
+ println(std::cout, val);
         utree::iterator i = val.begin();
         ++++i;
         val.insert(i, "Right in the middle");
         BOOST_ASSERT(val.size() == 4);
- println(val);
+ println(std::cout, val);
         val.pop_back();
- println(val);
+ println(std::cout, val);
         BOOST_ASSERT(val.size() == 3);
         val.erase(val.end());
- println(val);
+ println(std::cout, val);
         BOOST_ASSERT(val.size() == 2);
 
         val.insert(val.begin(), val2.begin(), val2.end());
- println(val);
+ println(std::cout, val);
     }
 
     {
@@ -95,7 +95,7 @@
         val.insert(val.end(), "Chuckie");
         val.insert(val.end(), "Poly");
         val.insert(val.end(), "Mochi");
- println(val);
+ println(std::cout, val);
     }
 
     {


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