40 node_(node), head_(0), tail_(0), size_(0), capacity_(capacity)
59 std::size_t bytes_to_write = std::min(bytes, capacity -
size_);
60 if (bytes_to_write != bytes)
64 "You are trying to overwrite parts of the circular buffer that have not yet been read!");
68 if (bytes_to_write <= capacity -
head_)
72 head_ += bytes_to_write;
73 if (
head_ == capacity)
79 std::size_t size_1 = capacity -
head_;
80 memcpy(
data_ + head_, data, size_1);
81 std::size_t size_2 = bytes_to_write - size_1;
82 memcpy(
data_, data + size_1, size_2);
86 size_ += bytes_to_write;
87 return bytes_to_write;
95 std::size_t bytes_to_read = std::min(bytes,
size_);
96 if (bytes_to_read != bytes)
100 "You are trying to read parts of the circular buffer that have not yet been written!");
111 tail_ += bytes_to_read;
112 if (
tail_ == capacity)
119 std::size_t size_1 = capacity -
tail_;
120 memcpy(data,
data_ + tail_, size_1);
121 std::size_t size_2 = bytes_to_read - size_1;
122 memcpy(data + size_1,
data_, size_2);
126 size_ -= bytes_to_read;
127 return bytes_to_read;
std::size_t read(uint8_t *data, std::size_t bytes)
Returns number of bytes read.
ROSaicNodeBase * node_
Pointer to the node.
std::size_t capacity() const
Returns capacity_.
void log(LogLevel logLevel, const std::string &s)
Log function to provide abstraction of ROS loggers.
std::size_t write(const uint8_t *data, std::size_t bytes)
Returns number of bytes written.
std::size_t size_
Number of bytes that have been written but not yet read.
CircularBuffer(ROSaicNodeBase *node, std::size_t capacity)
Constructor of CircularBuffer.
~CircularBuffer()
Destructor of CircularBuffer.
std::size_t capacity_
Capacity of the circular buffer.
std::size_t head_
Specifies where we start writing.
Declares a class for creating, writing to and reading from a circular bufffer.
std::size_t tail_
Specifies where we start reading.
This class is the base class for abstraction.