I have an output stream manipulator that allows the user of it to print out a list in a given range (first, last) or by a container c (c.begin(), c.end()).

I am looking at adding this into the boost library. The source code for it is in the attachment (printlist.hpp).

An example of its use is:

std::vector< int >               v( 10 );
std::generate( v.begin(), v.end(), std::rand );

// default list: "[ i1, i2, ..., in ]"
std::cout << "list = " << stl::formatlist( v ) << '\n';

// parenthesis: "( i1, i2, ..., in )"
std::cout << "list = " << stl::formatlist( v ).format( '(', ')' ) << '\n';

// special: "[ i1 | i2 | ... | in ]"
std::cout << "list = " << stl::formatlist( v ).format( '|' ).space( true, true ) << '\n';


At the moment I have only tested it on MS VC++.NET 7.0, but have only used the C++ standard language and the iostream header, so it should work on compliant compilers (with non-advanced template support).

Is there any interest for this? Does anyone have comments, suggestions or questions? How do I go about adding this into boost and making it boost-compliant?

Thanks for your assistance,

-rhd-
mailto:[EMAIL PROTECTED]


_________________________________________________________________


#ifndef __STL__PRINTLIST // STL Extension
#define __STL__PRINTLIST // helper routines to output a list in the range [first, last)
# include <iostream>
/* example usage:


     std::vector< int >               v( 10 );
     std::generate( v.begin(), v.end(), std::rand );

std::cout << "list = " << stl::formatlist( v ) << '\n';
std::cout << "list = " << stl::formatlist( v ).format( '(', ')' ) << '\n';
std::cout << "list = " << stl::formatlist( v ).format( '|' ).space( true, true ) << '\n';
*/
namespace stl
{
// formatlist_t: helper class that does the main work for list formatting


template< typename ForwardIterator >
class formatlist_t
{
private: // list format control
char __openlist;
char __closelist;
char __seperator;
public:
inline char openlist() const throw();
inline char closelist() const throw();
inline char seperator() const throw();
public:
inline formatlist_t & format( char, char, char ) throw(); // open, close, seperator
inline formatlist_t & format( char, char ) throw(); // open, close
inline formatlist_t & format( char ) throw(); // seperator
private: // spacing control
bool __spaceBeforeSep;
bool __spaceAfterSep;
bool __spaceAroundList;
public:
inline bool spaceBeforeSep() const throw();
inline bool spaceAfterSep() const throw();
inline bool spaceAroundList() const throw();
public:
inline formatlist_t & space( bool, bool, bool ) throw(); // before, after, around
inline formatlist_t & space( bool, bool ) throw(); // before, after
inline formatlist_t & space( bool ) throw(); // around
private: // list range
ForwardIterator first;
ForwardIterator last;
public:
inline ForwardIterator begin() const throw();
inline ForwardIterator end() const throw();
public:
typedef ForwardIterator iterator;
public:
inline formatlist_t( ForwardIterator, ForwardIterator );
};


     template< typename ForwardIterator >
     char formatlist_t< ForwardIterator >::openlist() const throw()
     {
        return( __openlist );
     }
     template< typename ForwardIterator >
     char formatlist_t< ForwardIterator >::closelist() const throw()
     {
        return( __closelist );
     }
     template< typename ForwardIterator >
     char formatlist_t< ForwardIterator >::seperator() const throw()
     {
        return( __seperator );
     }

template< typename ForwardIterator >
formatlist_t< ForwardIterator > & formatlist_t< ForwardIterator >::format( char ol, char cl, char s ) throw()
{
__openlist = ol;
__closelist = cl;
__seperator = s;
return( *this );
}
template< typename ForwardIterator >
formatlist_t< ForwardIterator > & formatlist_t< ForwardIterator >::format( char ol, char cl ) throw()
{
__openlist = ol;
__closelist = cl;
return( *this );
}
template< typename ForwardIterator >
formatlist_t< ForwardIterator > & formatlist_t< ForwardIterator >::format( char s ) throw()
{
__seperator = s;
return( *this );
}


     template< typename ForwardIterator >
     bool formatlist_t< ForwardIterator >::spaceBeforeSep() const throw()
     {
        return( __spaceBeforeSep );
     }
     template< typename ForwardIterator >
     bool formatlist_t< ForwardIterator >::spaceAfterSep() const throw()
     {
        return( __spaceAfterSep );
     }
     template< typename ForwardIterator >
     bool formatlist_t< ForwardIterator >::spaceAroundList() const throw()
     {
        return( __spaceAroundList );
     }

template< typename ForwardIterator >
formatlist_t< ForwardIterator > & formatlist_t< ForwardIterator >::space( bool bs, bool as, bool al ) throw()
{
__spaceBeforeSep = bs;
__spaceAfterSep = as;
__spaceAroundList = al;
return( *this );
}
template< typename ForwardIterator >
formatlist_t< ForwardIterator > & formatlist_t< ForwardIterator >::space( bool bs, bool as ) throw()
{
__spaceBeforeSep = bs;
__spaceAfterSep = as;
return( *this );
}
template< typename ForwardIterator >
formatlist_t< ForwardIterator > & formatlist_t< ForwardIterator >::space( bool al ) throw()
{
__spaceAroundList = al;
return( *this );
}


     template< typename ForwardIterator >
     ForwardIterator formatlist_t< ForwardIterator >::begin() const throw()
     {
        return( first );
     }
     template< typename ForwardIterator >
     ForwardIterator formatlist_t< ForwardIterator >::end() const throw()
     {
        return( last );
     }

template< typename ForwardIterator >
formatlist_t< ForwardIterator >::formatlist_t( ForwardIterator f, ForwardIterator l ):
first( f ),
last( l )
{
__openlist = '[';
__closelist = ']';
__seperator = ',';
__spaceBeforeSep = false;
__spaceAfterSep = true;
__spaceAroundList = true;
}


// output routine for list formatting

     template< typename CharT, class TraitsT, typename ForwardIterator >
     inline std::basic_ostream< CharT, TraitsT > & operator<<
     (
        std::basic_ostream< CharT, TraitsT > & os,
        formatlist_t< ForwardIterator >      & fi
     )
     {
        ForwardIterator               first = fi.begin();
        ForwardIterator               last  = fi.end();

os << static_cast< CharT >( fi.openlist());
if( fi.spaceAroundList()) os << static_cast< CharT >( ' ' );


while( first != last )
{
os << *first++;
if( first != last )
{
if( fi.spaceBeforeSep()) os << static_cast< CharT >( ' ' );
os << static_cast< CharT >( fi.seperator());
if( fi.spaceAfterSep()) os << static_cast< CharT >( ' ' );
}
}


if( fi.spaceAroundList()) os << static_cast< CharT >( ' ' );
return( os << static_cast< CharT >( fi.closelist()));
}


// formatlist manipulator: these are the main forms of the manipulator function

template< typename ForwardIterator >
inline formatlist_t< ForwardIterator > formatlist( ForwardIterator first, ForwardIterator last )
{
return( formatlist_t< ForwardIterator >( first, last ));
}
template< typename Container >
inline formatlist_t< typename Container::iterator > formatlist( Container & c )
{
return( formatlist_t< typename Container::iterator >( c.begin(), c.end()));
}
}
#endif


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Reply via email to