libfilezilla
Loading...
Searching...
No Matches
basic_buffer< Policy > Class Template Reference

The buffer class is a simple buffer where data can be appended at the end and consumed at the front. Think of it as a deque with contiguous storage. More...

#include <buffer.hpp>

Public Types

using value_type = unsigned char

Public Member Functions

 basic_buffer (size_t capacity)
 Initially reserves the passed capacity.
 basic_buffer (std::string_view const &data)
 basic_buffer (basic_buffer const &buf)
 basic_buffer (basic_buffer &&buf) noexcept
basic_buffer & operator= (basic_buffer const &buf)
basic_buffer & operator= (basic_buffer &&buf) noexcept
basic_buffer & operator= (std::string_view const &v)
unsigned char const * get () const
 Undefined if buffer is empty.
unsigned char * get ()
unsigned char * data ()
 Same as get().
unsigned char const * data () const
unsigned char * get (size_t write_size)
 Returns a writable buffer guaranteed to be large enough for write_size bytes, call add when done.
void add (size_t added)
 Increase size by the passed amount. Call this after having obtained a writable buffer with get(size_t write_size).
template<typename T, std::enable_if_t< std::is_signed_v< T >, int > = 0>
void add (T added)
 Overload of add for signed types, only adds if value is positive.
void consume (size_t consumed)
 Removes consumed bytes from the beginning of the buffer.
size_t size () const
void clear ()
void clear_and_free ()
void append (unsigned char const *data, size_t len)
 Appends the passed data to the buffer.
void append (std::string_view const &str)
void append (std::basic_string_view< uint8_t > const &data)
void append (std::vector< uint8_t > const &data)
void append (basic_buffer const &b)
void append (unsigned char v)
void append (size_t len, unsigned char c)
void push_back (unsigned char v)
basic_buffer & operator+= (unsigned char v)
basic_buffer & operator+= (std::string_view const &str)
basic_buffer & operator+= (std::vector< uint8_t > const &data)
basic_buffer & operator+= (basic_buffer const &b)
bool empty () const
 operator bool () const
size_t capacity () const
void reserve (size_t capacity)
void resize (size_t size)
unsigned char operator[] (size_t i) const
 Gets element at offset i. Does not do bounds checking.
unsigned char & operator[] (size_t i)
bool operator== (basic_buffer const &rhs) const
bool operator== (std::string_view const &rhs) const
bool operator!= (basic_buffer const &rhs) const
bool operator!= (std::string_view const &rhs) const
std::string_view to_view () const
void wipe ()
void wipe_unused ()

Detailed Description

template<buffer_wipe_policy Policy>
class fz::basic_buffer< Policy >

The buffer class is a simple buffer where data can be appended at the end and consumed at the front. Think of it as a deque with contiguous storage.

This class is useful when buffering data for sending over the network, or for buffering data for further piecemeal processing after having received it.

In general, copying/moving data around is expensive and allocations are even more expensive. Using this class helps to limit both to the bare minimum.

Member Function Documentation

◆ add()

template<buffer_wipe_policy Policy>
template<typename T, std::enable_if_t< std::is_signed_v< T >, int > = 0>
void add ( T added)
inline

Overload of add for signed types, only adds if value is positive.

Does nothing on values <= 0, useful for directly passing the ssize_t result of the system's recv() or read() functions.

◆ append()

template<buffer_wipe_policy Policy>
void append ( unsigned char const * data,
size_t len )

Appends the passed data to the buffer.

The number of reallocations as result to repeated append are amortized O(1)

◆ clear()

template<buffer_wipe_policy Policy>
void clear ( )

Does not release the memory.

◆ consume()

template<buffer_wipe_policy Policy>
void consume ( size_t consumed)

Removes consumed bytes from the beginning of the buffer.

Undefined if consumed > size()

◆ get()

template<buffer_wipe_policy Policy>
unsigned char * get ( size_t write_size)

Returns a writable buffer guaranteed to be large enough for write_size bytes, call add when done.

The returned pointer is pointing just after the data already stored in the buffer.

Calling this function does not does not affect size().

See also
append
Example:
size_t to_read = 44;
int read = recv(fd, buf.get(to_read), to_read, 0); // Read stuff from some socket
if (read > 0)
buf.add(read); // Adjust size with the amount actually written into the buffer
unsigned char const * get() const
Undefined if buffer is empty.
Definition buffer.hpp:58
void add(size_t added)
Increase size by the passed amount. Call this after having obtained a writable buffer with get(size_t...
Definition buffer.hpp:200
@ read
Data has become available.
Definition process.hpp:29

The documentation for this class was generated from the following file: