diff options
Diffstat (limited to 'tiger-compiler/lib/misc/indent.cc')
| -rw-r--r-- | tiger-compiler/lib/misc/indent.cc | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/tiger-compiler/lib/misc/indent.cc b/tiger-compiler/lib/misc/indent.cc new file mode 100644 index 0000000..03e15cd --- /dev/null +++ b/tiger-compiler/lib/misc/indent.cc @@ -0,0 +1,57 @@ +/** + ** \file misc/indent.cc + ** \brief Implementation of indentation relative functions. + **/ + +#include <iomanip> +#include <ostream> + +#include <misc/contract.hh> +#include <misc/indent.hh> + +namespace misc +{ + namespace + { + /// The current indentation level for \a o. + inline long int& indent(std::ostream& o) + { + // The slot to store the current indentation level. + static const long int indent_index = std::ios::xalloc(); + return o.iword(indent_index); + } + + } // namespace + + std::ostream& incindent(std::ostream& o) + { + indent(o) += 2; + return o; + } + + std::ostream& decindent(std::ostream& o) + { + precondition(indent(o)); + indent(o) -= 2; + return o; + } + + std::ostream& resetindent(std::ostream& o) + { + indent(o) = 0; + return o; + } + + std::ostream& iendl(std::ostream& o) + { + o << std::endl; + // Be sure to be able to restore the stream flags. + char fill = o.fill(' '); + return o << std::setw(indent(o)) << "" << std::setfill(fill); + } + + std::ostream& incendl(std::ostream& o) { return o << incindent << iendl; } + + std::ostream& decendl(std::ostream& o) { return o << decindent << iendl; } + +} // namespace misc |
