2011-08-28 15:17:58 +02:00
|
|
|
/*
|
2013-01-08 19:21:54 +01:00
|
|
|
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
|
2011-08-28 15:17:58 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-07-27 01:13:27 +02:00
|
|
|
#ifndef LUABINDER_H
|
|
|
|
#define LUABINDER_H
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
// this file is and must be included only from luainterface.h
|
|
|
|
#include "luainterface.h"
|
|
|
|
#include "luaexception.h"
|
|
|
|
|
2012-08-01 09:49:09 +02:00
|
|
|
#include <framework/stdext/traits.h>
|
|
|
|
#include <tuple>
|
|
|
|
|
2011-08-28 00:32:16 +02:00
|
|
|
/// This namespace contains some dirty metaprogamming that uses a lot of C++0x features
|
2011-08-14 04:09:11 +02:00
|
|
|
/// The purpose here is to create templates that can bind any function from C++
|
|
|
|
/// and expose in lua environment. This is done combining variadic templates,
|
|
|
|
/// lambdas, tuples and some type traits features from the new C++0x standard to create
|
2011-08-28 00:32:16 +02:00
|
|
|
/// templates that can detect functions's arguments and then generate lambdas. These lambdas
|
2011-08-14 04:09:11 +02:00
|
|
|
/// pops arguments from lua stack, call the bound C++ function and then
|
|
|
|
/// pushes the result to lua.
|
2011-07-27 01:13:27 +02:00
|
|
|
namespace luabinder
|
|
|
|
{
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Pack arguments from lua stack into a tuple recursively
|
2011-07-27 01:13:27 +02:00
|
|
|
template<int N>
|
|
|
|
struct pack_values_into_tuple {
|
|
|
|
template<typename Tuple>
|
2011-08-14 04:09:11 +02:00
|
|
|
static void call(Tuple& tuple, LuaInterface* lua) {
|
2011-07-27 01:13:27 +02:00
|
|
|
typedef typename std::tuple_element<N-1, Tuple>::type ValueType;
|
2011-08-14 04:09:11 +02:00
|
|
|
std::get<N-1>(tuple) = lua->polymorphicPop<ValueType>();
|
2011-07-27 01:13:27 +02:00
|
|
|
pack_values_into_tuple<N-1>::call(tuple, lua);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<>
|
|
|
|
struct pack_values_into_tuple<0> {
|
|
|
|
template<typename Tuple>
|
2012-06-17 17:21:46 +02:00
|
|
|
static void call(Tuple& tuple, LuaInterface* lua) { }
|
2011-07-27 01:13:27 +02:00
|
|
|
};
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
/// C++ function caller that can push results to lua
|
2011-07-27 01:13:27 +02:00
|
|
|
template<typename Ret, typename F, typename... Args>
|
|
|
|
typename std::enable_if<!std::is_void<Ret>::value, int>::type
|
2011-08-19 20:53:23 +02:00
|
|
|
call_fun_and_push_result(const F& f, LuaInterface* lua, const Args&... args) {
|
2011-07-27 01:13:27 +02:00
|
|
|
Ret ret = f(args...);
|
2012-07-05 14:38:48 +02:00
|
|
|
int numRets = lua->polymorphicPush(ret);
|
|
|
|
return numRets;
|
2011-07-27 01:13:27 +02:00
|
|
|
}
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
/// C++ void function caller
|
2011-07-27 01:13:27 +02:00
|
|
|
template<typename Ret, typename F, typename... Args>
|
|
|
|
typename std::enable_if<std::is_void<Ret>::value, int>::type
|
2011-08-19 20:53:23 +02:00
|
|
|
call_fun_and_push_result(const F& f, LuaInterface* lua, const Args&... args) {
|
2011-07-27 01:13:27 +02:00
|
|
|
f(args...);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Expand arguments from tuple for later calling the C++ function
|
2011-07-27 01:13:27 +02:00
|
|
|
template<int N, typename Ret>
|
|
|
|
struct expand_fun_arguments {
|
|
|
|
template<typename Tuple, typename F, typename... Args>
|
2011-08-19 20:53:23 +02:00
|
|
|
static int call(const Tuple& tuple, const F& f, LuaInterface* lua, const Args&... args) {
|
|
|
|
return expand_fun_arguments<N-1,Ret>::call(tuple, f, lua, std::get<N-1>(tuple), args...);
|
2011-07-27 01:13:27 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
template<typename Ret>
|
|
|
|
struct expand_fun_arguments<0,Ret> {
|
|
|
|
template<typename Tuple, typename F, typename... Args>
|
2011-08-19 20:53:23 +02:00
|
|
|
static int call(const Tuple& tuple, const F& f, LuaInterface* lua, const Args&... args) {
|
2011-07-27 01:13:27 +02:00
|
|
|
return call_fun_and_push_result<Ret>(f, lua, args...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Bind different types of functions generating a lambda
|
2011-07-27 01:13:27 +02:00
|
|
|
template<typename Ret, typename F, typename Tuple>
|
|
|
|
LuaCppFunction bind_fun_specializer(const F& f) {
|
|
|
|
enum { N = std::tuple_size<Tuple>::value };
|
2012-06-17 01:19:43 +02:00
|
|
|
return [=](LuaInterface* lua) -> int {
|
2012-06-26 00:13:30 +02:00
|
|
|
while(lua->stackSize() != N) {
|
|
|
|
if(lua->stackSize() < N)
|
|
|
|
g_lua.pushNil();
|
|
|
|
else
|
|
|
|
g_lua.pop();
|
|
|
|
}
|
2011-07-27 01:13:27 +02:00
|
|
|
Tuple tuple;
|
|
|
|
pack_values_into_tuple<N>::call(tuple, lua);
|
|
|
|
return expand_fun_arguments<N,Ret>::call(tuple, f, lua);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-04-04 15:16:33 +02:00
|
|
|
/// Bind a customized function
|
|
|
|
inline
|
|
|
|
LuaCppFunction bind_fun(const std::function<int(LuaInterface*)>& f) {
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Bind a std::function
|
2011-07-27 01:13:27 +02:00
|
|
|
template<typename Ret, typename... Args>
|
|
|
|
LuaCppFunction bind_fun(const std::function<Ret(Args...)>& f) {
|
2012-08-01 09:49:09 +02:00
|
|
|
typedef typename std::tuple<typename stdext::remove_const_ref<Args>::type...> Tuple;
|
|
|
|
return bind_fun_specializer<typename stdext::remove_const_ref<Ret>::type,
|
2011-07-27 01:13:27 +02:00
|
|
|
decltype(f),
|
|
|
|
Tuple>(f);
|
|
|
|
}
|
|
|
|
|
2012-01-06 04:29:26 +01:00
|
|
|
/// Specialization for lambdas
|
|
|
|
template<typename F>
|
|
|
|
struct bind_lambda_fun;
|
|
|
|
|
|
|
|
template<typename Lambda, typename Ret, typename... Args>
|
|
|
|
struct bind_lambda_fun<Ret(Lambda::*)(Args...) const> {
|
|
|
|
static LuaCppFunction call(const Lambda& f) {
|
2012-08-01 09:49:09 +02:00
|
|
|
typedef typename std::tuple<typename stdext::remove_const_ref<Args>::type...> Tuple;
|
|
|
|
return bind_fun_specializer<typename stdext::remove_const_ref<Ret>::type,
|
2012-01-06 04:29:26 +01:00
|
|
|
decltype(f),
|
|
|
|
Tuple>(f);
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Lambda>
|
2012-06-17 17:21:46 +02:00
|
|
|
typename std::enable_if<std::is_constructible<decltype(&Lambda::operator())>::value, LuaCppFunction>::type bind_fun(const Lambda& f) {
|
2012-01-06 04:29:26 +01:00
|
|
|
typedef decltype(&Lambda::operator()) F;
|
|
|
|
return bind_lambda_fun<F>::call(f);
|
|
|
|
}
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Convert to C++ functions pointers to std::function then bind
|
2011-07-27 01:13:27 +02:00
|
|
|
template<typename Ret, typename... Args>
|
|
|
|
LuaCppFunction bind_fun(Ret (*f)(Args...)) {
|
|
|
|
return bind_fun(std::function<Ret(Args...)>(f));
|
|
|
|
}
|
|
|
|
|
2012-06-17 17:21:46 +02:00
|
|
|
/// Create member function lambdas
|
|
|
|
template<typename Ret, typename C, typename... Args>
|
2012-08-01 09:49:09 +02:00
|
|
|
std::function<Ret(const stdext::shared_object_ptr<C>&, const Args&...)> make_mem_func(Ret (C::* f)(Args...)) {
|
2012-06-17 17:21:46 +02:00
|
|
|
auto mf = std::mem_fn(f);
|
2012-08-01 09:49:09 +02:00
|
|
|
return [=](const stdext::shared_object_ptr<C>& obj, const Args&... args) mutable -> Ret {
|
2012-06-17 17:21:46 +02:00
|
|
|
if(!obj)
|
|
|
|
throw LuaException("failed to call a member function because the passed object is nil");
|
|
|
|
return mf(obj.get(), args...);
|
|
|
|
};
|
2011-11-03 20:07:07 +01:00
|
|
|
}
|
2012-06-17 17:21:46 +02:00
|
|
|
template<typename C, typename... Args>
|
2012-08-01 09:49:09 +02:00
|
|
|
std::function<void(const stdext::shared_object_ptr<C>&, const Args&...)> make_mem_func(void (C::* f)(Args...)) {
|
2012-06-17 17:21:46 +02:00
|
|
|
auto mf = std::mem_fn(f);
|
2012-08-01 09:49:09 +02:00
|
|
|
return [=](const stdext::shared_object_ptr<C>& obj, const Args&... args) mutable -> void {
|
2012-06-17 17:21:46 +02:00
|
|
|
if(!obj)
|
|
|
|
throw LuaException("failed to call a member function because the passed object is nil");
|
|
|
|
mf(obj.get(), args...);
|
|
|
|
};
|
2011-07-27 01:13:27 +02:00
|
|
|
}
|
|
|
|
|
2012-06-17 17:21:46 +02:00
|
|
|
/// Create member function lambdas for singleton classes
|
|
|
|
template<typename Ret, typename C, typename... Args>
|
|
|
|
std::function<Ret(const Args&...)> make_mem_func_singleton(Ret (C::* f)(Args...), C* instance) {
|
|
|
|
auto mf = std::mem_fn(f);
|
|
|
|
return [=](Args... args) mutable -> Ret { return mf(instance, args...); };
|
2011-07-27 01:13:27 +02:00
|
|
|
}
|
2012-06-17 17:21:46 +02:00
|
|
|
template<typename C, typename... Args>
|
|
|
|
std::function<void(const Args&...)> make_mem_func_singleton(void (C::* f)(Args...), C* instance) {
|
|
|
|
auto mf = std::mem_fn(f);
|
|
|
|
return [=](Args... args) mutable -> void { mf(instance, args...); };
|
2011-07-27 01:13:27 +02:00
|
|
|
}
|
|
|
|
|
2012-06-17 17:21:46 +02:00
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Bind member functions
|
2012-01-06 04:29:26 +01:00
|
|
|
template<typename C, typename Ret, class FC, typename... Args>
|
2012-06-17 17:21:46 +02:00
|
|
|
LuaCppFunction bind_mem_fun(Ret (FC::* f)(Args...)) {
|
2012-08-01 09:49:09 +02:00
|
|
|
typedef typename std::tuple<stdext::shared_object_ptr<FC>, typename stdext::remove_const_ref<Args>::type...> Tuple;
|
2012-06-17 17:21:46 +02:00
|
|
|
auto lambda = make_mem_func<Ret,FC>(f);
|
2012-08-01 09:49:09 +02:00
|
|
|
return bind_fun_specializer<typename stdext::remove_const_ref<Ret>::type,
|
2012-06-17 17:21:46 +02:00
|
|
|
decltype(lambda),
|
|
|
|
Tuple>(lambda);
|
2011-07-27 01:13:27 +02:00
|
|
|
}
|
2012-06-17 17:21:46 +02:00
|
|
|
|
|
|
|
/// Bind singleton member functions
|
2012-01-06 04:29:26 +01:00
|
|
|
template<typename C, typename Ret, class FC, typename... Args>
|
2012-06-17 17:21:46 +02:00
|
|
|
LuaCppFunction bind_singleton_mem_fun(Ret (FC::*f)(Args...), C *instance) {
|
2012-08-01 09:49:09 +02:00
|
|
|
typedef typename std::tuple<typename stdext::remove_const_ref<Args>::type...> Tuple;
|
2012-06-17 17:21:46 +02:00
|
|
|
assert(instance);
|
|
|
|
auto lambda = make_mem_func_singleton<Ret,FC>(f, static_cast<FC*>(instance));
|
2012-08-01 09:49:09 +02:00
|
|
|
return bind_fun_specializer<typename stdext::remove_const_ref<Ret>::type,
|
2012-06-17 17:21:46 +02:00
|
|
|
decltype(lambda),
|
|
|
|
Tuple>(lambda);
|
2011-07-27 01:13:27 +02:00
|
|
|
}
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Bind customized member functions
|
2012-01-06 04:29:26 +01:00
|
|
|
template<typename C>
|
|
|
|
LuaCppFunction bind_mem_fun(int (C::*f)(LuaInterface*)) {
|
2011-07-27 01:13:27 +02:00
|
|
|
auto mf = std::mem_fn(f);
|
2013-02-05 21:30:16 +01:00
|
|
|
return [=](LuaInterface* lua) mutable -> int {
|
2012-08-01 09:49:09 +02:00
|
|
|
auto obj = lua->castValue<stdext::shared_object_ptr<C>>(1);
|
2011-08-14 04:09:11 +02:00
|
|
|
lua->remove(1);
|
2011-07-27 01:13:27 +02:00
|
|
|
return mf(obj, lua);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
#endif
|