Add Handle class.

This commit is contained in:
King_DuckZ 2020-04-28 14:25:35 +02:00
parent 67a5fab3c4
commit 79f59f5d36
5 changed files with 49 additions and 0 deletions

View file

@ -44,6 +44,7 @@ executable(meson.project_name(),
'src/wren/def_configuration.cpp', 'src/wren/def_configuration.cpp',
'src/wren/dynafunc_maker.cpp', 'src/wren/dynafunc_maker.cpp',
'src/dynafunc_' + arch + '_' + os + '.S', 'src/dynafunc_' + arch + '_' + os + '.S',
'src/wren/handle.cpp',
dependencies: [wren_dep], dependencies: [wren_dep],
install: true, install: true,
) )

14
src/wren/handle.cpp Normal file
View file

@ -0,0 +1,14 @@
#include "handle.hpp"
#include "vm.hpp"
#include <cassert>
namespace wren {
Handle::~Handle() noexcept {
release();
}
void Handle::release() noexcept {
assert(m_vm);
m_vm->release(*this);
}
} //namespace wren

26
src/wren/handle.hpp Normal file
View file

@ -0,0 +1,26 @@
#pragma once
typedef struct WrenHandle WrenHandle;
namespace wren {
class VM;
class Handle {
public:
explicit Handle (VM* vm, WrenHandle* handle) noexcept :
m_handle(handle),
m_vm(vm)
{
}
~Handle() noexcept;
void release() noexcept;
operator WrenHandle*() { return m_handle; }
operator bool() const { return nullptr != m_handle; }
private:
WrenHandle* m_handle;
VM* m_vm;
};
} //namespace wren

View file

@ -164,6 +164,11 @@ namespace wren {
return interpret(module_name.c_str(), script.c_str()); return interpret(module_name.c_str(), script.c_str());
} }
void VM::release (Handle& handle) noexcept {
if (handle)
wrenReleaseHandle(m_local->wvm, handle);
}
template <typename T> template <typename T>
T VM::slot (int slot_num) { T VM::slot (int slot_num) {
return slot_specialized<T>(m_local->wvm, slot_num); return slot_specialized<T>(m_local->wvm, slot_num);

View file

@ -2,6 +2,7 @@
#include "has_method.hpp" #include "has_method.hpp"
#include "error_type.hpp" #include "error_type.hpp"
#include "handle.hpp"
#include <string> #include <string>
#include <memory> #include <memory>
#include <tuple> #include <tuple>
@ -14,6 +15,7 @@ namespace wren {
class Configuration; class Configuration;
class VM; class VM;
class DynafuncMaker; class DynafuncMaker;
class Handle;
typedef void(*foreign_method_t)(VM*); typedef void(*foreign_method_t)(VM*);
typedef void(*finalizer_t)(void*); typedef void(*finalizer_t)(void*);
@ -54,6 +56,7 @@ namespace wren {
void interpret (const char* module_name, const char* script); void interpret (const char* module_name, const char* script);
void interpret (const std::string& module_name, const std::string& script); void interpret (const std::string& module_name, const std::string& script);
void release (Handle& handle) noexcept;
template <typename T> T slot (int slot_num); template <typename T> T slot (int slot_num);
#if __cpp_concepts >= 201907 #if __cpp_concepts >= 201907