Boost logo

Boost-Commit :

From: jefffaust_at_[hidden]
Date: 2007-06-04 00:32:09


Author: jefffaust
Date: 2007-06-04 00:32:08 EDT (Mon, 04 Jun 2007)
New Revision: 4433
URL: http://svn.boost.org/trac/boost/changeset/4433

Log:
Use explore::make_iterator_range instead of boost::make_iterator_range when printing.

Text files modified:
   sandbox/explore/boost/explore/explore.hpp | 502 ++++++++++++++++++++-------------------
   sandbox/explore/libs/explore/test/custom_format_simple.cpp | 4
   sandbox/explore/libs/explore/test/std_map.cpp | 6
   3 files changed, 257 insertions(+), 255 deletions(-)

Modified: sandbox/explore/boost/explore/explore.hpp
==============================================================================
--- sandbox/explore/boost/explore/explore.hpp (original)
+++ sandbox/explore/boost/explore/explore.hpp 2007-06-04 00:32:08 EDT (Mon, 04 Jun 2007)
@@ -33,48 +33,50 @@
 #include <boost/range/functions.hpp>
 #include <boost/range/iterator_range.hpp>
 
+#include "iterator_range.hpp"
+
 
 namespace explore {
- using namespace boost::mpl;
+ using namespace boost::mpl;
 
- namespace detail {
+ namespace detail {
 
- //
- // some boilerplate stuff for the is_streamable metafunction
- //
+ //
+ // some boilerplate stuff for the is_streamable metafunction
+ //
 
- struct AlmostAnything
- {
+ struct AlmostAnything
+ {
             template<typename T> AlmostAnything( T const &);
- };
+ };
 
- //
- // operator<< cannot have a variable argument list, so we add this
- // one to the overload set, which should have quite low priority.
- //
- char operator<<( std::ostream &, AlmostAnything const &);
+ //
+ // operator<< cannot have a variable argument list, so we add this
+ // one to the overload set, which should have quite low priority.
+ //
+ char operator<<( std::ostream &, AlmostAnything const &);
 
- template< typename T> T& make_t();
+ template< typename T> T& make_t();
 
- // TODO: cleanup, not using char return type.
- template< typename T>
- struct is_streamable : bool_< sizeof( std::cout << make_t<T>()) != sizeof( char)>
- {
- };
+ // TODO: cleanup, not using char return type.
+ template< typename T>
+ struct is_streamable : bool_< sizeof( std::cout << make_t<T>()) != sizeof( char)>
+ {
+ };
 
- };
+ };
 
 
- template< typename T>
- struct is_streamable : detail::is_streamable<T> {};
+ template< typename T>
+ struct is_streamable : detail::is_streamable<T> {};
 
 
- //
- // Generally, if we can stream it, we'll stream it. If not, we'll try
- // to treat it as a container of some kind.
- //
- template< typename T>
- struct print_as_container : not_< is_streamable< T> > {};
+ //
+ // Generally, if we can stream it, we'll stream it. If not, we'll try
+ // to treat it as a container of some kind.
+ //
+ template< typename T>
+ struct print_as_container : not_< is_streamable< T> > {};
 
 
     //
@@ -87,7 +89,7 @@
     //
     template< typename T, std::size_t size>
     struct print_as_container< T[size]> : true_ {};
-
+
     //
     // use our own implementation of streaming instead of the one
     // provided for iterator range.
@@ -112,18 +114,18 @@
     template< typename K, typename V, typename P, typename A>
     struct is_map< std::multimap< K, V, P, A> > : true_ {};
 
- //
- // A container policy determines whether an item of a certain type
- // gets broken up into items or if its going to be streamed directly
- //
- // by default, delegate to the 'global' metafunction,
- // which normally returns true iff a type cannot be streamed
- //
- struct default_container_policy
- {
- template< typename T>
- struct print_as_container : explore::print_as_container<T> {};
- };
+ //
+ // A container policy determines whether an item of a certain type
+ // gets broken up into items or if its going to be streamed directly
+ //
+ // by default, delegate to the 'global' metafunction,
+ // which normally returns true iff a type cannot be streamed
+ //
+ struct default_container_policy
+ {
+ template< typename T>
+ struct print_as_container : explore::print_as_container<T> {};
+ };
 
     //
     // Meta functions that are used by the default implementation of format
@@ -132,118 +134,118 @@
     // range when there are no other elements left.
     //
 
- //
- // metafunction that removes the first element of a sequence unless that would
- // make the sequence empty
- //
- template< typename sequence_type>
- struct pop_front_if :
- eval_if< bool_<size<sequence_type>::value == 1>,
- identity<sequence_type>,
- pop_front< sequence_type>
- >
- {};
-
- //
- // metafunction that replaces the leftmost element of a sequence with another type,
- // except if the sequence contained only 1 element, in which case the new head type is
- // pushed in front of the last one.
- //
- template< typename sequence_type, typename head_type>
- struct replace_head :
- push_front< BOOST_DEDUCED_TYPENAME pop_front_if< sequence_type>::type, head_type> {};
-
-
- //
- // A range format describes how the elements of a range are being
- // formatted.
- // This version supports only simple 'opener', 'delimiter', 'closer'
- // formats.
- struct basic_range_format
- {
- typedef char char_;
+ //
+ // metafunction that removes the first element of a sequence unless that would
+ // make the sequence empty
+ //
+ template< typename sequence_type>
+ struct pop_front_if :
+ eval_if< bool_<size<sequence_type>::value == 1>,
+ identity<sequence_type>,
+ pop_front< sequence_type>
+ >
+ {};
+
+ //
+ // metafunction that replaces the leftmost element of a sequence with another type,
+ // except if the sequence contained only 1 element, in which case the new head type is
+ // pushed in front of the last one.
+ //
+ template< typename sequence_type, typename head_type>
+ struct replace_head :
+ push_front< BOOST_DEDUCED_TYPENAME pop_front_if< sequence_type>::type, head_type> {};
+
+
+ //
+ // A range format describes how the elements of a range are being
+ // formatted.
+ // This version supports only simple 'opener', 'delimiter', 'closer'
+ // formats.
+ struct basic_range_format
+ {
+ typedef char char_;
 
         //
         // not used currently.
- static char_ *header() { return "";}
- static char_ *footer() { return "";}
+ static char_ *header() { return "";}
+ static char_ *footer() { return "";}
 
         //
         // default implementation of the next_format
         // meta function.
         // This defines how the format for the next recursion level is
         // chosen.
- template< typename sequence_type>
- struct next_format : pop_front_if< sequence_type> {};
- };
-
- // TODO: support other char types by making this a template
- struct default_range_format : basic_range_format
- {
- // only for sequences
- static char_ opening() { return '[';}
- static char_ closing() { return ']';}
- static char_ *delimiter() { return ", ";}
-
- // string_ escape( const string_ &);
- };
-
- struct map_pair_format : basic_range_format
- {
- static char_ *opening() { return "";}
- static char_ *closing() { return "";}
- static char_ delimiter() { return ':';}
- // string_ escape( const string_ &);
- };
-
- struct map_pair_format_selector
- {
- template< typename T>
- struct range_format: identity< map_pair_format> {};
- };
-
- struct map_range_format : default_range_format
- {
- template< typename sequence_type>
- struct next_format : replace_head< sequence_type, map_pair_format_selector> {};
- };
-
- // select a range format based on the type of the container
- struct default_range_format_selector
- {
+ template< typename sequence_type>
+ struct next_format : pop_front_if< sequence_type> {};
+ };
+
+ // TODO: support other char types by making this a template
+ struct default_range_format : basic_range_format
+ {
+ // only for sequences
+ static char_ opening() { return '[';}
+ static char_ closing() { return ']';}
+ static char_ *delimiter() { return ", ";}
+
+ // string_ escape( const string_ &);
+ };
+
+ struct map_pair_format : basic_range_format
+ {
+ static char_ *opening() { return "";}
+ static char_ *closing() { return "";}
+ static char_ delimiter() { return ':';}
+ // string_ escape( const string_ &);
+ };
+
+ struct map_pair_format_selector
+ {
+ template< typename T>
+ struct range_format: identity< map_pair_format> {};
+ };
+
+ struct map_range_format : default_range_format
+ {
+ template< typename sequence_type>
+ struct next_format : replace_head< sequence_type, map_pair_format_selector> {};
+ };
+
+ // select a range format based on the type of the container
+ struct default_range_format_selector
+ {
 
         template< typename T>
         struct range_format : if_<
- is_map< T>,
- map_range_format,
- default_range_format>{};
+ is_map< T>,
+ map_range_format,
+ default_range_format>{};
 
- };
+ };
 
 
- struct default_format : boost::mpl::list< default_range_format_selector> {};
+ struct default_format : boost::mpl::list< default_range_format_selector> {};
 
     //
     // Several container printer overloads.
     //
     template< typename format_type,
- typename container_policy_type,
- typename item_type
+ typename container_policy_type,
+ typename item_type
>
     std::ostream &print_container( const item_type &item, std::ostream &stream )
     {
- // the first element of the format sequence is our current format-selector
- typedef BOOST_DEDUCED_TYPENAME front<format_type>::type format_selector;
+ // the first element of the format sequence is our current format-selector
+ typedef BOOST_DEDUCED_TYPENAME front<format_type>::type format_selector;
+
+ // the format-selector gives us a formatter, based on the type of the thing we need to format
+ typedef BOOST_DEDUCED_TYPENAME format_selector::template range_format<item_type>::type formatter;
 
- // the format-selector gives us a formatter, based on the type of the thing we need to format
- typedef BOOST_DEDUCED_TYPENAME format_selector::template range_format<item_type>::type formatter;
+ // the formatter in it's turn tells us what the format for the next depth in the tree will be
+ typedef BOOST_DEDUCED_TYPENAME formatter::template next_format< format_type>::type next_format_type;
 
- // the formatter in it's turn tells us what the format for the next depth in the tree will be
- typedef BOOST_DEDUCED_TYPENAME formatter::template next_format< format_type>::type next_format_type;
 
-
- stream << formatter::opening();
- bool printing_first_item = true;
+ stream << formatter::opening();
+ bool printing_first_item = true;
 
         typedef BOOST_DEDUCED_TYPENAME boost::range_const_iterator< const item_type>::type iterator_type;
         typedef BOOST_DEDUCED_TYPENAME boost::iterator_value< iterator_type>::type value_type;
@@ -251,73 +253,73 @@
         for (
             iterator_type i = boost::const_begin( item);
             i != boost::const_end( item);
- ++i)
- {
- if (!printing_first_item)
- {
- stream << formatter::delimiter();
- }
-
- print( *i, stream, next_format_type(), container_policy_type());
- printing_first_item = false;
- }
- stream << formatter::closing();
+ ++i)
+ {
+ if (!printing_first_item)
+ {
+ stream << formatter::delimiter();
+ }
+
+ print( *i, stream, next_format_type(), container_policy_type());
+ printing_first_item = false;
+ }
+ stream << formatter::closing();
 
- return stream;
+ return stream;
     }
 
     // TODO: generalize for tuples
     template< typename format_type,
