36 #ifndef CHARRINGBUFFER_H 37 #define CHARRINGBUFFER_H 39 #if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT) 40 #error "Only <final/final.h> can be included directly." 49 #include <final/util/fstring.h> 55 template <std::
size_t>
62 template <
typename T, std::
size_t Capacity>
70 template <
typename Type, std::
size_t N>
75 using iterator_category = std::forward_iterator_tag;
76 using value_type = Type;
77 using difference_type = std::ptrdiff_t;
78 using pointer = Type*;
79 using reference = Type&;
81 explicit ring_iterator (pointer p, std::size_t start, std::size_t pos)
100 inline auto operator * ()
const noexcept -> reference
102 return ptr[(offset + index) % N];
105 inline auto operator -> ()
const noexcept -> pointer
112 return lhs.index == rhs.index
113 && lhs.ptr == rhs.ptr
114 && lhs.offset == rhs.offset;
119 return ! (lhs == rhs);
124 pointer ptr{
nullptr};
125 const std::size_t offset{0U};
126 std::size_t index{0U};
133 tmp.index += std::size_t(size);
141 using difference_type = std::ptrdiff_t;
143 using reference = T&;
144 using const_reference =
const T&;
145 using value_type = T;
155 inline auto operator [] (std::size_t index) noexcept -> reference
157 static_assert ( Capacity > 0,
"Ring buffer has no memory" );
158 return buffer[(head + index) % Capacity];
161 inline auto operator [] (std::size_t index)
const noexcept -> const_reference
163 static_assert ( Capacity > 0,
"Ring buffer has no memory" );
164 return buffer[(head + index) % Capacity];
168 virtual inline auto getClassName()
const ->
FString 170 return "FRingBuffer";
173 inline auto getSize()
const noexcept -> std::size_t
178 inline auto getCapacity()
const noexcept -> std::size_t
183 inline auto begin() noexcept ->
iterator 185 return iterator(buffer.data(), head, 0);
193 inline auto end() noexcept ->
iterator 195 return iterator(buffer.data(), head, getSize());
203 inline auto front() noexcept -> reference
206 return empty_element;
211 inline auto front()
const noexcept -> const_reference
214 return empty_element;
219 inline auto back() noexcept -> reference
222 return empty_element;
224 std::size_t index = (tail == 0) ? Capacity - 1 : tail - 1;
225 return buffer[index];
228 inline auto back()
const noexcept -> const_reference
231 return empty_element;
233 std::size_t index = (tail == 0) ? Capacity - 1 : tail - 1;
234 return buffer[index];
238 inline void clear() noexcept
246 inline auto isEmpty()
const noexcept ->
bool 248 return elements == 0;
251 inline auto hasData()
const noexcept ->
bool 256 inline auto isFull()
const noexcept ->
bool 258 return elements == Capacity;
262 inline void push (
const T& item) noexcept
267 static_assert ( Capacity > 0,
"Ring buffer has no memory" );
269 tail = (tail + 1) % Capacity;
273 inline void push_back (
const T& item) noexcept
278 template <
typename... Args>
279 inline void emplace (Args&&... args)
284 static_assert ( Capacity > 0,
"Ring buffer has no memory" );
285 buffer[tail] = T(std::forward<Args>(args)...);
286 tail = (tail + 1) % Capacity;
290 template <
typename... Args>
291 inline void emplace_back (Args&&... args)
293 emplace (std::forward<Args>(args)...);
296 inline void pop() noexcept
301 static_assert ( Capacity > 0,
"Ring buffer has no memory" );
302 head = (head + 1) % Capacity;
306 inline void pop_front() noexcept
311 inline void pop (std::size_t s) noexcept
316 static_assert ( Capacity > 0,
"Ring buffer has no memory" );
317 s = std::min(s, elements);
318 head = (head + s) % Capacity;
324 std::array<value_type, Capacity> buffer;
325 value_type empty_element{};
326 std::size_t head{0U};
327 std::size_t tail{0U};
328 std::size_t elements{0U};
339 template <std::
size_t Capacity>
352 inline auto getClassName()
const ->
FString override 354 return "CharRingBuffer";
358 auto strncmp_front (
const char*
string, std::size_t length)
const noexcept ->
bool 360 if ( length > getSize() )
365 const auto min_length = std::min(length, getCapacity());
366 return std::memcmp(
string, &front(), min_length) == 0;
369 auto l1 = std::min(length, getCapacity() - head);
370 auto l2 = length - l1;
372 if ( std::memcmp(
string, &front(), l1) != 0 )
378 return std::memcmp(
string + l1, &buffer[0], l2) == 0;
384 #endif // CHARRINGBUFFER_H Definition: class_template.cpp:25
Definition: char_ringbuffer.h:71
Definition: ftermoutput.h:58
Definition: char_ringbuffer.h:56