Buffer¶
Buffers are all about reading and writing data. The evbuffer is a simple utility
within libevent that is used throughout the code-base where data is involved. The Buffer
class in Pembroke wraps that functionality, but with an interface that is more idiomatic
to C++.
pembroke::Buffer¶
-
class
pembroke::Buffer¶ Buffer is a simple wrapper over libevent’s
evbufferthat is needed when reading or writing to/from a network or file-system.You may find that not all methods or utilities are are possible with an
evbufferare exposed as only the methods essential to this libraries functionalities are included.- Note
While the underlying C-style API mostly represents contents as strings, or bytes via
unsigned char, you can also decide to work purely withstd::bytemethods. To readstd::bytes(and avoid copies), you’ll have to work with ByteSlice, which is a poor man’sstd:spanin a pre-C++20 world.- See
Public Functions
-
Buffer() noexcept¶ Construct a new, empty buffer object.
-
Buffer(Buffer&&) noexcept¶ Move-construct a buffer-object. The underlying
evbufferwill be transferred into the newly constructed class.
-
auto
operator=(Buffer&&) noexcept -> Buffer&¶ Move-assign a buffer-object. The underlying
evbufferwill be transferred into the newly constructed class.
-
void
add(std::string_view str_view) noexcept¶ Write a string-view of data into the buffer. The data being pointed to will be copied into the buffer.
- Note
Useful for writing
charliterals orstd::stringand avoid unnecessary copyig of the data.- Parameters
str_view: View of data to write to buffer
-
void
add(const char *c_str, size_t n_chars) noexcept¶ Write a c-style string for given number of characters. The sub-section of data will be copied into the buffer.
- Pre
n_charsshould not be larger than the length ofc_stras this method does not check for null-terminating characters.- Parameters
c_str: The C-style string to add to the buffern_chars: The number of characters to write, from the start of the string
-
void
add(const std::byte *bytes, size_t n_bytes) noexcept¶ Write an array of bytes, the length of
n_bytes. The data will be copied into the buffer.- Pre
n_bytesshould not be larger than the length ofbytes.- See
- Parameters
bytes: Pointer to head ofstd::bytearrayn_bytes: The number ofstd::bytes to write to he buffer, starting frombytes.
-
void
add(const ByteSlice &byte_slice) noexcept¶ Write an array of bytes. The data will be copied into the buffer.
Very similar to
Buffer::add(const std::byte *, size_t), but using the ByteSlice. The two methods are identical in functionality, but with this method adding parity to the read method:Buffer::bytes().- See
- Parameters
byte_slice: Slice ofstd::bytes to append to the buffer.
-
auto
length() noexcept -> size_t¶ Return the length of the data currently stored in the buffer. Same as Buffer::size().
-
auto
size() noexcept -> size_t¶ Return the length of the data currently stored in the buffer. Same as Buffer::length().
-
auto
view_str() noexcept -> std::string_view¶ Return a string-view into the buffer.
-
auto
str() noexcept -> std::string¶ Return a
std::stringof the buffer contents. Note that this will perform a copy of the underlying buffer data. If no copies are desired, see Buffer::view_str() or Buffer::bytes() methods.