1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef FORMATTER_H_
18
19 #define FORMATTER_H_
20
21 #include <android-base/macros.h>
22 #include <functional>
23 #include <string>
24
25 namespace android {
26
27 // Two styles to use a Formatter.
28 // One is with .indent() calls and operator<<.
29 // out << "if (good) {\n"; out.indent(); out << "blah\nblah\n"; out.unindent(); out << "}\n";
30 // The other is with chain calls and lambda functions
31 // out.sIf("good", [&] { out("blah").endl()("blah").endl(); }).endl();
32 struct Formatter {
33
34 // Assumes ownership of file. Directed to stdout if file == NULL.
35 Formatter(FILE *file = NULL);
36 ~Formatter();
37
38 void indent(size_t level = 1);
39 void unindent(size_t level = 1);
40
41 // Note that The last \n after the last line is NOT added automatically.
42 // out.indent(2, [&] {
43 // out << "Meow\n";
44 // });
45 Formatter &indent(size_t level, std::function<void(void)> func);
46
47 // Note that The last \n after the last line is NOT added automatically.
48 // out.indent([&] {
49 // out << "Meow\n";
50 // });
51 Formatter &indent(std::function<void(void)> func);
52
53 // A block inside braces.
54 // * No space will be added before the opening brace.
55 // * The last \n before the closing brace is added automatically.
56 // * There will NOT be a \n after the closing brace.
57 // out.block([&] {
58 // out << "one();\n"
59 // << "two();\n";
60 // });
61 // is equivalent to
62 // out << "{\n"
63 // << "one();\ntwo();\n" // func()
64 // << "}";
65 Formatter &block(std::function<void(void)> func);
66
67 // A synonym to (*this) << "\n";
68 Formatter &endl();
69
70 // out.sIf("z == 1", [&] {
71 // out << "doGoodStuff();\n";
72 // }).sElseIf("z == 2", [&] {
73 // out << "doBadStuff();\n";
74 // }).sElse([&] {
75 // out << "logFatal();\n";
76 // }).endl();
77 // note that there will be a space before the "else"-s.
78 Formatter &sIf(const std::string &cond, std::function<void(void)> block);
79 Formatter &sElseIf(const std::string &cond, std::function<void(void)> block);
80 Formatter &sElse(std::function<void(void)> block);
81
82 // out.sFor("int i = 0; i < 10; i++", [&] {
83 // out << "printf(\"%d\", i);\n";
84 // }).endl();
85 Formatter &sFor(const std::string &stmts, std::function<void(void)> block);
86
87 // out.sTry([&] {
88 // out << "throw RemoteException();\n"
89 // }).sCatch("RemoteException ex", [&] {
90 // out << "ex.printStackTrace();\n"
91 // }).sFinally([&] {
92 // // cleanup
93 // }).endl();
94 // note that there will be a space before the "catch"-s.
95 Formatter &sTry(std::function<void(void)> block);
96 Formatter &sCatch(const std::string &exception, std::function<void(void)> block);
97 Formatter &sFinally(std::function<void(void)> block);
98
99 // out.sWhile("z < 10", [&] {
100 // out << "z++;\n";
101 // }).endl();
102 Formatter &sWhile(const std::string &cond, std::function<void(void)> block);
103
104 // out.join(v.begin(), v.end(), ",", [&](const auto &e) {
105 // out << toString(e);
106 // });
107 template<typename I>
108 Formatter &join(const I begin, const I end, const std::string &separator,
109 std::function<void(const typename std::iterator_traits<I>::value_type &)> func);
110
111 Formatter &operator<<(const std::string &out);
112
113 Formatter &operator<<(char c);
114 Formatter &operator<<(signed char c);
115 Formatter &operator<<(unsigned char c);
116
117 Formatter &operator<<(short c);
118 Formatter &operator<<(unsigned short c);
119 Formatter &operator<<(int c);
120 Formatter &operator<<(unsigned int c);
121 Formatter &operator<<(long c);
122 Formatter &operator<<(unsigned long c);
123 Formatter &operator<<(long long c);
124 Formatter &operator<<(unsigned long long c);
125 Formatter &operator<<(float c);
126 Formatter &operator<<(double c);
127 Formatter &operator<<(long double c);
128
129 // Any substrings matching "space" will be stripped out of the output.
130 void setNamespace(const std::string &space);
131
132 // Puts a prefix before each line. This is useful if
133 // you want to start a // comment block, for example.
134 // The prefix will be put before the indentation.
135 // Will be effective the next time cursor is at the start of line.
136 void setLinePrefix(const std::string& prefix);
137 // Remove the line prefix.
138 void unsetLinePrefix();
139
140 private:
141 FILE *mFile;
142 size_t mIndentDepth;
143 bool mAtStartOfLine;
144
145 std::string mSpace;
146 std::string mLinePrefix;
147
148 void output(const std::string &text) const;
149
150 DISALLOW_COPY_AND_ASSIGN(Formatter);
151 };
152
153 template<typename I>
join(const I begin,const I end,const std::string & separator,std::function<void (const typename std::iterator_traits<I>::value_type &)> func)154 Formatter &Formatter::join(const I begin, const I end, const std::string &separator,
155 std::function<void(const typename std::iterator_traits<I>::value_type &)> func) {
156 for (I iter = begin; iter != end; ++iter) {
157 if (iter != begin) {
158 (*this) << separator;
159 }
160 func(*iter);
161 }
162 return (*this);
163 }
164
165 } // namespace android
166
167 #endif // FORMATTER_H_
168
169