P4C
The P4 Compiler
Loading...
Searching...
No Matches
indent.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef LIB_INDENT_H_
18#define LIB_INDENT_H_
19
20#include <iomanip>
21#include <iostream>
22#include <vector>
23
24namespace P4 {
25
26class indent_t {
27 int indent;
28
29 public:
30 static int tabsz;
31 indent_t() : indent(0) {}
32 explicit indent_t(int i) : indent(i) {}
33 indent_t &operator++() {
34 ++indent;
35 return *this;
36 }
37 indent_t &operator--() {
38 --indent;
39 return *this;
40 }
41 indent_t operator++(int) {
42 indent_t rv = *this;
43 ++indent;
44 return rv;
45 }
46 indent_t operator--(int) {
47 indent_t rv = *this;
48 --indent;
49 return rv;
50 }
51 friend std::ostream &operator<<(std::ostream &os, indent_t i);
52 indent_t operator+(int v) {
53 indent_t rv = *this;
54 rv.indent += v;
55 return rv;
56 }
57 indent_t operator-(int v) {
58 indent_t rv = *this;
59 rv.indent -= v;
60 return rv;
61 }
62 indent_t &operator+=(int v) {
63 indent += v;
64 return *this;
65 }
66 indent_t &operator-=(int v) {
67 indent -= v;
68 return *this;
69 }
70 static indent_t &getindent(std::ostream &);
71};
72
73inline std::ostream &operator<<(std::ostream &os, indent_t i) {
74 os << std::setw(i.indent * i.tabsz) << "";
75 return os;
76}
77
78namespace IndentCtl {
79inline std::ostream &endl(std::ostream &out) {
80 return out << std::endl << indent_t::getindent(out);
81}
82inline std::ostream &indent(std::ostream &out) {
83 ++indent_t::getindent(out);
84 return out;
85}
86inline std::ostream &unindent(std::ostream &out) {
87 --indent_t::getindent(out);
88 return out;
89}
90
92 // an indent that can be added to any stream and unrolls when the object is destroyed
93 std::vector<std::ostream *> streams; // streams that have been indented
94
95 public:
96 TempIndent(const TempIndent &) = delete; // not copyable
97 TempIndent() = default;
98 friend std::ostream &operator<<(std::ostream &out, TempIndent &ti) {
99 ti.streams.push_back(&out);
100 return out << indent;
101 }
102 friend std::ostream &operator<<(std::ostream &out, TempIndent &&ti) {
103 ti.streams.push_back(&out);
104 return out << indent;
105 }
106 const char *pop_back() {
107 if (!streams.empty()) {
108 *streams.back() << unindent;
109 streams.pop_back();
110 }
111 return "";
112 }
113 const char *reset() {
114 for (auto *out : streams) *out << unindent;
115 streams.clear();
116 return "";
117 }
118 ~TempIndent() {
119 for (auto *out : streams) *out << unindent;
120 }
121};
122
123} // namespace IndentCtl
124} // namespace P4
125
126#endif /* LIB_INDENT_H_ */
Definition indent.h:91
Definition indent.h:26
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24