9b8528fee4
* Add explicit keyword to conversion constructors. * Add NOLINT(implicit) comments for implicit conversion constructors. Bug: 28341362 * Use const reference type for read-only parameters. Bug: 30407689 * Use const reference type to avoid unnecessary copy. Bug: 30413862 Test: build with WITH_TIDY=1 Change-Id: Id6d21961f313a1ad92b15a37fdaa5be9e8ab48e1
64 lines
1.3 KiB
C++
64 lines
1.3 KiB
C++
#ifndef __INDENT_PRINTER_H
|
|
#define __INDENT_PRINTER_H
|
|
|
|
class IndentPrinter {
|
|
public:
|
|
explicit IndentPrinter(FILE* stream, int indentSize=2)
|
|
: mStream(stream)
|
|
, mIndentSize(indentSize)
|
|
, mIndent(0)
|
|
, mNeedsIndent(true) {
|
|
}
|
|
|
|
void indent(int amount = 1) {
|
|
mIndent += amount;
|
|
if (mIndent < 0) {
|
|
mIndent = 0;
|
|
}
|
|
}
|
|
|
|
void print(const char* fmt, ...) {
|
|
doIndent();
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
vfprintf(mStream, fmt, args);
|
|
va_end(args);
|
|
}
|
|
|
|
void println(const char* fmt, ...) {
|
|
doIndent();
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
vfprintf(mStream, fmt, args);
|
|
va_end(args);
|
|
fputs("\n", mStream);
|
|
mNeedsIndent = true;
|
|
}
|
|
|
|
void println() {
|
|
doIndent();
|
|
fputs("\n", mStream);
|
|
mNeedsIndent = true;
|
|
}
|
|
|
|
private:
|
|
void doIndent() {
|
|
if (mNeedsIndent) {
|
|
int numSpaces = mIndent * mIndentSize;
|
|
while (numSpaces > 0) {
|
|
fputs(" ", mStream);
|
|
numSpaces--;
|
|
}
|
|
mNeedsIndent = false;
|
|
}
|
|
}
|
|
|
|
FILE* mStream;
|
|
const int mIndentSize;
|
|
int mIndent;
|
|
bool mNeedsIndent;
|
|
};
|
|
|
|
#endif // __INDENT_PRINTER_H
|
|
|