summaryrefslogtreecommitdiffstats
path: root/net/tools/flip_server/simple_buffer.h
blob: c10558ac1497c9e255a817f746340977144a2105 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_TOOLS_FLIP_SERVER_SIMPLE_BUFFER_H__
#define NET_TOOLS_FLIP_SERVER_SIMPLE_BUFFER_H__

#include <string>

#include "net/tools/flip_server/buffer_interface.h"
#include "net/tools/flip_server/other_defines.h"

namespace net {

class SimpleBuffer : public BufferInterface {
 public:
  SimpleBuffer();
  explicit SimpleBuffer(int size);
  virtual ~SimpleBuffer() {
    delete[] storage_;
  }

  std::string str() const;

  typedef char * iterator;
  typedef const char * const_iterator;

  iterator begin() { return storage_ + read_idx_; }
  const_iterator begin() const { return storage_ + read_idx_; }

  iterator end() { return storage_ + write_idx_; }
  const_iterator end() const { return storage_ + write_idx_; }

  // The following functions all override pure virtual functions
  // in BufferInterface. See buffer_interface.h for a description
  // of what they do.
  virtual int ReadableBytes() const;
  virtual int BufferSize() const;
  virtual int BytesFree() const;

  virtual bool Empty() const;
  virtual bool Full() const;

  virtual int Write(const char* bytes, int size);

  virtual void GetWritablePtr(char **ptr, int* size) const;

  virtual void GetReadablePtr(char **ptr, int* size) const;

  virtual int Read(char* bytes, int size);

  virtual void Clear();

  // This can be an expensive operation: costing a new/delete, and copying of
  // all existing data. Even if the existing buffer does not need to be
  // resized, unread data may still need to be non-destructively copied to
  // consolidate fragmented free space.
  virtual bool Reserve(int size);

  virtual void AdvanceReadablePtr(int amount_to_advance);

  virtual void AdvanceWritablePtr(int amount_to_advance);

  void Swap(SimpleBuffer* other) {
    char* tmp = storage_;
    storage_ = other->storage_;
    other->storage_ = tmp;

    int tmp_int = write_idx_;
    write_idx_ = other->write_idx_;
    other->write_idx_ = tmp_int;

    tmp_int = read_idx_;
    read_idx_ = other->read_idx_;
    other->read_idx_ = tmp_int;

    tmp_int = storage_size_;
    storage_size_ = other->storage_size_;
    other->storage_size_ = tmp_int;
  }

 protected:
  char* storage_;
  int write_idx_;
  int read_idx_;
  int storage_size_;

 private:
  //DISALLOW_COPY_AND_ASSIGN(SimpleBuffer);
};

}  // namespace net

#endif  // NET_TOOLS_FLIP_SERVER_SIMPLE_BUFFER_H__