C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
swapping_buffer.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include "../scope.hpp"
26
27#include <span>
28
29namespace essence::memory {
34 template <typename T>
36 public:
40 swapping_buffer() noexcept : swapping_{} {}
41
47 swapping_buffer(std::span<T> swapped_out, std::span<T> unswapped_out) noexcept
48 : swapping_{}, swapped_out_{swapped_out}, unswapped_out_{unswapped_out}, current_out_{unswapped_out} {}
49
52 void reset(std::span<const T> input) noexcept {
53 current_in_ = input;
54 current_out_ = unswapped_out_;
55 swapping_ = false;
56 }
57
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;
67 reset(input);
68 }
69
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_] {
83 set_out(swapped, unswapped);
84 reset({});
85 };
86
87 return scope_exit{entry_handler, exit_handler};
88 };
89
93 void swap() noexcept {
94 current_in_ = current_out_;
95
96 if ((swapping_ = !swapping_)) {
97 current_out_ = swapped_out_;
98 } else {
99 current_out_ = unswapped_out_;
100 }
101 }
102
107 [[nodiscard]] std::span<const T> in() const noexcept {
108 return current_in_;
109 }
110
115 [[nodiscard]] std::span<T>& out() noexcept {
116 return current_out_;
117 }
118
123 [[nodiscard]] std::span<T> original_out() const noexcept {
124 return swapping_ ? swapped_out_ : unswapped_out_;
125 }
126
127 private:
128 bool swapping_;
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_;
133 };
134} // namespace essence::memory
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