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
11namespace Kinoko {
12
13typedef int8_t s8;
14typedef int16_t s16;
15typedef int32_t s32;
16typedef int64_t s64;
17
18typedef uint8_t u8;
19typedef uint16_t u16;
20typedef uint32_t u32;
21typedef uint64_t u64;
22
23typedef float f32;
24typedef double f64;
25
30template <typename T>
32public:
34 owning_span() : m_data(nullptr), m_size(0) {}
35
37 owning_span(size_t count) : m_data(EGG::egg_new_array<T>(count)), m_size(count) {}
38
41 owning_span(const std::span<const T> &span)
42 : m_data(EGG::egg_new_array<T>(span.size())), m_size(span.size()) {
43 std::copy(span.begin(), span.end(), m_data);
44 }
45
48 owning_span(const owning_span &rhs) : m_size(rhs.m_size) {
49 m_data = EGG::egg_new_array<T>(m_size);
50 std::copy(rhs.begin(), rhs.end(), m_data);
51 }
52
56 m_data = rhs.m_data;
57 rhs.m_data = nullptr;
58
59 m_size = rhs.m_size;
60 rhs.m_size = 0;
61 }
62
66 if (this != &rhs) {
67 EGG::egg_delete_array(m_data, m_size);
68 m_size = rhs.m_size;
69 m_data = EGG::egg_new_array<T>(m_size);
70 std::copy(rhs.begin(), rhs.end(), m_data);
71 }
72
73 return *this;
74 }
75
79 if (this != &rhs) {
80 EGG::egg_delete_array(m_data, m_size);
81 m_data = rhs.m_data;
82 rhs.m_data = nullptr;
83 m_size = rhs.m_size;
84 rhs.m_size = 0;
85 }
86
87 return *this;
88 }
89
92 EGG::egg_delete_array(m_data, m_size);
93 }
94
97 [[nodiscard]] T &operator[](size_t idx) {
98 ASSERT(idx < m_size);
99 return m_data[idx];
100 }
101
104 [[nodiscard]] const T &operator[](size_t idx) const {
105 ASSERT(idx < m_size);
106 return m_data[idx];
107 }
108
110 [[nodiscard]] T &front() {
111 ASSERT(m_size > 0);
112 return m_data[0];
113 }
114
116 [[nodiscard]] const T &front() const {
117 ASSERT(m_size > 0);
118 return m_data[0];
119 }
120
122 [[nodiscard]] T &back() {
123 ASSERT(m_size > 0);
124 return m_data[m_size - 1];
125 }
126
128 [[nodiscard]] const T &back() const {
129 ASSERT(m_size > 0);
130 return m_data[m_size - 1];
131 }
132
133 [[nodiscard]] T *begin() {
134 return m_data;
135 }
136
137 [[nodiscard]] T *end() {
138 return m_data + m_size;
139 }
140
141 [[nodiscard]] const T *begin() const {
142 return m_data;
143 }
144
145 [[nodiscard]] const T *end() const {
146 return m_data + m_size;
147 }
148
150 [[nodiscard]] bool empty() const {
151 return m_size == 0;
152 }
153
155 [[nodiscard]] size_t size() const {
156 return m_size;
157 }
158
160 [[nodiscard]] std::span<const T> view() const {
161 return {m_data, m_size};
162 }
163
164private:
166 size_t m_size;
167};
168
169} // namespace Kinoko
owning_span()
Uninitialized buffer.
Definition Types.hh:34
const T & back() const
Retrieves the last element in the buffer.
Definition Types.hh:128
owning_span & operator=(const owning_span &rhs)
Copy assignment operator.
Definition Types.hh:65
const T & front() const
Retrieves the first element in the buffer.
Definition Types.hh:116
T & back()
Retrieves the last element in the buffer.
Definition Types.hh:122
owning_span(const owning_span &rhs)
Copy constructor.
Definition Types.hh:48
bool empty() const
Returns true if the buffer is uninitialized.
Definition Types.hh:150
T & front()
Retrieves the first element in the buffer.
Definition Types.hh:110
size_t size() const
Returns the number of elements that fit in the buffer.
Definition Types.hh:155
T * m_data
Pointer to the underlying buffer.
Definition Types.hh:165
~owning_span()
Destroys the underlying buffer on teardown.
Definition Types.hh:91
owning_span(size_t count)
Allocates a buffer of T elements. Does not initialize any elements.
Definition Types.hh:37
owning_span & operator=(owning_span &&rhs)
Move assignment operator.
Definition Types.hh:78
size_t m_size
The number of T elements that fit in the buffer.
Definition Types.hh:166
const T & operator[](size_t idx) const
Indexes into the underlying buffer.
Definition Types.hh:104
T & operator[](size_t idx)
Indexes into the underlying buffer.
Definition Types.hh:97
owning_span(owning_span &&rhs)
Move constructor.
Definition Types.hh:55
owning_span(const std::span< const T > &span)
Performs a deep copy from a std::span of const T.
Definition Types.hh:41
std::span< const T > view() const
Returns a read-only view of the entire buffer.
Definition Types.hh:160
EGG core library.
Definition Allocator.hh:5