MostlyHarmless 0.0.1
 
Loading...
Searching...
No Matches
marvin_SwapBuffer.h
Go to the documentation of this file.
1// ========================================================================================================
2// _______ _______ ______ ___ ___ _______ _______
3// | | | _ | __ \ | |_ _| | |
4// | | | < | |_| |_| |
5// |__|_|__|___|___|___|__|\_____/|_______|__|____|
6//
7// This file is part of the Marvin open source library and is licensed under the terms of the MIT License.
8//
9// ========================================================================================================
10
11#ifndef MARVIN_SWAPBUFFER_H
12#define MARVIN_SWAPBUFFER_H
14#include <type_traits>
15#include <vector>
16#include <span>
17#include <cassert>
18
19namespace marvin::containers {
24 template <typename T>
25 requires std::is_default_constructible_v<T> &&
26 std::is_copy_constructible_v<T> &&
27 std::is_move_constructible_v<T>
28 class SwapBuffer final {
29 public:
34 SwapBuffer() = default;
35 SwapBuffer(const SwapBuffer<T>& other) = default;
36 SwapBuffer(SwapBuffer<T>&& other) noexcept = default;
37
42 explicit SwapBuffer(size_t len) {
43 m_a.resize(len);
44 m_b.resize(len);
45 }
46
53 SwapBuffer(size_t len, T fillV) {
54 m_a.resize(len);
55 m_b.resize(len);
56 std::fill(m_a.begin(), m_a.end(), fillV);
57 std::fill(m_b.begin(), m_b.end(), fillV);
58 }
59
60 ~SwapBuffer() noexcept = default;
61
62 SwapBuffer& operator=(const SwapBuffer<T>& other) = default;
63 SwapBuffer& operator=(SwapBuffer<T>&& other) noexcept = default;
64
70 [[nodiscard]] size_t size() const noexcept {
71 assert(m_a.size() == m_b.size());
72 return m_a.size();
73 }
74
81 void reserve(size_t toReserve) {
82 m_a.reserve(toReserve);
83 m_b.reserve(toReserve);
84 }
85
91 void resize(size_t newSize) {
92 m_a.resize(newSize);
93 m_b.resize(newSize);
94 }
95
100 [[nodiscard]] std::span<T> getFrontBuffer() {
101 auto& buffToUse = m_aIsFrontBuffer ? m_a : m_b;
102 return buffToUse;
103 }
104
109 [[nodiscard]] std::span<T> getBackBuffer() {
110 auto& buffToUse = m_aIsFrontBuffer ? m_b : m_a;
111 return buffToUse;
112 }
113
119 void swap() noexcept {
120 m_aIsFrontBuffer = !m_aIsFrontBuffer;
121 }
122
123
124 private:
125 std::vector<T> m_a;
126 std::vector<T> m_b;
127 bool m_aIsFrontBuffer{ true };
128 };
129} // namespace marvin::containers
130#endif
SwapBuffer(SwapBuffer< T > &&other) noexcept=default
void reserve(size_t toReserve)
Definition marvin_SwapBuffer.h:81
void swap() noexcept
Definition marvin_SwapBuffer.h:119
size_t size() const noexcept
Definition marvin_SwapBuffer.h:70
SwapBuffer(size_t len)
Definition marvin_SwapBuffer.h:42
std::span< T > getFrontBuffer()
Definition marvin_SwapBuffer.h:100
SwapBuffer(size_t len, T fillV)
Definition marvin_SwapBuffer.h:53
SwapBuffer(const SwapBuffer< T > &other)=default
std::span< T > getBackBuffer()
Definition marvin_SwapBuffer.h:109
void resize(size_t newSize)
Definition marvin_SwapBuffer.h:91
~SwapBuffer() noexcept=default
Views, wrappers and custom containers.
Definition marvin_BufferView.h:15