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 | public: |
35 | struct FormatNumberRespectingIntegers { |
36 | FormatNumberRespectingIntegers(double number) |
37 | : value(number) { } |
38 | |
39 | double value; |
40 | }; |
41 | |
42 | enum Formatting { |
43 | SVGStyleRect = 1 << 0, // "at (0,0) size 10x10" |
44 | NumberRespectingIntegers = 1 << 1, |
45 | LayoutUnitsAsIntegers = 1 << 2, |
46 | }; |
47 | |
48 | using FormattingFlags = unsigned; |
49 | |
50 | enum class LineMode { SingleLine, MultipleLine }; |
51 | TextStream(LineMode lineMode = LineMode::MultipleLine, FormattingFlags formattingFlags = 0) |
52 | : m_formattingFlags(formattingFlags) |
53 | , m_multiLineMode(lineMode == LineMode::MultipleLine) |
54 | { |
55 | } |
56 | |
57 | WTF_EXPORT_PRIVATE TextStream& operator<<(bool); |
58 | WTF_EXPORT_PRIVATE TextStream& operator<<(int); |
59 | WTF_EXPORT_PRIVATE TextStream& operator<<(unsigned); |
60 | WTF_EXPORT_PRIVATE TextStream& operator<<(long); |
61 | WTF_EXPORT_PRIVATE TextStream& operator<<(unsigned long); |
62 | WTF_EXPORT_PRIVATE TextStream& operator<<(long long); |
63 | |
64 | WTF_EXPORT_PRIVATE TextStream& operator<<(unsigned long long); |
65 | WTF_EXPORT_PRIVATE TextStream& operator<<(float); |
66 | WTF_EXPORT_PRIVATE TextStream& operator<<(double); |
67 | WTF_EXPORT_PRIVATE TextStream& operator<<(const char*); |
68 | WTF_EXPORT_PRIVATE TextStream& operator<<(const void*); |
69 | WTF_EXPORT_PRIVATE TextStream& operator<<(const String&); |
70 | // Deprecated. Use the NumberRespectingIntegers FormattingFlag instead. |
71 | WTF_EXPORT_PRIVATE TextStream& operator<<(const FormatNumberRespectingIntegers&); |
72 | |
73 | #ifdef __OBJC__ |
74 | WTF_EXPORT_PRIVATE TextStream& operator<<(id<NSObject>); |
75 | #endif |
76 | |
77 | FormattingFlags formattingFlags() const { return m_formattingFlags; } |
78 | void setFormattingFlags(FormattingFlags flags) { m_formattingFlags = flags; } |
79 | |
80 | bool hasFormattingFlag(Formatting flag) const { return m_formattingFlags & flag; } |
81 | |
82 | template<typename T> |
83 | void dumpProperty(const String& name, const T& value) |
84 | { |
85 | TextStream& ts = *this; |
86 | ts.startGroup(); |
87 | ts << name << " " << value; |
88 | ts.endGroup(); |
89 | } |
90 | |
91 | WTF_EXPORT_PRIVATE String release(); |
92 | |
93 | WTF_EXPORT_PRIVATE void startGroup(); |
94 | WTF_EXPORT_PRIVATE void endGroup(); |
95 | WTF_EXPORT_PRIVATE void nextLine(); // Output newline and indent. |
96 | |
97 | int indent() const { return m_indent; } |
98 | void setIndent(int indent) { m_indent = indent; } |
99 | void increaseIndent(int amount = 1) { m_indent += amount; } |
100 | void decreaseIndent(int amount = 1) { m_indent -= amount; ASSERT(m_indent >= 0); } |
101 | |
102 | WTF_EXPORT_PRIVATE void writeIndent(); |
103 | |
104 | // Stream manipulators. |
105 | TextStream& operator<<(TextStream& (*func)(TextStream&)) |
106 | { |
107 | return (*func)(*this); |
108 | } |
109 | |
110 | struct Repeat { |
111 | Repeat(unsigned inWidth, char inCharacter) |
112 | : width(inWidth), character(inCharacter) |
113 | { } |
114 | unsigned width { 0 }; |
115 | char character { ' ' }; |
116 | }; |
117 | |
118 | TextStream& operator<<(const Repeat& repeated) |
119 | { |
120 | for (unsigned i = 0; i < repeated.width; ++i) |
121 | m_text.append(repeated.character); |
122 | |
123 | return *this; |
124 | } |
125 | |
126 | class IndentScope { |
127 | public: |
128 | IndentScope(TextStream& ts, int amount = 1) |
129 | : m_stream(ts) |
130 | , m_amount(amount) |
131 | { |
132 | m_stream.increaseIndent(m_amount); |
133 | } |
134 | ~IndentScope() |
135 | { |
136 | m_stream.decreaseIndent(m_amount); |
137 | } |
138 | |
139 | private: |
140 | TextStream& m_stream; |
141 | int m_amount; |
142 | }; |
143 | |
144 | class GroupScope { |
145 | public: |
146 | GroupScope(TextStream& ts) |
147 | : m_stream(ts) |
148 | { |
149 | m_stream.startGroup(); |
150 | } |
151 | ~GroupScope() |
152 | { |
153 | m_stream.endGroup(); |
154 | } |
155 | |
156 | private: |
157 | TextStream& m_stream; |
158 | }; |
159 | |
160 | private: |
161 | StringBuilder m_text; |
162 | FormattingFlags m_formattingFlags { 0 }; |
163 | int m_indent { 0 }; |
164 | bool m_multiLineMode { true }; |
165 | }; |
166 | |
167 | inline TextStream& indent(TextStream& ts) |
168 | { |
169 | ts.writeIndent(); |
170 | return ts; |
171 | } |
172 | |
173 | template<typename Item> |
174 | TextStream& operator<<(TextStream& ts, const Vector<Item>& vector) |
175 | { |
176 | ts << "[" ; |
177 | |
178 | unsigned size = vector.size(); |
179 | for (unsigned i = 0; i < size; ++i) { |
180 | ts << vector[i]; |
181 | if (i < size - 1) |
182 | ts << ", " ; |
183 | } |
184 | |
185 | return ts << "]" ; |
186 | } |
187 | |
188 | template<typename Option> |
189 | TextStream& operator<<(TextStream& ts, const OptionSet<Option>& options) |
190 | { |
191 | ts << "[" ; |
192 | bool needComma = false; |
193 | for (auto option : options) { |
194 | if (needComma) |
195 | ts << ", " ; |
196 | needComma = true; |
197 | ts << option; |
198 | } |
199 | return ts << "]" ; |
200 | } |
201 | |
202 | // Deprecated. Use TextStream::writeIndent() instead. |
203 | WTF_EXPORT_PRIVATE void writeIndent(TextStream&, int indent); |
204 | |
205 | } // namespace WTF |
206 | |
207 | using WTF::TextStream; |
208 | using WTF::indent; |
209 | |