IDL compiler front-end library
Loading...
Searching...
No Matches
IndentedOut.h
Go to the documentation of this file.
1
2/*
3* Nirvana IDL front-end library.
4*
5* This is a part of the Nirvana project.
6*
7* Author: Igor Popov
8*
9* Copyright (c) 2021 Igor Popov.
10*
11* This program is free software; you can redistribute it and/or modify
12* it under the terms of the GNU Lesser General Public License as published by
13* the Free Software Foundation; either version 3 of the License, or
14* (at your option) any later version.
15*
16* This program is distributed in the hope that it will be useful,
17* but WITHOUT ANY WARRANTY; without even the implied warranty of
18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19* GNU General Public License for more details.
20*
21* You should have received a copy of the GNU Lesser General Public
22* License along with this library. If not, see <http://www.gnu.org/licenses/>.
23*
24* Send comments and/or bug reports to:
25* popov.nirvana@gmail.com
26*/
27#ifndef IDLFE_BE_INDENTEDOUT_H_
28#define IDLFE_BE_INDENTEDOUT_H_
29#pragma once
30
31#include <fstream>
32#include <filesystem>
33#include <assert.h>
34
36namespace BE {
37
39class IndentedOut : public std::ofstream
40{
41public:
44
49 IndentedOut (const std::filesystem::path& file);
50
53 {
54 close ();
55 }
56
63 void open (const std::filesystem::path& file);
64
66 void close ();
67
69 void indent () noexcept
70 {
71 isbuf_.indent ();
72 }
73
75 void unindent () noexcept
76 {
77 isbuf_.unindent ();
78 }
79
81 void empty_line ()
82 {
83 isbuf_.empty_line ();
84 }
85
87 unsigned indentation () const noexcept
88 {
89 return isbuf_.indentation ();
90 }
91
93 char last_char () const noexcept
94 {
95 return isbuf_.last_char ();
96 }
97
99 size_t size () const noexcept
100 {
101 return isbuf_.size ();
102 }
103
104private:
105 class IndentedStreambuf : public std::streambuf
106 {
107 public:
108 IndentedStreambuf ();
109
110 void init (std::ostream& s);
111 void term (std::ostream& s);
112
113 void indent () noexcept
114 {
115 ++indentation_;
116 }
117
118 void unindent () noexcept
119 {
120 assert (indentation_ > 0);
121 if (indentation_ > 0)
122 --indentation_;
123 }
124
125 unsigned indentation () const noexcept
126 {
127 return indentation_;
128 }
129
130 void empty_line ();
131
132 const char last_char () const noexcept
133 {
134 return last_char_;
135 }
136
137 size_t size () const noexcept
138 {
139 return size_;
140 }
141
142 protected:
143 virtual int overflow (int c);
144
145 private:
146 int put_char (char c);
147
148 private:
149 std::streambuf* out_;
150 unsigned indentation_;
151 char last_char_;
152 bool bol_;
153 bool empty_line_;
154 size_t size_;
155 };
156
157 IndentedStreambuf isbuf_;
158};
159
160}
161
162#endif
Output file stream with indentation.
Definition IndentedOut.h:40
size_t size() const noexcept
Definition IndentedOut.h:99
~IndentedOut()
Destructor.
Definition IndentedOut.h:52
char last_char() const noexcept
Definition IndentedOut.h:93
void open(const std::filesystem::path &file)
Opens the file.
void empty_line()
Ensure that empty line inserted at the current position.
Definition IndentedOut.h:81
IndentedOut(const std::filesystem::path &file)
void indent() noexcept
Increase indentation.
Definition IndentedOut.h:69
void close()
Closes the file.
void unindent() noexcept
Decrease indentation.
Definition IndentedOut.h:75
IndentedOut()
Default constructor.
unsigned indentation() const noexcept
Definition IndentedOut.h:87
Back-end support utility classes.
Definition IndentedOut.h:36