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