V8 Project
ostreams.h
Go to the documentation of this file.
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 
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:
75  data_[0] = '\0';
76  }
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 
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 
117 };
118 
119 
120 // Wrappers to disambiguate uint16_t and uc16.
121 struct AsUC16 {
122  explicit AsUC16(uint16_t v) : value(v) {}
124 };
125 
126 
128  explicit AsReversiblyEscapedUC16(uint16_t v) : value(v) {}
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.
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_
DISALLOW_COPY_AND_ASSIGN(OFStream)
virtual OFStream & flush() OVERRIDE
Definition: ostreams.cc:161
virtual OFStream & write(const char *s, size_t n) OVERRIDE
Definition: ostreams.cc:155
virtual ~OStream()
Definition: ostreams.h:22
OStream & operator<<(OStream &(*manipulator)(OStream &os))
Definition: ostreams.h:25
virtual OStream & flush()=0
DISALLOW_COPY_AND_ASSIGN(OStream)
OStream & dec()
Definition: ostreams.cc:95
OStream & put(char c)
Definition: ostreams.h:46
OStream & print(const char *format, T x)
Definition: ostreams.cc:21
virtual OStream & write(const char *s, size_t n)=0
OStream & operator<<(const char *s)
Definition: ostreams.h:45
virtual OStringStream & flush() OVERRIDE
Definition: ostreams.cc:138
static char * allocate(size_t n)
Definition: ostreams.h:91
const char * data() const
Definition: ostreams.h:81
size_t capacity() const
Definition: ostreams.h:80
const char * c_str() const
Definition: ostreams.h:84
size_t size() const
Definition: ostreams.h:79
static void deallocate(char *s, size_t n)
Definition: ostreams.h:92
DISALLOW_COPY_AND_ASSIGN(OStringStream)
void reserve(size_t requested_capacity)
Definition: ostreams.cc:143
virtual OStringStream & write(const char *s, size_t n) OVERRIDE
Definition: ostreams.cc:127
#define OVERRIDE
unsigned short uint16_t
Definition: unicode.cc:23
OStream & endl(OStream &os)
Definition: ostreams.cc:112
OStream & hex(OStream &os)
Definition: ostreams.cc:117
OStream & flush(OStream &os)
Definition: ostreams.cc:107
OStream & dec(OStream &os)
Definition: ostreams.cc:122
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20
AsUC16(uint16_t v)
Definition: ostreams.h:122
#define T(name, string, precedence)
Definition: token.cc:25