Projects / API
std::ring
A C++ standard library-like implementation of a ring buffer.
- Status
- Complete
- Type
- API
- Origin
- Personal
- Duration
- 2017
- Role
- Creator
- Contribution
- Original idea, design, and implementation.
Case study
Project details
The std::ring class was my first attempt at really trying to make C++ code that looked and behaved like C++. It was an application of almost every C++ paradigm I knew at the time: Templates, rule of 5, iterators, and RAII.
Download the header file from GitHub
The project uses the GNU GENERAL PUBLIC LICENSE Version 2 so you are free to copy, redistribute, and modify it.
The data structure can be instantiated similarly to many standard library containers. The one exception is that since a ring has a fixed size, it does not have a default constructor and cannot be instantiated with size 0.
Here is some example usage.
// A ring with 8 slots.
std::ring<int> testRing1(8);
// Create a ring using an initializer list.
std::initializer_list<int> il = { 0, 1, 2, 3, 4, 5, 6 };
std::ring<int> testRing2(il);
// Create a ring by copying from an iterator.
std::vector<int> ringInput{ 0, 1, 2, 3, 4, 5, 6 };
std::ring<int> testRing3(ringInput.begin(), ringInput.end());
// Range-based for loops are supported.
for(auto value : testRing3)
{
std::cout << value << std::endl;
}
// Manually move the ring pointer and set values.
testRing3.advance();
testRing3.current() = 20;
testRing3.retreat()
Resources