- typename container_policy_type,
- typename F,
- typename S
+ typename container_policy_type,
+ typename F,
+ typename S
>
     std::ostream &print_container( const std::pair<F,S> &item, std::ostream &stream )
     {
- typedef std::pair<F,S> item_type;
+ typedef std::pair<F,S> item_type;
 
- // the first element of the format sequence is our current format-selector
- typedef BOOST_DEDUCED_TYPENAME front<format_type>::type format_selector;
+ // the first element of the format sequence is our current format-selector
+ typedef BOOST_DEDUCED_TYPENAME front<format_type>::type format_selector;
 
- // the format-selector gives us a formatter, based on the type of the thing we need to format
- typedef BOOST_DEDUCED_TYPENAME format_selector:: template range_format<item_type>::type formatter;
+ // the format-selector gives us a formatter, based on the type of the thing we need to format
+ typedef BOOST_DEDUCED_TYPENAME format_selector:: template range_format<item_type>::type formatter;
 
- // the formatter in it's turn tells us what the format for the next depth in the tree will be
- typedef BOOST_DEDUCED_TYPENAME formatter:: template next_format< format_type>::type next_format_type;
-
- stream << formatter::opening();
- print( item.first, stream, next_format_type(), container_policy_type());
- stream << formatter::delimiter();
- print( item.second, stream, next_format_type(), container_policy_type());
- return stream << formatter::closing();
+ // the formatter in it's turn tells us what the format for the next depth in the tree will be
+ typedef BOOST_DEDUCED_TYPENAME formatter:: template next_format< format_type>::type next_format_type;
+
+ stream << formatter::opening();
+ print( item.first, stream, next_format_type(), container_policy_type());
+ stream << formatter::delimiter();
+ print( item.second, stream, next_format_type(), container_policy_type());
+ return stream << formatter::closing();
     }
 
