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 class IndentScope {
106 public:
107 IndentScope(TextStream& ts, int amount = 1)
108 : m_stream(ts)
109 , m_amount(amount)
110 {
111 m_stream.increaseIndent(m_amount);
112 }
113 ~IndentScope()
114 {
115 m_stream.decreaseIndent(m_amount);
116 }
117
118 private:
119 TextStream& m_stream;
120 int m_amount;
121 };
122
123 class GroupScope {
124 public:
125 GroupScope(TextStream& ts)
126 : m_stream(ts)
127 {
128 m_stream.startGroup();
129 }
130 ~GroupScope()
131 {
132 m_stream.endGroup();
133 }
134
135 private:
136 TextStream& m_stream;
137 };
138
139private:
140 StringBuilder m_text;
141 FormattingFlags m_formattingFlags { 0 };
142 int m_indent { 0 };
143 bool m_multiLineMode { true };
144};
145
146inline TextStream& indent(TextStream& ts)
147{
148 ts.writeIndent();
149 return ts;
150}
151
152template<typename Item>
153TextStream& operator<<(TextStream& ts, const Vector<Item>& vector)
154{
155 ts << "[";
156
157 unsigned size = vector.size();
158 for (unsigned i = 0; i < size; ++i) {
159 ts << vector[i];
160 if (i < size - 1)
161 ts << ", ";
162 }
163
164 return ts << "]";
165}
166
167// Deprecated. Use TextStream::writeIndent() instead.
168WTF_EXPORT_PRIVATE void writeIndent(TextStream&, int indent);
169
170} // namespace WTF
171
172using WTF::TextStream;
173using WTF::indent;
174