| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Apple Inc. All rights reserved. | 2 * Copyright (C) 2012 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 #include "wtf/StdLibExtras.h" | 31 #include "wtf/StdLibExtras.h" |
| 32 #include "wtf/WTFExport.h" | 32 #include "wtf/WTFExport.h" |
| 33 #include <stdarg.h> | 33 #include <stdarg.h> |
| 34 | 34 |
| 35 namespace WTF { | 35 namespace WTF { |
| 36 | 36 |
| 37 class CString; | 37 class CString; |
| 38 class String; | 38 class String; |
| 39 | 39 |
| 40 class WTF_EXPORT PrintStream { | 40 class WTF_EXPORT PrintStream { |
| 41 USING_FAST_MALLOC(PrintStream); WTF_MAKE_NONCOPYABLE(PrintStream); | 41 USING_FAST_MALLOC(PrintStream); |
| 42 public: | 42 WTF_MAKE_NONCOPYABLE(PrintStream); |
| 43 PrintStream(); | |
| 44 virtual ~PrintStream(); | |
| 45 | 43 |
| 46 void printf(const char* format, ...) WTF_ATTRIBUTE_PRINTF(2, 3); | 44 public: |
| 47 virtual void vprintf(const char* format, va_list) WTF_ATTRIBUTE_PRINTF(2, 0)
= 0; | 45 PrintStream(); |
| 46 virtual ~PrintStream(); |
| 48 | 47 |
| 49 // Typically a no-op for many subclasses of PrintStream, this is a hint that | 48 void printf(const char* format, ...) WTF_ATTRIBUTE_PRINTF(2, 3); |
| 50 // the implementation should flush its buffers if it had not done so already
. | 49 virtual void vprintf(const char* format, va_list) WTF_ATTRIBUTE_PRINTF(2, 0) =
0; |
| 51 virtual void flush(); | |
| 52 | 50 |
| 53 template <typename T> | 51 // Typically a no-op for many subclasses of PrintStream, this is a hint that |
| 54 void print(const T& value) | 52 // the implementation should flush its buffers if it had not done so already. |
| 55 { | 53 virtual void flush(); |
| 56 printInternal(*this, value); | |
| 57 } | |
| 58 | 54 |
| 59 template <typename T1, typename... RemainingTypes> | 55 template <typename T> |
| 60 void print(const T1& value1, const RemainingTypes&... values) | 56 void print(const T& value) { |
| 61 { | 57 printInternal(*this, value); |
| 62 print(value1); | 58 } |
| 63 print(values...); | 59 |
| 64 } | 60 template <typename T1, typename... RemainingTypes> |
| 61 void print(const T1& value1, const RemainingTypes&... values) { |
| 62 print(value1); |
| 63 print(values...); |
| 64 } |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 WTF_EXPORT void printInternal(PrintStream&, const char*); | 67 WTF_EXPORT void printInternal(PrintStream&, const char*); |
| 68 WTF_EXPORT void printInternal(PrintStream&, const CString&); | 68 WTF_EXPORT void printInternal(PrintStream&, const CString&); |
| 69 WTF_EXPORT void printInternal(PrintStream&, const String&); | 69 WTF_EXPORT void printInternal(PrintStream&, const String&); |
| 70 inline void printInternal(PrintStream& out, char* value) { printInternal(out, st
atic_cast<const char*>(value)); } | 70 inline void printInternal(PrintStream& out, char* value) { |
| 71 inline void printInternal(PrintStream& out, CString& value) { printInternal(out,
static_cast<const CString&>(value)); } | 71 printInternal(out, static_cast<const char*>(value)); |
| 72 inline void printInternal(PrintStream& out, String& value) { printInternal(out,
static_cast<const String&>(value)); } | 72 } |
| 73 inline void printInternal(PrintStream& out, CString& value) { |
| 74 printInternal(out, static_cast<const CString&>(value)); |
| 75 } |
| 76 inline void printInternal(PrintStream& out, String& value) { |
| 77 printInternal(out, static_cast<const String&>(value)); |
| 78 } |
| 73 WTF_EXPORT void printInternal(PrintStream&, bool); | 79 WTF_EXPORT void printInternal(PrintStream&, bool); |
| 74 WTF_EXPORT void printInternal(PrintStream&, int); | 80 WTF_EXPORT void printInternal(PrintStream&, int); |
| 75 WTF_EXPORT void printInternal(PrintStream&, unsigned); | 81 WTF_EXPORT void printInternal(PrintStream&, unsigned); |
| 76 WTF_EXPORT void printInternal(PrintStream&, long); | 82 WTF_EXPORT void printInternal(PrintStream&, long); |
| 77 WTF_EXPORT void printInternal(PrintStream&, unsigned long); | 83 WTF_EXPORT void printInternal(PrintStream&, unsigned long); |
| 78 WTF_EXPORT void printInternal(PrintStream&, long long); | 84 WTF_EXPORT void printInternal(PrintStream&, long long); |
| 79 WTF_EXPORT void printInternal(PrintStream&, unsigned long long); | 85 WTF_EXPORT void printInternal(PrintStream&, unsigned long long); |
| 80 WTF_EXPORT void printInternal(PrintStream&, float); | 86 WTF_EXPORT void printInternal(PrintStream&, float); |
| 81 WTF_EXPORT void printInternal(PrintStream&, double); | 87 WTF_EXPORT void printInternal(PrintStream&, double); |
| 82 | 88 |
| 83 template <typename T> | 89 template <typename T> |
| 84 void printInternal(PrintStream& out, const T& value) | 90 void printInternal(PrintStream& out, const T& value) { |
| 85 { | 91 value.dump(out); |
| 86 value.dump(out); | |
| 87 } | 92 } |
| 88 | 93 |
| 89 #define MAKE_PRINT_ADAPTOR(Name, Type, function) \ | 94 #define MAKE_PRINT_ADAPTOR(Name, Type, function) \ |
| 90 class Name { \ | 95 class Name { \ |
| 91 public: \ | 96 public: \ |
| 92 Name(const Type& value) \ | 97 Name(const Type& value) \ |
| 93 : m_value(value) \ | 98 : m_value(value) { \ |
| 94 { \ | 99 } \ |
| 95 } \ | 100 void dump(PrintStream& out) const { \ |
| 96 void dump(PrintStream& out) const \ | 101 function(out, m_value); \ |
| 97 { \ | 102 } \ |
| 98 function(out, m_value); \ | 103 \ |
| 99 } \ | 104 private: \ |
| 100 private: \ | 105 Type m_value; \ |
| 101 Type m_value; \ | 106 } |
| 102 } | |
| 103 | 107 |
| 104 #define MAKE_PRINT_METHOD_ADAPTOR(Name, Type, method) \ | 108 #define MAKE_PRINT_METHOD_ADAPTOR(Name, Type, method) \ |
| 105 class Name { \ | 109 class Name { \ |
| 106 public: \ | 110 public: \ |
| 107 Name(const Type& value) \ | 111 Name(const Type& value) \ |
| 108 : m_value(value) \ | 112 : m_value(value) { \ |
| 109 { \ | 113 } \ |
| 110 } \ | 114 void dump(PrintStream& out) const { \ |
| 111 void dump(PrintStream& out) const \ | 115 m_value.method(out); \ |
| 112 { \ | 116 } \ |
| 113 m_value.method(out); \ | 117 \ |
| 114 } \ | 118 private: \ |
| 115 private: \ | 119 const Type& m_value; \ |
| 116 const Type& m_value; \ | 120 } |
| 117 } | |
| 118 | 121 |
| 119 #define MAKE_PRINT_METHOD(Type, dumpMethod, method) \ | 122 #define MAKE_PRINT_METHOD(Type, dumpMethod, method) \ |
| 120 MAKE_PRINT_METHOD_ADAPTOR(DumperFor_##method, Type, dumpMethod); \ | 123 MAKE_PRINT_METHOD_ADAPTOR(DumperFor_##method, Type, dumpMethod); \ |
| 121 DumperFor_##method method() const { return DumperFor_##method(*this); } | 124 DumperFor_##method method() const { return DumperFor_##method(*this); } |
| 122 | 125 |
| 123 // Use an adaptor-based dumper for characters to avoid situations where | 126 // 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 | 127 // 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. | 128 // as ASCII when you wanted it to print as a number. |
| 126 void dumpCharacter(PrintStream&, char); | 129 void dumpCharacter(PrintStream&, char); |
| 127 MAKE_PRINT_ADAPTOR(CharacterDump, char, dumpCharacter); | 130 MAKE_PRINT_ADAPTOR(CharacterDump, char, dumpCharacter); |
| 128 | 131 |
| 129 template <typename T> | 132 template <typename T> |
| 130 class PointerDump { | 133 class PointerDump { |
| 131 public: | 134 public: |
| 132 PointerDump(const T* ptr) | 135 PointerDump(const T* ptr) |
| 133 : m_ptr(ptr) | 136 : m_ptr(ptr) { |
| 134 { | 137 } |
| 135 } | |
| 136 | 138 |
| 137 void dump(PrintStream& out) const | 139 void dump(PrintStream& out) const { |
| 138 { | 140 if (m_ptr) |
| 139 if (m_ptr) | 141 printInternal(out, *m_ptr); |
| 140 printInternal(out, *m_ptr); | 142 else |
| 141 else | 143 out.print("(null)"); |
| 142 out.print("(null)"); | 144 } |
| 143 } | |
| 144 | 145 |
| 145 private: | 146 private: |
| 146 const T* m_ptr; | 147 const T* m_ptr; |
| 147 }; | 148 }; |
| 148 | 149 |
| 149 template <typename T> | 150 template <typename T> |
| 150 PointerDump<T> pointerDump(const T* ptr) { return PointerDump<T>(ptr); } | 151 PointerDump<T> pointerDump(const T* ptr) { |
| 152 return PointerDump<T>(ptr); |
| 153 } |
| 151 | 154 |
| 152 } // namespace WTF | 155 } // namespace WTF |
| 153 | 156 |
| 154 using WTF::CharacterDump; | 157 using WTF::CharacterDump; |
| 155 using WTF::PointerDump; | 158 using WTF::PointerDump; |
| 156 using WTF::PrintStream; | 159 using WTF::PrintStream; |
| 157 using WTF::pointerDump; | 160 using WTF::pointerDump; |
| 158 | 161 |
| 159 #endif // PrintStream_h | 162 #endif // PrintStream_h |
| 160 | |
| OLD | NEW |