2013-12-03 03:22:51 +08:00
|
|
|
// The MIT License (MIT)
|
|
|
|
|
|
|
|
// Copyright (c) 2013 Danny Y., Rapptz
|
|
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
// this software and associated documentation files (the "Software"), to deal in
|
|
|
|
// the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
// subject to the following conditions:
|
|
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
#ifndef SOL_LUA_FUNC_HPP
|
|
|
|
#define SOL_LUA_FUNC_HPP
|
|
|
|
|
|
|
|
#include "stack.hpp"
|
2013-12-10 03:12:38 +08:00
|
|
|
#include <memory>
|
2013-12-03 03:22:51 +08:00
|
|
|
|
|
|
|
namespace sol {
|
2013-12-09 12:05:03 +08:00
|
|
|
|
|
|
|
template<typename TFx>
|
|
|
|
struct static_lua_func {
|
2013-12-09 12:09:07 +08:00
|
|
|
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t;
|
2013-12-14 03:59:46 +08:00
|
|
|
typedef function_traits<fx_t> fx_traits;
|
2013-12-16 05:27:20 +08:00
|
|
|
|
2013-12-09 12:09:07 +08:00
|
|
|
template<typename... Args>
|
|
|
|
static int typed_call(types<void>, types<Args...> t, fx_t* fx, lua_State* L) {
|
|
|
|
stack::pop_call(L, fx, t);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-16 11:09:06 +08:00
|
|
|
template<typename... Args>
|
|
|
|
static int typed_call(types<>, types<Args...> t, fx_t* fx, lua_State* L) {
|
|
|
|
return typed_call(types<void>(), t, fx, L);
|
|
|
|
}
|
|
|
|
|
2013-12-16 05:27:20 +08:00
|
|
|
template<typename... Ret, typename... Args>
|
|
|
|
static int typed_call(types<Ret...>, types<Args...> t, fx_t* fx, lua_State* L) {
|
2013-12-16 11:09:06 +08:00
|
|
|
typedef typename multi_return<Ret...>::type return_type;
|
|
|
|
return_type r = stack::pop_call(L, fx, t);
|
|
|
|
stack::push(L, std::move(r));
|
2013-12-16 05:27:20 +08:00
|
|
|
return sizeof...(Ret);
|
2013-12-09 12:09:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int call(lua_State* L) {
|
2014-04-26 07:41:03 +08:00
|
|
|
auto udata = stack::get_user<fx_t*>(L);
|
|
|
|
fx_t* fx = udata.first;
|
|
|
|
int r = typed_call(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), fx, L);
|
2013-12-09 12:09:07 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operator()(lua_State* L) {
|
|
|
|
return call(L);
|
2013-12-09 12:05:03 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename TFx>
|
|
|
|
struct static_object_lua_func {
|
2014-04-25 12:05:42 +08:00
|
|
|
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t;
|
2013-12-14 03:59:46 +08:00
|
|
|
typedef function_traits<fx_t> fx_traits;
|
2013-12-09 12:09:07 +08:00
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
static int typed_call(types<void>, types<Args...>, T& item, fx_t& ifx, lua_State* L) {
|
2014-04-25 12:05:42 +08:00
|
|
|
auto fx = [&item, &ifx](Args&&... args) -> void { (item.*ifx)(std::forward<Args>(args)...); };
|
2013-12-09 12:09:07 +08:00
|
|
|
stack::pop_call(L, fx, types<Args...>());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-16 11:09:06 +08:00
|
|
|
template<typename... Args>
|
|
|
|
static int typed_call(types<>, types<Args...> t, T& item, fx_t& ifx, lua_State* L) {
|
|
|
|
return typed_call(types<void>(), t, item, ifx, L);
|
2013-12-09 12:09:07 +08:00
|
|
|
}
|
|
|
|
|
2013-12-16 05:27:20 +08:00
|
|
|
template<typename... Ret, typename... Args>
|
|
|
|
static int typed_call(types<Ret...>, types<Args...>, T& item, fx_t& ifx, lua_State* L) {
|
2013-12-16 11:09:06 +08:00
|
|
|
typedef typename multi_return<Ret...>::type return_type;
|
|
|
|
auto fx = [&item, &ifx](Args&&... args) -> return_type { return (item.*ifx)(std::forward<Args>(args)...); };
|
|
|
|
return_type r = stack::pop_call(L, fx, types<Args...>());
|
|
|
|
stack::push(L, std::move(r));
|
2013-12-16 05:27:20 +08:00
|
|
|
return sizeof...(Ret);
|
2013-12-09 12:09:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int call(lua_State* L) {
|
2014-04-26 07:41:03 +08:00
|
|
|
auto memberdata = stack::get_user<fx_t>(L, 1);
|
|
|
|
auto objdata = stack::get_user<T*>(L, memberdata.second);
|
|
|
|
fx_t& memfx = memberdata.first;
|
|
|
|
T& obj = *objdata.first;
|
|
|
|
int r = typed_call(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), obj, memfx, L);
|
2013-12-09 12:09:07 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operator()(lua_State* L) {
|
|
|
|
return call(L);
|
|
|
|
}
|
2013-12-09 12:05:03 +08:00
|
|
|
};
|
2013-12-03 03:22:51 +08:00
|
|
|
|
2013-12-03 04:42:03 +08:00
|
|
|
struct lua_func {
|
2013-12-09 12:09:07 +08:00
|
|
|
static int call(lua_State* L) {
|
2014-04-26 07:41:03 +08:00
|
|
|
void** pinheritancedata = static_cast<void**>(stack::get<lightuserdata_t>(L, 1).value);
|
2013-12-10 03:12:38 +08:00
|
|
|
void* inheritancedata = *pinheritancedata;
|
2013-12-09 12:09:07 +08:00
|
|
|
if (inheritancedata == nullptr)
|
2013-12-12 18:07:58 +08:00
|
|
|
throw sol_error("call from Lua to C++ function has null data");
|
2013-12-10 03:12:38 +08:00
|
|
|
lua_func* pfx = static_cast<lua_func*>(inheritancedata);
|
|
|
|
lua_func& fx = *pfx;
|
2013-12-09 12:09:07 +08:00
|
|
|
int r = fx(L);
|
|
|
|
return r;
|
|
|
|
}
|
2013-12-10 03:12:38 +08:00
|
|
|
|
|
|
|
static int gc(lua_State* L) {
|
2014-04-26 07:41:03 +08:00
|
|
|
void** puserdata = static_cast<void**>(stack::get<userdata_t>(L, 1).value);
|
2013-12-10 03:12:38 +08:00
|
|
|
void* userdata = *puserdata;
|
|
|
|
lua_func* ptr = static_cast<lua_func*>(userdata);
|
2013-12-10 06:01:52 +08:00
|
|
|
std::default_delete<lua_func> dx{};
|
2013-12-10 03:12:38 +08:00
|
|
|
dx(ptr);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-12-09 12:09:07 +08:00
|
|
|
|
|
|
|
virtual int operator()(lua_State*) {
|
2013-12-12 18:07:58 +08:00
|
|
|
throw sol_error("failure to call specialized wrapped C++ function from Lua");
|
2013-12-03 04:42:03 +08:00
|
|
|
}
|
|
|
|
|
2013-12-12 18:07:58 +08:00
|
|
|
virtual ~lua_func() {}
|
2013-12-03 04:42:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename TFx>
|
2014-04-25 03:04:02 +08:00
|
|
|
struct functor_lua_func : public lua_func {
|
2013-12-03 04:42:03 +08:00
|
|
|
typedef decltype(&TFx::operator()) fx_t;
|
2013-12-14 03:59:46 +08:00
|
|
|
typedef function_traits<fx_t> fx_traits;
|
2014-04-26 07:41:03 +08:00
|
|
|
TFx fx;
|
2013-12-03 04:42:03 +08:00
|
|
|
|
2013-12-03 07:11:25 +08:00
|
|
|
template<typename... FxArgs>
|
2014-04-25 03:04:02 +08:00
|
|
|
functor_lua_func(FxArgs&&... fxargs): fx(std::forward<FxArgs>(fxargs)...) {}
|
2013-12-03 04:42:03 +08:00
|
|
|
|
2013-12-16 05:27:20 +08:00
|
|
|
template<typename... Args>
|
2013-12-16 11:09:06 +08:00
|
|
|
int operator()(types<void>, types<Args...> t, lua_State* L) {
|
2013-12-16 05:27:20 +08:00
|
|
|
stack::pop_call(L, fx, t);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-03 07:11:25 +08:00
|
|
|
template<typename... Args>
|
2013-12-16 11:09:06 +08:00
|
|
|
int operator()(types<>, types<Args...> t, lua_State* L) {
|
|
|
|
return (*this)(types<void>(), t, L);
|
2013-12-03 04:42:03 +08:00
|
|
|
}
|
|
|
|
|
2013-12-16 05:27:20 +08:00
|
|
|
template<typename... Ret, typename... Args>
|
|
|
|
int operator()(types<Ret...>, types<Args...> t, lua_State* L) {
|
2013-12-16 11:09:06 +08:00
|
|
|
typedef typename multi_return<Ret...>::type return_type;
|
|
|
|
return_type r = stack::pop_call(L, fx, t);
|
|
|
|
stack::push(L, r);
|
2013-12-16 05:27:20 +08:00
|
|
|
return sizeof...(Ret);
|
2013-12-03 04:42:03 +08:00
|
|
|
}
|
2014-04-26 07:41:03 +08:00
|
|
|
|
|
|
|
virtual int operator()(lua_State* L) override {
|
|
|
|
return (*this)(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), L);
|
|
|
|
}
|
2013-12-03 04:42:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename TFx, typename T = TFx, bool is_member_pointer = std::is_member_function_pointer<TFx>::value>
|
2014-04-25 03:04:02 +08:00
|
|
|
struct function_lua_func : public lua_func {
|
2013-12-03 04:42:03 +08:00
|
|
|
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t;
|
2013-12-14 03:59:46 +08:00
|
|
|
typedef function_traits<fx_t> fx_traits;
|
2014-04-25 11:38:32 +08:00
|
|
|
fx_t fx;
|
2013-12-03 04:42:03 +08:00
|
|
|
|
2013-12-03 07:11:25 +08:00
|
|
|
template<typename... FxArgs>
|
2014-04-25 03:04:02 +08:00
|
|
|
function_lua_func(FxArgs&&... fxargs): fx(std::forward<FxArgs>(fxargs)...) {}
|
2013-12-03 04:42:03 +08:00
|
|
|
|
2013-12-03 07:11:25 +08:00
|
|
|
template<typename... Args>
|
2013-12-03 12:33:23 +08:00
|
|
|
int operator()(types<void>, types<Args...> t, lua_State* L) {
|
2013-12-03 04:42:03 +08:00
|
|
|
stack::pop_call(L, fx, t);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-16 11:09:06 +08:00
|
|
|
template<typename... Args>
|
|
|
|
int operator()(types<>, types<Args...> t, lua_State* L) {
|
|
|
|
return (*this)(types<void>(), t, L);
|
|
|
|
}
|
|
|
|
|
2013-12-16 05:27:20 +08:00
|
|
|
template<typename... Ret, typename... Args>
|
|
|
|
int operator()(types<Ret...>, types<Args...> t, lua_State* L) {
|
2013-12-16 11:09:06 +08:00
|
|
|
typedef typename multi_return<Ret...>::type return_type;
|
|
|
|
return_type r = stack::pop_call(L, fx, t);
|
|
|
|
stack::push(L, std::move(r));
|
2013-12-16 05:27:20 +08:00
|
|
|
return sizeof...(Ret);
|
2013-12-03 04:42:03 +08:00
|
|
|
}
|
2013-12-16 06:09:23 +08:00
|
|
|
|
|
|
|
virtual int operator()(lua_State* L) override {
|
|
|
|
return (*this)(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), L);
|
|
|
|
}
|
2013-12-03 04:42:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename TFx, typename T>
|
2014-04-25 03:04:02 +08:00
|
|
|
struct function_lua_func<TFx, T, true> : public lua_func {
|
2013-12-03 04:42:03 +08:00
|
|
|
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t;
|
2013-12-14 03:59:46 +08:00
|
|
|
typedef function_traits<fx_t> fx_traits;
|
2014-04-25 11:38:32 +08:00
|
|
|
struct functor {
|
2013-12-09 13:01:28 +08:00
|
|
|
T member;
|
2014-04-25 11:38:32 +08:00
|
|
|
fx_t invocation;
|
2013-12-03 07:11:25 +08:00
|
|
|
|
2013-12-03 08:15:23 +08:00
|
|
|
template<typename... FxArgs>
|
2014-04-25 11:38:32 +08:00
|
|
|
functor(T m, FxArgs&&... fxargs): member(std::move(m)), invocation(std::forward<FxArgs>(fxargs)...) {}
|
2013-12-03 07:11:25 +08:00
|
|
|
|
2013-12-03 08:15:23 +08:00
|
|
|
template<typename... Args>
|
2013-12-03 12:33:23 +08:00
|
|
|
typename fx_traits::return_type operator()(Args&&... args) {
|
2013-12-09 13:01:28 +08:00
|
|
|
return (member.*invocation)(std::forward<Args>(args)...);
|
2013-12-03 08:15:23 +08:00
|
|
|
}
|
2013-12-03 07:11:25 +08:00
|
|
|
} fx;
|
|
|
|
|
|
|
|
template<typename... FxArgs>
|
2014-04-25 03:04:02 +08:00
|
|
|
function_lua_func(T m, FxArgs&&... fxargs): fx(std::move(m), std::forward<FxArgs>(fxargs)...) {}
|
2013-12-03 04:42:03 +08:00
|
|
|
|
2013-12-16 06:09:23 +08:00
|
|
|
template<typename... Args>
|
|
|
|
int operator()(types<void>, types<Args...> t, lua_State* L) {
|
|
|
|
stack::pop_call(L, fx, t);
|
|
|
|
return 0;
|
2013-12-03 04:42:03 +08:00
|
|
|
}
|
|
|
|
|
2013-12-16 05:27:20 +08:00
|
|
|
template<typename... Args>
|
|
|
|
int operator()(types<>, types<Args...> t, lua_State* L) {
|
|
|
|
return (*this)(types<void>(), t, L);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Ret, typename... Args>
|
|
|
|
int operator()(types<Ret...>, types<Args...> t, lua_State* L) {
|
2013-12-16 11:09:06 +08:00
|
|
|
typedef typename multi_return<Ret...>::type return_type;
|
|
|
|
return_type r = stack::pop_call(L, fx, t);
|
|
|
|
stack::push(L, std::move(r));
|
2013-12-16 05:27:20 +08:00
|
|
|
return sizeof...(Ret);
|
2013-12-03 04:42:03 +08:00
|
|
|
}
|
2013-12-16 06:09:23 +08:00
|
|
|
|
|
|
|
virtual int operator()(lua_State* L) override {
|
|
|
|
return (*this)(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), L);
|
|
|
|
}
|
2013-12-03 04:42:03 +08:00
|
|
|
};
|
|
|
|
|
2014-04-25 11:38:32 +08:00
|
|
|
template<typename TFx, typename T>
|
|
|
|
struct class_lua_func : public lua_func {
|
|
|
|
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t;
|
|
|
|
typedef function_traits<fx_t> fx_traits;
|
|
|
|
struct functor {
|
2014-04-26 07:41:03 +08:00
|
|
|
T* item;
|
2014-04-25 11:38:32 +08:00
|
|
|
fx_t invocation;
|
|
|
|
|
|
|
|
template<typename... FxArgs>
|
2014-04-26 07:41:03 +08:00
|
|
|
functor(FxArgs&&... fxargs): item(nullptr), invocation(std::forward<FxArgs>(fxargs)...) {}
|
2014-04-25 11:38:32 +08:00
|
|
|
|
2014-04-26 07:41:03 +08:00
|
|
|
void pre_call(lua_State* L) {
|
|
|
|
void* userdata = lua_touserdata(L, 0);
|
|
|
|
item = static_cast<T*>(userdata);
|
|
|
|
}
|
2014-04-25 11:38:32 +08:00
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
typename fx_traits::return_type operator()(Args&&... args) {
|
2014-04-26 07:41:03 +08:00
|
|
|
T& member = *item;
|
|
|
|
return (member.*invocation)(std::forward<Args>(args)...);
|
2014-04-25 11:38:32 +08:00
|
|
|
}
|
|
|
|
} fx;
|
|
|
|
|
|
|
|
template<typename... FxArgs>
|
|
|
|
class_lua_func(FxArgs&&... fxargs): fx(std::forward<FxArgs>(fxargs)...) {}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
int operator()(types<void>, types<Args...> t, lua_State* L) {
|
2014-04-26 07:41:03 +08:00
|
|
|
stack::pop_call(L, fx, t);
|
2014-04-25 11:38:32 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
int operator()(types<>, types<Args...> t, lua_State* L) {
|
|
|
|
return (*this)(types<void>(), t, L);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Ret, typename... Args>
|
|
|
|
int operator()(types<Ret...>, types<Args...> t, lua_State* L) {
|
|
|
|
typedef typename multi_return<Ret...>::type return_type;
|
2014-04-26 07:41:03 +08:00
|
|
|
return_type r = stack::pop_call(L, fx, t);
|
2014-04-25 11:38:32 +08:00
|
|
|
stack::push(L, std::move(r));
|
|
|
|
return sizeof...(Ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int operator()(lua_State* L) override {
|
2014-04-26 07:41:03 +08:00
|
|
|
fx.pre_call(L);
|
2014-04-25 11:38:32 +08:00
|
|
|
return (*this)(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), L);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-12-03 03:22:51 +08:00
|
|
|
} // sol
|
|
|
|
|
|
|
|
#endif // SOL_LUA_FUNC_HPP
|