source: xplcommon/src/xplcommon/Buffer.h@ 29:54c2d451f8a0

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

Implemented a blocking stream and a corresponding trst program

File size: 5.6 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 whether the buffer is empty or not.
99 */
100 bool isEmpty() const;
101
102 /**
103 * Get the available capacity in the buffer.
104 */
105 size_t getAvailable() const;
106
107 /**
108 * Reset the buffer.
109 */
110 void reset();
111
112 /**
113 * Add the given amount to the length. Should not exceed the
114 * capacity!
115 *
116 * @return the actual amount added.
117 */
118 size_t addLength(size_t n);
119
120 /**
121 * Append the given data to the contents of the given buffer.
122 *
123 * @return the number of bytes actually appended
124 */
125 size_t append(const void* src, size_t size);
126
127 /**
128 * Extract data into the given buffer from the given offset.
129 *
130 * @return the number of bytes actually extracted.
131 */
132 size_t extract(void* dest, size_t size, size_t offset = 0);
133};
134
135//------------------------------------------------------------------------------
136// Inline definitions
137//------------------------------------------------------------------------------
138
139inline Buffer::Buffer(size_t capacity) :
140 data(new unsigned char[capacity]),
141 capacity(capacity),
142 length(0)
143{
144}
145
146//------------------------------------------------------------------------------
147
148inline Buffer::~Buffer()
149{
150 delete[] data;
151}
152
153//------------------------------------------------------------------------------
154
155inline size_t Buffer::getCapacity() const
156{
157 return capacity;
158}
159
160//------------------------------------------------------------------------------
161
162inline const unsigned char* Buffer::getData() const
163{
164 return data;
165}
166
167//------------------------------------------------------------------------------
168
169inline unsigned char* Buffer::getData()
170{
171 return data;
172}
173
174//------------------------------------------------------------------------------
175
176inline size_t Buffer::getLength() const
177{
178 return length;
179}
180
181//------------------------------------------------------------------------------
182
183inline bool Buffer::isEmpty() const
184{
185 return length==0;
186}
187
188//------------------------------------------------------------------------------
189
190inline size_t Buffer::getAvailable() const
191{
192 return capacity - length;
193}
194
195//------------------------------------------------------------------------------
196
197inline void Buffer::reset()
198{
199 length = 0;
200}
201
202//------------------------------------------------------------------------------
203
204inline size_t Buffer::addLength(size_t n)
205{
206 size_t toAdd = std::min(n, getAvailable());
207 length += toAdd;
208 return toAdd;
209}
210
211//------------------------------------------------------------------------------
212
213} /* namespace xplcommon */
214
215//------------------------------------------------------------------------------
216#endif // XPLCOMMON_BUFFER_H
217
218// Local Variables:
219// mode: C++
220// c-basic-offset: 4
221// indent-tabs-mode: nil
222// End:
Note: See TracBrowser for help on using the repository browser.