From 13a022d36255be1ad0d4015e8e428a5c5a2101dd Mon Sep 17 00:00:00 2001 From: Nigel Stewart Date: Mon, 30 Nov 2026 20:57:33 +1000 Subject: [PATCH] Add README.md with an introduction to the library Add a short front page for the GitHub repository, in the style of other Boost libraries such as Beast: - The Boost logo, title, and CI and documentation badges for master and develop. - What a circular buffer is, how boost::circular_buffer relates to the standard containers, the diagram from the documentation, and a short example (adapted from example/circular_buffer_example.cpp) that compiles as C++03 and C++17. - Features: header-only, C++03 onwards, fixed memory with no hidden allocation, STL compliance, contiguous storage, debug support, and the space-optimized variant. - Typical uses, and a note that the buffer is not thread-safe. - Getting started with CMake, the dependencies, links to the documentation and examples, building the tests with b2 or the standalone CMake build, git branches, contributing, and the license. Add the official Boost logo as doc/images/boost-logo.svg, copied from boostorg/website-v2-docs (antora-ui/src/img/boost-logo-transparent.svg). A local copy is used because GitHub often won't display SVGs linked from raw.githubusercontent.com. Its use follows the Boost Logo Policy: the library is part of Boost, the logo appears once and isn't altered, and the README credits The C++ Alliance. --- README.md | 144 ++++++++++++++++++++++++++++++++++++++ doc/images/boost-logo.svg | 1 + 2 files changed, 145 insertions(+) create mode 100644 README.md create mode 100644 doc/images/boost-logo.svg diff --git a/README.md b/README.md new file mode 100644 index 00000000..c70096ea --- /dev/null +++ b/README.md @@ -0,0 +1,144 @@ +

+ Boost +

+ +

Boost.CircularBuffer

+ +

A fixed-capacity, STL-compliant ring buffer for C++03 and later.

+ +| Branch | CI | Documentation | +|-----------|----|---------------| +| `master` | [![CI](https://github.com/boostorg/circular_buffer/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/boostorg/circular_buffer/actions/workflows/ci.yml?query=branch%3Amaster) | [![Documentation](https://img.shields.io/badge/docs-master-brightgreen.svg)](https://www.boost.org/doc/libs/master/doc/html/circular_buffer.html) | +| `develop` | [![CI](https://github.com/boostorg/circular_buffer/actions/workflows/ci.yml/badge.svg?branch=develop)](https://github.com/boostorg/circular_buffer/actions/workflows/ci.yml?query=branch%3Adevelop) | [![Documentation](https://img.shields.io/badge/docs-develop-brightgreen.svg)](https://www.boost.org/doc/libs/develop/doc/html/circular_buffer.html) | + +## Introduction + +A [circular buffer](https://en.wikipedia.org/wiki/Circular_buffer), also +called a *ring* or *cyclic* buffer, holds a fixed number of elements. When it +is full, each new element overwrites the oldest one. + +`boost::circular_buffer` is a standard container that behaves this way. It +works like `std::deque` or `std::vector`, with random-access iterators and +constant-time insertion and removal at either end, and it works with the +standard algorithms. Once its capacity is set, it doesn't allocate memory +until you change the capacity yourself. + +![circular_buffer](doc/images/circular_buffer.png) + +```cpp +#include + +int main() +{ + // Create a circular buffer with a capacity for 3 integers. + boost::circular_buffer cb(3); + + cb.push_back(1); + cb.push_back(2); + cb.push_back(3); // The buffer is full: 1 2 3 + + cb.push_back(4); // Overwrite 1 with 4: 2 3 4 + cb.push_back(5); // Overwrite 2 with 5: 3 4 5 + + cb.pop_back(); // 5 is removed: 3 4 + cb.pop_front(); // 3 is removed: 4 + + return cb[0] == 4 ? 0 : 1; +} +``` + +## Features + +- **Header-only.** There's nothing to build or link. +- **C++03 and later.** CI tests C++03 through the latest standard with GCC, + Clang and MSVC. +- **Fixed memory.** The buffer doesn't allocate behind your back, so it suits + real-time and performance-critical code. +- **STL-compliant.** It has random-access iterators, allocator support and + move semantics, and works with the `` functions. +- **Contiguous storage.** The elements are held in at most two contiguous + pieces. `array_one()`, `array_two()` and `linearize()` give C APIs direct + access to them. +- **Debug support.** With `BOOST_CB_ENABLE_DEBUG=1`, using an invalidated + iterator triggers an assertion. + +There's also `boost::circular_buffer_space_optimized`, which takes the same +fixed maximum capacity but allocates memory only as it needs it, and frees it +again as the buffer shrinks. + +## Typical uses + +- Keeping the most recent *N* samples, readings or log lines. +- A cache of the last *N* items inserted. +- A fixed-capacity FIFO or LIFO queue. +- The storage for a bounded producer/consumer queue. See + [`example/circular_buffer_bound_example.cpp`](example/circular_buffer_bound_example.cpp). + +Like the standard containers, `circular_buffer` isn't thread-safe. If several +threads share one buffer, you have to provide the locking yourself. + +## Getting started + +Boost.CircularBuffer is part of [Boost](https://www.boost.org). If you have +Boost installed, you already have it: just include +``. + +With CMake: + +```cmake +find_package(Boost REQUIRED) +target_link_libraries(my_app PRIVATE Boost::headers) +``` + +Within the Boost superproject, link `Boost::circular_buffer` instead. + +The library depends only on other header-only Boost libraries: Assert, +ConceptCheck, Config, Core, Move, ThrowException and TypeTraits. + +## Documentation + +- [Documentation](https://www.boost.org/doc/libs/release/doc/html/circular_buffer.html): + introduction, rationale, examples and the full reference. +- [`example/`](example/): small programs you can compile. + +## Building the tests + +From a Boost superproject checkout, with this repository at +`libs/circular_buffer`: + +```sh +./b2 libs/circular_buffer/test cxxstd=03,11,17 +``` + +Or as a standalone CMake project against an installed Boost (1.85 or newer, +CMake 3.10 or newer): + +```sh +cmake -S . -B build -G Ninja -DBUILD_TESTING=ON +ninja -C build check # build and run the tests +ninja -C build check-valgrind # the same under valgrind +``` + +## Git branches + +| Branch | Purpose | +|-----------|---------| +| `master` | The latest release. Builds with the `master` branch of the Boost superproject. | +| `develop` | Work for the next release. Pull requests go here. | + +## Contributing + +Bug reports, questions and pull requests are welcome on +[GitHub](https://github.com/boostorg/circular_buffer/issues). Please send pull +requests to `develop`, and include tests for both containers where they apply. + +## License + +Distributed under the +[Boost Software License, Version 1.0](https://www.boost.org/LICENSE_1_0.txt). + +Originally written by Jan Gaspar (2003–2008). + +The Boost logo is a trademark of [The C++ Alliance](https://cppalliance.org), +used according to the +[Boost Logo Policy](https://www.boost.org/doc/contributor-guide/docs/logo-policy-media-guide.html). diff --git a/doc/images/boost-logo.svg b/doc/images/boost-logo.svg new file mode 100644 index 00000000..45de5470 --- /dev/null +++ b/doc/images/boost-logo.svg @@ -0,0 +1 @@ + \ No newline at end of file