source: xplcommon/src/xplcommon/Buffer.h@ 5:db1fa4c31a20

Last change on this file since 5:db1fa4c31a20 was 5:db1fa4c31a20, checked in by István Váradi <ivaradi@…>, 11 years ago

Added a generic buffer class

File size: 5.4 KB
Line 
1// Copyright (c) 2012 by István Váradi
2
3// This file is part of libxplcommon, a common utility library for
4// development related to X-Plane
5
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8
9// 1. Redistributions of source code must retain the above copyright notice, this
10// list of conditions and the following disclaimer.
11// 2. Redistributions in binary form must reproduce the above copyright notice,
12// this list of conditions and the following disclaimer in the documentation
13// and/or other materials provided with the distribution.
14
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26// The views and conclusions contained in the software and documentation are those
27// of the authors and should not be interpreted as representing official policies,
28// either expressed or implied, of the FreeBSD Project.
29
30#ifndef XPLCOMMON_BUFFER_H
31#define XPLCOMMON_BUFFER_H
32//------------------------------------------------------------------------------
33
34#include <algorithm>
35
36#include <cstdlib>
37
38//------------------------------------------------------------------------------
39
40namespace xplcommon {
41
42//------------------------------------------------------------------------------
43
44/**
45 * Base class for buffers. It maintains a memory area of a certain
46 * capacity and the length of the data in the buffer.
47 */
48class Buffer
49{
50private:
51 /**
52 * The data itself.
53 */
54 unsigned char* data;
55
56 /**
57 * The capacity of the buffer.
58 */
59 size_t capacity;
60
61 /**
62 * The amount of data in the buffer.
63 */
64 size_t length;
65
66public:
67 /**
68 * Construct the buffer with the given capacity.
69 */
70 Buffer(size_t capacity);
71
72 /**
73 * Destroy the buffer.
74 */
75 ~Buffer();
76
77 /**
78 * Get the capacity of the buffer.
79 */
80 size_t getCapacity() const;
81
82 /**
83 * Get the pointer to the data.
84 */
85 const unsigned char* getData() const;
86
87 /**
88 * Get the pointer to the data for modification.
89 */
90 unsigned char* getData();
91
92 /**
93 * Get the length of the data.
94 */
95 size_t getLength() const;
96
97 /**
98 * Get the available capacity in the buffer.
99 */
100 size_t getAvailable() const;
101
102 /**
103 * Reset the buffer.
104 */
105 void reset();
106
107 /**
108 * Add the given amount to the length. Should not exceed the
109 * capacity!
110 *
111 * @return the actual amount added.
112 */
113 size_t addLength(size_t n);
114
115 /**
116 * Append the given data to the contents of the given buffer.
117 *
118 * @return the number of bytes actually appended
119 */
120 size_t append(const void* src, size_t size);
121
122 /**
123 * Extract data into the given buffer from the given offset.
124 *
125 * @return the number of bytes actually extracted.
126 */
127 size_t extract(void* dest, size_t size, size_t offset = 0);
128};
129
130//------------------------------------------------------------------------------
131// Inline definitions
132//------------------------------------------------------------------------------
133
134inline Buffer::Buffer(size_t capacity) :
135 data(new unsigned char[capacity]),
136 capacity(capacity),
137 length(0)
138{
139}
140
141//------------------------------------------------------------------------------
142
143inline Buffer::~Buffer()
144{
145 delete[] data;
146}
147
148//------------------------------------------------------------------------------
149
150inline size_t Buffer::getCapacity() const
151{
152 return capacity;
153}
154
155//------------------------------------------------------------------------------
156
157inline const unsigned char* Buffer::getData() const
158{
159 return data;
160}
161
162//------------------------------------------------------------------------------
163
164inline unsigned char* Buffer::getData()
165{
166 return data;
167}
168
169//------------------------------------------------------------------------------
170
171inline size_t Buffer::getLength() const
172{
173 return length;
174}
175
176//------------------------------------------------------------------------------
177
178inline size_t Buffer::getAvailable() const
179{
180 return capacity - length;
181}
182
183//------------------------------------------------------------------------------
184
185inline void Buffer::reset()
186{
187 length = 0;
188}
189
190//------------------------------------------------------------------------------
191
192inline size_t Buffer::addLength(size_t n)
193{
194 size_t toAdd = std::min(n, getAvailable());
195 length += toAdd;
196 return toAdd;
197}
198
199//------------------------------------------------------------------------------
200
201} /* namespace xplcommon */
202
203//------------------------------------------------------------------------------
204#endif // XPLCOMMON_BUFFER_H
205
206// Local Variables:
207// mode: C++
208// c-basic-offset: 4
209// indent-tabs-mode: nil
210// End:
Note: See TracBrowser for help on using the repository browser.