- //
- // the print_item methods of this class are called whenever print has deduced that
- // the item to print is some container-type
- //
- struct container_printer
- {
+ //
+ // the print_item methods of this class are called whenever print has deduced that
+ // the item to print is some container-type
+ //
+ struct container_printer
+ {
         template<
             typename format_type,
- typename container_policy_type,
- typename item_type
+ typename container_policy_type,
+ typename item_type
>
         static std::ostream &print_item( const item_type &item, std::ostream &stream)
         {
             return print_container<format_type, container_policy_type>( item, stream);
         }
 
-
- };
-
+
+ };
+
     struct pointer_printer
- {
- template< typename format_type,
- typename container_policy_type,
- typename item_type
- >
- static std::ostream &print_item( const item_type *item, std::ostream &stream )
+ {
+ template< typename format_type,
+ typename container_policy_type,
+ typename item_type
+ >
+ static std::ostream &print_item( const item_type *item, std::ostream &stream )
         {
             if( NULL == item )
             {
@@ -325,87 +327,87 @@
             }
             //
             // DH - changed until we figure out what special things to do with pointer types
- // return print(*item, stream, format_type(), container_policy_type());
+ // return print(*item, stream, format_type(), container_policy_type());
+ return stream << item;
+ }
+ };
+
+ struct item_printer
+ {
+ template< typename format_type,
+ typename container_policy_type,
+ typename item_type
+ >
+ static std::ostream &print_item( const item_type &item, std::ostream &stream )
+ {
             return stream << item;
         }
- };
+ };
+
+ /**
+ * This is the main entrance to print.
+ * This function tries to stream it's first argument (the item) to the stream given as a second argument.
+ * If the item has no stream operator, print will try to treat it as some form of container and call itself recursively
+ * on every element of the container.
+ *
+ * Print ask the container policy whether to treat the item as a container. If directed so, it will delegate to the
+ * print_container free function in namespace scope.
+ * Otherwise, it will try to stream the item using the expression "stream << item".
+ *
+ * \param &item the item to stream
+ * \param &stream the stream that receives the item or its elements
+ * \param &format the format to use (string escaping, delimiters etc.)
+ * \param &policy a type that decides when a type needs to be treated as a container.
+ * \return the original stream (second argument)
+ */
+ template< typename format_type,
+ typename container_policy_type ,
+ typename item_type
+ >
+ std::ostream &print( const item_type &item,
+ std::ostream &stream,
+ const format_type &format,
+ const container_policy_type &policy)
+ {
+ typedef typename eval_if<
+ typename container_policy_type:: template print_as_container< item_type>::type,
+ identity<container_printer>,
+ eval_if<
+ typename boost::is_pointer<item_type>::type,
+ identity<pointer_printer>,
+ identity<item_printer>
+ >
+ >::type printer_type;
 
- struct item_printer
- {
- template< typename format_type,
- typename container_policy_type,
- typename item_type
- >
- static std::ostream &print_item( const item_type &item, std::ostream &stream )
- {
- return stream << item;
- }
- };
-
- /**
- * This is the main entrance to print.
- * This function tries to stream it's first argument (the item) to the stream given as a second argument.
- * If the item has no stream operator, print will try to treat it as some form of container and call itself recursively
- * on every element of the container.
- *
- * Print ask the container policy whether to treat the item as a container. If directed so, it will delegate to the
- * print_container free function in namespace scope.
- * Otherwise, it will try to stream the item using the expression "stream << item".
- *
- * \param &item the item to stream
- * \param &stream the stream that receives the item or its elements
- * \param &format the format to use (string escaping, delimiters etc.)
- * \param &policy a type that decides when a type needs to be treated as a container.
- * \return the original stream (second argument)
- */
- template< typename format_type,
- typename container_policy_type ,
- typename item_type
- >
- std::ostream &print( const item_type &item,
- std::ostream &stream,
- const format_type &format,
- const container_policy_type &policy)
- {
- typedef typename eval_if<
- typename container_policy_type:: template print_as_container< item_type>::type,
- identity<container_printer>,
- eval_if<
- typename boost::is_pointer<item_type>::type,
- identity<pointer_printer>,
- identity<item_printer>
- >
- >::type printer_type;
 
-
         return printer_type::template print_item< format_type, container_policy_type>( item, stream);
- }
+ }
+
+ template < typename item_type>
+ std::ostream &print( const item_type &item, std::ostream &stream = std::cout)
+ {
+ return print( item, stream, default_format(), default_container_policy());
+ }
+
+ template< typename format_type,
+ typename container_policy_type ,
+ class InputIterator
+ >
+ std::ostream &print( InputIterator first,
+ InputIterator last,
+ std::ostream &stream,
+ const format_type &format,
+ const container_policy_type &policy)
+ {
+ return print( explore::make_iterator_range(first, last), stream, format, policy );
+ }
+
+ template<class InputIterator>
+ std::ostream &print(InputIterator first, InputIterator last, std::ostream &stream = std::cout)
+ {
+ return print( explore::make_iterator_range(first, last), stream );
+ }
 
