diff options
author | Robin Gareus <robin@gareus.org> | 2016-03-18 19:59:37 +0100 |
---|---|---|
committer | Robin Gareus <robin@gareus.org> | 2016-03-18 19:59:37 +0100 |
commit | 2c71196a6cc878866e0deda9653d0e59ac39b960 (patch) | |
tree | 4b1c235dcd15d2c43c009dce663c2235095e339d /libs/lua | |
parent | ee2d88a5ccbb8ddf5dd6e900f04fdc576131ca9f (diff) |
allow to bind functions with reference args in global lua namespace
Diffstat (limited to 'libs/lua')
-rw-r--r-- | libs/lua/LuaBridge/detail/CFunctions.h | 42 | ||||
-rw-r--r-- | libs/lua/LuaBridge/detail/Namespace.h | 11 |
2 files changed, 53 insertions, 0 deletions
diff --git a/libs/lua/LuaBridge/detail/CFunctions.h b/libs/lua/LuaBridge/detail/CFunctions.h index fa78b78fd6..29b361b690 100644 --- a/libs/lua/LuaBridge/detail/CFunctions.h +++ b/libs/lua/LuaBridge/detail/CFunctions.h @@ -248,6 +248,48 @@ struct CFunc //---------------------------------------------------------------------------- /** + lua_CFunction to call a function with references as arguments. + */ + template <class FnPtr, + class ReturnType = typename FuncTraits <FnPtr>::ReturnType> + struct CallRef + { + typedef typename FuncTraits <FnPtr>::Params Params; + static int f (lua_State* L) + { + assert (isfulluserdata (L, lua_upvalueindex (1))); + FnPtr const& fnptr = *static_cast <FnPtr const*> (lua_touserdata (L, lua_upvalueindex (1))); + assert (fnptr != 0); + ArgList <Params, 1> args (L); + Stack <typename FuncTraits <FnPtr>::ReturnType>::push (L, FuncTraits <FnPtr>::call (fnptr, args)); + LuaRef v (newTable (L)); + FuncArgs <Params, 0>::refs (v, args); + v.push(L); + return 2; + } + }; + + template <class FnPtr> + struct CallRef <FnPtr, void> + { + typedef typename FuncTraits <FnPtr>::Params Params; + static int f (lua_State* L) + { + assert (isfulluserdata (L, lua_upvalueindex (1))); + FnPtr const& fnptr = *static_cast <FnPtr const*> (lua_touserdata (L, lua_upvalueindex (1))); + assert (fnptr != 0); + ArgList <Params, 1> args (L); + FuncTraits <FnPtr>::call (fnptr, args); + LuaRef v (newTable (L)); + FuncArgs <Params, 0>::refs (v, args); + v.push(L); + return 1; + } + }; + + + //---------------------------------------------------------------------------- + /** lua_CFunction to call a class member function with a return value. The member function pointer is in the first upvalue. diff --git a/libs/lua/LuaBridge/detail/Namespace.h b/libs/lua/LuaBridge/detail/Namespace.h index af41b0a5a6..3450156738 100644 --- a/libs/lua/LuaBridge/detail/Namespace.h +++ b/libs/lua/LuaBridge/detail/Namespace.h @@ -1382,6 +1382,17 @@ public: return *this; } + template <class FP> + Namespace& addRefFunction (char const* name, FP const fp) + { + assert (lua_istable (L, -1)); + + new (lua_newuserdata (L, sizeof (fp))) FP (fp); + lua_pushcclosure (L, &CFunc::CallRef <FP>::f, 1); + rawsetfield (L, -2, name); + + return *this; + } //---------------------------------------------------------------------------- /** |