scsl 0.1.1
Shimmering Clarity Standard Library
Buffer.h
Go to the documentation of this file.
1
11
12#ifndef KGE_BUFFER_H
13#define KGE_BUFFER_H
14
15#include <iostream>
16#include <cstdint>
17
18
19namespace scsl {
20
33class Buffer {
34public:
36 Buffer();
37
41 explicit Buffer(size_t initialCapacity);
42
44 explicit Buffer(const char *s);
45
47 explicit Buffer(const std::string s);
48
49 ~Buffer()
50 { this->Reclaim(); }
51
53 uint8_t *Contents() const
54 { return this->contents; }
55
58 size_t Length() const
59 { return this->length; };
60
62 size_t Capacity() const
63 { return this->capacity; }
64
69 bool Append(const char *s);
70
75 bool Append(const std::string s);
76
82 bool Append(const uint8_t *data, const size_t datalen);
83
88 bool Append(const uint8_t c);
89
95 bool Insert(const size_t index, const char *s);
96
102 bool Insert(const size_t index, const std::string s);
103
110 bool
111 Insert(const size_t index, const uint8_t *data, const size_t datalen);
112
118 bool Insert(const size_t index, const uint8_t c);
119
125 bool Remove(const size_t index, const size_t count);
126
131 bool Remove(size_t index); // remove single char
132
133 /* memory management */
134
141 void Resize(size_t newCapacity);
142
147 size_t Trim();
148
152 { this->autoTrim = false; }
153
157 { this->autoTrim = true; }
158
163 { return this->autoTrim; }
164
167 void Clear();
168
171 void Reclaim();
172
177 void HexDump(std::ostream &os);
178
187 uint8_t &operator[](size_t index);
188
192 friend bool operator==(const Buffer &lhs, const Buffer &rhs);
193
194private:
195 size_t mustGrow(size_t delta) const;
196
197 bool shiftRight(size_t offset, size_t delta);
198
199 bool shiftLeft(size_t offset, size_t delta);
200
201 uint8_t *contents;
202 size_t length;
203 size_t capacity;
204 bool autoTrim;
205};
206
208std::ostream &operator<<(std::ostream &os, const Buffer &buf);
209
212inline bool operator!=(const Buffer &lhs, const Buffer &rhs) { return !(lhs == rhs); };
213
214} // namespace scsl
215
216
217#endif // KGE_BUFFER_H
Definition: Buffer.h:33
size_t Capacity() const
Capacity returns the amount of memory allocated to the Buffer.
Definition: Buffer.h:62
void HexDump(std::ostream &os)
Definition: Buffer.cc:246
uint8_t * Contents() const
Contents returns the Buffer's contents.
Definition: Buffer.h:53
Buffer()
A Buffer can be constructed empty, with no memory allocated (yet).
Definition: Buffer.cc:47
void EnableAutoTrim()
Definition: Buffer.h:156
size_t Length() const
Definition: Buffer.h:58
friend bool operator==(const Buffer &lhs, const Buffer &rhs)
Definition: Buffer.cc:344
void Clear()
Definition: Buffer.cc:207
uint8_t & operator[](size_t index)
Definition: Buffer.cc:330
size_t Trim()
Definition: Buffer.cc:192
void Resize(size_t newCapacity)
Definition: Buffer.cc:170
void DisableAutoTrim()
Definition: Buffer.h:151
bool Append(const char *s)
Definition: Buffer.cc:78
bool Remove(const size_t index, const size_t count)
Definition: Buffer.cc:153
bool AutoTrimIsEnabled()
Definition: Buffer.h:162
bool Insert(const size_t index, const char *s)
Definition: Buffer.cc:119
void Reclaim()
Definition: Buffer.cc:218
scsl is the top-level namespace containing all the code in this library.
Definition: scsl.h:37
std::ostream & operator<<(std::ostream &os, Arena &arena)
Definition: Arena.cc:281
bool operator!=(const Buffer &lhs, const Buffer &rhs)
Definition: Buffer.h:212