All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Public Attributes | List of all members
util::addIndent Struct Reference

Stream modifier that makes it "indented". More...

#include <StreamIndenter.h>

Public Member Functions

 addIndent (std::string indent, std::string firstIndent)
 
 addIndent (std::string const &indent)
 

Public Attributes

std::string firstIndent
 
std::string indent
 

Detailed Description

Stream modifier that makes it "indented".

The intended use of this class is to be "inserted" into an output stream object in order to gain a simple indentation:

Example:

std::cout << util::addIndent("> ", "") << "First line: not indented."
<< "\nSecond line: indented by '> '."
<< "\nThird line: indented by '> '."
;

Technically, after the first insertion (std::cout << addIndent(...)) the returned object is not std::cout any more, but a wrapper around it which intercepts all the insertion operations (operator<<).

The wrapper is designed to steal the stream object if it is a temporary one (e.g. std::ofstream{ "test.txt" } << addIndent("\> ") << ...) and to reference to it otherwise. Unless the wrapper object is somehow saved, it is destroyed (together with the stream if it was "stolen") as soon as the statement falls out of scope. Therefore, in this two-lines example:

std::cout << "First line: not indented."
<< "\nSecond line: also not indented."
;

the first line does effectively nothing (the wrapper is created, ready to indent, but it is then immediately destroyed) while the second line performs normal std::cout output.

Note
The "first line" is actually the first insertion into the wrapped stream. For example, std::cout << "A!" << util::addIndent("\> ", "$ ") << "First line."; will produce A!$ First line..
It is in principle possible to create modifiers with a special behaviour to interact directly with the wrapper (for example, change the indentation level or indentation string, reset to the first line indentation, ...). Such modifiers are not implemented so far.

Definition at line 69 of file StreamIndenter.h.

Constructor & Destructor Documentation

util::addIndent::addIndent ( std::string  indent,
std::string  firstIndent 
)
inline

Definition at line 72 of file StreamIndenter.h.

73  : firstIndent{ std::move(firstIndent) }, indent{ std::move(indent) }
74  {}
std::string indent
std::string firstIndent
util::addIndent::addIndent ( std::string const &  indent)
inline

Definition at line 75 of file StreamIndenter.h.

76  : addIndent{ indent, indent } {}
addIndent(std::string indent, std::string firstIndent)
std::string indent

Member Data Documentation

std::string util::addIndent::firstIndent

Definition at line 70 of file StreamIndenter.h.

std::string util::addIndent::indent

Definition at line 70 of file StreamIndenter.h.


The documentation for this struct was generated from the following file: