summaryrefslogtreecommitdiff
path: root/tiger-compiler/lib/misc/indent.cc
blob: 03e15cdc58a039c40f480491f9e311fb4f19377a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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