EuropaTools/include/europa/util/UniqueArray.hpp
modeco80 d0b8d0f3ea libeuropa/io/yatf: Fix YATF 24-bpp support
This completely redoes the garbage flags stuff I did before. OOPS. This seems a lot closer to the format, and also exports everything Starfighter has properly.
2025-01-16 01:12:53 -05:00

81 lines
No EOL
1.5 KiB
C++

//
// EuropaTools
//
// (C) 2021-2025 modeco80 <lily.modeco80@protonmail.ch>
//
// SPDX-License-Identifier: MIT
//
#pragma once
#include <cstddef>
#include <memory>
namespace europa::util {
/// A little ergonomic wrapper over
/// std::unique_ptr<T[]> for buffers
/// that need to track their size as well.
template <class T>
struct UniqueArray final {
UniqueArray() = default;
explicit UniqueArray(std::size_t size) {
Resize(size);
}
UniqueArray(const UniqueArray&) = delete;
UniqueArray(UniqueArray&& move) {
array = std::move(move.array);
size = move.size;
// invalidate moved-from array to default state
move.array = nullptr;
move.size = 0;
}
UniqueArray& operator=(UniqueArray&& move) {
array = std::move(move.array);
size = move.size;
// invalidate moved-from array to default state
move.array = nullptr;
move.size = 0;
}
void Resize(std::size_t size) {
this->array = std::make_unique<T[]>(size);
this->size = size;
}
/// Clears this array.
void Clear() {
this->array.reset();
this->size = 0;
}
T& operator[](std::size_t index) {
return (array.get())[index];
}
const T& operator[](std::size_t index) const {
return (array.get())[index];
}
T* Data() {
return array.get();
}
const T* Data() const {
return array.get();
}
std::size_t Size() const {
return size;
}
private:
std::unique_ptr<T[]> array {};
std::size_t size {};
};
} // namespace europa::util