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
31namespace WTF {
32
33class TextStream {
34public:
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 increaseIndent(int amount = 1) { m_indent += amount; }
99 void decreaseIndent(int amount = 1) { m_indent -= amount; ASSERT(m_indent >= 0); }
100
101 WTF_EXPORT_PRIVATE void writeIndent();
102
103 // Stream manipulators.
104 TextStream& operator<<(TextStream& (*func)(TextStream&))
105 {
106 return (*func)(*this);
107 }
108
109 struct Repeat {
110 Repeat(unsigned inWidth, char inCharacter)
111 : width(inWidth), character(inCharacter)
112 { }
113 unsigned width { 0 };
114 char character { ' ' };
115 };
116
117 TextStream& operator<<(const Repeat& repeated)
118 {
119 for (unsigned i = 0; i < repeated.width; ++i)
120 m_text.append(repeated.character);
121
122 return *this;
123 }
124
125 class IndentScope {
126 public:
127 IndentScope(TextStream& ts, int amount = 1)
128 : m_stream(ts)
129 , m_amount(amount)
130 {
131 m_stream.increaseIndent(m_amount);
132 }
133 ~IndentScope()
134 {
135 m_stream.decreaseIndent(m_amount);
136 }
137
138 private:
139 TextStream& m_stream;
140 int m_amount;
141 };
142
143 class GroupScope {
144 public:
145 GroupScope(TextStream& ts)
146 : m_stream(ts)
147 {
148 m_stream.startGroup();
149 }
150 ~GroupScope()
151 {
152 m_stream.endGroup();
153 }
154
155 private:
156 TextStream& m_stream;
157 };
158
159private:
160 StringBuilder m_text;
161 FormattingFlags m_formattingFlags { 0 };
162 int m_indent { 0 };
163 bool m_multiLineMode { true };
164};
165
166inline TextStream& indent(TextStream& ts)
167{
168 ts.writeIndent();
169 return ts;
170}
171
172template<typename Item>
173TextStream& operator<<(TextStream& ts, const Vector<Item>& vector)
174{
175 ts << "[";
176
177 unsigned size = vector.size();
178 for (unsigned i = 0; i < size; ++i) {
179 ts << vector[i];
180 if (i < size - 1)
181 ts << ", ";
182 }
183
184 return ts << "]";
185}
186
187template<typename Option>
188TextStream& operator<<(TextStream& ts, const OptionSet<Option>& options)
189{
190 ts << "[";
191 bool needComma = false;
192 for (auto option : options) {
193 if (needComma)
194 ts << ", ";
195 needComma = true;
196 ts << option;
197 }
198 return ts << "]";
199}
200
201// Deprecated. Use TextStream::writeIndent() instead.
202WTF_EXPORT_PRIVATE void writeIndent(TextStream&, int indent);
203
204} // namespace WTF
205
206using WTF::TextStream;
207using WTF::indent;
208