- template < typename item_type>
- std::ostream &print( const item_type &item, std::ostream &stream = std::cout)
- {
- return print( item, stream, default_format(), default_container_policy());
- }
-
- template< typename format_type,
- typename container_policy_type ,
- class InputIterator
- >
- std::ostream &print( InputIterator first,
- InputIterator last,
- std::ostream &stream,
- const format_type &format,
- const container_policy_type &policy)
- {
- return print( boost::make_iterator_range(first, last), stream, format, policy );
- }
-
- template<class InputIterator>
- std::ostream &print(InputIterator first, InputIterator last, std::ostream &stream = std::cout)
- {
- return print( boost::make_iterator_range(first, last), stream );
- }
-
 
     //
     // The container_formatter type to allow "inline" streaming like the following
@@ -437,7 +439,7 @@
     };
 
     template< typename formatter>
- unbound_container_format_class< formatter> container_format( const formatter &)
+ unbound_container_format_class< formatter> container_format( const formatter &)
     {
         return unbound_container_format_class<formatter>();
     }
@@ -448,13 +450,13 @@
     }
 
     template< typename container, typename formatter>
- container_format_class< container, formatter> operator%( unbound_container_format_class<formatter> &u, const container &c)
+ container_format_class< container, formatter> operator%( unbound_container_format_class<formatter> &u, const container &c)
     {
         return container_format_class< container, formatter>( c);
     }
 
     template< typename container, typename formatter>
