A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
Types.hh
1#pragma once
2
3#include <Logger.hh>
4
5#include <egg/core/Heap.hh>
6
7#include <cstdint>
8#include <span>
9#include <type_traits>
10#include <utility>
11
12namespace Kinoko {
13
14typedef int8_t s8;
15typedef int16_t s16;
16typedef int32_t s32;
17typedef int64_t s64;
18
19typedef uint8_t u8;
20typedef uint16_t u16;
21typedef uint32_t u32;
22typedef uint64_t u64;
23
24typedef float f32;
25typedef double f64;
26
31template <typename T>
33public:
35 owning_span() : m_data(nullptr), m_size(0) {}
36
38 owning_span(size_t count) : m_data(EGG::egg_new_array<T>(count)), m_size(count) {}
39
42 owning_span(const std::span<const T> &span)
43 : m_data(EGG::egg_new_array<T>(span.size())), m_size(span.size()) {
44 std::copy(span.begin(), span.end(), m_data);
45 }
46
49 owning_span(const owning_span &rhs) : m_size(rhs.m_size) {
50 m_data = EGG::egg_new_array<T>(m_size);
51 std::copy(rhs.begin(), rhs.end(), m_data);
52 }
53
57 m_data = rhs.m_data;
58 rhs.m_data = nullptr;
59
60 m_size = rhs.m_size;
61 rhs.m_size = 0;
62 }
63
67 if (this != &rhs) {
68 EGG::egg_delete_array(m_data, m_size);
69 m_size = rhs.m_size;
70 m_data = EGG::egg_new_array<T>(m_size);
71 std::copy(rhs.begin(), rhs.end(), m_data);
72 }
73
74 return *this;
75 }
76
80 if (this != &rhs) {
81 EGG::egg_delete_array(m_data, m_size);
82 m_data = rhs.m_data;
83 rhs.m_data = nullptr;
84 m_size = rhs.m_size;
85 rhs.m_size = 0;
86 }
87
88 return *this;
89 }
90
93 EGG::egg_delete_array(m_data, m_size);
94 }
95
98 [[nodiscard]] T &operator[](size_t idx) {
99 ASSERT(idx < m_size);
100 return m_data[idx];
101 }
102
105 [[nodiscard]] const T &operator[](size_t idx) const {
106 ASSERT(idx < m_size);
107 return m_data[idx];
108 }
109
111 [[nodiscard]] T &front() {
112 ASSERT(m_size > 0);
113 return m_data[0];
114 }
115
117 [[nodiscard]] const T &front() const {
118 ASSERT(m_size > 0);
119 return m_data[0];
120 }
121
123 [[nodiscard]] T &back() {
124 ASSERT(m_size > 0);
125 return m_data[m_size - 1];
126 }
127
129 [[nodiscard]] const T &back() const {
130 ASSERT(m_size > 0);
131 return m_data[m_size - 1];
132 }
133
134 [[nodiscard]] T *begin() {
135 return m_data;
136 }
137
138 [[nodiscard]] T *end() {
139 return m_data + m_size;
140 }
141
142 [[nodiscard]] const T *begin() const {
143 return m_data;
144 }
145
146 [[nodiscard]] const T *end() const {
147 return m_data + m_size;
148 }
149
151 [[nodiscard]] bool empty() const {
152 return m_size == 0;
153 }
154
156 [[nodiscard]] size_t size() const {
157 return m_size;
158 }
159
161 [[nodiscard]] std::span<const T> view() const {
162 return {m_data, m_size};
163 }
164
165private:
167 size_t m_size;
168};
169
176template <typename T>
178public:
180 fixed_vector() : m_data(nullptr), m_size(0), m_capacity(0) {}
181
187
191 if (rhs.initialized()) {
192 allocate(rhs.m_capacity);
193 for (size_t i = 0; i < rhs.m_size; ++i) {
194 push_back(rhs[i]);
195 }
196 }
197 }
198
202 : m_data(rhs.m_data), m_size(rhs.m_size), m_capacity(rhs.m_capacity) {
203 rhs.m_data = nullptr;
204 rhs.m_size = 0;
205 rhs.m_capacity = 0;
206 }
207
211 if (this != &rhs) {
212 destroy();
213 if (rhs.initialized()) {
214 allocate(rhs.m_capacity);
215 for (size_t i = 0; i < rhs.m_size; ++i) {
216 push_back(rhs[i]);
217 }
218 }
219 }
220
221 return *this;
222 }
223
227 if (this != &rhs) {
228 destroy();
229
230 m_data = rhs.m_data;
231 m_size = rhs.m_size;
232 m_capacity = rhs.m_capacity;
233
234 rhs.m_data = nullptr;
235 rhs.m_size = 0;
236 rhs.m_capacity = 0;
237 }
238
239 return *this;
240 }
241
245 destroy();
246 }
247
251 T &push_back(const T &obj) {
252 ASSERT(initialized() && !full());
253 new (m_data + m_size++) T(obj);
254 return back();
255 }
256
260 T &push_back(T &&obj) {
261 ASSERT(initialized() && !full());
262 new (m_data + m_size++) T(std::move(obj));
263 return back();
264 }
265
270 template <typename... Args>
271 T &emplace_back(Args &&...args) {
272 ASSERT(initialized() && !full());
273 new (m_data + m_size++) T(std::forward<Args>(args)...);
274 return back();
275 }
276
278 void pop_back() {
279 ASSERT(initialized() && !empty());
280 m_data[--m_size].~T();
281 }
282
285 void reserve(size_t capacity) {
286 ASSERT(!initialized());
288 }
289
292 [[nodiscard]] bool empty() const {
293 return m_size == 0;
294 }
295
298 [[nodiscard]] bool full() const {
299 return m_size == m_capacity;
300 }
301
304 [[nodiscard]] bool initialized() const {
305 return m_data && m_capacity != 0;
306 }
307
310 [[nodiscard]] size_t size() const {
311 return m_size;
312 }
313
316 [[nodiscard]] size_t capacity() const {
317 return m_capacity;
318 }
319
322 T &front() {
323 ASSERT(m_size > 0);
324 return *m_data;
325 }
326
329 const T &front() const {
330 ASSERT(m_size > 0);
331 return *m_data;
332 }
333
336 T &back() {
337 ASSERT(m_size > 0);
338 return m_data[m_size - 1];
339 }
340
343 const T &back() const {
344 ASSERT(m_size > 0);
345 return m_data[m_size - 1];
346 }
347
351 T &operator[](size_t idx) {
352 ASSERT(idx < m_size);
353 return m_data[idx];
354 }
355
359 const T &operator[](size_t idx) const {
360 ASSERT(idx < m_size);
361 return m_data[idx];
362 }
363
366 T *begin() noexcept {
367 ASSERT(initialized());
368 return m_data;
369 }
370
373 const T *begin() const noexcept {
374 ASSERT(initialized());
375 return m_data;
376 }
377
380 T *end() noexcept {
381 ASSERT(initialized());
382 return m_data + m_size;
383 }
384
387 const T *end() const noexcept {
388 ASSERT(initialized());
389 return m_data + m_size;
390 }
391
392private:
394 void destroy() {
395 while (m_size > 0) {
396 pop_back();
397 }
398
399 EGG::egg_free(m_data);
400 m_data = nullptr;
401 m_capacity = 0;
402 }
403
406 void allocate(size_t capacity) {
407 ASSERT(!initialized());
408 m_data = static_cast<T *>(
409 EGG::egg_alloc(sizeof(T) * capacity, static_cast<s32>(alignof(T))));
410 m_capacity = capacity;
411 }
412
413 T *m_data; // The underlying array pointer.
414 size_t m_size; // The number of existing elements in the array.
415 size_t m_capacity; // The maximum number of elements that can exist in the array.
416};
417
418} // namespace Kinoko
T & push_back(const T &obj)
Copies a new element into the array.
Definition Types.hh:251
fixed_vector(fixed_vector &&rhs)
Move constructor.
Definition Types.hh:201
size_t capacity() const
Gets the maximum number of elements that can exist in the array.
Definition Types.hh:316
bool empty() const
Checks if there are no existing elements in the array.
Definition Types.hh:292
fixed_vector & operator=(fixed_vector &&rhs)
Move assignment operator.
Definition Types.hh:226
const T * end() const noexcept
Iterator for the end of the existing array.
Definition Types.hh:387
const T & back() const
Gets the last existing element in the array.
Definition Types.hh:343
fixed_vector()
Non-initializing constructor.
Definition Types.hh:180
T & push_back(T &&obj)
Moves a new element into the array.
Definition Types.hh:260
bool initialized() const
Checks if the array exists and if the capacity is non-zero.
Definition Types.hh:304
fixed_vector(const fixed_vector &rhs)
Copy constructor.
Definition Types.hh:190
T & operator[](size_t idx)
Indexes the array. Validates that the object exists.
Definition Types.hh:351
fixed_vector & operator=(const fixed_vector &rhs)
Copy assignment operator.
Definition Types.hh:210
const T * begin() const noexcept
Iterator for the beginning of the existing array.
Definition Types.hh:373
T * end() noexcept
Iterator for the end of the existing array.
Definition Types.hh:380
T & emplace_back(Args &&...args)
Creates a new element in-place in the array.
Definition Types.hh:271
T & back()
Gets the last existing element in the array.
Definition Types.hh:336
~fixed_vector()
Destructor.
Definition Types.hh:244
void pop_back()
Deletes the last existing element from the array.
Definition Types.hh:278
void destroy()
Destroys existing elements and frees the buffer, resetting to an uninitialized state.
Definition Types.hh:394
bool full() const
Checks if all elements exist in the array.
Definition Types.hh:298
const T & operator[](size_t idx) const
Indexes the array. Validates that the object exists.
Definition Types.hh:359
T * begin() noexcept
Iterator for the beginning of the existing array.
Definition Types.hh:366
size_t size() const
Gets the number of existing elements in the array.
Definition Types.hh:310
const T & front() const
Gets the first element in the array.
Definition Types.hh:329
T & front()
Gets the first element in the array.
Definition Types.hh:322
fixed_vector(size_t capacity)
Initializing constructor.
Definition Types.hh:184
void allocate(size_t capacity)
Allocates the array.
Definition Types.hh:406
void reserve(size_t capacity)
Initializes the vector with the provided capacity.
Definition Types.hh:285
owning_span()
Uninitialized buffer.
Definition Types.hh:35
const T & back() const
Retrieves the last element in the buffer.
Definition Types.hh:129
owning_span & operator=(const owning_span &rhs)
Copy assignment operator.
Definition Types.hh:66
const T & front() const
Retrieves the first element in the buffer.
Definition Types.hh:117
T & back()
Retrieves the last element in the buffer.
Definition Types.hh:123
owning_span(const owning_span &rhs)
Copy constructor.
Definition Types.hh:49
bool empty() const
Returns true if the buffer is uninitialized.
Definition Types.hh:151
T & front()
Retrieves the first element in the buffer.
Definition Types.hh:111
size_t size() const
Returns the number of elements that fit in the buffer.
Definition Types.hh:156
T * m_data
Pointer to the underlying buffer.
Definition Types.hh:166
~owning_span()
Destroys the underlying buffer on teardown.
Definition Types.hh:92
owning_span(size_t count)
Allocates a buffer of T elements. Does not initialize any elements.
Definition Types.hh:38
owning_span & operator=(owning_span &&rhs)
Move assignment operator.
Definition Types.hh:79
size_t m_size
The number of T elements that fit in the buffer.
Definition Types.hh:167
const T & operator[](size_t idx) const
Indexes into the underlying buffer.
Definition Types.hh:105
T & operator[](size_t idx)
Indexes into the underlying buffer.
Definition Types.hh:98
owning_span(owning_span &&rhs)
Move constructor.
Definition Types.hh:56
owning_span(const std::span< const T > &span)
Performs a deep copy from a std::span of const T.
Definition Types.hh:42
std::span< const T > view() const
Returns a read-only view of the entire buffer.
Definition Types.hh:161
EGG core library.
Definition Allocator.hh:5