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