OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium 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 // Streams classes. | 5 // Streams classes. |
6 // | 6 // |
7 // These memory-resident streams are used for serialzing data into a sequential | 7 // These memory-resident streams are used for serialzing data into a sequential |
8 // region of memory. | 8 // region of memory. |
9 // Streams are divided into SourceStreams for reading and SinkStreams for | 9 // Streams are divided into SourceStreams for reading and SinkStreams for |
10 // writing. Streams are aggregated into Sets which allows several streams to be | 10 // writing. Streams are aggregated into Sets which allows several streams to be |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 // Returns a pointer to contiguously allocated Length() bytes in the stream. | 140 // Returns a pointer to contiguously allocated Length() bytes in the stream. |
141 // Writing to the stream invalidates the pointer. The SinkStream continues to | 141 // Writing to the stream invalidates the pointer. The SinkStream continues to |
142 // own the memory. | 142 // own the memory. |
143 const uint8* Buffer() const { | 143 const uint8* Buffer() const { |
144 return reinterpret_cast<const uint8*>(buffer_.c_str()); | 144 return reinterpret_cast<const uint8*>(buffer_.c_str()); |
145 } | 145 } |
146 | 146 |
147 // Hints that the stream will grow by an additional |length| bytes. | 147 // Hints that the stream will grow by an additional |length| bytes. |
148 void Reserve(size_t length) { buffer_.reserve(length + buffer_.length()); } | 148 void Reserve(size_t length) { buffer_.reserve(length + buffer_.length()); } |
149 | 149 |
| 150 // Finished with this stream and any storage it has. |
| 151 void Retire(); |
| 152 |
150 private: | 153 private: |
151 std::string buffer_; // Use a string to manage the stream's memory. | 154 std::string buffer_; // Use a string to manage the stream's memory. |
152 | 155 |
153 DISALLOW_COPY_AND_ASSIGN(SinkStream); | 156 DISALLOW_COPY_AND_ASSIGN(SinkStream); |
154 }; | 157 }; |
155 | 158 |
156 // A SourceStreamSet is a set of SourceStreams. | 159 // A SourceStreamSet is a set of SourceStreams. |
157 class SourceStreamSet { | 160 class SourceStreamSet { |
158 public: | 161 public: |
159 SourceStreamSet(); | 162 SourceStreamSet(); |
(...skipping 20 matching lines...) Expand all Loading... |
180 // Returns 'true' if all streams are completely consumed. | 183 // Returns 'true' if all streams are completely consumed. |
181 bool Empty() const; | 184 bool Empty() const; |
182 | 185 |
183 private: | 186 private: |
184 size_t count_; | 187 size_t count_; |
185 SourceStream streams_[kMaxStreams]; | 188 SourceStream streams_[kMaxStreams]; |
186 | 189 |
187 DISALLOW_COPY_AND_ASSIGN(SourceStreamSet); | 190 DISALLOW_COPY_AND_ASSIGN(SourceStreamSet); |
188 }; | 191 }; |
189 | 192 |
| 193 // A SinkStreamSet is a set of SinkStreams. Data is collected by writing to the |
| 194 // component streams. When data collection is complete, it is destructively |
| 195 // transferred, either by flattening into one stream (CopyTo), or transfering |
| 196 // data pairwise into another SinkStreamSet by calling that SinkStreamSet's |
| 197 // WriteSet method. |
190 class SinkStreamSet { | 198 class SinkStreamSet { |
191 public: | 199 public: |
192 SinkStreamSet(); | 200 SinkStreamSet(); |
193 ~SinkStreamSet(); | 201 ~SinkStreamSet(); |
194 | 202 |
195 // Initializes the SinkStreamSet to have |stream_index_limit| streams. Must | 203 // Initializes the SinkStreamSet to have |stream_index_limit| streams. Must |
196 // be <= kMaxStreams. If Init is not called the default is has kMaxStream. | 204 // be <= kMaxStreams. If Init is not called the default is has kMaxStream. |
197 void Init(size_t stream_index_limit); | 205 void Init(size_t stream_index_limit); |
198 | 206 |
199 // Returns a pointer to a substream. | 207 // Returns a pointer to a substream. |
200 SinkStream* stream(size_t id) { return id < count_ ? &streams_[id] : NULL; } | 208 SinkStream* stream(size_t id) { return id < count_ ? &streams_[id] : NULL; } |
201 | 209 |
202 // CopyTo serializes the streams in the SinkStreamSet into a single target | 210 // CopyTo serializes the streams in this SinkStreamSet into a single target |
203 // stream or file. The serialized format may be re-read by initializing a | 211 // stream. The serialized format may be re-read by initializing a |
204 // SourceStreamSet with a buffer containing the data. | 212 // SourceStreamSet with a buffer containing the data. |
205 bool CopyTo(SinkStream* combined_stream); | 213 bool CopyTo(SinkStream* combined_stream); |
206 | 214 |
207 // Writes the streams of |set| into the corresponding streams of |this|. | 215 // Writes the streams of |set| into the corresponding streams of |this|. |
208 // Stream zero first has some metadata written to it. |set| becomes retired. | 216 // Stream zero first has some metadata written to it. |set| becomes retired. |
209 // Partner to SourceStreamSet::ReadSet. | 217 // Partner to SourceStreamSet::ReadSet. |
210 bool WriteSet(SinkStreamSet* set); | 218 bool WriteSet(SinkStreamSet* set); |
211 | 219 |
212 private: | 220 private: |
213 void CopyHeaderTo(SinkStream* stream); | 221 void CopyHeaderTo(SinkStream* stream); |
214 | 222 |
215 size_t count_; | 223 size_t count_; |
216 SinkStream streams_[kMaxStreams]; | 224 SinkStream streams_[kMaxStreams]; |
217 | 225 |
218 DISALLOW_COPY_AND_ASSIGN(SinkStreamSet); | 226 DISALLOW_COPY_AND_ASSIGN(SinkStreamSet); |
219 }; | 227 }; |
220 | 228 |
221 } // namespace | 229 } // namespace |
222 #endif // COURGETTE_STREAMS_H_ | 230 #endif // COURGETTE_STREAMS_H_ |
OLD | NEW |