1 | /* |
2 | * Copyright (C) 2004, 2008 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include <wtf/Forward.h> |
29 | #include <wtf/text/StringBuilder.h> |
30 | |
31 | namespace WTF { |
32 | |
33 | class TextStream { |
34 | WTF_MAKE_FAST_ALLOCATED; |
35 | public: |
36 | struct FormatNumberRespectingIntegers { |
37 | WTF_MAKE_STRUCT_FAST_ALLOCATED; |
38 | FormatNumberRespectingIntegers(double number) |
39 | : value(number) { } |
40 | |
41 | double value; |
42 | }; |
43 | |
44 | enum Formatting { |
45 | SVGStyleRect = 1 << 0, // "at (0,0) size 10x10" |
46 | NumberRespectingIntegers = 1 << 1, |
47 | LayoutUnitsAsIntegers = 1 << 2, |
48 | }; |
49 | |
50 | using FormattingFlags = unsigned; |
51 | |
52 | enum class LineMode { SingleLine, MultipleLine }; |
53 | TextStream(LineMode lineMode = LineMode::MultipleLine, FormattingFlags formattingFlags = 0) |
54 | : m_formattingFlags(formattingFlags) |
55 | , m_multiLineMode(lineMode == LineMode::MultipleLine) |
56 | { |
57 | } |
58 | |
59 | WTF_EXPORT_PRIVATE TextStream& operator<<(bool); |
60 | WTF_EXPORT_PRIVATE TextStream& operator<<(char); |
61 | WTF_EXPORT_PRIVATE TextStream& operator<<(int); |
62 | WTF_EXPORT_PRIVATE TextStream& operator<<(unsigned); |
63 | WTF_EXPORT_PRIVATE TextStream& operator<<(long); |
64 | WTF_EXPORT_PRIVATE TextStream& operator<<(unsigned long); |
65 | WTF_EXPORT_PRIVATE TextStream& operator<<(long long); |
66 | |
67 | WTF_EXPORT_PRIVATE TextStream& operator<<(unsigned long long); |
68 | WTF_EXPORT_PRIVATE TextStream& operator<<(float); |
69 | WTF_EXPORT_PRIVATE TextStream& operator<<(double); |
70 | WTF_EXPORT_PRIVATE TextStream& operator<<(const char*); |
71 | WTF_EXPORT_PRIVATE TextStream& operator<<(const void*); |
72 | WTF_EXPORT_PRIVATE TextStream& operator<<(const String&); |
73 | // Deprecated. Use the NumberRespectingIntegers FormattingFlag instead. |
74 | WTF_EXPORT_PRIVATE TextStream& operator<<(const FormatNumberRespectingIntegers&); |
75 | |
76 | #ifdef __OBJC__ |
77 | WTF_EXPORT_PRIVATE TextStream& operator<<(id<NSObject>); |
78 | #endif |
79 | |
80 | FormattingFlags formattingFlags() const { return m_formattingFlags; } |
81 | void setFormattingFlags(FormattingFlags flags) { m_formattingFlags = flags; } |
82 | |
83 | bool hasFormattingFlag(Formatting flag) const { return m_formattingFlags & flag; } |
84 | |
85 | template<typename T> |
86 | void dumpProperty(const String& name, const T& value) |
87 | { |
88 | TextStream& ts = *this; |
89 | ts.startGroup(); |
90 | ts << name << " " << value; |
91 | ts.endGroup(); |
92 | } |
93 | |
94 | WTF_EXPORT_PRIVATE String release(); |
95 | |
96 | WTF_EXPORT_PRIVATE void startGroup(); |
97 | WTF_EXPORT_PRIVATE void endGroup(); |
98 | WTF_EXPORT_PRIVATE void nextLine(); // Output newline and indent. |
99 | |
100 | int indent() const { return m_indent; } |
101 | void setIndent(int indent) { m_indent = indent; } |
102 | void increaseIndent(int amount = 1) { m_indent += amount; } |
103 | void decreaseIndent(int amount = 1) { m_indent -= amount; ASSERT(m_indent >= 0); } |
104 | |
105 | WTF_EXPORT_PRIVATE void writeIndent(); |
106 | |
107 | // Stream manipulators. |
108 | TextStream& operator<<(TextStream& (*func)(TextStream&)) |
109 | { |
110 | return (*func)(*this); |
111 | } |
112 | |
113 | struct Repeat { |
114 | WTF_MAKE_STRUCT_FAST_ALLOCATED; |
115 | Repeat(unsigned inWidth, char inCharacter) |
116 | : width(inWidth), character(inCharacter) |
117 | { } |
118 | unsigned width { 0 }; |
119 | char character { ' ' }; |
120 | }; |
121 | |
122 | TextStream& operator<<(const Repeat& repeated) |
123 | { |
124 | for (unsigned i = 0; i < repeated.width; ++i) |
125 | m_text.append(repeated.character); |
126 | |
127 | return *this; |
128 | } |
129 | |
130 | class IndentScope { |
131 | public: |
132 | IndentScope(TextStream& ts, int amount = 1) |
133 | : m_stream(ts) |
134 | , m_amount(amount) |
135 | { |
136 | m_stream.increaseIndent(m_amount); |
137 | } |
138 | ~IndentScope() |
139 | { |
140 | m_stream.decreaseIndent(m_amount); |
141 | } |
142 | |
143 | private: |
144 | TextStream& m_stream; |
145 | int m_amount; |
146 | }; |
147 | |
148 | class GroupScope { |
149 | public: |
150 | GroupScope(TextStream& ts) |
151 | : m_stream(ts) |
152 | { |
153 | m_stream.startGroup(); |
154 | } |
155 | ~GroupScope() |
156 | { |
157 | m_stream.endGroup(); |
158 | } |
159 | |
160 | private: |
161 | TextStream& m_stream; |
162 | }; |
163 | |
164 | private: |
165 | StringBuilder m_text; |
166 | FormattingFlags m_formattingFlags { 0 }; |
167 | int m_indent { 0 }; |
168 | bool m_multiLineMode { true }; |
169 | }; |
170 | |
171 | inline TextStream& indent(TextStream& ts) |
172 | { |
173 | ts.writeIndent(); |
174 | return ts; |
175 | } |
176 | |
177 | template<typename Item> |
178 | TextStream& operator<<(TextStream& ts, const Optional<Item>& item) |
179 | { |
180 | if (item) |
181 | return ts << item.value(); |
182 | |
183 | return ts << "nullopt" ; |
184 | } |
185 | |
186 | template<typename Item> |
187 | TextStream& operator<<(TextStream& ts, const Vector<Item>& vector) |
188 | { |
189 | ts << "[" ; |
190 | |
191 | unsigned size = vector.size(); |
192 | for (unsigned i = 0; i < size; ++i) { |
193 | ts << vector[i]; |
194 | if (i < size - 1) |
195 | ts << ", " ; |
196 | } |
197 | |
198 | return ts << "]" ; |
199 | } |
200 | |
201 | template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg> |
202 | TextStream& operator<<(TextStream& ts, const HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>& map) |
203 | { |
204 | ts << "{" ; |
205 | |
206 | bool first = true; |
207 | for (const auto& keyValuePair : map) { |
208 | ts << keyValuePair.key << ": " << keyValuePair.value; |
209 | if (!first) |
210 | ts << ", " ; |
211 | first = false; |
212 | } |
213 | |
214 | return ts << "}" ; |
215 | } |
216 | |
217 | template<typename ValueArg, typename HashArg, typename TraitsArg> |
218 | TextStream& operator<<(TextStream& ts, const HashSet<ValueArg, HashArg, TraitsArg>& set) |
219 | { |
220 | ts << "[" ; |
221 | |
222 | bool first = true; |
223 | for (const auto& item : set) { |
224 | ts << item; |
225 | if (!first) |
226 | ts << ", " ; |
227 | first = false; |
228 | } |
229 | |
230 | return ts << "]" ; |
231 | } |
232 | |
233 | template<typename Option> |
234 | TextStream& operator<<(TextStream& ts, const OptionSet<Option>& options) |
235 | { |
236 | ts << "[" ; |
237 | bool needComma = false; |
238 | for (auto option : options) { |
239 | if (needComma) |
240 | ts << ", " ; |
241 | needComma = true; |
242 | ts << option; |
243 | } |
244 | return ts << "]" ; |
245 | } |
246 | |
247 | // Deprecated. Use TextStream::writeIndent() instead. |
248 | WTF_EXPORT_PRIVATE void writeIndent(TextStream&, int indent); |
249 | |
250 | } // namespace WTF |
251 | |
252 | using WTF::TextStream; |
253 | using WTF::indent; |
254 | |