Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<p align="center">
<img src="doc/images/boost-logo.svg" alt="Boost" width="120">
</p>

<h1 align="center">Boost.CircularBuffer</h1>

<p align="center">A fixed-capacity, STL-compliant ring buffer for C++03 and later.</p>

| 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 <boost/circular_buffer.hpp>

int main()
{
// Create a circular buffer with a capacity for 3 integers.
boost::circular_buffer<int> 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 `<algorithm>` 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
`<boost/circular_buffer.hpp>`.

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).
1 change: 1 addition & 0 deletions doc/images/boost-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading