Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify sol::wrap #1269

Open
Smertig opened this issue Nov 3, 2021 · 0 comments
Open

Simplify sol::wrap #1269

Smertig opened this issue Nov 3, 2021 · 0 comments

Comments

@Smertig
Copy link
Contributor

Smertig commented Nov 3, 2021

Now sol::wrap is implemented as follows:

template <typename F, F f>
struct wrap {
typedef F type;
static int call(lua_State* L) noexcept(noexcept(c_call<type, f>(L))) {
return c_call<type, f>(L);
}
};

This is not so user-friendly API, because one should use sol::wrap<decltype(<expr>), <expr>> (or write macro to avoid copy-pasting).

I suggest replacing it with:

template <auto F>
struct wrap {
    using type = decltype(F);

    ...
};
  • sol2 requires C++17, so there should be no concern about using auto in non-type template parameters
  • this is a breaking change, however upcoming v4.0.0 release is the right time to do it
  • if you don't want to break anything, you can simply add template <auto F> struct foobar with different name, but sol::wrap is a really good name for such a wrapper

P.S. you can also refactor sol::function_detail with auto F feature without breaking sol2 API:

namespace function_detail {
template <typename F, F fx>
inline int call_wrapper_variable(std::false_type, lua_State* L) {
typedef meta::bind_traits<meta::unqualified_t<F>> traits_type;
typedef typename traits_type::args_list args_list;
typedef meta::tuple_types<typename traits_type::return_type> return_type;
return stack::call_into_lua(return_type(), args_list(), L, 1, fx);
}
template <typename R, typename V, V, typename T>
inline int call_set_assignable(std::false_type, T&&, lua_State* L) {
return luaL_error(L, "cannot write to this type: copy assignment/constructor not available");
}
template <typename R, typename V, V variable, typename T>
inline int call_set_assignable(std::true_type, lua_State* L, T&& mem) {
(mem.*variable) = stack::get<R>(L, 2);
return 0;
}
template <typename R, typename V, V, typename T>
inline int call_set_variable(std::false_type, lua_State* L, T&&) {
return luaL_error(L, "cannot write to a const variable");
}
template <typename R, typename V, V variable, typename T>
inline int call_set_variable(std::true_type, lua_State* L, T&& mem) {
return call_set_assignable<R, V, variable>(std::is_assignable<std::add_lvalue_reference_t<R>, R>(), L, std::forward<T>(mem));
}
template <typename V, V variable>
inline int call_wrapper_variable(std::true_type, lua_State* L) {
typedef meta::bind_traits<meta::unqualified_t<V>> traits_type;
typedef typename traits_type::object_type T;
typedef typename traits_type::return_type R;
auto& mem = stack::get<T>(L, 1);
switch (lua_gettop(L)) {
case 1: {
decltype(auto) r = (mem.*variable);
stack::push_reference(L, std::forward<decltype(r)>(r));
return 1;
}
case 2:
return call_set_variable<R, V, variable>(meta::neg<std::is_const<R>>(), L, mem);
default:
return luaL_error(L, "incorrect number of arguments to member variable function call");
}
}
template <typename F, F fx>
inline int call_wrapper_function(std::false_type, lua_State* L) {
return call_wrapper_variable<F, fx>(std::is_member_object_pointer<F>(), L);
}
template <typename F, F fx>
inline int call_wrapper_function(std::true_type, lua_State* L) {
return call_detail::call_wrapped<void, false, false>(L, fx);
}
template <typename F, F fx>
int call_wrapper_entry(lua_State* L) noexcept(meta::bind_traits<F>::is_noexcept) {
return call_wrapper_function<F, fx>(std::is_member_function_pointer<meta::unqualified_t<F>>(), L);
}
template <typename... Fxs>
struct c_call_matcher {
template <typename Fx, std::size_t I, typename R, typename... Args>
int operator()(types<Fx>, meta::index_value<I>, types<R>, types<Args...>, lua_State* L, int, int) const {
typedef meta::at_in_pack_t<I, Fxs...> target;
return target::call(L);
}
};
template <typename F, F fx>
inline int c_call_raw(std::true_type, lua_State* L) {
return fx(L);
}
template <typename F, F fx>
inline int c_call_raw(std::false_type, lua_State* L) {
#ifdef __clang__
return detail::trampoline(L, function_detail::call_wrapper_entry<F, fx>);
#else
return detail::typed_static_trampoline<decltype(&function_detail::call_wrapper_entry<F, fx>), (&function_detail::call_wrapper_entry<F, fx>)>(L);
#endif // fuck you clang :c
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant