Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / tools / balsa / simple_buffer.h
bloba550617824027aa8b8db5e1a363d02665f42c0b3
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.
5 #ifndef NET_TOOLS_BALSA_SIMPLE_BUFFER_H__
6 #define NET_TOOLS_BALSA_SIMPLE_BUFFER_H__
8 #include <string>
10 #include "base/compiler_specific.h"
11 #include "net/tools/balsa/buffer_interface.h"
13 namespace net {
15 class SimpleBuffer : public BufferInterface {
16 public:
17 SimpleBuffer();
18 explicit SimpleBuffer(int size);
19 virtual ~SimpleBuffer();
21 std::string str() const;
23 typedef char * iterator;
24 typedef const char * const_iterator;
26 iterator begin() { return storage_ + read_idx_; }
27 const_iterator begin() const { return storage_ + read_idx_; }
29 iterator end() { return storage_ + write_idx_; }
30 const_iterator end() const { return storage_ + write_idx_; }
32 // The following functions all override pure virtual functions
33 // in BufferInterface. See buffer_interface.h for a description
34 // of what they do.
35 virtual int ReadableBytes() const OVERRIDE;
36 virtual int BufferSize() const OVERRIDE;
37 virtual int BytesFree() const OVERRIDE;
39 virtual bool Empty() const OVERRIDE;
40 virtual bool Full() const OVERRIDE;
42 virtual int Write(const char* bytes, int size) OVERRIDE;
44 virtual void GetWritablePtr(char **ptr, int* size) const OVERRIDE;
46 virtual void GetReadablePtr(char **ptr, int* size) const OVERRIDE;
48 virtual int Read(char* bytes, int size) OVERRIDE;
50 virtual void Clear() OVERRIDE;
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.
56 virtual bool Reserve(int size) OVERRIDE;
58 virtual void AdvanceReadablePtr(int amount_to_advance) OVERRIDE;
60 virtual void AdvanceWritablePtr(int amount_to_advance) OVERRIDE;
62 void Swap(SimpleBuffer* other) {
63 char* tmp = storage_;
64 storage_ = other->storage_;
65 other->storage_ = tmp;
67 int tmp_int = write_idx_;
68 write_idx_ = other->write_idx_;
69 other->write_idx_ = tmp_int;
71 tmp_int = read_idx_;
72 read_idx_ = other->read_idx_;
73 other->read_idx_ = tmp_int;
75 tmp_int = storage_size_;
76 storage_size_ = other->storage_size_;
77 other->storage_size_ = tmp_int;
80 protected:
81 char* storage_;
82 int write_idx_;
83 int read_idx_;
84 int storage_size_;
86 private:
87 //DISALLOW_COPY_AND_ASSIGN(SimpleBuffer);
90 } // namespace net
92 #endif // NET_TOOLS_BALSA_SIMPLE_BUFFER_H__