Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2003-04-28 10:42:40


Terje Slettebo wrote:

>In the following, I compare the approaches a little (between my library and
>John's; I'll let Reece speak for his. :) ).

Thanks. :)

My library is designed to be as flexible as possible while also being
unobtrusive. I have not gone for overloading operator<< like is found in
Terje's library, because I saw no easy way to do this; so I chose the
manipulator/generator approach.

The library initially supported basic list outputting, but it now supports
any object (as long as they have an outputter associated with them),
including nested
constructs. These are supported through heavy use of templates.
boost::io::basic_output is essentially the same as what is contained in
John's library.

assuming:

std::list< std::string > names;
names.push_back( "John" );
names.push_back( "James" );
names.push_back( "Corina" );

using namespace boost::io;

template< class FormatType >
class position_output
{
   public:
      // needed for deduction of the nested format type
      typedef FormatType listfmt_type;
   private:
      long pos;
   public:
      template< typename T, class OutputStream >
      OutputStream & operator()( OutputStream & os, const T & value ) const
      {
         return( os << '[' << pos << "] " << value );
      }
   public:
      position_output(): pos( 0 ){}
};

1: John, James, Corina

std::cout << formatlist( names )
             .format( '\0', '\0' ).space( false );

2: {John}, {James}, {Corina}

std::cout << formatlistex< char * >( names )
             .format( "{", "}", "}, {" )
             .space( false, false, false );

3: {[0] John}, {[1] James}, {[2] Corina}

std::cout << formatlistfn( names, position_output< char * >())
             .format( "{", "}", "}, {" )
             .space( false, false, false );

4: [0] John, [1] James, [2] Corina

std::cout << formatlistfn( names, position_output< char >())
             .format( '\0', '\0' ).space( false );

5:
[John]
[James]
[Corina]

std::cout << formatlistex< char * >( names )
             .format( "[", "]", "]\n[" ).space( false );

6:
[0] John
[1] James
[2] Corina

std::cout << formatlistfn( names, position_output< char >())
             .format( '\n' ).space( false, false, false );

7:
'[0] John'
'[1] James'
'[2] Corina'

std::cout << formatlistfn( names, position_output< char * >())
             .format( "'", "'", "'\n'" )
             .space( false, false, false );

NOTE: I am looking at supporting a pre/post outputter to simplify example 2,
3 and 7. This will also simplify things like producing XML tags.

-rhd-
mailto:msclrhd_at_[hidden]

_________________________________________________________________
Express yourself with cool emoticons http://www.msn.co.uk/messenger


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk