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 FormattingFlags formattingFlags() const { return m_formattingFlags; }
74 void setFormattingFlags(FormattingFlags flags) { m_formattingFlags = flags; }
75
76 bool hasFormattingFlag(Formatting flag) const { return m_formattingFlags & flag; }
77
78 template<typename T>
79 void dumpProperty(const String& name, const T& value)
80 {
81 TextStream& ts = *this;
82 ts.startGroup();
83 ts << name << " " << value;
84 ts.endGroup();
85 }
86
87 WTF_EXPORT_PRIVATE String release();
88
89 WTF_EXPORT_PRIVATE void startGroup();
90 WTF_EXPORT_PRIVATE void endGroup();
91 WTF_EXPORT_PRIVATE void nextLine(); // Output newline and indent.
92
93 int indent() const { return m_indent; }
94 void increaseIndent(int amount = 1) { m_indent += amount; }
95 void decreaseIndent(int amount = 1) { m_indent -= amount; ASSERT(m_indent >= 0); }
96
97 WTF_EXPORT_PRIVATE void writeIndent();
98
99 // Stream manipulators.
100 TextStream& operator<<(TextStream& (*func)(TextStream&))
101 {
102 return (*func)(*this);
103 }
104
105 struct Repeat {
106 Repeat(unsigned inWidth, char inCharacter)
107 : width(inWidth), character(inCharacter)
108 { }
109 unsigned width { 0 };
110 char character { ' ' };
111 };
112
113 TextStream& operator<<(const Repeat& repeated)
114 {
115 for (unsigned i = 0; i < repeated.width; ++i)
116 m_text.append(repeated.character);
117
118 return *this;
119 }
120
121 class IndentScope {
122 public:
123 IndentScope(TextStream& ts, int amount = 1)
124 : m_stream(ts)
125 , m_amount(amount)
126 {
127 m_stream.increaseIndent(m_amount);
128 }
129 ~IndentScope()
130 {
131 m_stream.decreaseIndent(m_amount);
132 }
133
134 private:
135 TextStream& m_stream;
136 int m_amount;
137 };
138
139 class GroupScope {
140 public:
141 GroupScope(TextStream& ts)
142 : m_stream(ts)
143 {
144 m_stream.startGroup();
145 }
146 ~GroupScope()
147 {
148 m_stream.endGroup();
149 }
150
151 private:
152 TextStream& m_stream;
153 };
154
155private:
156 StringBuilder m_text;
157 FormattingFlags m_formattingFlags { 0 };
158 int m_indent { 0 };
159 bool m_multiLineMode { true };
160};
161
162inline TextStream& indent(TextStream& ts)
163{
164 ts.writeIndent();
165 return ts;
166}
167
168template<typename Item>
169TextStream& operator<<(TextStream& ts, const Vector<Item>& vector)
170{
171 ts << "[";
172
173 unsigned size = vector.size();
174 for (unsigned i = 0; i < size; ++i) {
175 ts << vector[i];
176 if (i < size - 1)
177 ts << ", ";
178 }
179
180 return ts << "]";
181}
182
183// Deprecated. Use TextStream::writeIndent() instead.
184WTF_EXPORT_PRIVATE void writeIndent(TextStream&, int indent);
185
186} // namespace WTF
187
188using WTF::TextStream;
189using WTF::indent;
190