scsl 1.0.1
Shimmering Clarity Standard Library
|
Basic line buffer. More...
#include <Buffer.h>
Public Member Functions | |
Buffer () | |
Construct an empty buffer with no memory allocated. | |
Buffer (size_t initialCapacity) | |
Buffer (const char *s) | |
Construct with a C-style string. | |
Buffer (const std::string &s) | |
\buffer Construct with an initial string. | |
~Buffer () | |
uint8_t * | Contents () const |
Retrieve the buffer's contents. | |
std::string | ToString () const |
size_t | Length () const |
The length of data stored in the buffer. | |
size_t | Capacity () const |
Return the amount of memory allocated for the Buffer. | |
bool | Append (const char *s) |
Append a C-style string to the end of the buffer. | |
bool | Append (const std::string &s) |
bool | Append (const uint8_t *data, const size_t datalen) |
Append a byte buffer to the end of the buffer. | |
bool | Append (const uint8_t c) |
Append a single character to the end of the buffer. | |
bool | Insert (const size_t index, const char *s) |
Insert a C-style string into the buffer at index. | |
bool | Insert (const size_t index, const std::string &s) |
Insert a string into the buffer at index. | |
bool | Insert (const size_t index, const uint8_t *data, const size_t datalen) |
Insert a uint8_t buffer into the buffer at index. | |
bool | Insert (const size_t index, const uint8_t c) |
Insert a character into the buffer at index. | |
bool | Remove (const size_t index, const size_t count) |
Remove count bytes from the buffer at index . | |
bool | Remove (size_t index) |
Remove removes a single byte from the buffer. | |
void | Resize (size_t newCapacity) |
Changes the capacity of the buffer to newCapacity . | |
size_t | Trim () |
Resize the Buffer capacity based on its length. | |
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) |
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).
scsl::Buffer::Buffer | ( | ) |
Construct an empty buffer with no memory allocated.
|
explicit |
\buffer Constructor with explicit memory capacity.
initialCapacity | The initial allocation size for the buffer. |
|
explicit |
Construct with a C-style string.
|
explicit |
\buffer Construct with an initial string.
scsl::Buffer::~Buffer | ( | ) |
bool scsl::Buffer::Append | ( | const char * | s | ) |
Append a C-style string to the end of the buffer.
s | The string to append. |
bool scsl::Buffer::Append | ( | const std::string & | s | ) |
Append Append 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 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 a single character to the end of the buffer.
c | The character to append. |
bool scsl::Buffer::AutoTrimIsEnabled | ( | ) |
AutoTrimIsEnabled returns true if autotrim is enabled.
size_t scsl::Buffer::Capacity | ( | ) | const |
Return the amount of memory allocated for the Buffer.
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.
uint8_t * scsl::Buffer::Contents | ( | ) | const |
Retrieve the buffer's contents.
void scsl::Buffer::DisableAutoTrim | ( | ) |
void scsl::Buffer::EnableAutoTrim | ( | ) |
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 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 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 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 a character into the buffer at index.
index | The index to insert the character at. |
c | The character to insert. |
size_t scsl::Buffer::Length | ( | ) | const |
The length of data 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 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 | ) |
std::string scsl::Buffer::ToString | ( | ) | const |
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.