forked from andreasfertig/cppinsights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutputFormatHelper.h
191 lines (158 loc) · 5.72 KB
/
OutputFormatHelper.h
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/******************************************************************************
*
* C++ Insights, copyright (C) by Andreas Fertig
* Distributed under an MIT license. See LICENSE for details
*
****************************************************************************/
#ifndef OUTPUT_FORMAT_HELPER_H
#define OUTPUT_FORMAT_HELPER_H
//-----------------------------------------------------------------------------
#include <utility>
#include "InsightsOnce.h"
#include "InsightsStrCat.h"
#include "InsightsStrongTypes.h"
//-----------------------------------------------------------------------------
namespace clang::insights {
//-----------------------------------------------------------------------------
/// \brief The C++ Insights formatter.
///
/// Most of the code is handed to \ref OutputFormatHelper for easy code formatting.
class OutputFormatHelper
{
public:
explicit OutputFormatHelper()
: OutputFormatHelper{0}
{
}
explicit OutputFormatHelper(const unsigned indent)
: mDefaultIndent{indent}
, mOutput{}
{
}
/// \brief Returns the current position in the output buffer.
size_t CurrentPos() const { return mOutput.length(); }
/// \brief Insert a string before the position \c atPos
void InsertAt(const size_t atPos, const std::string& data) { mOutput.insert(atPos, data); }
STRONG_BOOL(SkipIndenting);
/// \brief Set the indent level of this class to that of \c rhs.
void SetIndent(const OutputFormatHelper& rhs, const SkipIndenting skipIndenting = SkipIndenting::No)
{
if(&rhs != this) {
mDefaultIndent = rhs.mDefaultIndent;
if(SkipIndenting::No == skipIndenting) {
Indent(mDefaultIndent);
}
}
}
/// \brief Check whether the buffer is empty.
///
/// This also treats a string of just whitespaces as empty.
bool empty() const { return mOutput.empty() || (std::string::npos == mOutput.find_first_not_of(' ', 0)); }
/// \brief Returns a reference to the underlying string buffer.
std::string& GetString() { return mOutput; }
/// \brief Append a single character
///
/// Append a single character to the buffer
void Append(const char c) { mOutput += c; }
/// \brief Append a variable number of data
///
/// The \c StrCat function which is used ensures, that a \c StringRef or a char are converted appropriately.
template<typename T, typename... Args>
void Append(const T& first, Args&&... args)
{
details::StrCat(mOutput, first, std::forward<Args>(args)...);
}
/// \brief Same as \ref Append but adds a newline after the last argument.
///
/// Append a single character to the buffer
void AppendNewLine(const char c)
{
mOutput += c;
NewLine();
}
/// \brief Same as \ref Append but adds a newline after the last argument.
template<typename... Args>
void AppendNewLine(Args&&... args)
{
if constexpr(0 < sizeof...(args)) {
details::StrCat(mOutput, std::forward<Args>(args)...);
}
NewLine();
}
STRONG_BOOL(NameOnly);
/// \brief Append a \c ParamVarDecl array.
///
/// The parameter name is always added as well.
void AppendParameterList(const ArrayRef<ParmVarDecl*> parameters, const NameOnly nameOnly = NameOnly::No);
/// \brief Increase the current indention by \c SCOPE_INDENT
void IncreaseIndent() { mDefaultIndent += SCOPE_INDENT; }
/// \brief Decrease the current indention by \c SCOPE_INDENT
void DecreaseIndent() { mDefaultIndent -= SCOPE_INDENT; }
/// \brief Open a scope by inserting a '{' followed by an indented newline.
void OpenScope()
{
Append("{");
IncreaseIndent();
NewLine();
}
STRONG_BOOL(NoNewLineBefore);
/// \brief Close a scope by inserting a '}'
///
/// With the parameter \c newLineBefore a newline after the brace can be inserted.
void CloseScope(const NoNewLineBefore newLineBefore = NoNewLineBefore::No);
/// \brief Similiar to \ref CloseScope only this time a ';' is inserted after the brace.
void CloseScopeWithSemi()
{
CloseScope();
Append(";");
}
/// \brief Append a comma if needed.
void AppendComma(OnceFalse& needsComma)
{
if(needsComma) {
Append(", ");
}
}
/// \brief Append a semicolon and a newline.
void AppendSemiNewLine() { AppendNewLine(';'); }
/// \brief Append a argument list to the buffer.
///
/// This function takes care of the delimiting ',' between the parameters. The lambda \c lambda is called to each
/// argument after the comma was inserted.
/// Usage:
/// \code
/// ForEachArg(parameters, [&](const auto& p) {
/// // do something with p
/// });
/// \endcode
template<typename T, typename Lambda>
static inline void ForEachArg(const T& arguments, OutputFormatHelper& outputFormatHelper, Lambda&& lambda)
{
OnceFalse needsComma{};
for(const auto& arg : arguments) {
if(needsComma) {
outputFormatHelper.Append(", ");
}
lambda(arg);
}
}
private:
static constexpr unsigned SCOPE_INDENT{2};
unsigned mDefaultIndent;
std::string mOutput;
void Indent(unsigned count);
void NewLine()
{
mOutput += '\n';
Indent(mDefaultIndent);
}
void RemoveIndent();
template<typename T, typename Lambda>
inline void ForEachArg(const T& arguments, Lambda&& lambda)
{
ForEachArg(arguments, *this, lambda);
}
};
//-----------------------------------------------------------------------------
} // namespace clang::insights
#endif /* OUTPUT_FORMAT_HELPER_H */