scsl
0.2.0
Shimmering Clarity Standard Library
|
#include <Buffer.h>
Public Member Functions | |
Buffer () | |
A Buffer can be constructed empty, with no memory allocated (yet). | |
Buffer (size_t initialCapacity) | |
Buffer (const char *s) | |
A Buffer can be initialized with a starting C-style string. | |
Buffer (const std::string s) | |
A Buffer can be initialized with a starting string. | |
uint8_t * | Contents () const |
Contents returns the Buffer's contents. | |
size_t | Length () const |
size_t | Capacity () const |
Capacity returns the amount of memory allocated to the Buffer. | |
bool | Append (const char *s) |
bool | Append (const std::string s) |
bool | Append (const uint8_t *data, const size_t datalen) |
bool | Append (const uint8_t c) |
bool | Insert (const size_t index, const char *s) |
bool | Insert (const size_t index, const std::string s) |
bool | Insert (const size_t index, const uint8_t *data, const size_t datalen) |
bool | Insert (const size_t index, const uint8_t c) |
bool | Remove (const size_t index, const size_t count) |
bool | Remove (size_t index) |
void | Resize (size_t newCapacity) |
size_t | Trim () |
void | DisableAutoTrim () |
void | EnableAutoTrim () |
bool | AutoTrimIsEnabled () |
void | Clear () |
void | Reclaim () |
void | HexDump (std::ostream &os) |
uint8_t & | operator[] (size_t index) |
Friends | |
bool | operator== (const Buffer &lhs, const Buffer &rhs) |
Buffer is a basic line buffer.
The buffer manages its own internal memory, growing and shrinking as needed. Its capacity is separate from its length; the optimal capacity is determined as the nearest power of two that is greater than the length of the buffer. For example, if the buffer has a length of 5 bytes, 8 bytes will be allocated. If the buffer is 9 bytes, 16 bytes will be allocated.
The Append and Insert methods will call Resize as necessary to grow the buffer. Similarly the Remove methods will call Trim to reclaim some memory if possible, but only if AutoTrimIsEnabled (it is by default).
|
explicit |
A Buffer can be constructed with an explicit capacity.
initialCapacity | The initial allocation size for the buffer. |
bool scsl::Buffer::Append | ( | const char * | s | ) |
Append copies in a C-style string to the end of the buffer.
s | The string to append. |
bool scsl::Buffer::Append | ( | const std::string | s | ) |
Append copies in a string to the end of the buffer.
s | The string to append. |
bool scsl::Buffer::Append | ( | const uint8_t * | data, |
const size_t | datalen | ||
) |
Append copies in a byte buffer to the end of the buffer.
data | The byte buffer to insert. |
datalen | The length of the byte buffer. |
bool scsl::Buffer::Append | ( | const uint8_t | c | ) |
Append copies a single character to the end of the buffer.
c | The character to append. |
|
inline |
AutoTrimIsEnabled returns true if autotrim is enabled.
void scsl::Buffer::Clear | ( | ) |
Clear removes the data stored in the buffer. It will not call Trim; the capacity of the buffer will not be altered.
|
inline |
|
inline |
EnableAutoTrim enables automatically trimming memory after calls to Remove.
void scsl::Buffer::HexDump | ( | std::ostream & | os | ) |
HexDump dumps the data in the buffer to the output stream; it is intended as a debugging tool.
os | The output stream to write to. |
bool scsl::Buffer::Insert | ( | const size_t | index, |
const char * | s | ||
) |
Insert copies a C-style string into the buffer at index.
index | The index to insert the string at. |
s | The string to insert. |
bool scsl::Buffer::Insert | ( | const size_t | index, |
const std::string | s | ||
) |
Insert copies a string into the buffer at index.
index | The index the string should be inserted at. |
s | The string to insert. |
bool scsl::Buffer::Insert | ( | const size_t | index, |
const uint8_t * | data, | ||
const size_t | datalen | ||
) |
Insert copies a uint8_t buffer into the buffer at index.
index | The index to insert the buffer at. |
data | The buffer to insert. |
datalen | The size of the data buffer. |
bool scsl::Buffer::Insert | ( | const size_t | index, |
const uint8_t | c | ||
) |
Insert copies a character into the buffer at index.
index | The index to insert the character at. |
c | The character to insert. |
|
inline |
Length returns the length of the data currently stored in the buffer.
uint8_t & scsl::Buffer::operator[] | ( | size_t | index | ) |
This operator allows the data in the buffer to be accessed as if it were an array. If the index is out of bounds, it will throw a range_error.
std::range_error. |
index | The index to retrieve. |
void scsl::Buffer::Reclaim | ( | ) |
Reclaim the memory in the buffer: the buffer will call Clear, followed by deleting any allocated memory.
bool scsl::Buffer::Remove | ( | const size_t | index, |
const size_t | count | ||
) |
Remove removes count
bytes from the buffer at index
.
index | The starting index to remove bytes from. |
count | The number of bytes to remove. |
bool scsl::Buffer::Remove | ( | size_t | index | ) |
Remove removes a single byte from the buffer.
index | The index pointing to the byte to be removed. |
void scsl::Buffer::Resize | ( | size_t | newCapacity | ) |
size_t scsl::Buffer::Trim | ( | ) |
Two buffers are equal if their lengths are the same and their contents are the same. Equality is irrespective of their capacities.