https://github.com/niclasdimitriadis/ringbufferrange
Simple implementation of a ring buffer that makes its content accessible to STL algorithms including modern range based algorithms first introduced in C++20
https://github.com/niclasdimitriadis/ringbufferrange
cpp20 iterator ranges ringbuffer
Last synced: 7 months ago
JSON representation
Simple implementation of a ring buffer that makes its content accessible to STL algorithms including modern range based algorithms first introduced in C++20
- Host: GitHub
- URL: https://github.com/niclasdimitriadis/ringbufferrange
- Owner: NiclasDimitriadis
- License: mit
- Created: 2025-05-31T23:49:35.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-06-01T00:05:53.000Z (7 months ago)
- Last Synced: 2025-06-01T10:23:07.888Z (7 months ago)
- Topics: cpp20, iterator, ranges, ringbuffer
- Language: C++
- Homepage:
- Size: 62.5 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## RingBufferRange
Simple implementation of a ring buffer that makes its content accessible to STL algorithms including modern range based algorithms first introduced in C++20
- exposes an `Iterator` class for accessing entries
- only forward iteration is supported
#### RingBufferRange class template
`template`
`requires std::is_default_constructible_v && std::is_nothrow_copy_assignable_v`
`&& (std::has_single_bit(length))`
`struct RingBufferRange`
- `ContentType`: type that will be enqueued
- type constaints ensure that queue elements can be default constructed when the queue object is constructed, (nothrow) copy assigned when an element is enqueued and (nothrow) copy constructed when it's dequeued
- `length`: max number of elements the queue can contain, determines the size of the ring buffer
- constrained to powers of two to prevent costly modulus operations when computing indices in ring buffer
### relevant members and interfaces
#### `Iterator`
- forward iterator type exposed to access content of ring buffer
#### `Iterator::Iterator(ContentType* const, ContentType* const)`
- constructor for iterator member type, takes a pointer to the base of the ring buffer and a pointer to the first element as arguments
#### `Iterator::operator*`
`ContentType& Iterator::operator*() const noexcept`
- dereference operator
- returns a reference to the buffere entry the iterator object currently points to
#### `Iterator::operator++`
`const Iterator& Iterator::operator++() noexcept`
- increments iterator to next position in buffer
#### `Iterator::operator==`
`bool Iterator::operator==(const Iterator&) const noexcept`
- comparison operator
- determines whether two iterators point to the same position within the buffer
#### `begin`
`Iterator begin() const noexcept`
- returns an `Iterator` object pointing to the position after the last dequeued entry within the buffer
- points to the first element if buffer is not empty
#### `end`
`Iterator end() const noexcept`
- returns an `Iterator` pointing to the position after the last enqueued entry within the buffer
- points to position behind last element if buffer is not empty
#### `n_entries`
`std::uint64_t n_entries() const noexcept`
- returns the number of entries currently currently contained in the ring buffer
#### `pop`
`bool pop() noexcept`
- removes first entry from buffer if buffer is not empty
- returns `false` if buffer was not empty and `true` otherwise
#### `enqueue`
`bool enqueue(const ContentType&) noexcept`
- adds a new entry to the buffer provided its not currently filled to capacity
- returns `true` if entry could be added and `false` otherwise
#### `dequeue`
`std::optional dequeue() noexcept`
- removes the first element from the buffer provided the buffer is not empty and returns it within a `std::optional`
- returns a `std::optional` without a values if buffer was empty
#### `emplace`
`template`
`requires std::is_nothrow_constructible_v && std::is_move_assignable_v`
`bool emplace(Args... args) noexcept`
- contructs an entry in-place using `args` as arguments for the constructor of `ContentType`