Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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<<(int x); | |
| 31 OStream& operator<<(unsigned int x); | |
| 32 OStream& operator<<(long x); // NOLINT(runtime/int) | |
| 33 OStream& operator<<(unsigned long x); // NOLINT(runtime/int) | |
| 34 OStream& operator<<(long long x); // NOLINT(runtime/int) | |
| 35 OStream& operator<<(unsigned long long x); // NOLINT(runtime/int) | |
| 36 OStream& operator<<(double x); | |
| 37 OStream& operator<<(void* x); | |
| 38 OStream& operator<<(char x); | |
| 39 OStream& operator<<(signed char x); | |
| 40 OStream& operator<<(unsigned char x); | |
| 41 | |
| 42 // Character output. | |
| 43 OStream& operator<<(const char* s) { return write(s, strlen(s)); } | |
| 44 OStream& put(char c) { return write(&c, 1); } | |
| 45 | |
| 46 // Primitive format flag handling, can be extended if needed. | |
| 47 OStream& dec(); | |
| 48 OStream& hex(); | |
| 49 | |
| 50 virtual OStream& write(const char* s, size_t n) = 0; | |
| 51 virtual OStream& flush() = 0; | |
| 52 | |
| 53 private: | |
| 54 template<class T> OStream& print(const char* format, T x); | |
| 55 | |
| 56 bool hex_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(OStream); | |
| 59 }; | |
| 60 | |
| 61 | |
| 62 // Some manipulators. | |
| 63 OStream& flush(OStream& os); // NOLINT(runtime/references) | |
| 64 OStream& endl(OStream& os); // NOLINT(runtime/references) | |
| 65 OStream& dec(OStream& os); // NOLINT(runtime/references) | |
| 66 OStream& hex(OStream& os); // NOLINT(runtime/references) | |
| 67 | |
| 68 | |
| 69 // An output stream writing to a character buffer. | |
| 70 class OStringStream: public OStream { | |
| 71 public: | |
| 72 OStringStream() : size_(0), capacity_(32), data_(allocate(capacity_)) { } | |
| 73 ~OStringStream() { deallocate(data_, capacity_); } | |
| 74 | |
| 75 const size_t size() const { return size_; } | |
| 76 const size_t capacity() const { return capacity_; } | |
| 77 const char* data() const { return data_; } | |
| 78 | |
| 79 virtual OStringStream& write(const char* s, size_t n) V8_OVERRIDE; | |
| 80 virtual OStringStream& flush() V8_OVERRIDE; | |
| 81 | |
| 82 private: | |
| 83 // Primitive allocator interface, can be extracted if needed. | |
| 84 static char* allocate (size_t n) { return new char[n]; } | |
| 85 static void deallocate (char* s, size_t n) { delete[] s; } | |
| 86 | |
| 87 void reserve(size_t requested_capacity); | |
| 88 | |
| 89 size_t size_; | |
| 90 size_t capacity_; | |
| 91 char* data_; | |
| 92 | |
| 93 DISALLOW_COPY_AND_ASSIGN(OStringStream); | |
| 94 }; | |
| 95 | |
| 96 | |
| 97 // An output stream writing to a file. | |
| 98 class OFStream: public OStream { | |
| 99 public: | |
| 100 explicit OFStream(FILE* f) : f_(f), own_(false) { } | |
| 101 explicit OFStream(const char* p) : f_(fopen(p, "w")), own_(true) { } | |
|
Benedikt Meurer
2014/06/25 14:48:16
Please remove this constructor. It performs absolu
Sven Panne
2014/06/26 06:56:19
But this would be in line with ignoring basically
| |
| 102 virtual ~OFStream() { close(); } | |
| 103 | |
| 104 void close(); | |
| 105 | |
| 106 virtual OFStream& write(const char* s, size_t n) V8_OVERRIDE; | |
| 107 virtual OFStream& flush() V8_OVERRIDE; | |
| 108 | |
| 109 private: | |
| 110 FILE* const f_; | |
| 111 bool own_; | |
| 112 | |
| 113 DISALLOW_COPY_AND_ASSIGN(OFStream); | |
| 114 }; | |
| 115 | |
| 116 } } // namespace v8::internal | |
| 117 | |
| 118 #endif // V8_OSTREAMS_H_ | |
| OLD | NEW |