- std::ostream &operator<<( std::ostream &strm, const container_format_class< container, formatter> &cf)
+ std::ostream &operator<<( std::ostream &strm, const container_format_class< container, formatter> &cf)
     {
         return print( cf.get_container(), strm);
     }

Modified: sandbox/explore/libs/explore/test/custom_format_simple.cpp
==============================================================================
--- sandbox/explore/libs/explore/test/custom_format_simple.cpp (original)
+++ sandbox/explore/libs/explore/test/custom_format_simple.cpp 2007-06-04 00:32:08 EDT (Mon, 04 Jun 2007)
@@ -46,7 +46,7 @@
 
     str_out.str("");
 
- explore::print(boost::make_iterator_range(vi.begin(), ++(++vi.begin())), str_out, basic_lg_format(), explore::default_container_policy());
+ explore::print(explore::make_iterator_range(vi.begin(), ++(++vi.begin())), str_out, basic_lg_format(), explore::default_container_policy());
     BOOST_CHECK_EQUAL(str_out.str(), "<=1#2=>");
 
     str_out.str("");
@@ -116,7 +116,7 @@
 
     str_out.str("");
 
- explore::print(boost::make_iterator_range(mis.begin(), ++(++mis.begin())), str_out, basic_lg_format(), explore::default_container_policy());
+ explore::print(explore::make_iterator_range(mis.begin(), ++(++mis.begin())), str_out, basic_lg_format(), explore::default_container_policy());
     BOOST_CHECK_EQUAL(str_out.str(), "<=<=1#first=>#<=2#second=>=>");
 }
 

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 2007-06-04 00:32:08 EDT (Mon, 04 Jun 2007)
@@ -45,7 +45,7 @@
 
     str_out.str("");
 
- explore::print(boost::make_iterator_range(mis.begin(), ++(++mis.begin())), str_out);
+ explore::print(explore::make_iterator_range(mis.begin(), ++(++mis.begin())), str_out);
     BOOST_CHECK_EQUAL(str_out.str(), "[1:first, 2:second]");
 }
 
@@ -111,7 +111,7 @@
 
     str_out.str("");
 
- explore::print(boost::make_iterator_range(mmis.begin(), ++(++mmis.begin())), str_out);
+ explore::print(explore::make_iterator_range(mmis.begin(), ++(++mmis.begin())), str_out);
     BOOST_CHECK_EQUAL(str_out.str(), "[1:first, 1:single]");
 }
 
@@ -182,7 +182,7 @@
 
     str_out.str("");
 
- explore::print(boost::make_iterator_range(mivi.begin(), ++(++mivi.begin())), str_out);
+ explore::print(explore::make_iterator_range(mivi.begin(), ++(++mivi.begin())), str_out);
     BOOST_CHECK_EQUAL(str_out.str(), "[1:[1, 2, 3], 2:[1, 2, 3]]");
 }
 


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