Nugget
Loading...
Searching...
No Matches
buffer.hh
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2025 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#include <stdint.h>
28
30
31namespace Utilities {
32
62template <typename T, class Allocator>
63class Buffer {
67
68 public:
69 Buffer() { m_size.clear(); }
70 Buffer(size_t size) {
71 m_size.clear();
72 resize(size);
73 }
74 Buffer(T* data, size_t size) {
75 m_data = data;
76 m_size.set<SizeField>(size);
77 m_size.set<IsExternalField>(true);
78 }
79 Buffer(const Buffer& other) {
80 uint32_t size = other.m_size.get<SizeField>();
81 m_size.set<SizeField>(size);
82 m_size.set<IsExternalField>(false);
83 m_data = Allocator::template allocate<T>(size);
84 Allocator::copy(m_data, other.m_data, size);
85 }
86 Buffer(Buffer&& other) noexcept : m_data(other.m_data), m_size(other.m_size) {
87 other.m_data = nullptr;
88 other.m_size.clear();
89 }
91 if (!m_size.get<IsExternalField>()) {
92 Allocator::deallocate(m_data);
93 }
94 }
95
96 Buffer& operator=(const Buffer& other) {
97 if (this != &other) {
98 uint32_t size = other.m_size.get<SizeField>();
99 if (m_size.get<IsExternalField>()) {
100 m_data = Allocator::template allocate<T>(size);
101 } else {
102 m_data = Allocator::template reallocate<T>(m_data, size);
103 }
104 Allocator::copy(m_data, other.m_data, size);
105 m_size.set<SizeField>(size);
106 m_size.set<IsExternalField>(false);
107 }
108 return *this;
109 }
110 Buffer& operator=(Buffer&& other) noexcept {
111 if (this != &other) {
112 if (!m_size.get<IsExternalField>()) {
113 Allocator::deallocate(m_data);
114 }
115 m_data = other.m_data;
116 m_size = other.m_size;
117 other.m_data = nullptr;
118 other.m_size.clear();
119 }
120 return *this;
121 }
122
123 void clear() {
124 if (!m_size.get<IsExternalField>()) {
125 Allocator::deallocate(m_data);
126 }
127 m_data = nullptr;
128 m_size.clear();
129 }
130
131 void resize(size_t size) {
132 if (!m_size.get<IsExternalField>()) {
133 m_data = Allocator::template reallocate<T>(m_data, size);
134 }
135 m_size.set<SizeField>(size);
136 }
137
138 T* data() { return m_data; }
139 const T* data() const { return m_data; }
140 size_t size() const { return m_size.get<SizeField>(); }
141 T& operator[](size_t index) { return m_data[index]; }
142 const T& operator[](size_t index) const { return m_data[index]; }
143 T* begin() { return m_data; }
144 T* end() { return m_data + m_size.get<SizeField>(); }
145 const T* begin() const { return m_data; }
146 const T* end() const { return m_data + m_size.get<SizeField>(); }
147 bool empty() const { return size() == 0; }
148
149 private:
150 T* m_data = nullptr;
151 Size m_size;
152};
153
154} // namespace Utilities
A class that manages a buffer of data.
Definition buffer.hh:63
Buffer(const Buffer &other)
Definition buffer.hh:79
const T & operator[](size_t index) const
Definition buffer.hh:142
Buffer(Buffer &&other) noexcept
Definition buffer.hh:86
T * data()
Definition buffer.hh:138
T * end()
Definition buffer.hh:144
T * begin()
Definition buffer.hh:143
void resize(size_t size)
Definition buffer.hh:131
bool empty() const
Definition buffer.hh:147
T & operator[](size_t index)
Definition buffer.hh:141
const T * data() const
Definition buffer.hh:139
Buffer & operator=(const Buffer &other)
Definition buffer.hh:96
~Buffer()
Definition buffer.hh:90
Buffer(size_t size)
Definition buffer.hh:70
Buffer & operator=(Buffer &&other) noexcept
Definition buffer.hh:110
const T * end() const
Definition buffer.hh:146
Buffer(T *data, size_t size)
Definition buffer.hh:74
const T * begin() const
Definition buffer.hh:145
void clear()
Definition buffer.hh:123
size_t size() const
Definition buffer.hh:140
Buffer()
Definition buffer.hh:69
Definition bitfield.hh:36
A bit field that can hold multiple bit field elements of different types.
Definition bitfield.hh:155
constexpr void set(Field::Type v_)
Definition bitfield.hh:164
constexpr Field::Type get() const
Definition bitfield.hh:157
void clear()
Definition bitfield.hh:170
A bit field element to be used in a BitField.
Definition bitfield.hh:130
void uint32_t(classId, spec)