|
| 1 | +# Copilot Instructions for RingBufferCpp |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +This is a modern, header-only C++17 ring buffer (circular buffer) implementation designed for performance, safety, and ease of use. The project demonstrates C++17 best practices and is tested with Google Test. |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +- **Header-only library**: All implementation is in `RingBuffer.hpp` |
| 10 | +- **Template-based**: Generic implementation using `template<typename T, size_t N, bool Overwrite>` |
| 11 | +- **Namespace**: All code is in the `buffers` namespace |
| 12 | +- **Iterator support**: Custom forward iterators in `buffers::detail` namespace |
| 13 | + |
| 14 | +## Code Standards |
| 15 | + |
| 16 | +### C++ Version and Style |
| 17 | + |
| 18 | +- Use **C++17** features and idioms |
| 19 | +- Follow modern C++ best practices: |
| 20 | + - Perfect forwarding with `std::forward` |
| 21 | + - `constexpr if` for compile-time branching |
| 22 | + - Conditional `noexcept` specifications |
| 23 | + - RAII and strong exception safety guarantees |
| 24 | + - `[[nodiscard]]` attributes where appropriate |
| 25 | + |
| 26 | +### Naming Conventions |
| 27 | + |
| 28 | +- **Classes/Types**: `snake_case` (e.g., `ring_buffer`, `ring_buffer_iterator`) |
| 29 | +- **Member variables**: `snake_case_` with trailing underscore (e.g., `source_`, `index_`, `count_`) |
| 30 | +- **Functions/Methods**: `snake_case` (e.g., `push_back`, `pop_front`, `cbegin`) |
| 31 | +- **Template parameters**: PascalCase (e.g., `T`, `N`, `Overwrite`, `C`) |
| 32 | + |
| 33 | +### Code Organization |
| 34 | + |
| 35 | +- Place implementation details in `buffers::detail` namespace |
| 36 | +- Use forward declarations before implementation |
| 37 | +- Include necessary headers at the top (`<iostream>`, `<type_traits>`, `<algorithm>`, `<cstring>`, `<vector>`) |
| 38 | +- Use `#pragma once` for header guards |
| 39 | + |
| 40 | +## Building and Testing |
| 41 | + |
| 42 | +### Build Commands |
| 43 | + |
| 44 | +```bash |
| 45 | +mkdir build && cd build |
| 46 | +cmake .. |
| 47 | +cmake --build . |
| 48 | +``` |
| 49 | + |
| 50 | +### Test Commands |
| 51 | + |
| 52 | +From within the `build` directory: |
| 53 | + |
| 54 | +```bash |
| 55 | +ctest --output-on-failure |
| 56 | +``` |
| 57 | + |
| 58 | +### Test Framework |
| 59 | + |
| 60 | +- Use **Google Test** framework |
| 61 | +- Test file: `test_main.cpp` |
| 62 | +- GoogleTest is fetched automatically via CMake's `FetchContent` |
| 63 | +- Tests use the pattern: `TEST(RingBufferTest, TestName)` |
| 64 | + |
| 65 | +## Testing Guidelines |
| 66 | + |
| 67 | +When adding or modifying functionality: |
| 68 | + |
| 69 | +1. **Write tests first** or alongside implementation |
| 70 | +2. **Test naming**: The project uses numerical test names (`Test1`, `Test2`) with some descriptive ones (`Test6IteratorOrder`). Follow this convention for consistency. |
| 71 | +3. **Use Google Test macros**: `EXPECT_EQ`, `EXPECT_TRUE`, `EXPECT_FALSE`, etc. |
| 72 | +4. **Test edge cases**: |
| 73 | + - Empty buffer |
| 74 | + - Full buffer |
| 75 | + - Overwrite behavior |
| 76 | + - Iterator operations |
| 77 | + - Copy/move semantics |
| 78 | +5. **Verify both const and non-const operations** where applicable |
| 79 | + |
| 80 | +## Key Features to Maintain |
| 81 | + |
| 82 | +- **Overwrite mode**: When buffer is full, new elements can overwrite oldest |
| 83 | +- **STL-compatible interface**: `begin()`, `end()`, `cbegin()`, `cend()`, `size()`, `empty()` |
| 84 | +- **Constant time operations**: All core operations should be O(1) |
| 85 | +- **Exception safety**: Maintain strong exception guarantees |
| 86 | +- **Type safety**: Use SFINAE and type traits appropriately |
| 87 | + |
| 88 | +## Common Patterns in This Codebase |
| 89 | + |
| 90 | +- Use SFINAE with `typename std::enable_if<condition, int>::type* = nullptr` pattern (e.g., `typename std::enable_if<(!Z), int>::type* = nullptr` for non-const, `typename std::enable_if<(Z), int>::type* = nullptr` for const) |
| 91 | +- Iterator comparisons based on `count()` not `index()` |
| 92 | +- Modulo arithmetic for circular indexing: `index_ = ++index_ % N` |
| 93 | +- Template specialization for const/non-const iterators |
| 94 | + |
| 95 | +## What NOT to Do |
| 96 | + |
| 97 | +- Don't add dependencies beyond standard library (except GoogleTest for tests) |
| 98 | +- Don't break header-only design |
| 99 | +- Don't remove or weaken exception safety guarantees |
| 100 | +- Don't change API to be incompatible with STL conventions |
| 101 | +- Don't add features that would compromise O(1) operation complexity |
| 102 | +- Avoid dynamic allocation; maintain fixed-size design |
| 103 | + |
| 104 | +## Special Considerations |
| 105 | + |
| 106 | +- The buffer size `N` is a compile-time constant (template parameter) |
| 107 | +- Support both trivial and non-trivial types for `T` |
| 108 | +- Handle move-only types appropriately |
| 109 | +- Maintain compatibility with C++17 (the project has a `Simulate_Android_ToolChain` CMake option that when enabled uses C++14 with `-fno-exceptions`, but the default is C++17) |
0 commit comments