sptk2 logo
SPTK Home Page
Buffer.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/BufferStorage.h>
30
31#include <cstdlib>
32#include <cstring>
33#include <iostream>
34#include <memory>
35#include <string>
36
37namespace sptk {
38
49class SP_EXPORT Buffer
50 : public BufferStorage
51{
52
53public:
55
64 explicit Buffer(const String& str);
65
72 Buffer(const Buffer& other) = default;
73
80 Buffer(Buffer&& other) noexcept = default;
81
85 ~Buffer() noexcept override = default;
86
92 Buffer& operator=(Buffer&& other) DOESNT_THROW = default;
93
99 Buffer& operator=(const Buffer& other) = default;
100
107 void append(char ch) override
108 {
110 }
111
119 void append(const char* data, size_t sz = 0) override
120 {
121 BufferStorage::append(data, sz);
122 }
123
131 void append(const uint8_t* data, size_t sz) override
132 {
133 BufferStorage::append(data, sz);
134 }
135
142 template<class T>
143 void append(T val)
144 {
145 append((char*) &val, sizeof(val));
146 }
147
154 void append(const std::string& str)
155 {
156 return append(str.c_str(), str.length());
157 }
158
165 void append(const String& str)
166 {
167 return append(str.c_str(), str.length());
168 }
169
176 void append(const Buffer& buffer)
177 {
178 return append(buffer.data(), buffer.bytes());
179 }
180
185 uint8_t& operator[](size_t index)
186 {
187 return data()[index];
188 }
189
194 const uint8_t& operator[](size_t index) const
195 {
196 return data()[index];
197 }
198
204 bool operator==(const Buffer& other) const;
205
211 bool operator!=(const Buffer& other) const;
212
217 void loadFromFile(const fs::path& fileName);
218
223 void saveToFile(const fs::path& fileName) const;
224
230 Buffer& operator=(const String& str);
231
237 Buffer& operator=(const char* str);
238
242 explicit operator String() const
243 {
244 return String((const char*) data(), bytes());
245 }
246};
247
248using SBuffer = std::shared_ptr<Buffer>;
249
253SP_EXPORT std::ostream& operator<<(std::ostream&, const Buffer& buffer);
254
258} // namespace sptk
Definition: BufferStorage.h:44
BufferStorage()
Definition: BufferStorage.h:51
size_t bytes() const
Definition: BufferStorage.h:213
virtual void append(char ch)
uint8_t * data()
Definition: BufferStorage.h:106
Definition: Buffer.h:51
Buffer & operator=(const char *str)
void append(const uint8_t *data, size_t sz) override
Definition: Buffer.h:131
void loadFromFile(const fs::path &fileName)
Buffer(const String &str)
void append(const std::string &str)
Definition: Buffer.h:154
void saveToFile(const fs::path &fileName) const
Buffer & operator=(const String &str)
bool operator!=(const Buffer &other) const
const uint8_t & operator[](size_t index) const
Definition: Buffer.h:194
~Buffer() noexcept override=default
void append(T val)
Definition: Buffer.h:143
void append(const String &str)
Definition: Buffer.h:165
Buffer(Buffer &&other) noexcept=default
Buffer(const Buffer &other)=default
void append(const Buffer &buffer)
Definition: Buffer.h:176
bool operator==(const Buffer &other) const
void append(const char *data, size_t sz=0) override
Definition: Buffer.h:119
uint8_t & operator[](size_t index)
Definition: Buffer.h:185
Definition: String.h:49
SP_EXPORT std::ostream & operator<<(std::ostream &, const Buffer &buffer)

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