diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 00000000..17b0fcbd --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,232 @@ +# CLAUDE.md + +Guidance for AI assistants and developers working on Boost.CircularBuffer. + +## What this is + +A header-only Boost library that provides two STL-compliant containers: + +- `boost::circular_buffer`: a fixed-capacity ring buffer. When it + is full, `push_back` overwrites the front and `push_front` overwrites the back. +- `boost::circular_buffer_space_optimized`: an adaptor that derives + from `circular_buffer`. It allocates memory on demand, within a + `capacity_control` (a maximum capacity plus a minimum reserved capacity). + +The library is old (2003–2008, Jan Gaspar) and widely deployed. **Backward +compatibility and C++03 support are hard requirements.** CI builds C++03 +through C++2c on gcc 4.8 and later, clang, and MSVC. + +## Layout + +``` +include/boost/ + circular_buffer.hpp umbrella header: config macros, then includes the four below in order + circular_buffer_fwd.hpp forward declarations and default allocator + circular_buffer/ + debug.hpp cb_details::debug_iterator_base / debug_iterator_registry (BOOST_CB_ENABLE_DEBUG) + details.hpp cb_details: iterator, const/nonconst_traits, capacity_control, + assign_n / assign_range helpers, uninitialized_* helpers + base.hpp class circular_buffer (the bulk of the implementation, ~3000 lines) + space_optimized.hpp class circular_buffer_space_optimized +test/ + test.hpp shared test types (MyInteger with exception trigger, InstanceCounter, ...) + common.ipp tests shared by both containers, parameterised by the CB_CONTAINER macro + base_test.cpp circular_buffer: #define CB_CONTAINER circular_buffer + common.ipp + extras + space_optimized_test.cpp circular_buffer_space_optimized: same pattern + soft_iterator_invalidation.cpp, constant_erase_test.cpp, bounded_buffer_comparison.cpp + Jamfile.v2 source of truth for which tests run; CMake reuses it via boost_test_jamfile + cmake_install_test/, cmake_subdir_test/ CMake integration tests (CI only) +example/ examples; snippets are imported into the docs +doc/circular_buffer.qbk Quickbook documentation +CMakeLists.txt generated by `boostdep --cmake circular_buffer`. Do not hand-edit the dependency list +build.jam b2 project and dependency list +``` + +## Key implementation facts + +- **Storage model** (`base.hpp`): `m_buff` (start of the allocation), `m_end` + (one past the end of the allocation), `m_first` (first element), `m_last` + (one past the last element, wrapping), and `m_size`. When the buffer is full, + `m_first == m_last`. Use `increment`, `decrement`, `add`, `sub` and + `map_pointer` for pointer arithmetic that wraps. Never use raw `++`/`--` + across the wrap point. +- Elements can be in two contiguous pieces: `array_one()` / `array_two()`. + `linearize()` rotates them into one piece. +- **Iterators** (`details.hpp`): a single `cb_details::iterator` + template is used for both `iterator` and `const_iterator`, selected by + `nonconst_traits` / `const_traits`. `nonconst_self` refers to the mutable + iterator type, so that a `const_iterator` can be constructed from an + `iterator`. The end iterator is represented by `m_it == 0`. +- **Debug mode**: `BOOST_CB_ENABLE_DEBUG=1` makes iterators derive from + `debug_iterator_base` and register with the container, so invalidated + iterators are caught by `BOOST_CB_ASSERT`. It is off by default. Some + behaviour differs between modes: for example, `constant_erase_test` and + `soft_iterator_invalidation` only make sense with debug off. +- **Internal macros**: `BOOST_CB_ASSERT`, `BOOST_CB_IS_CONVERTIBLE` and + `BOOST_CB_ASSERT_TEMPLATED_ITERATOR_CONSTRUCTORS` are defined in + `circular_buffer.hpp` and `#undef`'d at its end. As a result, the + sub-headers are only usable through the umbrella header. If you add a macro, + follow the same define/undef pattern. +- `BOOST_CB_TEST` is defined by `test/test.hpp`. It exposes test-only members, + such as `circular_buffer_space_optimized::internal_capacity()`. +- **Move semantics** use Boost.Move (`BOOST_RV_REF`, `boost::move`) so the same code works in C++03. + Native-C++11-only code goes under `#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES` + (or the relevant `BOOST_NO_CXX11_*` macro). +- **Allocators** are accessed through `boost/core/allocator_access.hpp` + (`boost::allocator_construct`, `allocator_destroy`, `allocator_pointer`, + ...), never by calling allocator members directly. The allocator is stored + via `boost::empty_value`. +- **Exceptions** use `BOOST_TRY` / `BOOST_CATCH(...)` / `BOOST_CATCH_END` / + `BOOST_RETHROW` and `boost::throw_exception`. The code must build with + `BOOST_NO_EXCEPTIONS`. +- **Scalar fast paths**: `erase_begin(n)`, `erase_end(n)` and `clear()` take + constant time for scalar `T`, because they dispatch on `is_scalar` + and no destructors run. Preserve these complexity + guarantees, since they are documented. + +## Conventions + +- **Language level**: C++03. Do not use `auto`, `nullptr`, `override`, + `= default`, `= delete`, lambdas, `static_assert` or `>>` in nested templates. + Use Boost.Config instead: `BOOST_NOEXCEPT`, `BOOST_NOEXCEPT_IF`, + `BOOST_STATIC_ASSERT`, `BOOST_DEDUCED_TYPENAME`, and `BOOST_WORKAROUND` for + compiler workarounds. +- **Dependencies**: keep them minimal. Current direct dependencies are + assert, concept_check, config, core, move, throw_exception and type_traits. + If you add one, update `CMakeLists.txt`, `build.jam` and + `test/cmake_subdir_test/CMakeLists.txt` together. +- **Self-contained headers**: each header must `#include` what it uses, both + Boost and standard headers. +- **Formatting**: 4-space indent, 120 columns, K&R braces (opening brace on + the same line), `T* p` pointer style, `template `, + `public:`/`private:` at class indent, and no indentation inside namespaces. + Close namespaces with `} // namespace cb_details`. Match the surrounding code. + If a `.clang-format` file is present, format only the lines you change + (`git clang-format`). Don't reformat unrelated code in a functional change. +- **Doxygen API docs**: every public member in `base.hpp` and + `space_optimized.hpp` has a `/*! ... */` block with `\pre`, `\post`, + `\throws`, `\par Exception Safety`, `\par Iterator Invalidation`, + `\par Complexity` and `\sa` sections. New or changed public API must keep + these blocks complete and accurate. Comments are hand-formatted, so don't + reflow them. +- **API parity**: a feature added to `circular_buffer` usually needs a + matching wrapper in `circular_buffer_space_optimized`, and tests in + `common.ipp` so that both containers are covered. +- **Docs and examples**: `doc/circular_buffer.qbk` imports code from + `example/*.cpp` between `//[name` and `//]` markers. Keep the markers intact + and keep the snippets readable as documentation. + +## Testing + +Tests use `boost/core/lightweight_test.hpp` (`BOOST_TEST`, +`BOOST_TEST_EQ`, ...). Each test is a plain function called from `main()`, +which returns `boost::report_errors()`. To add a test, write a function and +call it from `main()`. Shared behaviour goes in `common.ipp` and its +`run_common_tests()`. + +Expected coverage for a change: +- both containers (via `common.ipp`) where applicable +- non-trivial element types (`MyInteger`, `InstanceCounter`) as well as `int`. + Use `MyInteger::set_exception_trigger(n)` for exception-safety paths. +- a full buffer that wraps (`m_first` in the middle), not just the linear case +- debug on and debug off (`BOOST_CB_ENABLE_DEBUG=1` / unset) +- C++03 and a modern standard + +### Running tests + +Inside a Boost superproject checkout (`libs/circular_buffer`), which is how CI +runs them: + +```sh +./b2 libs/circular_buffer/test toolset=gcc cxxstd=03,11,17 variant=debug,release +``` + +For a quick standalone run against a separate Boost checkout at `$BOOST` +(this repo's `include/` must come first, so that it shadows the superproject's +copy): + +```sh +INC="-Iinclude $(for d in $BOOST/libs/*/include; do case $d in *circular_buffer*) ;; *) printf -- '-isystem %s ' $d;; esac; done)" +for std in c++03 c++11; do + for dbg in 0 1; do + for t in base_test space_optimized_test; do + g++ -std=$std -Wall -Wextra -DBOOST_CB_ENABLE_DEBUG=$dbg $INC test/$t.cpp -o /tmp/$t && /tmp/$t || echo "FAIL $t $std dbg=$dbg" + done + done +done +# debug-off only: +for t in soft_iterator_invalidation constant_erase_test; do + g++ -std=c++11 $INC test/$t.cpp -o /tmp/$t && /tmp/$t || echo "FAIL $t" +done +``` + +`bounded_buffer_comparison.cpp` and `circular_buffer_bound_example.cpp` need +the compiled Boost.Thread and Boost.Timer libraries. Without them, check them +with `-fsyntax-only`. + +Also compile with clang using `-Wall -Wextra -Wdeprecated-copy`. Warnings in +user builds are treated as bugs. + +## Documentation + +The documentation is built with Boost's standard toolchain, driven by b2 from +`doc/jamfile.v2`. Generated HTML is not committed: the Boost release process +builds it through the `boostdoc` alias, and the repo's `index.html` redirects +to `boost-root/doc/html/circular_buffer.html`. + +| Stage | Tool | Source | +|---|---|---| +| Narrative | Quickbook 1.6 | `doc/circular_buffer.qbk` | +| API reference | Doxygen, then BoostBook | `/*! */` comments in `circular_buffer.hpp`, `base.hpp`, `space_optimized.hpp` (emitted as `autodoc.xml`) | +| Index | AutoIndex | `doc/circular_buffer.idx` | +| Rendering | BoostBook XSL (xsltproc; FOP for PDF) | the combined DocBook | + +Where content lives: +- **Reference**: Doxygen blocks in the headers, with the sections listed + under Conventions. Doxygen runs with `HIDE_UNDOC_MEMBERS=YES`, so a public + member without a comment block is missing from the reference. +- **Narrative, rationale and release notes**: `circular_buffer.qbk`. +- **Code samples**: `[import ../example/*.cpp]` (and + `../test/bounded_buffer_comparison.cpp`), referenced by the names in their + `//[name ... //]` markers. Renaming or removing a marker breaks the doc build. +- **Diagrams**: `doc/images/*.png`. + +When making a change: +1. If you change public API, update the Doxygen block, in both containers + where applicable. +2. Add an entry to the **Release Notes** section of `circular_buffer.qbk` for + user-visible changes. +3. A new example goes in `example/` with snippet markers. Add it to + `example/jamfile.v2`, and `[import]` it in the `.qbk` if the docs show it. + +Building requires a Boost superproject checkout, because the `.qbk` includes +`../../../tools/auto_index/...` and images are installed into +`boost-root/doc/html/images`. From `boost-root/libs/circular_buffer/doc`: + +```sh +b2 # HTML into boost-root/doc/html +b2 pdfinstall # doc/circular_buffer.pdf +``` + +The build needs a `user-config.jam` containing `using quickbook ; using doxygen ; +using boostbook ; using auto-index ;`, the system packages `doxygen`, +`xsltproc`, `docbook-xsl` and `docbook-xml`, and `quickbook` and `auto_index` +built from `boost-root/tools`. Check `AutoDoxywarnings.log` in the build +output. + +Known issues: +- Nothing in CI builds the docs, so a broken `[import]` or snippet marker + goes unnoticed until release. +- Release notes stop at Boost 1.56. +- The "Trac Tickets" section points at the retired svn.boost.org Trac (issues + are now on GitHub). +- The Doxygen `PREDEFINED` list doesn't map `BOOST_NOEXCEPT` / + `BOOST_NOEXCEPT_IF`, which the headers use widely. It also still contains + `BOOST_UNITS_*` entries copied from Boost.Units. + +## Git workflow + +- `develop` is the integration branch and `master` is the release branch. + PRs target `develop`. +- Keep formatting-only changes in their own commits, separate from functional + changes.