From 30975c14053a2d1e21236a7425b34ef66cb2c54f Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sun, 26 Apr 2020 10:49:57 +0200 Subject: [PATCH] Implement a generic forwarding function that replaces the manually written ones. --- src/wren/vm.hpp | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/src/wren/vm.hpp b/src/wren/vm.hpp index a9f6542..4394175 100644 --- a/src/wren/vm.hpp +++ b/src/wren/vm.hpp @@ -53,27 +53,21 @@ namespace wren { define_method_info(resolve_module_fn, resolve_module, const char*, const char*, const char*); define_method_info(load_module_fn, load_module, char*, VM*, const char*); - namespace fwd { - template - inline void write_fn (Configuration& obj, VM* vm, const char* text) { - reinterpret_cast(obj).write_fn(vm, text); + template struct AnyFunctionWrap; + template + struct AnyFunctionWrap { + template + inline static R call(Configuration& obj, Args... args) { + return (reinterpret_cast(obj).*M)(args...); } - - template - inline void error_fn (Configuration& obj, VM* vm, ErrorType et, const char* module, int line, const char* message) { - reinterpret_cast(obj).error_fn(vm, et, module, line, message); + }; + template + struct AnyFunctionWrap { + template + inline static R call(Configuration&, Args... args) { + return (*M)(args...); } - - template - inline const char* resolve_module_fn (Configuration& obj, VM* vm, void* memory, std::size_t new_size) { - return reinterpret_cast(obj).resolve_module_fn(vm, memory, new_size); - } - - template - inline char* load_module_fn (Configuration& obj, VM* vm, const char* name) { - return reinterpret_cast(obj).load_module_fn(vm, name); - } - } //namespace fwd + }; } //namespace detail template @@ -89,12 +83,12 @@ namespace wren { ret.vm = this; if constexpr (method_write::exists::value) - ret.write_fn = &detail::fwd::write_fn; + ret.write_fn = &detail::AnyFunctionWrap::template call<&T::write_fn>; else ret.write_fn = nullptr; if constexpr (method_error::exists::value) - ret.error_fn = &detail::fwd::error_fn; + ret.error_fn = &detail::AnyFunctionWrap::template call<&T::error_fn>; else ret.error_fn = nullptr; @@ -105,12 +99,12 @@ namespace wren { static_assert(not method_reallocate::exists::value or method_reallocate::is_static::value, "Realloc function must be a static function"); if constexpr (method_resolve_module::exists::value) - ret.resolve_module_fn = &detail::fwd::resolve_module_fn; + ret.resolve_module_fn = &detail::AnyFunctionWrap::template call<&T::resolve_module_fn>; else ret.resolve_module_fn = nullptr; if constexpr (method_load_module::exists::value) - ret.load_module_fn = &detail::fwd::load_module_fn; + ret.load_module_fn = &detail::AnyFunctionWrap::template call<&T::load_module_fn>; else ret.load_module_fn = nullptr;