|
| 1 | +# RingBuffer |
| 2 | + |
| 3 | +[](https://github.com/bugparty/RingBufferCpp/actions/workflows/cmake.yml) |
| 4 | +[](LICENSE) |
| 5 | +[](https://en.cppreference.com/w/cpp/17) |
| 6 | +[](#) |
| 7 | + |
| 8 | +A modern, header-only C++ implementation of a fixed-size circular buffer (ring buffer), designed for performance, safety, and ease of testing with Google Test. |
| 9 | + |
| 10 | +## Features |
| 11 | + |
| 12 | +- 🌀 Templated for any type `T` |
| 13 | +- 🔄 Optional overwrite mode when full |
| 14 | +- ⚡ Constant time `push_back`, `pop_front`, `front`, `back`, `size`, and `capacity` operations |
| 15 | +- 📚 STL-style interface with iterators |
| 16 | +- 🛡️ Fully exception-safe and compliant with C++17 |
| 17 | +- 🔗 Header-only for easy integration |
| 18 | +- ✅ Unit tested with Google Test |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## 🔍 Modern C++ Highlights |
| 23 | + |
| 24 | +This project demonstrates many modern C++ best practices: |
| 25 | + |
| 26 | +| Feature | Description | |
| 27 | +|-----------------------------------|-------------| |
| 28 | +| ✅ **Perfect Forwarding** | Efficiently constructs elements in-place using `std::forward` | |
| 29 | +| ✅ **`constexpr if` Branching** | Compile-time control flow based on type traits | |
| 30 | +| ✅ **Conditional `noexcept`** | Automatically propagates `noexcept` based on `value_type` properties | |
| 31 | +| ✅ **Exception Safety** | Strong guarantees using RAII and manual object lifetime control | |
| 32 | +| ✅ **Overwrite Mode** | Enable circular overwriting when buffer is full | |
| 33 | +| ✅ **STL-Compatible Interface** | Supports `begin()`, `end()`, `size()`, `empty()`, etc. | |
| 34 | +| ✅ **Unit Tested** | With Google Test and `FetchContent` integration via CMake | |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +## Getting Started |
| 39 | + |
| 40 | +### Prerequisites |
| 41 | + |
| 42 | +- C++17 compatible compiler |
| 43 | +- CMake 3.21 or newer |
| 44 | +- Git (for GoogleTest fetch) |
| 45 | + |
| 46 | +### Build Instructions |
| 47 | + |
| 48 | +```bash |
| 49 | +git clone https://github.com/yourusername/RingBuffer.git |
| 50 | +cd RingBuffer |
| 51 | +mkdir build && cd build |
| 52 | +cmake .. |
| 53 | +cmake --build . |
| 54 | +ctest --output-on-failure |
| 55 | +``` |
| 56 | +This will: |
| 57 | + 1. Fetch and build GoogleTest using FetchContent |
| 58 | + 2. Build the RingBufferTest executable from test_main.cpp |
| 59 | + 3. Run all unit tests |
| 60 | + |
| 61 | +⸻ |
| 62 | + |
| 63 | +Usage |
| 64 | + |
| 65 | +```cpp |
| 66 | +#include "RingBuffer.hpp" |
| 67 | + |
| 68 | +ring_buffer<int, 8> buf; |
| 69 | + |
| 70 | +buf.push_back(42); |
| 71 | +buf.push_back(1337); |
| 72 | + |
| 73 | +std::cout << buf.front() << ", " << buf.back() << std::endl; |
| 74 | + |
| 75 | +buf.pop_front(); |
| 76 | +``` |
| 77 | + |
| 78 | +With overwrite mode: |
| 79 | + |
| 80 | +```cpp |
| 81 | +ring_buffer<int, 3, true> overwrite_buf; |
| 82 | +overwrite_buf.push_back(1); |
| 83 | +overwrite_buf.push_back(2); |
| 84 | +overwrite_buf.push_back(3); |
| 85 | +overwrite_buf.push_back(4); // Overwrites 1 |
| 86 | +``` |
| 87 | + |
| 88 | + |
| 89 | +⸻ |
| 90 | + |
| 91 | +Project Structure |
| 92 | + |
| 93 | +RingBuffer/ |
| 94 | +├── CMakeLists.txt # CMake build config with GoogleTest support |
| 95 | +├── RingBuffer.hpp # Header-only ring buffer implementation |
| 96 | +├── test_main.cpp # Unit tests using GoogleTest |
| 97 | +├── LICENSE # MIT License |
| 98 | +└── README.md # This file |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +⸻ |
| 103 | + |
| 104 | +License |
| 105 | + |
| 106 | +This project is licensed under the MIT License - see the LICENSE file for details. |
| 107 | + |
0 commit comments