Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef V8_OSTREAMS_H_ |
| 6 | #define V8_OSTREAMS_H_ |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | #include <stdio.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #include "include/v8config.h" |
| 13 | #include "src/base/macros.h" |
| 14 | |
| 15 | namespace v8 { |
| 16 | namespace internal { |
| 17 | |
| 18 | // An abstract base class for output streams with a cut-down standard interface. |
| 19 | class OStream { |
| 20 | public: |
| 21 | OStream() : hex_(false) { } |
| 22 | virtual ~OStream() { } |
| 23 | |
| 24 | // For manipulators like 'os << endl' or 'os << flush', etc. |
| 25 | OStream& operator<<(OStream& (*manipulator)(OStream& os)) { |
| 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 | |
| 55 | private: |
| 56 | template<class T> OStream& print(const char* format, T x); |
| 57 | |
| 58 | bool hex_; |
| 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 | }; |
| 102 | |
| 103 | |
| 104 | // An output stream writing to a file. |
| 105 | class OFStream: public OStream { |
| 106 | public: |
| 107 | explicit OFStream(FILE* f) : f_(f) { } |
| 108 | virtual ~OFStream() { } |
| 109 | |
| 110 | virtual OFStream& write(const char* s, size_t n) OVERRIDE; |
| 111 | virtual OFStream& flush() OVERRIDE; |
| 112 | |
| 113 | private: |
| 114 | FILE* const f_; |
| 115 | |
| 116 | DISALLOW_COPY_AND_ASSIGN(OFStream); |
| 117 | }; |
| 118 | |
| 119 | |
| 120 | // Wrappers to disambiguate uint16_t and uc16. |
| 121 | struct AsUC16 { |
| 122 | explicit AsUC16(uint16_t v) : value(v) {} |
| 123 | uint16_t value; |
| 124 | }; |
| 125 | |
| 126 | |
| 127 | struct AsReversiblyEscapedUC16 { |
| 128 | explicit AsReversiblyEscapedUC16(uint16_t v) : value(v) {} |
| 129 | uint16_t value; |
| 130 | }; |
| 131 | |
| 132 | |
| 133 | // Writes the given character to the output escaping everything outside of |
| 134 | // printable/space ASCII range. Additionally escapes '\' making escaping |
| 135 | // reversible. |
| 136 | OStream& operator<<(OStream& os, const AsReversiblyEscapedUC16& c); |
| 137 | |
| 138 | // Writes the given character to the output escaping everything outside |
| 139 | // of printable ASCII range. |
| 140 | OStream& operator<<(OStream& os, const AsUC16& c); |
| 141 | } } // namespace v8::internal |
| 142 | |
| 143 | #endif // V8_OSTREAMS_H_ |