Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / PrintStream.h
blob1386d414eb6d6b3fab573b4316702206a0e994c6
1 /*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
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.
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.
26 #ifndef PrintStream_h
27 #define PrintStream_h
29 #include <stdarg.h>
30 #include "wtf/FastAllocBase.h"
31 #include "wtf/Noncopyable.h"
32 #include "wtf/StdLibExtras.h"
33 #include "wtf/WTFExport.h"
35 namespace WTF {
37 class CString;
38 class String;
40 class WTF_EXPORT PrintStream {
41 WTF_MAKE_FAST_ALLOCATED(PrintStream); WTF_MAKE_NONCOPYABLE(PrintStream);
42 public:
43 PrintStream();
44 virtual ~PrintStream();
46 void printf(const char* format, ...) WTF_ATTRIBUTE_PRINTF(2, 3);
47 virtual void vprintf(const char* format, va_list) WTF_ATTRIBUTE_PRINTF(2, 0) = 0;
49 // Typically a no-op for many subclasses of PrintStream, this is a hint that
50 // the implementation should flush its buffers if it had not done so already.
51 virtual void flush();
53 template<typename T>
54 void print(const T& value)
56 printInternal(*this, value);
59 template<typename T1, typename... RemainingTypes>
60 void print(const T1& value1, const RemainingTypes&... values)
62 print(value1);
63 print(values...);
67 WTF_EXPORT void printInternal(PrintStream&, const char*);
68 WTF_EXPORT void printInternal(PrintStream&, const CString&);
69 WTF_EXPORT void printInternal(PrintStream&, const String&);
70 inline void printInternal(PrintStream& out, char* value) { printInternal(out, static_cast<const char*>(value)); }
71 inline void printInternal(PrintStream& out, CString& value) { printInternal(out, static_cast<const CString&>(value)); }
72 inline void printInternal(PrintStream& out, String& value) { printInternal(out, static_cast<const String&>(value)); }
73 WTF_EXPORT void printInternal(PrintStream&, bool);
74 WTF_EXPORT void printInternal(PrintStream&, int);
75 WTF_EXPORT void printInternal(PrintStream&, unsigned);
76 WTF_EXPORT void printInternal(PrintStream&, long);
77 WTF_EXPORT void printInternal(PrintStream&, unsigned long);
78 WTF_EXPORT void printInternal(PrintStream&, long long);
79 WTF_EXPORT void printInternal(PrintStream&, unsigned long long);
80 WTF_EXPORT void printInternal(PrintStream&, float);
81 WTF_EXPORT void printInternal(PrintStream&, double);
83 template<typename T>
84 void printInternal(PrintStream& out, const T& value)
86 value.dump(out);
89 #define MAKE_PRINT_ADAPTOR(Name, Type, function) \
90 class Name { \
91 public: \
92 Name(const Type& value) \
93 : m_value(value) \
94 { \
95 } \
96 void dump(PrintStream& out) const \
97 { \
98 function(out, m_value); \
99 } \
100 private: \
101 Type m_value; \
104 #define MAKE_PRINT_METHOD_ADAPTOR(Name, Type, method) \
105 class Name { \
106 public: \
107 Name(const Type& value) \
108 : m_value(value) \
111 void dump(PrintStream& out) const \
113 m_value.method(out); \
115 private: \
116 const Type& m_value; \
119 #define MAKE_PRINT_METHOD(Type, dumpMethod, method) \
120 MAKE_PRINT_METHOD_ADAPTOR(DumperFor_##method, Type, dumpMethod); \
121 DumperFor_##method method() const { return DumperFor_##method(*this); }
123 // Use an adaptor-based dumper for characters to avoid situations where
124 // you've "compressed" an integer to a character and it ends up printing
125 // as ASCII when you wanted it to print as a number.
126 void dumpCharacter(PrintStream&, char);
127 MAKE_PRINT_ADAPTOR(CharacterDump, char, dumpCharacter);
129 template<typename T>
130 class PointerDump {
131 public:
132 PointerDump(const T* ptr)
133 : m_ptr(ptr)
137 void dump(PrintStream& out) const
139 if (m_ptr)
140 printInternal(out, *m_ptr);
141 else
142 out.print("(null)");
144 private:
145 const T* m_ptr;
148 template<typename T>
149 PointerDump<T> pointerDump(const T* ptr) { return PointerDump<T>(ptr); }
151 } // namespace WTF
153 using WTF::CharacterDump;
154 using WTF::PointerDump;
155 using WTF::PrintStream;
156 using WTF::pointerDump;
158 #endif // PrintStream_h