Chromium Code Reviews| Index: src/ostreams.h |
| diff --git a/src/ostreams.h b/src/ostreams.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1e26b7b479440aa68cdcddadd5e7501fbb1ddc67 |
| --- /dev/null |
| +++ b/src/ostreams.h |
| @@ -0,0 +1,118 @@ |
| +// Copyright 2014 the V8 project authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef V8_OSTREAMS_H_ |
| +#define V8_OSTREAMS_H_ |
| + |
| +#include <stddef.h> |
| +#include <stdio.h> |
| +#include <string.h> |
| + |
| +#include "include/v8config.h" |
| +#include "src/base/macros.h" |
| + |
| +namespace v8 { |
| +namespace internal { |
| + |
| +// An abstract base class for output streams with a cut-down standard interface. |
| +class OStream { |
| + public: |
| + OStream() : hex_(false) { } |
| + virtual ~OStream() { } |
| + |
| + // For manipulators like 'os << endl' or 'os << flush', etc. |
| + OStream& operator<<(OStream& (*manipulator)(OStream& os)) { |
| + return manipulator(*this); |
| + } |
| + |
| + // Numeric conversions. |
| + OStream& operator<<(int x); |
| + OStream& operator<<(unsigned int x); |
| + OStream& operator<<(long x); // NOLINT(runtime/int) |
| + OStream& operator<<(unsigned long x); // NOLINT(runtime/int) |
| + OStream& operator<<(long long x); // NOLINT(runtime/int) |
| + OStream& operator<<(unsigned long long x); // NOLINT(runtime/int) |
| + OStream& operator<<(double x); |
| + OStream& operator<<(void* x); |
| + OStream& operator<<(char x); |
| + OStream& operator<<(signed char x); |
| + OStream& operator<<(unsigned char x); |
| + |
| + // Character output. |
| + OStream& operator<<(const char* s) { return write(s, strlen(s)); } |
| + OStream& put(char c) { return write(&c, 1); } |
| + |
| + // Primitive format flag handling, can be extended if needed. |
| + OStream& dec(); |
| + OStream& hex(); |
| + |
| + virtual OStream& write(const char* s, size_t n) = 0; |
| + virtual OStream& flush() = 0; |
| + |
| + private: |
| + template<class T> OStream& print(const char* format, T x); |
| + |
| + bool hex_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(OStream); |
| +}; |
| + |
| + |
| +// Some manipulators. |
| +OStream& flush(OStream& os); // NOLINT(runtime/references) |
| +OStream& endl(OStream& os); // NOLINT(runtime/references) |
| +OStream& dec(OStream& os); // NOLINT(runtime/references) |
| +OStream& hex(OStream& os); // NOLINT(runtime/references) |
| + |
| + |
| +// An output stream writing to a character buffer. |
| +class OStringStream: public OStream { |
| + public: |
| + OStringStream() : size_(0), capacity_(32), data_(allocate(capacity_)) { } |
| + ~OStringStream() { deallocate(data_, capacity_); } |
| + |
| + const size_t size() const { return size_; } |
| + const size_t capacity() const { return capacity_; } |
| + const char* data() const { return data_; } |
| + |
| + virtual OStringStream& write(const char* s, size_t n) V8_OVERRIDE; |
| + virtual OStringStream& flush() V8_OVERRIDE; |
| + |
| + private: |
| + // Primitive allocator interface, can be extracted if needed. |
| + static char* allocate (size_t n) { return new char[n]; } |
| + static void deallocate (char* s, size_t n) { delete[] s; } |
| + |
| + void reserve(size_t requested_capacity); |
| + |
| + size_t size_; |
| + size_t capacity_; |
| + char* data_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(OStringStream); |
| +}; |
| + |
| + |
| +// An output stream writing to a file. |
| +class OFStream: public OStream { |
| + public: |
| + explicit OFStream(FILE* f) : f_(f), own_(false) { } |
| + 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
|
| + virtual ~OFStream() { close(); } |
| + |
| + void close(); |
| + |
| + virtual OFStream& write(const char* s, size_t n) V8_OVERRIDE; |
| + virtual OFStream& flush() V8_OVERRIDE; |
| + |
| + private: |
| + FILE* const f_; |
| + bool own_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(OFStream); |
| +}; |
| + |
| +} } // namespace v8::internal |
| + |
| +#endif // V8_OSTREAMS_H_ |