Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2003-03-31 08:43:29


This utility class provides a mechanism for adding indentation to an I/O
stream. I was wondering who would be interested in it, or if anyone has
anything similar.

This utility could have many applications, some of which include:
* integration into a tracing library, allowing function calls to be
indented, making it easier to see code execution flow -- I have used this
technique to track an infinite recursion;
* integration into a code beautifier for C/C++ (indenting on braces) or
XML/HTML (indenting on tags with child nodes);
* tree output helper -- will indent a tree structure, removing the
indentation code from the output algorithm.

It has been designed to be as flexible as possible, allowing both the fill
character and the fill length to be customized. The indentation mechanism
(in the indent() function) is optimized to perform as few output operations
as possible.

There are presently two ways that you can add an indentation level. The
first is explicitly via a beginIndent() call, requiring a call to
endIndent(); the second is to use the indentor::block class (this calls
beginIndent() on construction and endIndent() on deconstruction).

There are several ways that I am looking at extending this:

[1] Parametizing the fill type from char to a template type CharT, allowing
it to be either char or wchar_t.

[2] Making use of a character traits type to abstract from explicit use of
std::memset.

[3] Adding safety checking on the endIndent() call to ensure that:
   m_indent.level >= 0

[4] Allowing the indent() function to optionally output a newline character
before it performs the indenting.

The example in the zip file provides a demonstration of how to use this
class. If anyone is interested in having a more complicated example of its
usage, or has any suggestions on how to improve it, comments on the utility,
all are welcome.

-rhd-
mailto:msclrhd_at_[hidden]

_________________________________________________________________
Worried what your kids see online? Protect them better with MSN 8
http://join.msn.com/?page=features/parental&pgmarket=en-gb&XAPID=186&DI=1059




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