Make code a bit shorter

This commit is contained in:
King_DuckZ 2020-04-26 10:56:28 +02:00
parent 30975c1405
commit ebd455c536

View file

@ -14,14 +14,14 @@ namespace wren {
namespace detail { namespace detail {
struct Callbacks { struct Callbacks {
void (*write_fn)(Configuration&, VM*, const char*); void (*write_fn)(Configuration&, VM*, const char*) {nullptr};
void (*error_fn)(Configuration&, VM*, ErrorType, const char*, int, const char*); void (*error_fn)(Configuration&, VM*, ErrorType, const char*, int, const char*) {nullptr};
void* (*reallocate_fn)(void*, std::size_t); void* (*reallocate_fn)(void*, std::size_t) {nullptr};
const char* (*resolve_module_fn)(Configuration&, VM*, const char*, const char*); const char* (*resolve_module_fn)(Configuration&, VM*, const char*, const char*) {nullptr};
char* (*load_module_fn)(Configuration&, VM*, const char*); char* (*load_module_fn)(Configuration&, VM*, const char*) {nullptr};
Configuration* config_obj; Configuration* config_obj {nullptr};
VM* vm; VM* vm {nullptr};
}; };
} //namespace detail } //namespace detail
@ -72,41 +72,27 @@ namespace wren {
template <typename T> template <typename T>
inline detail::Callbacks VM::to_callbacks (T& conf) { inline detail::Callbacks VM::to_callbacks (T& conf) {
using detail::method_write;
using detail::method_error;
using detail::method_reallocate; using detail::method_reallocate;
using detail::method_resolve_module;
using detail::method_load_module;
detail::Callbacks ret; detail::Callbacks ret;
ret.config_obj = &conf; ret.config_obj = &conf;
ret.vm = this; ret.vm = this;
if constexpr (method_write::exists<T>::value) if constexpr (detail::method_write::exists<T>::value)
ret.write_fn = &detail::AnyFunctionWrap<T, decltype(&T::write_fn)>::template call<&T::write_fn>; ret.write_fn = &detail::AnyFunctionWrap<T, decltype(&T::write_fn)>::template call<&T::write_fn>;
else
ret.write_fn = nullptr;
if constexpr (method_error::exists<T>::value) if constexpr (detail::method_error::exists<T>::value)
ret.error_fn = &detail::AnyFunctionWrap<T, decltype(&T::error_fn)>::template call<&T::error_fn>; ret.error_fn = &detail::AnyFunctionWrap<T, decltype(&T::error_fn)>::template call<&T::error_fn>;
else
ret.error_fn = nullptr;
if constexpr (method_reallocate::exists<T>::value and method_reallocate::is_static<T>::value) if constexpr (method_reallocate::exists<T>::value and method_reallocate::is_static<T>::value)
ret.reallocate_fn = &T::reallocate_fn; ret.reallocate_fn = &T::reallocate_fn;
else
ret.reallocate_fn = nullptr;
static_assert(not method_reallocate::exists<T>::value or method_reallocate::is_static<T>::value, "Realloc function must be a static function"); static_assert(not method_reallocate::exists<T>::value or method_reallocate::is_static<T>::value, "Realloc function must be a static function");
if constexpr (method_resolve_module::exists<T>::value) if constexpr (detail::method_resolve_module::exists<T>::value)
ret.resolve_module_fn = &detail::AnyFunctionWrap<T, decltype(&T::resolve_module_fn)>::template call<&T::resolve_module_fn>; ret.resolve_module_fn = &detail::AnyFunctionWrap<T, decltype(&T::resolve_module_fn)>::template call<&T::resolve_module_fn>;
else
ret.resolve_module_fn = nullptr;
if constexpr (method_load_module::exists<T>::value) if constexpr (detail::method_load_module::exists<T>::value)
ret.load_module_fn = &detail::AnyFunctionWrap<T, decltype(&T::load_module_fn)>::template call<&T::load_module_fn>; ret.load_module_fn = &detail::AnyFunctionWrap<T, decltype(&T::load_module_fn)>::template call<&T::load_module_fn>;
else
ret.load_module_fn = nullptr;
return ret; return ret;
} }