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.
12 #include "include/v8config.h"
13 #include "src/base/macros.h"
18 // An abstract base class for output streams with a cut-down standard interface.
21 OStream() : hex_(false) { }
22 virtual ~OStream() { }
24 // For manipulators like 'os << endl' or 'os << flush', etc.
25 OStream& operator<<(OStream& (*manipulator)(OStream& os)) {
26 return manipulator(*this);
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);
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); }
48 // Primitive format flag handling, can be extended if needed.
52 virtual OStream& write(const char* s, size_t n) = 0;
53 virtual OStream& flush() = 0;
56 template<class T> OStream& print(const char* format, T x);
60 DISALLOW_COPY_AND_ASSIGN(OStream);
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)
71 // An output stream writing to a character buffer.
72 class OStringStream: public OStream {
74 OStringStream() : size_(0), capacity_(32), data_(allocate(capacity_)) {
77 ~OStringStream() { deallocate(data_, capacity_); }
79 size_t size() const { return size_; }
80 size_t capacity() const { return capacity_; }
81 const char* data() const { return data_; }
83 // Internally, our character data is always 0-terminated.
84 const char* c_str() const { return data(); }
86 virtual OStringStream& write(const char* s, size_t n) OVERRIDE;
87 virtual OStringStream& flush() OVERRIDE;
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; }
94 void reserve(size_t requested_capacity);
100 DISALLOW_COPY_AND_ASSIGN(OStringStream);
104 // An output stream writing to a file.
105 class OFStream: public OStream {
107 explicit OFStream(FILE* f) : f_(f) { }
108 virtual ~OFStream() { }
110 virtual OFStream& write(const char* s, size_t n) OVERRIDE;
111 virtual OFStream& flush() OVERRIDE;
116 DISALLOW_COPY_AND_ASSIGN(OFStream);
120 // Wrappers to disambiguate uint16_t and uc16.
122 explicit AsUC16(uint16_t v) : value(v) {}
127 struct AsReversiblyEscapedUC16 {
128 explicit AsReversiblyEscapedUC16(uint16_t v) : value(v) {}
133 // Writes the given character to the output escaping everything outside of
134 // printable/space ASCII range. Additionally escapes '\' making escaping
136 OStream& operator<<(OStream& os, const AsReversiblyEscapedUC16& c);
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
143 #endif // V8_OSTREAMS_H_