CiCueTea v0.0.1
Loading...
Searching...
No Matches
DoubleBuffer.h
Go to the documentation of this file.
1//
2// DoubleBuffer.h
3// CQTDSP
4//
5// Created by Juan Sierra on 3/23/25.
6//
7
16
17#pragma once
18
19#include <array>
20
21namespace jsa::cicuetea {
22
28template <typename T>
30{
31 public:
36 void fill(const T& value)
37 {
38 push(value);
39 push(value);
40 }
41
46 void push(const T& value)
47 {
48 advance();
49 buffer[state] = value;
50 }
51
55 void advance()
56 {
57 state = !state;
58 }
59
64 T& next()
65 {
66 advance();
67 return buffer[state];
68 }
69
74 const T& current() const { return buffer[state]; }
75
80 const T& last() const { return buffer[!state]; }
81
86 T& last() { return buffer[!state]; }
87
92 T& current() { return buffer[state]; }
93
94 private:
95 std::array<T, 2> buffer;
96 bool state = false;
97};
98
99} // namespace jsa::cicuetea
A double-buffer implementation for storing and switching between two values.
Definition DoubleBuffer.h:30
const T & last() const
Retrieves the last buffer value.
Definition DoubleBuffer.h:80
T & last()
Retrieves the last buffer value.
Definition DoubleBuffer.h:86
T & next()
Advances the buffer state and retrieves the next buffer value.
Definition DoubleBuffer.h:64
void advance()
Advances the buffer state to the next buffer.
Definition DoubleBuffer.h:55
const T & current() const
Retrieves the current buffer value.
Definition DoubleBuffer.h:74
void push(const T &value)
Pushes a value into the buffer and advances the state.
Definition DoubleBuffer.h:46
T & current()
Retrieves the current buffer value.
Definition DoubleBuffer.h:92
void fill(const T &value)
Fills both buffers with the given value.
Definition DoubleBuffer.h:36