25#include "../scope.hpp"
29namespace essence::memory {
48 : swapping_{}, swapped_out_{swapped_out}, unswapped_out_{unswapped_out}, current_out_{unswapped_out} {}
52 void reset(std::span<const T> input)
noexcept {
54 current_out_ = unswapped_out_;
64 void set_out(std::span<T> swapped_out, std::span<T> unswapped_out, std::span<const T> input = {})
noexcept {
65 swapped_out_ = swapped_out;
66 unswapped_out_ = unswapped_out;
80 std::span<T> swapped_out, std::span<T> unswapped_out, std::span<const T> input = {})
noexcept {
81 const auto entry_handler = [&] {
set_out(swapped_out, unswapped_out, input); };
82 const auto exit_handler = [
this, swapped = swapped_out_, unswapped = unswapped_out_] {
87 return scope_exit{entry_handler, exit_handler};
94 current_in_ = current_out_;
96 if ((swapping_ = !swapping_)) {
97 current_out_ = swapped_out_;
99 current_out_ = unswapped_out_;
107 [[nodiscard]] std::span<const T>
in() const noexcept {
115 [[nodiscard]] std::span<T>&
out() noexcept {
124 return swapping_ ? swapped_out_ : unswapped_out_;
129 std::span<T> swapped_out_;
130 std::span<T> unswapped_out_;
131 std::span<const T> current_in_;
132 std::span<T> current_out_;
A general state machine for foreground and background buffer swapping.
Definition swapping_buffer.hpp:35
swapping_buffer() noexcept
Creates an instance.
Definition swapping_buffer.hpp:40
std::span< T > & out() noexcept
Gets the lvalue reference to the current output buffer, which is replaceable with a subspan.
Definition swapping_buffer.hpp:115
auto set_temporary_out(std::span< T > swapped_out, std::span< T > unswapped_out, std::span< const T > input={}) noexcept
Set temporary outputs which is scoped in the current context. The output buffers will be restored whe...
Definition swapping_buffer.hpp:79
void swap() noexcept
Swaps the buffers.
Definition swapping_buffer.hpp:93
std::span< T > original_out() const noexcept
Gets the original output buffer.
Definition swapping_buffer.hpp:123
void set_out(std::span< T > swapped_out, std::span< T > unswapped_out, std::span< const T > input={}) noexcept
Sets the outputs.
Definition swapping_buffer.hpp:64
std::span< const T > in() const noexcept
Gets the current input buffer.
Definition swapping_buffer.hpp:107
swapping_buffer(std::span< T > swapped_out, std::span< T > unswapped_out) noexcept
Creates an instance.
Definition swapping_buffer.hpp:47