CppParser
Loading...
Searching...
No Matches
cppindent.h
Go to the documentation of this file.
1/*
2 The MIT License (MIT)
3
4 Copyright (c) 2018 Satya Das
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy of
7 this software and associated documentation files (the "Software"), to deal in
8 the Software without restriction, including without limitation the rights to
9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10 the Software, and to permit persons to whom the Software is furnished to do so,
11 subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#pragma once
25
26#include <cstdint>
27#include <ostream>
28
33{
34public:
35 enum Type
36 {
42 };
43
44private:
45 std::uint16_t indentLevel_;
46 const std::uint16_t initialLevel_;
48
49public:
50 CppIndent(std::uint16_t initialLevel = 0, Type type = kDoubleSpace)
51 : indentLevel_(0)
52 , initialLevel_(initialLevel)
53 , type_(type)
54 {
55 }
56 std::uint16_t depth() const
57 {
59 }
61 {
63 return *this;
64 }
66 {
67 if (indentLevel_)
69 return *this;
70 }
72 {
73 CppIndent ret = *this;
75 return ret;
76 }
78 {
79 CppIndent ret = *this;
80 if (indentLevel_)
82 return ret;
83 }
84
85 const char* indentStr() const
86 {
87 static const char* indent[] = {"\t", " ", " ", " ", " "};
88 return indent[type_];
89 }
90 std::string toString() const
91 {
92 std::string ret;
93 for (std::uint16_t i = 0; i < initialLevel_ + indentLevel_; ++i)
94 {
95 ret += indentStr();
96 }
97
98 return ret;
99 }
100 void emit(std::ostream& stm) const
101 {
102 for (std::uint16_t i = 0; i < initialLevel_ + indentLevel_; ++i)
103 {
104 stm << indentStr();
105 }
106 }
107
109 {
110 CppIndent newIndent(0, type_);
111
112 return newIndent;
113 }
114};
Helper class to manage indentation while writing C++ file from AST.
Definition: cppindent.h:33
CppIndent operator++(int)
Definition: cppindent.h:71
CppIndent & operator++()
Definition: cppindent.h:60
@ kQuadSpace
Definition: cppindent.h:41
@ kTripleSpace
Definition: cppindent.h:40
@ kDoubleSpace
Definition: cppindent.h:39
@ kSingleSpace
Definition: cppindent.h:38
std::string toString() const
Definition: cppindent.h:90
Type type_
Definition: cppindent.h:47
const char * indentStr() const
Definition: cppindent.h:85
const std::uint16_t initialLevel_
Definition: cppindent.h:46
CppIndent operator--(int)
Definition: cppindent.h:77
std::uint16_t depth() const
Definition: cppindent.h:56
CppIndent resetted() const
Definition: cppindent.h:108
CppIndent(std::uint16_t initialLevel=0, Type type=kDoubleSpace)
Definition: cppindent.h:50
CppIndent & operator--()
Definition: cppindent.h:65
std::uint16_t indentLevel_
Definition: cppindent.h:45
void emit(std::ostream &stm) const
Definition: cppindent.h:100