2024-11-20 21:26:35 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
// common types
|
|
|
|
using u8 = std::uint8_t;
|
|
|
|
using i8 = std::int8_t;
|
|
|
|
using u16 = std::uint16_t;
|
|
|
|
using i16 = std::int16_t;
|
|
|
|
using u32 = std::uint32_t;
|
|
|
|
using i32 = std::int32_t;
|
|
|
|
using u64 = std::uint64_t;
|
|
|
|
using i64 = std::int64_t;
|
|
|
|
using usize = std::size_t;
|
|
|
|
using f32 = float;
|
|
|
|
using f64 = double;
|
|
|
|
|
|
|
|
/// like vector<T> but doesn't grow on its own
|
2024-11-20 21:29:22 -05:00
|
|
|
template <class T>
|
2024-11-20 21:26:35 -05:00
|
|
|
struct unique_buffer {
|
|
|
|
T* m_buffer;
|
|
|
|
size_t size;
|
2024-11-20 21:29:22 -05:00
|
|
|
|
|
|
|
public:
|
2024-11-20 21:26:35 -05:00
|
|
|
unique_buffer() {
|
|
|
|
m_buffer = nullptr;
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// forbid copying or movement for now
|
|
|
|
unique_buffer(const unique_buffer&) = delete;
|
|
|
|
unique_buffer(unique_buffer&&) = delete;
|
|
|
|
|
2024-11-20 21:29:22 -05:00
|
|
|
~unique_buffer() { resize(0); }
|
2024-11-20 21:26:35 -05:00
|
|
|
|
|
|
|
size_t get_size() const { return this->size; }
|
|
|
|
|
|
|
|
void resize(size_t new_size) {
|
2024-11-20 21:29:22 -05:00
|
|
|
if(m_buffer) {
|
2024-11-20 21:26:35 -05:00
|
|
|
delete[] m_buffer;
|
|
|
|
m_buffer = nullptr;
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
2024-11-20 21:29:22 -05:00
|
|
|
if(new_size) {
|
2024-11-20 21:26:35 -05:00
|
|
|
m_buffer = new T[new_size];
|
|
|
|
size = new_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T* data() { return &m_buffer[0]; }
|
|
|
|
|
|
|
|
const T* data() const { return &m_buffer[0]; }
|
|
|
|
};
|