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
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.
30 #include "wtf/FastAllocBase.h"
31 #include "wtf/Noncopyable.h"
32 #include "wtf/StdLibExtras.h"
33 #include "wtf/WTFExport.h"
40 class WTF_EXPORT PrintStream
{
41 WTF_MAKE_FAST_ALLOCATED(PrintStream
); WTF_MAKE_NONCOPYABLE(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.
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
)
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);
84 void printInternal(PrintStream
& out
, const T
& value
)
89 #define MAKE_PRINT_ADAPTOR(Name, Type, function) \
92 Name(const Type& value) \
96 void dump(PrintStream& out) const \
98 function(out, m_value); \
104 #define MAKE_PRINT_METHOD_ADAPTOR(Name, Type, method) \
107 Name(const Type& value) \
111 void dump(PrintStream& out) const \
113 m_value.method(out); \
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
);
132 PointerDump(const T
* ptr
)
137 void dump(PrintStream
& out
) const
140 printInternal(out
, *m_ptr
);
149 PointerDump
<T
> pointerDump(const T
* ptr
) { return PointerDump
<T
>(ptr
); }
153 using WTF::CharacterDump
;
154 using WTF::PointerDump
;
155 using WTF::PrintStream
;
156 using WTF::pointerDump
;
158 #endif // PrintStream_h