File: dbuffer.h

package info (click to toggle)
ufoai 2.5-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 82,128 kB
  • sloc: cpp: 225,227; python: 5,111; ansic: 4,133; php: 2,209; perl: 1,931; sh: 1,517; xml: 1,115; makefile: 401; sed: 11
file content (51 lines) | stat: -rw-r--r-- 1,237 bytes parent folder | download | duplicates (3)
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
/**
 * @file
 */

#pragma once

#include <sys/types.h>
#include <vector>
#include "../shared/sharedptr.h"


/** @defgroup dbuffer Data buffers (dbuffer)
 * @ingroup datastructure
 * A dbuffer is a dynamically sized buffer that stores arbitrary bytes
 * in a queue. It does not provide random access; characters may be
 * inserted only at the end and removed only from the beginning.
 */

class dbuffer
{
private:
	/** this is the length of the entries in the
	 * buffer, not the allocated size of the buffer */
	size_t _length;
	std::vector<char> _data;

public:
	dbuffer (int reserve = 512);
	dbuffer (const dbuffer &other);
	virtual ~dbuffer ();

	/* Append the given byte string to the buffer */
	void add (const char* , size_t);
	/* Read the given number of bytes from the start of the buffer */
	size_t get (char* , size_t) const;
	/* Read the given number of bytes from the given position */
	size_t getAt (size_t, char* , size_t) const;
	/* Remove the given number of bytes from the start of the buffer */
	size_t remove (size_t);
	/* Read and remove in one pass */
	size_t extract (char* , size_t);

	size_t length () const;
};

typedef SharedPtr<dbuffer> dbufferptr;

inline size_t dbuffer::length () const
{
	return _length;
}