1/*
2 * Copyright (C) 2012, 2014, 2015 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#ifndef PrintStream_h
27#define PrintStream_h
28
29#include <memory>
30#include <stdarg.h>
31#include <wtf/FastMalloc.h>
32#include <wtf/Noncopyable.h>
33#include <wtf/RawPointer.h>
34#include <wtf/RefPtr.h>
35#include <wtf/StdLibExtras.h>
36
37namespace WTF {
38
39class AtomicStringImpl;
40class CString;
41class String;
42class StringImpl;
43class StringView;
44class UniquedStringImpl;
45
46class PrintStream {
47 WTF_MAKE_FAST_ALLOCATED; WTF_MAKE_NONCOPYABLE(PrintStream);
48public:
49 PrintStream();
50 virtual ~PrintStream();
51
52 WTF_EXPORT_PRIVATE void printf(const char* format, ...) WTF_ATTRIBUTE_PRINTF(2, 3);
53 virtual void vprintf(const char* format, va_list) WTF_ATTRIBUTE_PRINTF(2, 0) = 0;
54
55 // Typically a no-op for many subclasses of PrintStream, this is a hint that
56 // the implementation should flush its buffers if it had not done so already.
57 virtual void flush();
58
59 template<typename T>
60 void print(const T& value)
61 {
62 printInternal(*this, value);
63 }
64
65 template<typename T, typename... Types>
66 void print(const T& value, const Types&... remainingValues)
67 {
68 printInternal(*this, value);
69 print(remainingValues...);
70 }
71};
72
73WTF_EXPORT_PRIVATE void printInternal(PrintStream&, const char*);
74WTF_EXPORT_PRIVATE void printInternal(PrintStream&, const StringView&);
75WTF_EXPORT_PRIVATE void printInternal(PrintStream&, const CString&);
76WTF_EXPORT_PRIVATE void printInternal(PrintStream&, const String&);
77WTF_EXPORT_PRIVATE void printInternal(PrintStream&, const StringImpl*);
78inline void printInternal(PrintStream& out, const AtomicStringImpl* value) { printInternal(out, bitwise_cast<const StringImpl*>(value)); }
79inline void printInternal(PrintStream& out, const UniquedStringImpl* value) { printInternal(out, bitwise_cast<const StringImpl*>(value)); }
80inline void printInternal(PrintStream& out, const UniquedStringImpl& value) { printInternal(out, &value); }
81inline void printInternal(PrintStream& out, char* value) { printInternal(out, static_cast<const char*>(value)); }
82inline void printInternal(PrintStream& out, CString& value) { printInternal(out, static_cast<const CString&>(value)); }
83inline void printInternal(PrintStream& out, String& value) { printInternal(out, static_cast<const String&>(value)); }
84inline void printInternal(PrintStream& out, StringImpl* value) { printInternal(out, static_cast<const StringImpl*>(value)); }
85inline void printInternal(PrintStream& out, AtomicStringImpl* value) { printInternal(out, static_cast<const AtomicStringImpl*>(value)); }
86inline void printInternal(PrintStream& out, UniquedStringImpl* value) { printInternal(out, static_cast<const UniquedStringImpl*>(value)); }
87inline void printInternal(PrintStream& out, UniquedStringImpl& value) { printInternal(out, &value); }
88WTF_EXPORT_PRIVATE void printInternal(PrintStream&, bool);
89WTF_EXPORT_PRIVATE void printInternal(PrintStream&, signed char); // NOTE: this prints as a number, not as a character; use CharacterDump if you want the character
90WTF_EXPORT_PRIVATE void printInternal(PrintStream&, unsigned char); // NOTE: see above.
91WTF_EXPORT_PRIVATE void printInternal(PrintStream&, short);
92WTF_EXPORT_PRIVATE void printInternal(PrintStream&, unsigned short);
93WTF_EXPORT_PRIVATE void printInternal(PrintStream&, int);
94WTF_EXPORT_PRIVATE void printInternal(PrintStream&, unsigned);
95WTF_EXPORT_PRIVATE void printInternal(PrintStream&, long);
96WTF_EXPORT_PRIVATE void printInternal(PrintStream&, unsigned long);
97WTF_EXPORT_PRIVATE void printInternal(PrintStream&, long long);
98WTF_EXPORT_PRIVATE void printInternal(PrintStream&, unsigned long long);
99WTF_EXPORT_PRIVATE void printInternal(PrintStream&, float);
100WTF_EXPORT_PRIVATE void printInternal(PrintStream&, double);
101WTF_EXPORT_PRIVATE void printInternal(PrintStream&, RawPointer);
102
103template<typename T>
104void printInternal(PrintStream& out, const T& value)
105{
106 value.dump(out);
107}
108
109#define MAKE_PRINT_ADAPTOR(Name, Type, function) \
110 class Name { \
111 public: \
112 Name(const Type& value) \
113 : m_value(value) \
114 { \
115 } \
116 void dump(PrintStream& out) const \
117 { \
118 function(out, m_value); \
119 } \
120 private: \
121 Type m_value; \
122 }
123
124#define MAKE_PRINT_METHOD_ADAPTOR(Name, Type, method) \
125 class Name { \
126 public: \
127 Name(const Type& value) \
128 : m_value(value) \
129 { \
130 } \
131 void dump(PrintStream& out) const \
132 { \
133 m_value.method(out); \
134 } \
135 private: \
136 const Type& m_value; \
137 }
138
139#define MAKE_PRINT_METHOD(Type, dumpMethod, method) \
140 MAKE_PRINT_METHOD_ADAPTOR(DumperFor_##method, Type, dumpMethod); \
141 DumperFor_##method method() const { return DumperFor_##method(*this); }
142
143// Use an adaptor-based dumper for characters to avoid situations where
144// you've "compressed" an integer to a character and it ends up printing
145// as ASCII when you wanted it to print as a number.
146WTF_EXPORT_PRIVATE void dumpCharacter(PrintStream&, char);
147MAKE_PRINT_ADAPTOR(CharacterDump, char, dumpCharacter);
148
149template<typename T>
150class PointerDump {
151public:
152 PointerDump(const T* ptr)
153 : m_ptr(ptr)
154 {
155 }
156
157 void dump(PrintStream& out) const
158 {
159 if (m_ptr)
160 printInternal(out, *m_ptr);
161 else
162 out.print("(null)");
163 }
164private:
165 const T* m_ptr;
166};
167
168template<typename T>
169PointerDump<T> pointerDump(const T* ptr) { return PointerDump<T>(ptr); }
170
171template<typename T>
172void printInternal(PrintStream& out, const std::unique_ptr<T>& value)
173{
174 out.print(pointerDump(value.get()));
175}
176
177template<typename T>
178void printInternal(PrintStream& out, const RefPtr<T>& value)
179{
180 out.print(pointerDump(value.get()));
181}
182
183template<typename T, typename U>
184class ValueInContext {
185public:
186 ValueInContext(const T& value, U* context)
187 : m_value(&value)
188 , m_context(context)
189 {
190 }
191
192 void dump(PrintStream& out) const
193 {
194 m_value->dumpInContext(out, m_context);
195 }
196
197private:
198 const T* m_value;
199 U* m_context;
200};
201
202template<typename T, typename U>
203ValueInContext<T, U> inContext(const T& value, U* context)
204{
205 return ValueInContext<T, U>(value, context);
206}
207
208template<typename T, typename U>
209class PointerDumpInContext {
210public:
211 PointerDumpInContext(const T* ptr, U* context)
212 : m_ptr(ptr)
213 , m_context(context)
214 {
215 }
216
217 void dump(PrintStream& out) const
218 {
219 if (m_ptr)
220 m_ptr->dumpInContext(out, m_context);
221 else
222 out.print("(null)");
223 }
224
225private:
226 const T* m_ptr;
227 U* m_context;
228};
229
230template<typename T, typename U>
231PointerDumpInContext<T, U> pointerDumpInContext(const T* ptr, U* context)
232{
233 return PointerDumpInContext<T, U>(ptr, context);
234}
235
236template<typename T, typename U>
237class ValueIgnoringContext {
238public:
239 ValueIgnoringContext(const U& value)
240 : m_value(&value)
241 {
242 }
243
244 void dump(PrintStream& out) const
245 {
246 T context;
247 m_value->dumpInContext(out, &context);
248 }
249
250private:
251 const U* m_value;
252};
253
254template<typename T, typename U>
255ValueIgnoringContext<T, U> ignoringContext(const U& value)
256{
257 return ValueIgnoringContext<T, U>(value);
258}
259
260} // namespace WTF
261
262using WTF::CharacterDump;
263using WTF::PointerDump;
264using WTF::PrintStream;
265using WTF::ignoringContext;
266using WTF::inContext;
267using WTF::pointerDump;
268using WTF::pointerDumpInContext;
269
270#endif // PrintStream_h
271
272