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
9typedef int8_t s8;
10typedef int16_t s16;
11typedef int32_t s32;
12typedef int64_t s64;
13
14typedef uint8_t u8;
15typedef uint16_t u16;
16typedef uint32_t u32;
17typedef uint64_t u64;
18
19typedef float f32;
20typedef double f64;
21
26template <typename T>
28public:
30 owning_span() : m_data(nullptr), m_size(0) {}
31
33 owning_span(size_t count) : m_data(new T[count]), m_size(count) {}
34
37 owning_span(const std::span<const T> &span) : m_data(new T[span.size()]), m_size(span.size()) {
38 std::copy(span.begin(), span.end(), m_data);
39 }
40
43 owning_span(const owning_span &rhs) : m_size(rhs.m_size) {
44 m_data = new T[m_size];
45 std::copy(rhs.begin(), rhs.end(), m_data);
46 }
47
51 m_data = rhs.m_data;
52 rhs.m_data = nullptr;
53
54 m_size = rhs.m_size;
55 rhs.m_size = 0;
56 }
57
61 if (this != &rhs) {
62 delete[] m_data;
63 m_size = rhs.m_size;
64 m_data = new T[m_size];
65 std::copy(rhs.begin(), rhs.end(), m_data);
66 }
67
68 return *this;
69 }
70
74 if (this != &rhs) {
75 delete[] m_data;
76 m_data = rhs.m_data;
77 rhs.m_data = nullptr;
78 m_size = rhs.m_size;
79 rhs.m_size = 0;
80 }
81
82 return *this;
83 }
84
87 delete[] m_data;
88 }
89
92 [[nodiscard]] T &operator[](size_t idx) {
93 ASSERT(idx < m_size);
94 return m_data[idx];
95 }
96
99 [[nodiscard]] const T &operator[](size_t idx) const {
100 ASSERT(idx < m_size);
101 return m_data[idx];
102 }
103
105 [[nodiscard]] T &front() {
106 ASSERT(m_size > 0);
107 return m_data[0];
108 }
109
111 [[nodiscard]] const T &front() const {
112 ASSERT(m_size > 0);
113 return m_data[0];
114 }
115
117 [[nodiscard]] T &back() {
118 ASSERT(m_size > 0);
119 return m_data[m_size - 1];
120 }
121
123 [[nodiscard]] const T &back() const {
124 ASSERT(m_size > 0);
125 return m_data[m_size - 1];
126 }
127
128 [[nodiscard]] T *begin() {
129 return m_data;
130 }
131
132 [[nodiscard]] T *end() {
133 return m_data + m_size;
134 }
135
136 [[nodiscard]] const T *begin() const {
137 return m_data;
138 }
139
140 [[nodiscard]] const T *end() const {
141 return m_data + m_size;
142 }
143
145 [[nodiscard]] bool empty() const {
146 return m_size == 0;
147 }
148
150 [[nodiscard]] size_t size() const {
151 return m_size;
152 }
153
155 [[nodiscard]] std::span<const T> view() const {
156 return {m_data, m_size};
157 }
158
159private:
161 size_t m_size;
162};
A contiguous storage container that manages the lifecycle of a buffer of a given size.
Definition Types.hh:27
~owning_span()
Destroys the underlying buffer on teardown.
Definition Types.hh:86
T & front()
Retrieves the first element in the buffer.
Definition Types.hh:105
const T & front() const
Retrieves the first element in the buffer.
Definition Types.hh:111
size_t m_size
The number of T elements that fit in the buffer.
Definition Types.hh:161
T * m_data
Pointer to the underlying buffer.
Definition Types.hh:160
owning_span(size_t count)
Allocates a buffer of T elements. Does not initialize any elements.
Definition Types.hh:33
owning_span(const owning_span &rhs)
Copy constructor.
Definition Types.hh:43
bool empty() const
Returns true if the buffer is uninitialized.
Definition Types.hh:145
owning_span(const std::span< const T > &span)
Performs a deep copy from a std::span of const T.
Definition Types.hh:37
owning_span & operator=(const owning_span &rhs)
Copy assignment operator.
Definition Types.hh:60
T & back()
Retrieves the last element in the buffer.
Definition Types.hh:117
std::span< const T > view() const
Returns a read-only view of the entire buffer.
Definition Types.hh:155
const T & operator[](size_t idx) const
Indexes into the underlying buffer.
Definition Types.hh:99
owning_span & operator=(owning_span &&rhs)
Move assignment operator.
Definition Types.hh:73
owning_span(owning_span &&rhs)
Move constructor.
Definition Types.hh:50
T & operator[](size_t idx)
Indexes into the underlying buffer.
Definition Types.hh:92
size_t size() const
Returns the number of elements that fit in the buffer.
Definition Types.hh:150
const T & back() const
Retrieves the last element in the buffer.
Definition Types.hh:123
owning_span()
Uninitialized buffer.
Definition Types.hh:30