scsl  0.2.4
Shimmering Clarity Standard Library
Buffer.h
Go to the documentation of this file.
1 
27 #ifndef KGE_BUFFER_H
28 #define KGE_BUFFER_H
29 
30 #include <cstdint>
31 #include <iostream>
32 
33 
34 namespace scsl {
35 
48 class Buffer {
49 public:
51  Buffer();
52 
56  explicit Buffer(size_t initialCapacity);
57 
59  explicit Buffer(const char *s);
60 
62  explicit Buffer(const std::string s);
63 
65  { this->Reclaim(); }
66 
68  uint8_t *Contents() const
69  { return this->contents; }
70 
73  size_t Length() const
74  { return this->length; };
75 
77  size_t Capacity() const
78  { return this->capacity; }
79 
84  bool Append(const char *s);
85 
90  bool Append(const std::string s);
91 
97  bool Append(const uint8_t *data, const size_t datalen);
98 
103  bool Append(const uint8_t c);
104 
110  bool Insert(const size_t index, const char *s);
111 
117  bool Insert(const size_t index, const std::string s);
118 
125  bool
126  Insert(const size_t index, const uint8_t *data, const size_t datalen);
127 
133  bool Insert(const size_t index, const uint8_t c);
134 
140  bool Remove(const size_t index, const size_t count);
141 
146  bool Remove(size_t index); // remove single char
147 
148  /* memory management */
149 
156  void Resize(size_t newCapacity);
157 
162  size_t Trim();
163 
167  { this->autoTrim = false; }
168 
172  { this->autoTrim = true; }
173 
178  { return this->autoTrim; }
179 
182  void Clear();
183 
186  void Reclaim();
187 
192  void HexDump(std::ostream &os);
193 
202  uint8_t &operator[](size_t index);
203 
207  friend bool operator==(const Buffer &lhs, const Buffer &rhs);
208 
209 private:
210  size_t mustGrow(size_t delta) const;
211 
212  bool shiftRight(size_t offset, size_t delta);
213 
214  bool shiftLeft(size_t offset, size_t delta);
215 
216  uint8_t *contents;
217  size_t length;
218  size_t capacity;
219  bool autoTrim;
220 };
221 
223 std::ostream &operator<<(std::ostream &os, const Buffer &buf);
224 
227 inline bool operator!=(const Buffer &lhs, const Buffer &rhs) { return !(lhs == rhs); };
228 
229 } // namespace scsl
230 
231 
232 #endif // KGE_BUFFER_H
Definition: Buffer.h:48
size_t Capacity() const
Capacity returns the amount of memory allocated to the Buffer.
Definition: Buffer.h:77
void HexDump(std::ostream &os)
Definition: Buffer.cc:260
Buffer()
A Buffer can be constructed empty, with no memory allocated (yet).
Definition: Buffer.cc:61
void EnableAutoTrim()
Definition: Buffer.h:171
size_t Length() const
Definition: Buffer.h:73
friend bool operator==(const Buffer &lhs, const Buffer &rhs)
Definition: Buffer.cc:358
~Buffer()
Definition: Buffer.h:64
void Clear()
Definition: Buffer.cc:221
uint8_t & operator[](size_t index)
Definition: Buffer.cc:344
size_t Trim()
Definition: Buffer.cc:206
void Resize(size_t newCapacity)
Definition: Buffer.cc:184
uint8_t * Contents() const
Contents returns the Buffer's contents.
Definition: Buffer.h:68
void DisableAutoTrim()
Definition: Buffer.h:166
bool Append(const char *s)
Definition: Buffer.cc:92
bool Remove(const size_t index, const size_t count)
Definition: Buffer.cc:167
bool AutoTrimIsEnabled()
Definition: Buffer.h:177
bool Insert(const size_t index, const char *s)
Definition: Buffer.cc:133
void Reclaim()
Definition: Buffer.cc:232
scsl is the top-level namespace containing all the code in this library.
Definition: scsl.h:43
std::ostream & operator<<(std::ostream &os, Arena &arena)
Definition: Arena.cc:299
bool operator!=(const Buffer &lhs, const Buffer &rhs)
Definition: Buffer.h:227