OLD | NEW |
---|---|
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_STRING_STREAM_H_ | 5 #ifndef V8_STRING_STREAM_H_ |
6 #define V8_STRING_STREAM_H_ | 6 #define V8_STRING_STREAM_H_ |
7 | 7 |
8 #include <memory> | |
9 | |
10 #include "src/allocation.h" | 8 #include "src/allocation.h" |
11 #include "src/handles.h" | 9 #include "src/handles.h" |
12 #include "src/vector.h" | 10 #include "src/vector.h" |
13 | 11 |
14 namespace v8 { | 12 namespace v8 { |
15 namespace internal { | 13 namespace internal { |
16 | 14 |
17 // Forward declarations. | 15 // Forward declarations. |
18 class ByteArray; | 16 class ByteArray; |
19 | 17 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 | 50 |
53 private: | 51 private: |
54 char* buffer_; | 52 char* buffer_; |
55 unsigned length_; | 53 unsigned length_; |
56 DISALLOW_COPY_AND_ASSIGN(FixedStringAllocator); | 54 DISALLOW_COPY_AND_ASSIGN(FixedStringAllocator); |
57 }; | 55 }; |
58 | 56 |
59 | 57 |
60 class FmtElm final { | 58 class FmtElm final { |
61 public: | 59 public: |
62 FmtElm(int value) : type_(INT) { // NOLINT | 60 #ifdef DEBUG |
titzer
2016/11/17 17:00:38
Could you do this macro stuff a different way? E.g
Clemens Hammacher
2016/11/17 18:40:01
Did it, and it's much cleaner indeed. Even though
| |
63 data_.u_int_ = value; | 61 #define FMT_ELM_CONS(attr, ctype, type, field, data) \ |
64 } | 62 attr FmtElm(ctype value) : type_(type) { data_.field = data; } |
65 explicit FmtElm(double value) : type_(DOUBLE) { | 63 #else |
66 data_.u_double_ = value; | 64 #define FMT_ELM_CONS(attr, ctype, type, field, data) \ |
67 } | 65 attr FmtElm(ctype value) { data_.field = data; } |
68 FmtElm(const char* value) : type_(C_STR) { // NOLINT | 66 #endif |
69 data_.u_c_str_ = value; | 67 FMT_ELM_CONS(, int, INT, u_int_, value) |
70 } | 68 FMT_ELM_CONS(explicit, double, DOUBLE, u_double_, value) |
71 FmtElm(const Vector<const uc16>& value) : type_(LC_STR) { // NOLINT | 69 FMT_ELM_CONS(, const char*, C_STR, u_c_str_, value) |
72 data_.u_lc_str_ = &value; | 70 FMT_ELM_CONS(, const Vector<const uc16>&, LC_STR, u_lc_str_, &value) |
73 } | 71 FMT_ELM_CONS(, Object*, OBJ, u_obj_, value) |
74 FmtElm(Object* value) : type_(OBJ) { // NOLINT | 72 FMT_ELM_CONS(, Handle<Object>, HANDLE, u_handle_, value.location()) |
75 data_.u_obj_ = value; | 73 FMT_ELM_CONS(, void*, POINTER, u_pointer_, value) |
76 } | 74 #undef FMT_ELM_CONS |
77 FmtElm(Handle<Object> value) : type_(HANDLE) { // NOLINT | 75 |
78 data_.u_handle_ = value.location(); | 76 template <typename T> |
79 } | 77 static FmtElm From(T t) { |
80 FmtElm(void* value) : type_(POINTER) { // NOLINT | 78 return t; |
81 data_.u_pointer_ = value; | |
82 } | 79 } |
83 | 80 |
84 private: | 81 private: |
85 friend class StringStream; | 82 friend class StringStream; |
83 #ifdef DEBUG | |
86 enum Type { INT, DOUBLE, C_STR, LC_STR, OBJ, HANDLE, POINTER }; | 84 enum Type { INT, DOUBLE, C_STR, LC_STR, OBJ, HANDLE, POINTER }; |
87 Type type_; | 85 Type type_; |
86 #endif | |
88 union { | 87 union { |
89 int u_int_; | 88 int u_int_; |
90 double u_double_; | 89 double u_double_; |
91 const char* u_c_str_; | 90 const char* u_c_str_; |
92 const Vector<const uc16>* u_lc_str_; | 91 const Vector<const uc16>* u_lc_str_; |
93 Object* u_obj_; | 92 Object* u_obj_; |
94 Object** u_handle_; | 93 Object** u_handle_; |
95 void* u_pointer_; | 94 void* u_pointer_; |
96 } data_; | 95 } data_; |
97 }; | 96 }; |
98 | 97 |
99 | 98 |
100 class StringStream final { | 99 class StringStream final { |
101 public: | 100 public: |
102 enum ObjectPrintMode { kPrintObjectConcise, kPrintObjectVerbose }; | 101 enum ObjectPrintMode { kPrintObjectConcise, kPrintObjectVerbose }; |
103 StringStream(StringAllocator* allocator, | 102 StringStream(StringAllocator* allocator, |
104 ObjectPrintMode object_print_mode = kPrintObjectVerbose) | 103 ObjectPrintMode object_print_mode = kPrintObjectVerbose) |
105 : allocator_(allocator), | 104 : allocator_(allocator), |
106 object_print_mode_(object_print_mode), | 105 object_print_mode_(object_print_mode), |
107 capacity_(kInitialCapacity), | 106 capacity_(kInitialCapacity), |
108 length_(0), | 107 length_(0), |
109 buffer_(allocator_->allocate(kInitialCapacity)) { | 108 buffer_(allocator_->allocate(kInitialCapacity)) { |
110 buffer_[0] = 0; | 109 buffer_[0] = 0; |
111 } | 110 } |
112 | 111 |
113 bool Put(char c); | 112 bool Put(char c); |
114 bool Put(String* str); | 113 bool Put(String* str); |
115 bool Put(String* str, int start, int end); | 114 bool Put(String* str, int start, int end); |
116 void Add(Vector<const char> format, Vector<FmtElm> elms); | 115 void Add(Vector<const char> format, Vector<FmtElm> elms); |
117 void Add(const char* format); | 116 void Add(const char* format) { Add(CStrVector(format)); } |
118 void Add(Vector<const char> format); | 117 void Add(Vector<const char> format) { Add(format, Vector<FmtElm>()); } |
119 void Add(const char* format, FmtElm arg0); | 118 |
120 void Add(const char* format, FmtElm arg0, FmtElm arg1); | 119 template <typename... Args> |
121 void Add(const char* format, FmtElm arg0, FmtElm arg1, FmtElm arg2); | 120 void Add(const char* format, Args... args) { |
122 void Add(const char* format, | 121 Add<Args...>(CStrVector(format), args...); |
123 FmtElm arg0, | 122 } |
124 FmtElm arg1, | 123 |
125 FmtElm arg2, | 124 template <typename... Args> |
126 FmtElm arg3); | 125 void Add(Vector<const char> format, Args... args) { |
127 void Add(const char* format, | 126 FmtElm elems[]{FmtElm::From(args)...}; |
128 FmtElm arg0, | 127 Add(format, ArrayVector(elems)); |
129 FmtElm arg1, | 128 } |
130 FmtElm arg2, | |
131 FmtElm arg3, | |
132 FmtElm arg4); | |
133 | 129 |
134 // Getting the message out. | 130 // Getting the message out. |
135 void OutputToFile(FILE* out); | 131 void OutputToFile(FILE* out); |
136 void OutputToStdOut() { OutputToFile(stdout); } | 132 void OutputToStdOut() { OutputToFile(stdout); } |
137 void Log(Isolate* isolate); | 133 void Log(Isolate* isolate); |
138 Handle<String> ToString(Isolate* isolate); | 134 Handle<String> ToString(Isolate* isolate); |
139 std::unique_ptr<char[]> ToCString() const; | 135 std::unique_ptr<char[]> ToCString() const; |
140 int length() const { return length_; } | 136 int length() const { return length_; } |
141 | 137 |
142 // Object printing support. | 138 // Object printing support. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
176 bool full() const { return (capacity_ - length_) == 1; } | 172 bool full() const { return (capacity_ - length_) == 1; } |
177 int space() const { return capacity_ - length_; } | 173 int space() const { return capacity_ - length_; } |
178 | 174 |
179 DISALLOW_IMPLICIT_CONSTRUCTORS(StringStream); | 175 DISALLOW_IMPLICIT_CONSTRUCTORS(StringStream); |
180 }; | 176 }; |
181 | 177 |
182 } // namespace internal | 178 } // namespace internal |
183 } // namespace v8 | 179 } // namespace v8 |
184 | 180 |
185 #endif // V8_STRING_STREAM_H_ | 181 #endif // V8_STRING_STREAM_H_ |
OLD | NEW |