obliteration/gui/core.hpp

134 lines
1.9 KiB
C++
Raw Normal View History

2023-10-09 10:50:29 +00:00
#pragma once
#include "core.h"
2023-10-09 10:50:29 +00:00
#include <stdlib.h>
template<typename T>
class Rust final {
2023-10-09 10:50:29 +00:00
public:
Rust() : m_ptr(nullptr) {}
explicit Rust(T *ptr) : m_ptr(ptr) {}
Rust(const Rust &) = delete;
2023-10-09 10:50:29 +00:00
Rust(Rust &&other) : m_ptr(other.m_ptr)
2023-10-09 10:50:29 +00:00
{
other.m_ptr = nullptr;
2023-10-09 10:50:29 +00:00
}
~Rust()
2023-10-09 10:50:29 +00:00
{
free();
2023-10-09 10:50:29 +00:00
}
Rust &operator=(const Rust &) = delete;
2023-10-09 10:50:29 +00:00
Rust &operator=(Rust &&other)
{
free();
m_ptr = other.m_ptr;
other.m_ptr = nullptr;
return *this;
2023-10-09 10:50:29 +00:00
}
Rust &operator=(T *ptr)
2023-10-09 10:50:29 +00:00
{
free();
m_ptr = ptr;
return *this;
2023-10-09 10:50:29 +00:00
}
operator T *() const { return m_ptr; }
operator bool() const { return m_ptr != nullptr; }
T **operator&()
2023-10-09 10:50:29 +00:00
{
free();
return &m_ptr;
2023-10-09 10:50:29 +00:00
}
T *get() const { return m_ptr; }
void free();
T *release()
{
auto p = m_ptr;
m_ptr = nullptr;
return p;
}
2023-10-09 10:50:29 +00:00
private:
T *m_ptr;
2023-10-09 10:50:29 +00:00
};
template<>
inline void Rust<char>::free()
{
::free(m_ptr);
m_ptr = nullptr;
}
2024-03-30 15:27:55 +00:00
template<>
inline void Rust<Debugger>::free()
{
if (m_ptr) {
debugger_free(m_ptr);
m_ptr = nullptr;
}
}
template<>
inline void Rust<DebugServer>::free()
{
if (m_ptr) {
debug_server_free(m_ptr);
m_ptr = nullptr;
}
}
template<>
inline void Rust<Param>::free()
{
if (m_ptr) {
param_close(m_ptr);
m_ptr = nullptr;
2024-03-30 15:27:55 +00:00
}
}
template<>
inline void Rust<Pkg>::free()
{
if (m_ptr) {
pkg_close(m_ptr);
m_ptr = nullptr;
}
}
template<>
inline void Rust<Profile>::free()
{
if (m_ptr) {
profile_free(m_ptr);
m_ptr = nullptr;
}
}
template<>
inline void Rust<RustError>::free()
{
if (m_ptr) {
error_free(m_ptr);
m_ptr = nullptr;
}
}
template<>
inline void Rust<Vmm>::free()
{
if (m_ptr) {
vmm_free(m_ptr);
m_ptr = nullptr;
2024-07-14 19:52:33 +00:00
}
}