[email protected] | 6c7e652 | 2013-09-29 15:27:44 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | 6a4c749 | 2009-12-04 01:21:22 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 6c7e652 | 2013-09-29 15:27:44 | [diff] [blame] | 5 | #ifndef NET_TOOLS_BALSA_SIMPLE_BUFFER_H__ |
6 | #define NET_TOOLS_BALSA_SIMPLE_BUFFER_H__ | ||||
[email protected] | 6a4c749 | 2009-12-04 01:21:22 | [diff] [blame] | 7 | |
8 | #include <string> | ||||
9 | |||||
[email protected] | addc1be | 2011-11-23 17:57:16 | [diff] [blame] | 10 | #include "base/compiler_specific.h" |
[email protected] | 6c7e652 | 2013-09-29 15:27:44 | [diff] [blame] | 11 | #include "net/tools/balsa/buffer_interface.h" |
[email protected] | 6a4c749 | 2009-12-04 01:21:22 | [diff] [blame] | 12 | |
[email protected] | 4929124c | 2009-12-14 20:25:05 | [diff] [blame] | 13 | namespace net { |
[email protected] | 6a4c749 | 2009-12-04 01:21:22 | [diff] [blame] | 14 | |
15 | class SimpleBuffer : public BufferInterface { | ||||
16 | public: | ||||
17 | SimpleBuffer(); | ||||
18 | explicit SimpleBuffer(int size); | ||||
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 19 | ~SimpleBuffer() override; |
[email protected] | 6a4c749 | 2009-12-04 01:21:22 | [diff] [blame] | 20 | |
[email protected] | 8be6253 | 2010-01-13 23:09:16 | [diff] [blame] | 21 | std::string str() const; |
[email protected] | 6a4c749 | 2009-12-04 01:21:22 | [diff] [blame] | 22 | |
23 | typedef char * iterator; | ||||
24 | typedef const char * const_iterator; | ||||
25 | |||||
26 | iterator begin() { return storage_ + read_idx_; } | ||||
27 | const_iterator begin() const { return storage_ + read_idx_; } | ||||
28 | |||||
29 | iterator end() { return storage_ + write_idx_; } | ||||
30 | const_iterator end() const { return storage_ + write_idx_; } | ||||
31 | |||||
32 | // The following functions all override pure virtual functions | ||||
33 | // in BufferInterface. See buffer_interface.h for a description | ||||
34 | // of what they do. | ||||
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 35 | int ReadableBytes() const override; |
36 | int BufferSize() const override; | ||||
37 | int BytesFree() const override; | ||||
[email protected] | 6a4c749 | 2009-12-04 01:21:22 | [diff] [blame] | 38 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 39 | bool Empty() const override; |
40 | bool Full() const override; | ||||
[email protected] | 6a4c749 | 2009-12-04 01:21:22 | [diff] [blame] | 41 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 42 | int Write(const char* bytes, int size) override; |
[email protected] | 6a4c749 | 2009-12-04 01:21:22 | [diff] [blame] | 43 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 44 | void GetWritablePtr(char** ptr, int* size) const override; |
[email protected] | 6a4c749 | 2009-12-04 01:21:22 | [diff] [blame] | 45 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 46 | void GetReadablePtr(char** ptr, int* size) const override; |
[email protected] | 6a4c749 | 2009-12-04 01:21:22 | [diff] [blame] | 47 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 48 | int Read(char* bytes, int size) override; |
[email protected] | 6a4c749 | 2009-12-04 01:21:22 | [diff] [blame] | 49 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 50 | void Clear() override; |
[email protected] | 6a4c749 | 2009-12-04 01:21:22 | [diff] [blame] | 51 | |
52 | // This can be an expensive operation: costing a new/delete, and copying of | ||||
53 | // all existing data. Even if the existing buffer does not need to be | ||||
54 | // resized, unread data may still need to be non-destructively copied to | ||||
55 | // consolidate fragmented free space. | ||||
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 56 | bool Reserve(int size) override; |
[email protected] | 6a4c749 | 2009-12-04 01:21:22 | [diff] [blame] | 57 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 58 | void AdvanceReadablePtr(int amount_to_advance) override; |
[email protected] | 6a4c749 | 2009-12-04 01:21:22 | [diff] [blame] | 59 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 60 | void AdvanceWritablePtr(int amount_to_advance) override; |
[email protected] | 6a4c749 | 2009-12-04 01:21:22 | [diff] [blame] | 61 | |
62 | void Swap(SimpleBuffer* other) { | ||||
63 | char* tmp = storage_; | ||||
64 | storage_ = other->storage_; | ||||
65 | other->storage_ = tmp; | ||||
66 | |||||
67 | int tmp_int = write_idx_; | ||||
68 | write_idx_ = other->write_idx_; | ||||
69 | other->write_idx_ = tmp_int; | ||||
70 | |||||
71 | tmp_int = read_idx_; | ||||
72 | read_idx_ = other->read_idx_; | ||||
73 | other->read_idx_ = tmp_int; | ||||
74 | |||||
75 | tmp_int = storage_size_; | ||||
76 | storage_size_ = other->storage_size_; | ||||
77 | other->storage_size_ = tmp_int; | ||||
78 | } | ||||
79 | |||||
80 | protected: | ||||
81 | char* storage_; | ||||
82 | int write_idx_; | ||||
83 | int read_idx_; | ||||
84 | int storage_size_; | ||||
85 | |||||
86 | private: | ||||
87 | //DISALLOW_COPY_AND_ASSIGN(SimpleBuffer); | ||||
88 | }; | ||||
89 | |||||
[email protected] | 4929124c | 2009-12-14 20:25:05 | [diff] [blame] | 90 | } // namespace net |
[email protected] | 6a4c749 | 2009-12-04 01:21:22 | [diff] [blame] | 91 | |
[email protected] | 6c7e652 | 2013-09-29 15:27:44 | [diff] [blame] | 92 | #endif // NET_TOOLS_BALSA_SIMPLE_BUFFER_H__ |