Add support for class constructor parameters

This commit is contained in:
King_DuckZ 2022-05-15 02:40:41 +02:00
parent d3ad818b8e
commit 996a089185

View file

@ -42,6 +42,12 @@ namespace wren {
std::same_as<std::remove_cv_t<T>, ModuleAndName>;
};
#endif
template <typename T, int... Indices, typename... Args>
inline void construct_foreign_class (
std::integer_sequence<int, Indices...>,
void* memory
);
} //namespace detail
#if __cpp_concepts >= 201907
@ -63,7 +69,7 @@ namespace wren {
template <typename R, std::size_t N, typename... Args>
R call (VM& vm, const ModuleAndName& object, const char (&method)[N], const Args&... args);
template <typename T>
template <typename T, typename... Args>
foreign_class_t make_foreign_class();
template <typename T, typename... Args>
@ -198,6 +204,15 @@ namespace wren {
template <typename T, typename R, typename... Args, R(T::*Method)(Args...)> struct MakeMethodBindable<Method> : MakeMethodBindableConstNonConst<T, R, Method, Args...> {};
template <typename T, typename R, typename... Args, R(T::*Method)(Args...)const> struct MakeMethodBindable<Method> : MakeMethodBindableConstNonConst<T, R, Method, Args...> {};
template <typename T, typename... Args, int... Indices>
inline void construct_foreign_class (
std::integer_sequence<int, Indices...>,
VM& vm,
void* memory
) {
new(memory) T{get<Args>(vm, Indices + 1)...};
}
} //namespace detail
#if __cpp_concepts >= 201907
@ -299,12 +314,12 @@ namespace wren {
return call<R, N, Args...>(vm, obj_handle, method, args...);
}
template <typename T>
template <typename T, typename... Args>
inline foreign_class_t make_foreign_class() {
foreign_class_t ret;
ret.allocate = [](VM& vm) {
void* const mem = vm.set_slot_new_foreign(0, 0, sizeof(T));
new(mem) T;
detail::construct_foreign_class<T, Args...>(std::make_integer_sequence<int, sizeof...(Args)>{}, vm, mem);
};
ret.finalize = [](void* mem) {
const auto cale = static_cast<T*>(mem);