OLD | NEW |
1 /* | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 * Copyright (C) 2012 Apple Inc. All rights reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
3 * | 3 // found in the LICENSE file. |
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 | 4 |
26 #ifndef PrintStream_h | 5 #include "platform/wtf/PrintStream.h" |
27 #define PrintStream_h | |
28 | 6 |
29 #include "wtf/Allocator.h" | 7 // The contents of this header was moved to platform/wtf as part of |
30 #include "wtf/Compiler.h" | 8 // WTF migration project. See the following post for details: |
31 #include "wtf/Noncopyable.h" | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
32 #include "wtf/StdLibExtras.h" | |
33 #include "wtf/WTFExport.h" | |
34 #include <stdarg.h> | |
35 | |
36 namespace WTF { | |
37 | |
38 class CString; | |
39 class String; | |
40 | |
41 class WTF_EXPORT PrintStream { | |
42 USING_FAST_MALLOC(PrintStream); | |
43 WTF_MAKE_NONCOPYABLE(PrintStream); | |
44 | |
45 public: | |
46 PrintStream(); | |
47 virtual ~PrintStream(); | |
48 | |
49 PRINTF_FORMAT(2, 3) void printf(const char* format, ...); | |
50 PRINTF_FORMAT(2, 0) virtual void vprintf(const char* format, va_list) = 0; | |
51 | |
52 // Typically a no-op for many subclasses of PrintStream, this is a hint that | |
53 // the implementation should flush its buffers if it had not done so already. | |
54 virtual void flush(); | |
55 | |
56 template <typename T> | |
57 void print(const T& value) { | |
58 printInternal(*this, value); | |
59 } | |
60 | |
61 template <typename T1, typename... RemainingTypes> | |
62 void print(const T1& value1, const RemainingTypes&... values) { | |
63 print(value1); | |
64 print(values...); | |
65 } | |
66 }; | |
67 | |
68 WTF_EXPORT void printInternal(PrintStream&, const char*); | |
69 WTF_EXPORT void printInternal(PrintStream&, const CString&); | |
70 WTF_EXPORT void printInternal(PrintStream&, const String&); | |
71 inline void printInternal(PrintStream& out, char* value) { | |
72 printInternal(out, static_cast<const char*>(value)); | |
73 } | |
74 inline void printInternal(PrintStream& out, CString& value) { | |
75 printInternal(out, static_cast<const CString&>(value)); | |
76 } | |
77 inline void printInternal(PrintStream& out, String& value) { | |
78 printInternal(out, static_cast<const String&>(value)); | |
79 } | |
80 WTF_EXPORT void printInternal(PrintStream&, bool); | |
81 WTF_EXPORT void printInternal(PrintStream&, int); | |
82 WTF_EXPORT void printInternal(PrintStream&, unsigned); | |
83 WTF_EXPORT void printInternal(PrintStream&, long); | |
84 WTF_EXPORT void printInternal(PrintStream&, unsigned long); | |
85 WTF_EXPORT void printInternal(PrintStream&, long long); | |
86 WTF_EXPORT void printInternal(PrintStream&, unsigned long long); | |
87 WTF_EXPORT void printInternal(PrintStream&, float); | |
88 WTF_EXPORT void printInternal(PrintStream&, double); | |
89 | |
90 template <typename T> | |
91 void printInternal(PrintStream& out, const T& value) { | |
92 value.dump(out); | |
93 } | |
94 | |
95 #define MAKE_PRINT_ADAPTOR(Name, Type, function) \ | |
96 class Name final { \ | |
97 STACK_ALLOCATED(); \ | |
98 \ | |
99 public: \ | |
100 Name(const Type& value) : m_value(value) {} \ | |
101 void dump(PrintStream& out) const { function(out, m_value); } \ | |
102 \ | |
103 private: \ | |
104 Type m_value; \ | |
105 } | |
106 | |
107 #define MAKE_PRINT_METHOD_ADAPTOR(Name, Type, method) \ | |
108 class Name final { \ | |
109 STACK_ALLOCATED(); \ | |
110 \ | |
111 public: \ | |
112 Name(const Type& value) : m_value(value) {} \ | |
113 void dump(PrintStream& out) const { m_value.method(out); } \ | |
114 \ | |
115 private: \ | |
116 const Type& m_value; \ | |
117 } | |
118 | |
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); } | |
122 | |
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); | |
128 | |
129 } // namespace WTF | |
130 | |
131 using WTF::CharacterDump; | |
132 using WTF::PrintStream; | |
133 | |
134 #endif // PrintStream_h | |
OLD | NEW |