File: outputbuffer.h

package info (click to toggle)
dc-qt 0.2.0.alpha-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,948 kB
  • ctags: 5,361
  • sloc: cpp: 28,936; makefile: 19
file content (57 lines) | stat: -rw-r--r-- 1,391 bytes parent folder | download | duplicates (4)
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
#ifndef outputbuffer_H__
#define outputbuffer_H__

#include <stdlib.h>
#include <iostream>
using namespace std;
#include <boost/shared_ptr.hpp>

namespace rpc {

/**
 * \brief A data accumulator.
 * This class is esentially a wrapper around a byte array which provides writing methods and automatic expantion.
 */
class OutputBuffer
{
public:
  OutputBuffer() { init(4*1024,4*1024); }
  OutputBuffer(int initialSize,int sizeIncrement) { init(initialSize,sizeIncrement); }
  ~OutputBuffer() { free(buf); }
  
  /** Resets the buffer so that it can be reused */
  void clear() {size = 0;}
  /** Returns a pointer to the data written to this buffer */
  const char* getByteArray() const {return buf;}
  /** Returs the size of the data written to this buffer */
  int getSize() const {return size;}

  /** Writes some data to the buffer, expanding it as necessary */
  void write(const void* data, int numBytes) {
    if(numBytes > capacity-size) {
  		while(numBytes > capacity-size) capacity+=increment;
      buf = (char*)realloc(buf,capacity);
    }
    memcpy(buf+size,data,numBytes);
    size+=numBytes;
  }

private:
  char* buf;
  int capacity;
  int size;
  int increment;

  void init(int aSize,int aIncrement) {
    buf = (char*)malloc(aSize);
    increment = aIncrement;
    size = 0;
    capacity = aSize;
  }

};

typedef boost::shared_ptr<OutputBuffer> OutputBufferPtr;
}

#endif