Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2003-04-03 07:17:12


Larry Evans wrote:
>Wouldn't the following:

> *this<<iomanip::setw(m_indent.length *
>m_indent.level)<<setfill(fill)<<fill;
> *this<<setfill(fill);

>do essentially what indentor< OutputFileType >::indent() does?

Thanks. I didn't think of that. I'll implement it into the code (with the
correction pointed out by Jason House).

>This implementation also requires the user to know when the
>beginning of line occurs. That does sound pretty reasonable,
>and I'm kinda wondering why I didn't do it in marg_ostream; however,
>marg_ostream doesn't require this, and I'm wondering whether other
>people think this feature is worth the extra complexity in
>marg_ostream.

There are two possible ways I see at simplifying your marg_ostream:
[1] Overload string operations only since you only really need to intercept
'\n' characters - this appears to be the simpler of the two solutions (as
there is no real need to overload for integer types, only strings and
characters).
[2] Write a stream buffer that intercepts the '\n' characters - this seems
overly complicated and a little overkill.

NOTE: I have not yet looked at the code for marg_ostream so I cannot give
any more detailed comments on it at the moment.

One of the reasons I chose not to have the '\n' character trigger the code
to perform the indentation is that you could have code like:

out.indent() << "This is a test" << '\n';
out.beginIndent();
   out.indent() << "Indented" << '\n';
out.endIndent();
out.indent() << "End of test" << '\n';

It makes sense to have the new lines at the end of the output. If, however,
the '\n' character triggered the indentation, you would need something like:

out << "This is a test";
out.beginIndent();
   out << "\nIndented";
out.endIndent();
out << "\nEnd of test";

which is less intuitive and can lead to mistakes if you are not careful. The
reason for this is that the indentation will be done at the wrong time and
lead to incorrect alignment of the string "Indented".

Letting the user control *when* indentation occurs gives greater
flexibility, for example if you were formatting an XML document, you could
choose not to indent on CDATA sections, or pre elements in HTML.

>Another indentor advantage is there's no need
>to define operator<< for all the primitive types as was done in
>marg_ostream.

That is due to the aim at keeping indentor's design as simple as possible,
while giving it as much flexability as possible. Redefining the operator<<
would have severly complicated the design.

-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