54 if (bytes == 0)
return 0;
57 std::size_t bytes_to_write = std::min(bytes, capacity -
size_);
59 if (bytes_to_write != bytes)
61 ROS_ERROR(
"You are trying to overwrite parts of the circular buffer that have not yet been read!");
65 if (bytes_to_write <= capacity -
head_)
69 head_ += bytes_to_write;
75 std::size_t size_1 = capacity -
head_;
76 memcpy(
data_ + head_, data, size_1);
77 std::size_t size_2 = bytes_to_write - size_1;
78 memcpy(
data_, data + size_1, size_2);
81 size_ += bytes_to_write;
83 return bytes_to_write;
89 if (bytes == 0)
return 0;
91 std::size_t bytes_to_read = std::min(bytes,
size_);
93 if (bytes_to_read != bytes)
95 ROS_ERROR(
"You are trying to read parts of the circular buffer that have not yet been written!");
99 if (bytes_to_read <= capacity -
tail_)
103 tail_ += bytes_to_read;
110 std::size_t size_1 = capacity -
tail_;
111 memcpy(data,
data_ + tail_, size_1);
112 std::size_t size_2 = bytes_to_read - size_1;
113 memcpy(data + size_1,
data_, size_2);
117 size_ -= bytes_to_read;
119 return bytes_to_read;
std::size_t tail_
Specifies where we start reading.
uint8_t * data_
Pointer that always points to the same memory address, hence could be const pointer.
std::size_t size_
Number of bytes that have been written but not yet read.
std::size_t write(const uint8_t *data, std::size_t bytes)
Returns number of bytes written.
CircularBuffer(std::size_t capacity)
Constructor of CircularBuffer.
std::size_t capacity_
Capacity of the circular buffer.
std::size_t head_
Specifies where we start writing.
std::size_t capacity() const
Returns capacity_.
Declares a class for creating, writing to and reading from a circular bufffer.
std::size_t read(uint8_t *data, std::size_t bytes)
Returns number of bytes read.
~CircularBuffer()
Destructor of CircularBuffer.