Cbeam
Loading...
Searching...
No Matches
circular_buffer.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2025 acrion innovations GmbH
3Authors: Stefan Zipproth, s.zipproth@acrion.ch
4
5This file is part of Cbeam, see https://github.com/acrion/cbeam and https://cbeam.org
6
7Cbeam is offered under a commercial and under the AGPL license.
8For commercial licensing, contact us at https://acrion.ch/sales. For AGPL licensing, see below.
9
10AGPL licensing:
11
12Cbeam is free software: you can redistribute it and/or modify
13it under the terms of the GNU Affero General Public License as published by
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
16
17Cbeam is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU Affero General Public License for more details.
21
22You should have received a copy of the GNU Affero General Public License
23along with Cbeam. If not, see <https://www.gnu.org/licenses/>.
24*/
25
26#pragma once
27
29
30#include <cstddef> // for std::size_t
31
32#include <array> // for std::array
33#include <utility> // for std::forward
34
35namespace cbeam::container
36{
45 template <typename T, std::size_t S>
47 {
48 public:
49 using value_type = T;
50 using size_type = std::size_t;
53 using iterator = typename std::array<T, S>::iterator;
54 using const_iterator = typename std::array<T, S>::const_iterator;
55
57 circular_buffer() = default;
58
61 auto begin() noexcept
62 {
63 return _buffer.begin();
64 }
65
68 auto end() noexcept
69 {
70 return _full ? _buffer.end() : _buffer.begin() + _i;
71 }
72
75 std::size_t size() const
76 {
77 return _full ? S : _i;
78 }
79
82 std::size_t max_size() const
83 {
84 return S;
85 }
86
93 void push_back(const T& t)
94 {
95 _buffer[_i] = t;
96 _i = (_i + 1) % S;
97 if (_i == 0)
98 {
99 _full = true;
100 }
101 }
102
103 bool empty() const noexcept { return !_full && _i == 0; }
104
105 void clear() noexcept
106 {
107 _i = 0;
108 _full = false;
109 }
110
117 template <typename... Args>
118 void emplace_back(Args&&... args)
119 {
120 _buffer[_i] = T(std::forward<Args>(args)...);
121 _i = (_i + 1) % S;
122 if (_i == 0)
123 {
124 _full = true;
125 }
126 }
127
133 {
134 if (pos >= size())
135 {
136 throw cbeam::error::out_of_range("Position out of range");
137 }
138
139 return _buffer[(pos + (_full ? _i : 0)) % S];
140 }
141
147 {
148 if (pos >= size())
149 {
150 throw cbeam::error::out_of_range("Position out of range");
151 }
152
153 return _buffer[(pos + (_full ? _i : 0)) % S];
154 }
155
160 {
161 return _buffer[(pos + (_full ? _i : 0)) % S];
162 }
163
168 {
169 return _buffer[(pos + (_full ? _i : 0)) % S];
170 }
171
175 {
176 return _buffer[_full ? _i : 0];
177 }
178
182 {
183 return _buffer[_full ? _i : 0];
184 }
185
189 {
190 return _buffer[(_i + S - 1) % S];
191 }
192
196 {
197 return _buffer[(_i + S - 1) % S];
198 }
199
200 private:
201 std::array<T, S> _buffer;
202 std::size_t _i{0};
203 bool _full{false};
204 };
205}
std::size_t size_type
Definition circular_buffer.hpp:50
const_reference operator[](size_type pos) const
Accesses the element at specified location without bounds checking (const version).
Definition circular_buffer.hpp:167
bool empty() const noexcept
Definition circular_buffer.hpp:103
reference front()
Accesses the first element in the buffer.
Definition circular_buffer.hpp:174
auto begin() noexcept
Gets an iterator to the beginning of the buffer.
Definition circular_buffer.hpp:61
void push_back(const T &t)
Adds an element to the back of the buffer.
Definition circular_buffer.hpp:93
const_reference front() const
Accesses the first element in the buffer (const version).
Definition circular_buffer.hpp:181
const_reference at(size_type pos) const
Accesses the element at specified location with bounds checking (const version).
Definition circular_buffer.hpp:146
void clear() noexcept
Definition circular_buffer.hpp:105
T value_type
Definition circular_buffer.hpp:49
void emplace_back(Args &&... args)
Adds an element to the back of the buffer.
Definition circular_buffer.hpp:118
typename std::array< T, S >::iterator iterator
Definition circular_buffer.hpp:53
value_type & reference
Definition circular_buffer.hpp:51
const value_type & const_reference
Definition circular_buffer.hpp:52
circular_buffer()=default
Default constructor.
reference operator[](size_type pos)
Accesses the element at specified location without bounds checking.
Definition circular_buffer.hpp:159
const_reference back() const
Accesses the last element in the buffer (const version).
Definition circular_buffer.hpp:195
typename std::array< T, S >::const_iterator const_iterator
Definition circular_buffer.hpp:54
std::size_t max_size() const
Gets the maximum number of elements the buffer can hold.
Definition circular_buffer.hpp:82
reference back()
Accesses the last element in the buffer.
Definition circular_buffer.hpp:188
auto end() noexcept
Gets an iterator to the end of the buffer.
Definition circular_buffer.hpp:68
std::size_t size() const
Gets the number of elements in the buffer.
Definition circular_buffer.hpp:75
reference at(size_type pos)
Accesses the element at specified location with bounds checking.
Definition circular_buffer.hpp:132
A Cbeam-specific out_of_range error that also behaves like std::out_of_range.
Definition out_of_range.hpp:50
Offers advanced container types with unique approaches to stability and interprocess sharing....
Definition buffer.hpp:44