Boost logo

Boost :

From: rogeeff (rogeeff_at_[hidden])
Date: 2001-12-20 12:56:48


Hi, all

It could be that I am wrong, but IMO boost::format library under
discussion does not bring ANY value, especially for the price it
cost. As far as I understand the rationale is to provide an ability
to store a output format for future reuse with iostreams. There other
cheaper and simplier solutions. Here the one:

To store a format you can provide a class like this:

namespace io_format {
class line_proc_error {
    error_format( int const& ec, std::string const& line,
                  Location const& loc )
    : arg1( ec ), arg2( line ), arg3( loc ) {}

    friend std::ostream& operator<<( std::ostream& str,
                                     line_proc_error const& f )
    {
       str <<
        "Error " << arg1 << " while processing line " << arg2
                 << " at location " << arg3;

      return str;
    }

private:
    int& arg1;
    std::string const& arg2;
};
} // namespace io_format

Or you can provide a function like this:

namespace io_format {
    std::ostream&
    line_proc_error( std::ostream& str,
                     int const& ec,
                     std::string const& line,
                     Location const& loc )
    {
       str <<
        "Error " << arg1 << " while processing line " << arg2
                 << " at location " << arg3;

      return str;
    }
} // namespace io_format

To automate format definition you can provide marcos ( probably some
mpl based solution). Then format definition will look like this:

BOOST_START_FORMAT_DEFINITION( line_proc_error,
                               3, (int, std::string, Location) )
"Error " << arg1 << " while processing line " << arg2
         << " at location "<< arg3;
BOOST_END_FORMAT_DEFINITION
 
And usage will look like this:

int ec;
std::string line;
Location loc;
...
(class based solution):
std::cout << io_format::line_proc_error( ec, line, loc );
(function based solution):
io_format::line_proc_error( std::cout, ec, line, loc );

Using boost::bind and function based solution will give you an
ability to bind any "positional" argument.

The features of this solution:

* Very simpe
* Provide the same functionallity as discussed boost::format (?)
* Does not inflict any runtime penalties
* Allows to used user defined types in a format (does boost::format
allows it?)

Regards,

Gennadiy.


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