sptk2 logo
SPTK Home Page
BufferStorage.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <sptk5/Exception.h>
30#include <sptk5/sptk.h>
31#include <string.h>
32
33namespace sptk {
34
43class SP_EXPORT BufferStorage
44{
45public:
52 : m_buffer(16)
53 {
54 }
55
63 explicit BufferStorage(size_t sz)
64 : m_buffer(sz + 1)
65 {
66 }
67
72 BufferStorage(const BufferStorage& bufferStorage) = default;
73
78 BufferStorage(BufferStorage&& bufferStorage) noexcept = default;
79
83 virtual ~BufferStorage() = default;
84
85 BufferStorage& operator=(const BufferStorage& bufferStorage) = default;
86 BufferStorage& operator=(BufferStorage&& bufferStorage) noexcept = default;
87
97 template<typename T>
98 BufferStorage(const T* data, size_t sz)
99 {
100 allocate((const uint8_t*) data, sz);
101 }
102
106 uint8_t* data()
107 {
108 return m_buffer.data();
109 }
110
114 const uint8_t* data() const
115 {
116 return m_buffer.data();
117 }
118
122 const char* c_str() const
123 {
124 return (const char*) m_buffer.data();
125 }
126
130 bool empty() const
131 {
132 return m_bytes == 0;
133 }
134
141 virtual void checkSize(size_t sz)
142 {
143 if (sz + 1 >= m_buffer.size())
144 {
145 adjustSize(sz);
146 }
147 }
148
156 template<typename T>
157 void set(const T* data, size_t sz)
158 {
159 _set((const uint8_t*) data, sz);
160 }
161
168 void set(const BufferStorage& data)
169 {
170 if (data.m_bytes == 0)
171 {
172 m_bytes = 0;
173 }
174 else
175 {
176 _set(data.m_buffer.data(), data.m_bytes);
177 }
178 }
179
186 void set(const String& data)
187 {
188 _set((const uint8_t*) data.c_str(), data.length());
189 }
190
195 size_t capacity() const
196 {
197 return m_buffer.size() - 1;
198 }
199
204 size_t length() const
205 {
206 return m_bytes;
207 }
208
213 size_t bytes() const
214 {
215 return m_bytes;
216 }
217
224 void bytes(size_t b)
225 {
226 if (m_bytes == b)
227 {
228 return;
229 }
230 if (b + 1 <= m_buffer.size())
231 {
232 m_bytes = b;
233 m_buffer[b] = 0;
234 return;
235 }
236 throw Exception("Attempt to set buffer size outside storage");
237 }
238
245 virtual void append(char ch);
246
254 virtual void append(const char* data, size_t sz = 0);
255
263 virtual void append(const uint8_t* data, size_t sz);
264
271 void reset(size_t sz = 0);
272
278 void fill(char ch, size_t count);
279
285 void erase(size_t offset, size_t length);
286
287protected:
292 void adjustSize(size_t sz);
293
298 void allocate(size_t size)
299 {
300 m_buffer.resize(size + 1);
301 m_bytes = 0;
302 m_buffer[size] = 0;
303 }
304
309 void allocate(const uint8_t* data, size_t size)
310 {
311 m_buffer.resize(size + 1);
312 m_bytes = size;
313 if (data != nullptr && size != 0)
314 {
315 memcpy(m_buffer.data(), data, size);
316 }
317 m_buffer[size] = 0;
318 }
319
324 void reallocate(size_t size)
325 {
326 m_buffer.resize(size + 1);
327 if (m_bytes > size)
328 {
329 m_bytes = size;
330 }
331 m_buffer[size] = 0;
332 }
333
337 void deallocate() noexcept
338 {
339 m_buffer.resize(1);
340 m_buffer[0] = 0;
341 m_bytes = 0;
342 }
343
344 void init(const uint8_t* data, size_t size, size_t bytes)
345 {
346 allocate(data, size);
347 m_bytes = bytes;
348 }
349
350private:
351 std::vector<uint8_t> m_buffer;
352 size_t m_bytes {0};
353
354
362 void _set(const uint8_t* data, size_t sz);
363};
364
368} // namespace sptk
Definition: BufferStorage.h:44
size_t length() const
Definition: BufferStorage.h:204
void fill(char ch, size_t count)
void erase(size_t offset, size_t length)
void adjustSize(size_t sz)
void bytes(size_t b)
Definition: BufferStorage.h:224
void reallocate(size_t size)
Definition: BufferStorage.h:324
BufferStorage(size_t sz)
Definition: BufferStorage.h:63
BufferStorage()
Definition: BufferStorage.h:51
void set(const T *data, size_t sz)
Definition: BufferStorage.h:157
size_t bytes() const
Definition: BufferStorage.h:213
void allocate(const uint8_t *data, size_t size)
Definition: BufferStorage.h:309
virtual void append(const uint8_t *data, size_t sz)
BufferStorage(const T *data, size_t sz)
Definition: BufferStorage.h:98
virtual void checkSize(size_t sz)
Definition: BufferStorage.h:141
void set(const String &data)
Definition: BufferStorage.h:186
void deallocate() noexcept
Definition: BufferStorage.h:337
virtual void append(char ch)
void reset(size_t sz=0)
const char * c_str() const
Definition: BufferStorage.h:122
virtual ~BufferStorage()=default
void allocate(size_t size)
Definition: BufferStorage.h:298
virtual void append(const char *data, size_t sz=0)
const uint8_t * data() const
Definition: BufferStorage.h:114
BufferStorage(BufferStorage &&bufferStorage) noexcept=default
void set(const BufferStorage &data)
Definition: BufferStorage.h:168
BufferStorage(const BufferStorage &bufferStorage)=default
uint8_t * data()
Definition: BufferStorage.h:106
size_t capacity() const
Definition: BufferStorage.h:195
bool empty() const
Definition: BufferStorage.h:130
SPTK generic exception class.
Definition: Exception.h:56
Definition: String.h:49

Fri Oct 14 2022 09:58:31: SPTK 5.4.1