Class MessageCacheCircularBuffer

Inheritance Relationships

Base Type

Class Documentation

class MessageCacheCircularBuffer : public rosbag2_cpp::cache::CacheBufferInterface

This class implements a circular buffer message cache. Since the buffer size is limited by the total byte size of the storage messages or a total messages duration rather than a fixed number of messages, a deque is used instead of a vector since older messages can always be dropped from the front and new messages added to the end. The buffer will never consume more than max_cache_size bytes, if max_cache_size > 0, and will never exceed max_cache_duration in time span, if max_cache_duration > 0. The buffer will log a warning message if an individual message exceeds the buffer size.

Public Functions

MessageCacheCircularBuffer() = delete
explicit MessageCacheCircularBuffer(size_t max_cache_size, uint32_t max_cache_duration = 0)

Parametrized constructor.

Parameters:
  • max_cache_size – Maximum amount of memory which could be occupied by the messages stored in the circular buffer. Note: If max_cache_size is zero, the circular buffer will be only bounded by the max_cache_duration.

  • max_cache_duration – Maximum duration in seconds of message sequence allowed to be stored in the circular buffer. Note: If max_cache_duration is zero, the circular buffer will be only bounded by the max_cache_size. If both are non-zero, both limits apply.

Throws:

std::invalid_argument – if both max_cache_size and max_cache_duration are zero.

virtual bool push(CacheBufferInterface::buffer_element_t msg) override

Pushes a new message into the circular buffer.

If buffer has space remaining, the message is pushed regardless of its size, but if this results in exceeding the buffer size or duration limits, old messages are dropped.

Parameters:

msg – Shared pointer to the rosbag2_storage::SerializedBagMessage to add to the buffer.

Returns:

True if message was successfully pushed. Returns false if msg is null or if buffer_bytes_size_ > 0 and msg->serialized_data->buffer_length > max_cache_size.

virtual void clear() override

Clear buffer.

virtual size_t size() override

Get number of elements in the buffer.

virtual const std::vector<CacheBufferInterface::buffer_element_t> &data() override

Get buffer data.