Indent.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2015 Imperial College London
5  * Copyright 2013-2015 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MIRTK_Indent_H
21 #define MIRTK_Indent_H
22 
23 #include "mirtk/Stream.h"
24 
25 
26 namespace mirtk {
27 
28 
29 /**
30  * Auxiliary class for output indentation.
31  */
32 class Indent
33 {
34 protected:
35 
36  /// Number of indentation levels
37  int _N;
38 
39  /// Number of whitespace characters per indentation level
41 
42 public:
43 
44  /// Constructor
45  Indent(int n = 0, int s = 2) : _N(n), _NumberOfSpaces(s) {}
46 
47  /// Copy constructor
48  Indent(const Indent &o) : _N(o._N), _NumberOfSpaces(o._NumberOfSpaces) {}
49 
50  /// Assignment operator
52  {
53  _N = n;
54  return *this;
55  }
56 
57  /// Assignment operator
58  Indent &operator= (const Indent &indent)
59  {
60  _N = indent._N;
61  _NumberOfSpaces = indent._NumberOfSpaces;
62  return *this;
63  }
64 
65  /// Pre-decrement operator
67  {
68  _N--;
69  return *this;
70  }
71 
72  /// Pre-increment operator
74  {
75  _N++;
76  return *this;
77  }
78 
79  /// Post-decrement operator
81  {
82  Indent pre(*this);
83  --(*this);
84  return pre;
85  }
86 
87  /// Post-increment operator
89  {
90  Indent pre(*this);
91  ++(*this);
92  return pre;
93  }
94 
95  /// Add indentation to this indentation
96  Indent &operator+= (const Indent &indent)
97  {
98  _N += indent._N;
99  return *this;
100  }
101 
102  /// Add two indentations
103  Indent operator+ (const Indent &indent) const
104  {
105  return Indent(_N + indent._N);
106  }
107 
108  /// Subtract indentation from this indentation
109  Indent &operator-= (const Indent &indent)
110  {
111  _N -= indent._N;
112  return *this;
113  }
114 
115  /// Subtract two indentations from another
116  Indent operator- (const Indent &indent) const
117  {
118  return Indent(_N + indent._N);
119  }
120 
121  /// Get indentation level
122  int Level() const
123  {
124  return _N;
125  }
126 
127  /// Set number of indentation whitespace characters per level
128  void SpacesPerLevel(int n)
129  {
130  _NumberOfSpaces = n;
131  }
132 
133  /// Get number of indentation whitespace characters per level
134  int SpacesPerLevel() const
135  {
136  return _NumberOfSpaces;
137  }
138 
139  /// Number of space characters
140  int Spaces() const
141  {
142  return _N * _NumberOfSpaces;
143  }
144 };
145 
146 // ---------------------------------------------------------------------------
147 // Streaming operator
148 inline ostream &operator<< (ostream &os, const Indent &indent)
149 {
150  for (int i = 0; i < indent.Spaces(); ++i) os << " ";
151  return os;
152 }
153 
154 
155 } // namespace mirtk
156 
157 #endif // MIRTK_Indent_H
int _N
Number of indentation levels.
Definition: Indent.h:37
int Level() const
Get indentation level.
Definition: Indent.h:122
Indent(const Indent &o)
Copy constructor.
Definition: Indent.h:48
Indent & operator=(int n)
Assignment operator.
Definition: Indent.h:51
Indent & operator++()
Pre-increment operator.
Definition: Indent.h:73
Indent & operator+=(const Indent &indent)
Add indentation to this indentation.
Definition: Indent.h:96
Indent operator-(const Indent &indent) const
Subtract two indentations from another.
Definition: Indent.h:116
Indent & operator-=(const Indent &indent)
Subtract indentation from this indentation.
Definition: Indent.h:109
Definition: IOConfig.h:41
int SpacesPerLevel() const
Get number of indentation whitespace characters per level.
Definition: Indent.h:134
int Spaces() const
Number of space characters.
Definition: Indent.h:140
int _NumberOfSpaces
Number of whitespace characters per indentation level.
Definition: Indent.h:40
Indent operator+(const Indent &indent) const
Add two indentations.
Definition: Indent.h:103
Indent(int n=0, int s=2)
Constructor.
Definition: Indent.h:45
Indent & operator--()
Pre-decrement operator.
Definition: Indent.h:66
void SpacesPerLevel(int n)
Set number of indentation whitespace characters per level.
Definition: Indent.h:128