lua binder improvments

This commit is contained in:
Eduardo Bart
2012-01-06 01:29:26 -02:00
parent b9e5a4e463
commit 0cb5facd7a
10 changed files with 132 additions and 68 deletions

View File

@@ -113,6 +113,27 @@ namespace luabinder
Tuple>(f);
}
/// 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) {
typedef typename std::tuple<typename remove_const_ref<Args>::type...> Tuple;
return bind_fun_specializer<typename remove_const_ref<Ret>::type,
decltype(f),
Tuple>(f);
}
};
template<typename Lambda>
typename std::enable_if<std::is_class<Lambda>::value, LuaCppFunction>::type bind_fun(const Lambda& f) {
typedef decltype(&Lambda::operator()) F;
return bind_lambda_fun<F>::call(f);
}
/// Bind a customized functions
template<>
inline
@@ -207,29 +228,29 @@ namespace luabinder
}
/// Bind member functions
template<typename Ret, typename Obj, typename... Args>
LuaCppFunction bind_mem_fun(Ret (Obj::*f)(Args...)) {
template<typename C, typename Ret, class FC, typename... Args>
LuaCppFunction bind_mem_fun(Ret (FC::*f)(Args...)) {
auto mf = std::mem_fn(f);
typedef typename std::tuple<std::shared_ptr<Obj>, typename remove_const_ref<Args>::type...> Tuple;
typedef typename std::tuple<std::shared_ptr<C>, typename remove_const_ref<Args>::type...> Tuple;
return bind_fun_specializer<typename remove_const_ref<Ret>::type,
decltype(mf),
Tuple>(mf);
}
template<typename Ret, typename Obj, typename... Args>
LuaCppFunction bind_mem_fun(Ret (Obj::*f)(Args...) const) {
template<typename C, typename Ret, class FC, typename... Args>
LuaCppFunction bind_mem_fun(Ret (FC::*f)(Args...) const) {
auto mf = std::mem_fn(f);
typedef typename std::tuple<std::shared_ptr<Obj>, typename remove_const_ref<Args>::type...> Tuple;
typedef typename std::tuple<std::shared_ptr<C>, typename remove_const_ref<Args>::type...> Tuple;
return bind_fun_specializer<typename remove_const_ref<Ret>::type,
decltype(mf),
Tuple>(mf);
}
/// Bind customized member functions
template<typename Obj>
LuaCppFunction bind_mem_fun(int (Obj::*f)(LuaInterface*)) {
template<typename C>
LuaCppFunction bind_mem_fun(int (C::*f)(LuaInterface*)) {
auto mf = std::mem_fn(f);
return [=](LuaInterface* lua) {
auto obj = lua->castValue<std::shared_ptr<Obj>>(1);
auto obj = lua->castValue<std::shared_ptr<C>>(1);
lua->remove(1);
return mf(obj, lua);
};

View File

@@ -90,25 +90,25 @@ public:
template<typename F>
void bindClassStaticFunction(const std::string& className, const std::string& functionName, const F& function);
template<class C, typename F>
void bindClassMemberFunction(const std::string& functionName, F C::*function);
template<class C, typename F>
void bindClassMemberFunction(const std::string& className, const std::string& functionName, F C::*function);
template<class C, typename F, class FC>
void bindClassMemberFunction(const std::string& functionName, F FC::*function);
template<class C, typename F, class FC>
void bindClassMemberFunction(const std::string& className, const std::string& functionName, F FC::*function);
template<class C, typename F1, typename F2>
void bindClassMemberField(const std::string& fieldName, F1 C::*getFunction, F2 C::*setFunction);
template<class C, typename F1, typename F2>
void bindClassMemberField(const std::string& className, const std::string& fieldName, F1 C::*getFunction, F2 C::*setFunction);
template<class C, typename F1, typename F2, class FC>
void bindClassMemberField(const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction);
template<class C, typename F1, typename F2, class FC>
void bindClassMemberField(const std::string& className, const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction);
template<class C, typename F>
void bindClassMemberGetField(const std::string& fieldName, F C::*getFunction);
template<class C, typename F>
void bindClassMemberGetField(const std::string& className, const std::string& fieldName, F C::*getFunction);
template<class C, typename F, class FC>
void bindClassMemberGetField(const std::string& fieldName, F FC::*getFunction);
template<class C, typename F, class FC>
void bindClassMemberGetField(const std::string& className, const std::string& fieldName, F FC::*getFunction);
template<class C, typename F>
void bindClassMemberSetField(const std::string& fieldName, F C::*setFunction);
template<class C, typename F>
void bindClassMemberSetField(const std::string& className, const std::string& fieldName, F C::*setFunction);
template<class C, typename F, class FC>
void bindClassMemberSetField(const std::string& fieldName, F FC::*setFunction);
template<class C, typename F, class FC>
void bindClassMemberSetField(const std::string& className, const std::string& fieldName, F FC::*setFunction);
template<typename F>
void bindGlobalFunction(const std::string& functionName, const F& function);
@@ -336,40 +336,40 @@ void LuaInterface::bindClassStaticFunction(const std::string& className, const s
registerClassStaticFunction(className, functionName, luabinder::bind_fun(function));
}
template<class C, typename F>
void LuaInterface::bindClassMemberFunction(const std::string& functionName, F C::*function) {
registerClassMemberFunction<C>(functionName, luabinder::bind_mem_fun(function));
template<class C, typename F, class FC>
void LuaInterface::bindClassMemberFunction(const std::string& functionName, F FC::*function) {
registerClassMemberFunction<C>(functionName, luabinder::bind_mem_fun<C>(function));
}
template<class C, typename F>
void LuaInterface::bindClassMemberFunction(const std::string& className, const std::string& functionName, F C::*function) {
registerClassMemberFunction(className, functionName, luabinder::bind_mem_fun(function));
template<class C, typename F, class FC>
void LuaInterface::bindClassMemberFunction(const std::string& className, const std::string& functionName, F FC::*function) {
registerClassMemberFunction(className, functionName, luabinder::bind_mem_fun<C>(function));
}
template<class C, typename F1, typename F2>
void LuaInterface::bindClassMemberField(const std::string& fieldName, F1 C::*getFunction, F2 C::*setFunction) {
registerClassMemberField<C>(fieldName, luabinder::bind_mem_fun(getFunction), luabinder::bind_mem_fun(setFunction));
template<class C, typename F1, typename F2, class FC>
void LuaInterface::bindClassMemberField(const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction) {
registerClassMemberField<C>(fieldName, luabinder::bind_mem_fun<C>(getFunction), luabinder::bind_mem_fun<C>(setFunction));
}
template<class C, typename F1, typename F2>
void LuaInterface::bindClassMemberField(const std::string& className, const std::string& fieldName, F1 C::*getFunction, F2 C::*setFunction) {
registerClassMemberField(className, fieldName, luabinder::bind_mem_fun(getFunction), luabinder::bind_mem_fun(setFunction));
template<class C, typename F1, typename F2, class FC>
void LuaInterface::bindClassMemberField(const std::string& className, const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction) {
registerClassMemberField(className, fieldName, luabinder::bind_mem_fun<C>(getFunction), luabinder::bind_mem_fun<C>(setFunction));
}
template<class C, typename F>
void LuaInterface::bindClassMemberGetField(const std::string& fieldName, F C::*getFunction) {
registerClassMemberField<C>(fieldName, luabinder::bind_mem_fun(getFunction), LuaCppFunction());
template<class C, typename F, class FC>
void LuaInterface::bindClassMemberGetField(const std::string& fieldName, F FC::*getFunction) {
registerClassMemberField<C>(fieldName, luabinder::bind_mem_fun<C>(getFunction), LuaCppFunction());
}
template<class C, typename F>
void LuaInterface::bindClassMemberGetField(const std::string& className, const std::string& fieldName, F C::*getFunction) {
registerClassMemberField(className, fieldName, luabinder::bind_mem_fun(getFunction), LuaCppFunction());
template<class C, typename F, class FC>
void LuaInterface::bindClassMemberGetField(const std::string& className, const std::string& fieldName, F FC::*getFunction) {
registerClassMemberField(className, fieldName, luabinder::bind_mem_fun<C>(getFunction), LuaCppFunction());
}
template<class C, typename F>
void LuaInterface::bindClassMemberSetField(const std::string& fieldName, F C::*setFunction) {
registerClassMemberField<C>(fieldName, LuaCppFunction(), luabinder::bind_mem_fun(setFunction));
template<class C, typename F, class FC>
void LuaInterface::bindClassMemberSetField(const std::string& fieldName, F FC::*setFunction) {
registerClassMemberField<C>(fieldName, LuaCppFunction(), luabinder::bind_mem_fun<C>(setFunction));
}
template<class C, typename F>
void LuaInterface::bindClassMemberSetField(const std::string& className, const std::string& fieldName, F C::*setFunction) {
registerClassMemberField(className, fieldName, LuaCppFunction(), luabinder::bind_mem_fun(setFunction));
template<class C, typename F, class FC>
void LuaInterface::bindClassMemberSetField(const std::string& className, const std::string& fieldName, F FC::*setFunction) {
registerClassMemberField(className, fieldName, LuaCppFunction(), luabinder::bind_mem_fun<C>(setFunction));
}
template<typename F>