AutoRelease better log and null handling

This commit is contained in:
Simone Coco
2024-05-20 15:13:30 +02:00
parent 315a522dbf
commit 7479ad46a6
6 changed files with 42 additions and 34 deletions

View File

@@ -8,13 +8,18 @@
namespace Util {
template <class T, typename... Args>
struct AutoRelease {
AutoRelease(void (*dtor)(T*)) : dtor(dtor) { }
AutoRelease(void (*dtor)(T*), const char* name = "") : dtor(dtor), name(name) { }
AutoRelease(T* (*ctor)(Args...), void (*dtor)(T*), Args... args) : dtor(dtor) {
AutoRelease(T* (*ctor)(Args...), void (*dtor)(T*), const char* name = "", Args... args) : dtor(dtor), name(name) {
thing = ctor(args...);
}
T* get() { return thing; }
T* get() {
if (!thing) {
Util::panic("AutoRelease::{} is null!", name);
}
return thing;
}
void Construct(T* (*ctor)(Args...), Args... args) {
if(thing) {
@@ -36,6 +41,7 @@ struct AutoRelease {
}
}
private:
const char* name = "";
T* thing = nullptr;
void (*dtor)(T*) = nullptr;
};