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 <cstdint>
6#include <span>
7#include <type_traits>
8
9namespace Kinoko {
10
11typedef int8_t s8;
12typedef int16_t s16;
13typedef int32_t s32;
14typedef int64_t s64;
15
16typedef uint8_t u8;
17typedef uint16_t u16;
18typedef uint32_t u32;
19typedef uint64_t u64;
20
21typedef float f32;
22typedef double f64;
23
28template <typename T>
30public:
32 owning_span() : m_data(nullptr), m_size(0) {}
33
35 owning_span(size_t count) : m_data(new T[count]), m_size(count) {}
36
39 owning_span(const std::span<const T> &span) : m_data(new T[span.size()]), m_size(span.size()) {
40 std::copy(span.begin(), span.end(), m_data);
41 }
42
45 owning_span(const owning_span &rhs) : m_size(rhs.m_size) {
46 m_data = new T[m_size];
47 std::copy(rhs.begin(), rhs.end(), m_data);
48 }
49
53 m_data = rhs.m_data;
54 rhs.m_data = nullptr;
55
56 m_size = rhs.m_size;
57 rhs.m_size = 0;
58 }
59
63 if (this != &rhs) {
64 delete[] m_data;
65 m_size = rhs.m_size;
66 m_data = new T[m_size];
67 std::copy(rhs.begin(), rhs.end(), m_data);
68 }
69
70 return *this;
71 }
72
76 if (this != &rhs) {
77 delete[] m_data;
78 m_data = rhs.m_data;
79 rhs.m_data = nullptr;
80 m_size = rhs.m_size;
81 rhs.m_size = 0;
82 }
83
84 return *this;
85 }
86
89 delete[] m_data;
90 }
91
94 [[nodiscard]] T &operator[](size_t idx) {
95 ASSERT(idx < m_size);
96 return m_data[idx];
97 }
98
101 [[nodiscard]] const T &operator[](size_t idx) const {
102 ASSERT(idx < m_size);
103 return m_data[idx];
104 }
105
107 [[nodiscard]] T &front() {
108 ASSERT(m_size > 0);
109 return m_data[0];
110 }
111
113 [[nodiscard]] const T &front() const {
114 ASSERT(m_size > 0);
115 return m_data[0];
116 }
117
119 [[nodiscard]] T &back() {
120 ASSERT(m_size > 0);
121 return m_data[m_size - 1];
122 }
123
125 [[nodiscard]] const T &back() const {
126 ASSERT(m_size > 0);
127 return m_data[m_size - 1];
128 }
129
130 [[nodiscard]] T *begin() {
131 return m_data;
132 }
133
134 [[nodiscard]] T *end() {
135 return m_data + m_size;
136 }
137
138 [[nodiscard]] const T *begin() const {
139 return m_data;
140 }
141
142 [[nodiscard]] const T *end() const {
143 return m_data + m_size;
144 }
145
147 [[nodiscard]] bool empty() const {
148 return m_size == 0;
149 }
150
152 [[nodiscard]] size_t size() const {
153 return m_size;
154 }
155
157 [[nodiscard]] std::span<const T> view() const {
158 return {m_data, m_size};
159 }
160
161private:
163 size_t m_size;
164};
165
166} // namespace Kinoko
A contiguous storage container that manages the lifecycle of a buffer of a given size.
Definition Types.hh:29
owning_span()
Uninitialized buffer.
Definition Types.hh:32
const T & back() const
Retrieves the last element in the buffer.
Definition Types.hh:125
owning_span & operator=(const owning_span &rhs)
Copy assignment operator.
Definition Types.hh:62
const T & front() const
Retrieves the first element in the buffer.
Definition Types.hh:113
T & back()
Retrieves the last element in the buffer.
Definition Types.hh:119
owning_span(const owning_span &rhs)
Copy constructor.
Definition Types.hh:45
bool empty() const
Returns true if the buffer is uninitialized.
Definition Types.hh:147
T & front()
Retrieves the first element in the buffer.
Definition Types.hh:107
size_t size() const
Returns the number of elements that fit in the buffer.
Definition Types.hh:152
T * m_data
Pointer to the underlying buffer.
Definition Types.hh:162
~owning_span()
Destroys the underlying buffer on teardown.
Definition Types.hh:88
owning_span(size_t count)
Allocates a buffer of T elements. Does not initialize any elements.
Definition Types.hh:35
owning_span & operator=(owning_span &&rhs)
Move assignment operator.
Definition Types.hh:75
size_t m_size
The number of T elements that fit in the buffer.
Definition Types.hh:163
const T & operator[](size_t idx) const
Indexes into the underlying buffer.
Definition Types.hh:101
T & operator[](size_t idx)
Indexes into the underlying buffer.
Definition Types.hh:94
owning_span(owning_span &&rhs)
Move constructor.
Definition Types.hh:52
owning_span(const std::span< const T > &span)
Performs a deep copy from a std::span of const T.
Definition Types.hh:39
std::span< const T > view() const
Returns a read-only view of the entire buffer.
Definition Types.hh:157