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