Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: src/string-stream.h

Issue 219173002: Cleanup string-stream module. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | src/string-stream.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2014 the V8 project authors. All rights reserved.
tfarina 2014/03/31 15:51:18 Are you sure it is OK to change copyright? I doubt
2 // Redistribution and use in source and binary forms, with or without 2 // Use of this source code is governed by a BSD-style license that can be
3 // modification, are permitted provided that the following conditions are 3 // found in the LICENSE file.
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 4
28 #ifndef V8_STRING_STREAM_H_ 5 #ifndef V8_STRING_STREAM_H_
29 #define V8_STRING_STREAM_H_ 6 #define V8_STRING_STREAM_H_
30 7
8 #include "handles.h"
9
31 namespace v8 { 10 namespace v8 {
32 namespace internal { 11 namespace internal {
33 12
34
tfarina 2014/03/31 15:51:18 why? don't we always leave two spaces between clas
35 class StringAllocator { 13 class StringAllocator {
36 public: 14 public:
37 virtual ~StringAllocator() {} 15 virtual ~StringAllocator() { }
38 // Allocate a number of bytes. 16 // Allocate a number of bytes.
39 virtual char* allocate(unsigned bytes) = 0; 17 virtual char* allocate(unsigned bytes) = 0;
40 // Allocate a larger number of bytes and copy the old buffer to the new one. 18 // Allocate a larger number of bytes and copy the old buffer to the new one.
41 // bytes is an input and output parameter passing the old size of the buffer 19 // bytes is an input and output parameter passing the old size of the buffer
42 // and returning the new size. If allocation fails then we return the old 20 // and returning the new size. If allocation fails then we return the old
43 // buffer and do not increase the size. 21 // buffer and do not increase the size.
44 virtual char* grow(unsigned* bytes) = 0; 22 virtual char* grow(unsigned* bytes) = 0;
45 }; 23 };
46 24
47 25
48 // Normal allocator uses new[] and delete[]. 26 // Normal allocator uses new[] and delete[].
49 class HeapStringAllocator: public StringAllocator { 27 class HeapStringAllocator V8_FINAL : public StringAllocator {
50 public: 28 public:
51 ~HeapStringAllocator() { DeleteArray(space_); } 29 ~HeapStringAllocator() { DeleteArray(space_); }
52 char* allocate(unsigned bytes); 30 virtual char* allocate(unsigned bytes) V8_OVERRIDE;
53 char* grow(unsigned* bytes); 31 virtual char* grow(unsigned* bytes) V8_OVERRIDE;
32
54 private: 33 private:
55 char* space_; 34 char* space_;
56 }; 35 };
57 36
58 37
59 // Allocator for use when no new c++ heap allocation is allowed. 38 // Allocator for use when no new c++ heap allocation is allowed.
60 // Given a preallocated buffer up front and does no allocation while 39 // Given a preallocated buffer up front and does no allocation while
61 // building message. 40 // building message.
62 class NoAllocationStringAllocator: public StringAllocator { 41 class NoAllocationStringAllocator V8_FINAL : public StringAllocator {
63 public: 42 public:
64 NoAllocationStringAllocator(char* memory, unsigned size); 43 NoAllocationStringAllocator(char* memory, unsigned size);
65 char* allocate(unsigned bytes) { return space_; } 44 virtual char* allocate(unsigned bytes) V8_OVERRIDE { return space_; }
66 char* grow(unsigned* bytes); 45 virtual char* grow(unsigned* bytes) V8_OVERRIDE;
46
67 private: 47 private:
68 unsigned size_; 48 unsigned size_;
69 char* space_; 49 char* space_;
70 }; 50 };
71 51
72 52
73 class FmtElm { 53 class FmtElm V8_FINAL {
74 public: 54 public:
75 FmtElm(int value) : type_(INT) { // NOLINT 55 FmtElm(int value) : type_(INT) { // NOLINT
76 data_.u_int_ = value; 56 data_.u_int_ = value;
77 } 57 }
78 explicit FmtElm(double value) : type_(DOUBLE) { 58 explicit FmtElm(double value) : type_(DOUBLE) {
79 data_.u_double_ = value; 59 data_.u_double_ = value;
80 } 60 }
81 FmtElm(const char* value) : type_(C_STR) { // NOLINT 61 FmtElm(const char* value) : type_(C_STR) { // NOLINT
82 data_.u_c_str_ = value; 62 data_.u_c_str_ = value;
83 } 63 }
(...skipping 19 matching lines...) Expand all
103 double u_double_; 83 double u_double_;
104 const char* u_c_str_; 84 const char* u_c_str_;
105 const Vector<const uc16>* u_lc_str_; 85 const Vector<const uc16>* u_lc_str_;
106 Object* u_obj_; 86 Object* u_obj_;
107 Object** u_handle_; 87 Object** u_handle_;
108 void* u_pointer_; 88 void* u_pointer_;
109 } data_; 89 } data_;
110 }; 90 };
111 91
112 92
113 class StringStream { 93 class StringStream V8_FINAL {
114 public: 94 public:
115 explicit StringStream(StringAllocator* allocator): 95 explicit StringStream(StringAllocator* allocator):
116 allocator_(allocator), 96 allocator_(allocator),
117 capacity_(kInitialCapacity), 97 capacity_(kInitialCapacity),
118 length_(0), 98 length_(0),
119 buffer_(allocator_->allocate(kInitialCapacity)) { 99 buffer_(allocator_->allocate(kInitialCapacity)) {
120 buffer_[0] = 0; 100 buffer_[0] = 0;
121 } 101 }
122 102
123 ~StringStream() {
124 }
125
126 bool Put(char c); 103 bool Put(char c);
127 bool Put(String* str); 104 bool Put(String* str);
128 bool Put(String* str, int start, int end); 105 bool Put(String* str, int start, int end);
129 void Add(Vector<const char> format, Vector<FmtElm> elms); 106 void Add(Vector<const char> format, Vector<FmtElm> elms);
130 void Add(const char* format); 107 void Add(const char* format);
131 void Add(Vector<const char> format); 108 void Add(Vector<const char> format);
132 void Add(const char* format, FmtElm arg0); 109 void Add(const char* format, FmtElm arg0);
133 void Add(const char* format, FmtElm arg0, FmtElm arg1); 110 void Add(const char* format, FmtElm arg0, FmtElm arg1);
134 void Add(const char* format, FmtElm arg0, FmtElm arg1, FmtElm arg2); 111 void Add(const char* format, FmtElm arg0, FmtElm arg1, FmtElm arg2);
135 void Add(const char* format, 112 void Add(const char* format,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 buffer_[0] = 0; 145 buffer_[0] = 0;
169 } 146 }
170 147
171 // Mentioned object cache support. 148 // Mentioned object cache support.
172 void PrintMentionedObjectCache(Isolate* isolate); 149 void PrintMentionedObjectCache(Isolate* isolate);
173 static void ClearMentionedObjectCache(Isolate* isolate); 150 static void ClearMentionedObjectCache(Isolate* isolate);
174 #ifdef DEBUG 151 #ifdef DEBUG
175 static bool IsMentionedObjectCacheClear(Isolate* isolate); 152 static bool IsMentionedObjectCacheClear(Isolate* isolate);
176 #endif 153 #endif
177 154
178
179 static const int kInitialCapacity = 16; 155 static const int kInitialCapacity = 16;
180 156
181 private: 157 private:
182 void PrintObject(Object* obj); 158 void PrintObject(Object* obj);
183 159
184 StringAllocator* allocator_; 160 StringAllocator* allocator_;
185 unsigned capacity_; 161 unsigned capacity_;
186 unsigned length_; // does not include terminating 0-character 162 unsigned length_; // does not include terminating 0-character
187 char* buffer_; 163 char* buffer_;
188 164
189 bool full() const { return (capacity_ - length_) == 1; } 165 bool full() const { return (capacity_ - length_) == 1; }
190 int space() const { return capacity_ - length_; } 166 int space() const { return capacity_ - length_; }
191 167
192 DISALLOW_IMPLICIT_CONSTRUCTORS(StringStream); 168 DISALLOW_IMPLICIT_CONSTRUCTORS(StringStream);
193 }; 169 };
194 170
195 171
196 // Utility class to print a list of items to a stream, divided by a separator. 172 // Utility class to print a list of items to a stream, divided by a separator.
197 class SimpleListPrinter { 173 class SimpleListPrinter V8_FINAL {
198 public: 174 public:
199 explicit SimpleListPrinter(StringStream* stream, char separator = ',') { 175 explicit SimpleListPrinter(StringStream* stream, char separator = ',') {
200 separator_ = separator; 176 separator_ = separator;
201 stream_ = stream; 177 stream_ = stream;
202 first_ = true; 178 first_ = true;
203 } 179 }
204 180
205 void Add(const char* str) { 181 void Add(const char* str) {
206 if (first_) { 182 if (first_) {
207 first_ = false; 183 first_ = false;
208 } else { 184 } else {
209 stream_->Put(separator_); 185 stream_->Put(separator_);
210 } 186 }
211 stream_->Add(str); 187 stream_->Add(str);
212 } 188 }
213 189
214 private: 190 private:
215 bool first_; 191 bool first_;
216 char separator_; 192 char separator_;
217 StringStream* stream_; 193 StringStream* stream_;
218 }; 194 };
219 195
220
221 } } // namespace v8::internal 196 } } // namespace v8::internal
222 197
223 #endif // V8_STRING_STREAM_H_ 198 #endif // V8_STRING_STREAM_H_
OLDNEW
« no previous file with comments | « no previous file | src/string-stream.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698