| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_OSTREAMS_H_ | 5 #ifndef V8_OSTREAMS_H_ |
| 6 #define V8_OSTREAMS_H_ | 6 #define V8_OSTREAMS_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <cstddef> |
| 9 #include <stdio.h> | 9 #include <cstdio> |
| 10 #include <string.h> | 10 #include <cstring> |
| 11 #include <ostream> // NOLINT |
| 12 #include <streambuf> |
| 11 | 13 |
| 12 #include "include/v8config.h" | 14 #include "include/v8config.h" |
| 13 #include "src/base/macros.h" | 15 #include "src/base/macros.h" |
| 14 | 16 |
| 15 namespace v8 { | 17 namespace v8 { |
| 16 namespace internal { | 18 namespace internal { |
| 17 | 19 |
| 18 // An abstract base class for output streams with a cut-down standard interface. | 20 class OFStreamBase : public std::streambuf { |
| 19 class OStream { | 21 protected: |
| 20 public: | 22 explicit OFStreamBase(FILE* f); |
| 21 OStream() : hex_(false) { } | 23 virtual ~OFStreamBase(); |
| 22 virtual ~OStream() { } | |
| 23 | 24 |
| 24 // For manipulators like 'os << endl' or 'os << flush', etc. | 25 virtual int_type sync() FINAL; |
| 25 OStream& operator<<(OStream& (*manipulator)(OStream& os)) { | 26 virtual int_type overflow(int_type c) FINAL; |
| 26 return manipulator(*this); | |
| 27 } | |
| 28 | |
| 29 // Numeric conversions. | |
| 30 OStream& operator<<(short x); // NOLINT(runtime/int) | |
| 31 OStream& operator<<(unsigned short x); // NOLINT(runtime/int) | |
| 32 OStream& operator<<(int x); | |
| 33 OStream& operator<<(unsigned int x); | |
| 34 OStream& operator<<(long x); // NOLINT(runtime/int) | |
| 35 OStream& operator<<(unsigned long x); // NOLINT(runtime/int) | |
| 36 OStream& operator<<(long long x); // NOLINT(runtime/int) | |
| 37 OStream& operator<<(unsigned long long x); // NOLINT(runtime/int) | |
| 38 OStream& operator<<(double x); | |
| 39 OStream& operator<<(void* x); | |
| 40 | |
| 41 // Character output. | |
| 42 OStream& operator<<(char x); | |
| 43 OStream& operator<<(signed char x); | |
| 44 OStream& operator<<(unsigned char x); | |
| 45 OStream& operator<<(const char* s) { return write(s, strlen(s)); } | |
| 46 OStream& put(char c) { return write(&c, 1); } | |
| 47 | |
| 48 // Primitive format flag handling, can be extended if needed. | |
| 49 OStream& dec(); | |
| 50 OStream& hex(); | |
| 51 | |
| 52 virtual OStream& write(const char* s, size_t n) = 0; | |
| 53 virtual OStream& flush() = 0; | |
| 54 | 27 |
| 55 private: | 28 private: |
| 56 template<class T> OStream& print(const char* format, T x); | 29 FILE* const f_; |
| 57 | 30 |
| 58 bool hex_; | 31 DISALLOW_COPY_AND_ASSIGN(OFStreamBase); |
| 59 | |
| 60 DISALLOW_COPY_AND_ASSIGN(OStream); | |
| 61 }; | |
| 62 | |
| 63 | |
| 64 // Some manipulators. | |
| 65 OStream& flush(OStream& os); // NOLINT(runtime/references) | |
| 66 OStream& endl(OStream& os); // NOLINT(runtime/references) | |
| 67 OStream& dec(OStream& os); // NOLINT(runtime/references) | |
| 68 OStream& hex(OStream& os); // NOLINT(runtime/references) | |
| 69 | |
| 70 | |
| 71 // An output stream writing to a character buffer. | |
| 72 class OStringStream: public OStream { | |
| 73 public: | |
| 74 OStringStream() : size_(0), capacity_(32), data_(allocate(capacity_)) { | |
| 75 data_[0] = '\0'; | |
| 76 } | |
| 77 ~OStringStream() { deallocate(data_, capacity_); } | |
| 78 | |
| 79 size_t size() const { return size_; } | |
| 80 size_t capacity() const { return capacity_; } | |
| 81 const char* data() const { return data_; } | |
| 82 | |
| 83 // Internally, our character data is always 0-terminated. | |
| 84 const char* c_str() const { return data(); } | |
| 85 | |
| 86 virtual OStringStream& write(const char* s, size_t n) OVERRIDE; | |
| 87 virtual OStringStream& flush() OVERRIDE; | |
| 88 | |
| 89 private: | |
| 90 // Primitive allocator interface, can be extracted if needed. | |
| 91 static char* allocate (size_t n) { return new char[n]; } | |
| 92 static void deallocate (char* s, size_t n) { delete[] s; } | |
| 93 | |
| 94 void reserve(size_t requested_capacity); | |
| 95 | |
| 96 size_t size_; | |
| 97 size_t capacity_; | |
| 98 char* data_; | |
| 99 | |
| 100 DISALLOW_COPY_AND_ASSIGN(OStringStream); | |
| 101 }; | 32 }; |
| 102 | 33 |
| 103 | 34 |
| 104 // An output stream writing to a file. | 35 // An output stream writing to a file. |
| 105 class OFStream: public OStream { | 36 class OFStream FINAL : private virtual OFStreamBase, public std::ostream { |
| 106 public: | 37 public: |
| 107 explicit OFStream(FILE* f) : f_(f) { } | 38 explicit OFStream(FILE* f); |
| 108 virtual ~OFStream() { } | 39 ~OFStream(); |
| 109 | |
| 110 virtual OFStream& write(const char* s, size_t n) OVERRIDE; | |
| 111 virtual OFStream& flush() OVERRIDE; | |
| 112 | 40 |
| 113 private: | 41 private: |
| 114 FILE* const f_; | |
| 115 | |
| 116 DISALLOW_COPY_AND_ASSIGN(OFStream); | 42 DISALLOW_COPY_AND_ASSIGN(OFStream); |
| 117 }; | 43 }; |
| 118 | 44 |
| 119 | 45 |
| 120 // Wrappers to disambiguate uint16_t and uc16. | 46 // Wrappers to disambiguate uint16_t and uc16. |
| 121 struct AsUC16 { | 47 struct AsUC16 { |
| 122 explicit AsUC16(uint16_t v) : value(v) {} | 48 explicit AsUC16(uint16_t v) : value(v) {} |
| 123 uint16_t value; | 49 uint16_t value; |
| 124 }; | 50 }; |
| 125 | 51 |
| 126 | 52 |
| 127 struct AsReversiblyEscapedUC16 { | 53 struct AsReversiblyEscapedUC16 { |
| 128 explicit AsReversiblyEscapedUC16(uint16_t v) : value(v) {} | 54 explicit AsReversiblyEscapedUC16(uint16_t v) : value(v) {} |
| 129 uint16_t value; | 55 uint16_t value; |
| 130 }; | 56 }; |
| 131 | 57 |
| 132 | 58 |
| 133 // Writes the given character to the output escaping everything outside of | 59 // Writes the given character to the output escaping everything outside of |
| 134 // printable/space ASCII range. Additionally escapes '\' making escaping | 60 // printable/space ASCII range. Additionally escapes '\' making escaping |
| 135 // reversible. | 61 // reversible. |
| 136 OStream& operator<<(OStream& os, const AsReversiblyEscapedUC16& c); | 62 std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c); |
| 137 | 63 |
| 138 // Writes the given character to the output escaping everything outside | 64 // Writes the given character to the output escaping everything outside |
| 139 // of printable ASCII range. | 65 // of printable ASCII range. |
| 140 OStream& operator<<(OStream& os, const AsUC16& c); | 66 std::ostream& operator<<(std::ostream& os, const AsUC16& c); |
| 141 } } // namespace v8::internal | 67 |
| 68 } // namespace internal |
| 69 } // namespace v8 |
| 142 | 70 |
| 143 #endif // V8_OSTREAMS_H_ | 71 #endif // V8_OSTREAMS_H_ |
| OLD | NEW |