Very Simple Cross-Platform Library
Buffer.h
1 // Copyright (c) 2013 by István Váradi
2 
3 // This file is part of VSCPL, a simple cross-platform utility library
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 
8 // 1. Redistributions of source code must retain the above copyright notice, this
9 // list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 // The views and conclusions contained in the software and documentation are those
26 // of the authors and should not be interpreted as representing official policies,
27 // either expressed or implied, of the FreeBSD Project.
28 
29 #ifndef HU_VARADIISTVAN_SCPL_IO_BUFFER_H
30 #define HU_VARADIISTVAN_SCPL_IO_BUFFER_H
31 //------------------------------------------------------------------------------
32 
33 #include <algorithm>
34 
35 #include <cstdlib>
36 #include <cstring>
37 
38 //------------------------------------------------------------------------------
39 
40 namespace hu { namespace varadiistvan { namespace scpl { namespace io {
41 
42 //------------------------------------------------------------------------------
43 
48 class Buffer
49 {
50 private:
54  unsigned char* data;
55 
59  size_t capacity;
60 
64  size_t length;
65 
66 public:
70  Buffer(size_t capacity);
71 
75  ~Buffer();
76 
80  size_t getCapacity() const;
81 
85  const unsigned char* getData() const;
86 
90  unsigned char* getData();
91 
95  size_t getLength() const;
96 
100  bool isEmpty() const;
101 
105  size_t getAvailable() const;
106 
110  void reset();
111 
118  size_t addLength(size_t n);
119 
125  size_t append(const void* src, size_t size);
126 
132  size_t extract(void* dest, size_t size, size_t offset = 0) const;
133 };
134 
135 //------------------------------------------------------------------------------
136 // Inline definitions
137 //------------------------------------------------------------------------------
138 
139 inline Buffer::Buffer(size_t capacity) :
140  data(new unsigned char[capacity]),
141  capacity(capacity),
142  length(0)
143 {
144 }
145 
146 //------------------------------------------------------------------------------
147 
149 {
150  delete[] data;
151 }
152 
153 //------------------------------------------------------------------------------
154 
155 inline size_t Buffer::getCapacity() const
156 {
157  return capacity;
158 }
159 
160 //------------------------------------------------------------------------------
161 
162 inline const unsigned char* Buffer::getData() const
163 {
164  return data;
165 }
166 
167 //------------------------------------------------------------------------------
168 
169 inline unsigned char* Buffer::getData()
170 {
171  return data;
172 }
173 
174 //------------------------------------------------------------------------------
175 
176 inline size_t Buffer::getLength() const
177 {
178  return length;
179 }
180 
181 //------------------------------------------------------------------------------
182 
183 inline bool Buffer::isEmpty() const
184 {
185  return length==0;
186 }
187 
188 //------------------------------------------------------------------------------
189 
190 inline size_t Buffer::getAvailable() const
191 {
192  return capacity - length;
193 }
194 
195 //------------------------------------------------------------------------------
196 
197 inline void Buffer::reset()
198 {
199  length = 0;
200 }
201 
202 //------------------------------------------------------------------------------
203 
204 inline 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 inline size_t Buffer::append(const void* src, size_t size)
214 {
215  size_t toCopy = std::min(size, getAvailable());
216  memcpy(data + length, src, toCopy);
217  length += toCopy;
218  return toCopy;
219 }
220 
221 //------------------------------------------------------------------------------
222 
223 inline size_t Buffer::extract(void* dest, size_t size, size_t offset) const
224 {
225  size_t toCopy = (offset>=length) ? 0 : std::min(size, length - offset);
226  memcpy(dest, data + offset, toCopy);
227  return toCopy;
228 }
229 
230 //------------------------------------------------------------------------------
231 
232 } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
233 
234 //------------------------------------------------------------------------------
235 #endif // HU_VARADIISTVAN_SCPL_IO_BUFFER_H
236 
237 // Local Variables:
238 // mode: C++
239 // c-basic-offset: 4
240 // indent-tabs-mode: nil
241 // End:
size_t addLength(size_t n)
Definition: Buffer.h:204
const unsigned char * getData() const
Definition: Buffer.h:162
size_t extract(void *dest, size_t size, size_t offset=0) const
Definition: Buffer.h:223
size_t append(const void *src, size_t size)
Definition: Buffer.h:213