Libftpp
A modern C++ library
RingBuffer Class Reference

RingBuffer class for managing a circular buffer of bytes. It provides methods to push and pop bytes, check buffer status, and retrieve buffer capacity and size. RingBuffer is a FIFO data structure The buffer operates in a circular manner, allowing efficient use of space. More...

#include <ring_buffer.hpp>

Public Member Functions

 RingBuffer ()
 
 RingBuffer (const size_t &size_buffer)
 
void push (const unsigned char &byte)
 
void push (const std::string &line)
 
void push (const std::vector< unsigned char > &bytes)
 
void pushInto (const void *data, const size_t &size)
 
unsigned char pop ()
 
std::vector< unsigned char > pop (const size_t &size)
 
void popInto (void *data, const size_t &size)
 
unsigned char peek () const
 
std::vector< unsigned char > peek (const size_t &size) const
 
void clear ()
 
bool isEmpty () const
 
bool isFull () const
 
size_t size () const
 
size_t capacity () const
 
size_t capacityAvailable () const
 

Detailed Description

RingBuffer class for managing a circular buffer of bytes. It provides methods to push and pop bytes, check buffer status, and retrieve buffer capacity and size. RingBuffer is a FIFO data structure The buffer operates in a circular manner, allowing efficient use of space.

RingBuffer ringBuffer(1024); // Create a ring buffer with a capacity of 1024 bytes
ringBuffer.push('A'); // Push a single byte
ringBuffer.push("Hello"); // Push a string
std::vector<unsigned char> data = {'W', 'o', 'r', 'l', 'd'};
ringBuffer.push(data); // Push a vector of bytes
unsigned char byte = ringBuffer.pop(); // Pop a single byte
std::vector<unsigned char> bytes = ringBuffer.pop(5); // Pop 5
std::vector<unsigned char> buffer(10);
ringBuffer.popInto(buffer.data(), buffer.size()); // Pop into a buffer
RingBuffer class for managing a circular buffer of bytes. It provides methods to push and pop bytes,...
Definition: ring_buffer.hpp:43
Note
By default the buffer size is MAX_BUFFER_SIZE
Exceptions
std::out_of_rangewhen trying to push to a full buffer or pop from an empty buffer.

Definition at line 42 of file ring_buffer.hpp.

Constructor & Destructor Documentation

◆ RingBuffer() [1/2]

RingBuffer::RingBuffer ( )

Definition at line 3 of file ring_buffer.cpp.

◆ RingBuffer() [2/2]

RingBuffer::RingBuffer ( const size_t &  size_buffer)

Definition at line 4 of file ring_buffer.cpp.

Member Function Documentation

◆ capacity()

size_t RingBuffer::capacity ( ) const

Definition at line 146 of file ring_buffer.cpp.

◆ capacityAvailable()

size_t RingBuffer::capacityAvailable ( ) const

Definition at line 151 of file ring_buffer.cpp.

Referenced by pushInto().

◆ clear()

void RingBuffer::clear ( )

Definition at line 156 of file ring_buffer.cpp.

◆ isEmpty()

bool RingBuffer::isEmpty ( ) const

Definition at line 131 of file ring_buffer.cpp.

Referenced by peek(), and pop().

◆ isFull()

bool RingBuffer::isFull ( ) const

Definition at line 136 of file ring_buffer.cpp.

Referenced by push().

◆ peek() [1/2]

unsigned char RingBuffer::peek ( ) const

Definition at line 101 of file ring_buffer.cpp.

References isEmpty().

◆ peek() [2/2]

std::vector< unsigned char > RingBuffer::peek ( const size_t &  size) const

Definition at line 111 of file ring_buffer.cpp.

References isEmpty(), and size().

◆ pop() [1/2]

unsigned char RingBuffer::pop ( )

Definition at line 44 of file ring_buffer.cpp.

References isEmpty().

◆ pop() [2/2]

std::vector< unsigned char > RingBuffer::pop ( const size_t &  size)

Definition at line 57 of file ring_buffer.cpp.

References size().

◆ popInto()

void RingBuffer::popInto ( void *  data,
const size_t &  size 
)

Definition at line 87 of file ring_buffer.cpp.

References size().

◆ push() [1/3]

void RingBuffer::push ( const std::string &  line)

Definition at line 9 of file ring_buffer.cpp.

References isFull(), and push().

◆ push() [2/3]

void RingBuffer::push ( const std::vector< unsigned char > &  bytes)

Definition at line 28 of file ring_buffer.cpp.

References isFull().

◆ push() [3/3]

void RingBuffer::push ( const unsigned char &  byte)

Definition at line 18 of file ring_buffer.cpp.

References isFull().

Referenced by push().

◆ pushInto()

void RingBuffer::pushInto ( const void *  data,
const size_t &  size 
)

Definition at line 73 of file ring_buffer.cpp.

References capacityAvailable(), and size().

◆ size()

size_t RingBuffer::size ( ) const

Definition at line 141 of file ring_buffer.cpp.

Referenced by peek(), pop(), popInto(), and pushInto().