//------------------------------------------------------------------------------ /* https://github.com/vinniefalco/LuaBridge Copyright 2016, Robin Gareus Copyright 2012, Vinnie Falco License: The MIT License (http://www.opensource.org/licenses/mit-license.php) 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. */ //============================================================================== // We use a structure so we can define everything in the header. // struct CFunc { //---------------------------------------------------------------------------- /** __index metamethod for a namespace or class static members. This handles: Retrieving functions and class static methods, stored in the metatable. Reading global and class static data, stored in the __propget table. Reading global and class properties, stored in the __propget table. */ static int indexMetaMethod (lua_State* L) { int result = 0; lua_getmetatable (L, 1); // push metatable of arg1 for (;;) { lua_pushvalue (L, 2); // push key arg2 lua_rawget (L, -2); // lookup key in metatable if (lua_isnil (L, -1)) // not found { lua_pop (L, 1); // discard nil rawgetfield (L, -1, "__propget"); // lookup __propget in metatable lua_pushvalue (L, 2); // push key arg2 lua_rawget (L, -2); // lookup key in __propget lua_remove (L, -2); // discard __propget if (lua_iscfunction (L, -1)) { lua_remove (L, -2); // discard metatable lua_pushvalue (L, 1); // push arg1 lua_call (L, 1, 1); // call cfunction result = 1; break; } else { assert (lua_isnil (L, -1)); lua_pop (L, 1); // discard nil and fall through } } else { assert (lua_istable (L, -1) || lua_iscfunction (L, -1)); lua_remove (L, -2); result = 1; break; } rawgetfield (L, -1, "__parent"); if (lua_istable (L, -1)) { // Remove metatable and repeat the search in __parent. lua_remove (L, -2); } else { // Discard metatable and return nil. assert (lua_isnil (L, -1)); lua_remove (L, -2); result = 1; break; } } return result; } //---------------------------------------------------------------------------- /** __newindex metamethod for a namespace or class static members. The __propset table stores proxy functions for assignment to: Global and class static data. Global and class properties. */ static int newindexMetaMethod (lua_State* L) { int result = 0; lua_getmetatable (L, 1); // push metatable of arg1 for (;;) { rawgetfield (L, -1, "__propset"); // lookup __propset in metatable assert (lua_istable (L, -1)); lua_pushvalue (L, 2); // push key arg2 lua_rawget (L, -2); // lookup key in __propset lua_remove (L, -2); // discard __propset if (lua_iscfunction (L, -1)) // ensure value is a cfunction { lua_remove (L, -2); // discard metatable lua_pushvalue (L, 3); // push new value arg3 lua_call (L, 1, 0); // call cfunction result = 0; break; } else { assert (lua_isnil (L, -1)); lua_pop (L, 1); } rawgetfield (L, -1, "__parent"); if (lua_istable (L, -1)) { // Remove metatable and repeat the search in __parent. lua_remove (L, -2); } else { assert (lua_isnil (L, -1)); lua_pop (L, 2); result = luaL_error (L,"no writable variable '%s'", lua_tostring (L, 2)); } } return result; } //---------------------------------------------------------------------------- /** lua_CFunction to report an error writing to a read-only value. The name of the variable is in the first upvalue. */ static int readOnlyError (lua_State* L) { std::string s; s = s + "'" + lua_tostring (L, lua_upvalueindex (1)) + "' is read-only"; return luaL_error (L, s.c_str ()); } //---------------------------------------------------------------------------- /** lua_CFunction to get a variable. This is used for global variables or class static data members. The pointer to the data is in the first upvalue. */ template static int getVariable (lua_State* L) { assert (lua_islightuserdata (L, lua_upvalueindex (1))); T const* ptr = static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (ptr != 0); Stack ::push (L, *ptr); return 1; } //---------------------------------------------------------------------------- /** lua_CFunction to set a variable. This is used for global variables or class static data members. The pointer to the data is in the first upvalue. */ template static int setVariable (lua_State* L) { assert (lua_islightuserdata (L, lua_upvalueindex (1))); T* ptr = static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (ptr != 0); *ptr = Stack ::get (L, 1); return 0; } //---------------------------------------------------------------------------- /** lua_CFunction to call a function with a return value. This is used for global functions, global properties, class static methods, and class static properties. The function pointer is in the first upvalue. */ template ::ReturnType> struct Call { typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); FnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args (L); Stack ::ReturnType>::push (L, FuncTraits ::call (fnptr, args)); return 1; } }; //---------------------------------------------------------------------------- /** lua_CFunction to call a function with no return value. This is used for global functions, global properties, class static methods, and class static properties. The function pointer is in the first upvalue. */ template struct Call { typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); FnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args (L); FuncTraits ::call (fnptr, args); return 0; } }; //---------------------------------------------------------------------------- /** lua_CFunction to call a function with references as arguments. */ template ::ReturnType> struct CallRef { typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); FnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args (L); Stack ::ReturnType>::push (L, FuncTraits ::call (fnptr, args)); LuaRef v (newTable (L)); FuncArgs ::refs (v, args); v.push(L); return 2; } }; template struct CallRef { typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); FnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args (L); FuncTraits ::call (fnptr, args); LuaRef v (newTable (L)); FuncArgs ::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. The class userdata object is at the top of the Lua stack. */ template ::ReturnType> struct CallMember { typedef typename FuncTraits ::ClassType T; typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); T* const t = Userdata::get (L, 1, false); MemFnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args (L); Stack ::push (L, FuncTraits ::call (t, fnptr, args)); return 1; } }; template ::ReturnType> struct CallConstMember { typedef typename FuncTraits ::ClassType T; typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); T const* const t = Userdata::get (L, 1, true); MemFnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args(L); Stack ::push (L, FuncTraits ::call (t, fnptr, args)); return 1; } }; template ::ReturnType> struct CallMemberPtr { typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); boost::shared_ptr* const t = Userdata::get > (L, 1, false); T* const tt = t->get(); if (!tt) { return luaL_error (L, "shared_ptr is nil"); } MemFnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args (L); Stack ::push (L, FuncTraits ::call (tt, fnptr, args)); return 1; } }; template struct CastMemberPtr { static int f (lua_State* L) { boost::shared_ptr t = luabridge::Stack >::get (L, 1); Stack >::push (L, boost::dynamic_pointer_cast (t)); return 1; } }; template struct ClassEqualCheck { static int f (lua_State* L) { T const* const t0 = Userdata::get (L, 1, true); T const* const t1 = Userdata::get (L, 2, true); Stack ::push (L, t0 == t1); return 1; } }; template struct CastClass { static int f (lua_State* L) { T * const t = Userdata::get (L, 1, false ); Stack ::push (L, dynamic_cast(t)); return 1; } }; template struct CastConstClass { static int f (lua_State* L) { T const* const t = Userdata::get (L, 1, true); Stack ::push (L, dynamic_cast(t)); return 1; } }; template struct PtrNullCheck { static int f (lua_State* L) { boost::shared_ptr t = luabridge::Stack >::get (L, 1); Stack ::push (L, t == 0); return 1; } }; template struct WPtrNullCheck { static int f (lua_State* L) { bool rv = true; boost::weak_ptr tw = luabridge::Stack >::get (L, 1); boost::shared_ptr const t = tw.lock(); if (t) { T* const tt = t.get(); rv = (tt == 0); } Stack ::push (L, rv); return 1; } }; template struct PtrEqualCheck { static int f (lua_State* L) { boost::shared_ptr t0 = luabridge::Stack >::get (L, 1); boost::shared_ptr t1 = luabridge::Stack >::get (L, 2); Stack ::push (L, t0 == t1); return 1; } }; template struct WPtrEqualCheck { static int f (lua_State* L) { bool rv = false; boost::weak_ptr tw0 = luabridge::Stack >::get (L, 1); boost::weak_ptr tw1 = luabridge::Stack >::get (L, 2); boost::shared_ptr const t0 = tw0.lock(); boost::shared_ptr const t1 = tw1.lock(); if (t0 && t1) { T* const tt0 = t0.get(); T* const tt1 = t1.get(); rv = (tt0 == tt1); } Stack ::push (L, rv); return 1; } }; template struct ClassEqualCheck > { static int f (lua_State* L) { return PtrEqualCheck::f (L); } }; template struct ClassEqualCheck > { static int f (lua_State* L) { return WPtrEqualCheck::f (L); } }; template static int getPtrProperty (lua_State* L) { boost::shared_ptr cp = luabridge::Stack >::get (L, 1); C const* const c = cp.get(); if (!c) { return luaL_error (L, "shared_ptr is nil"); } T C::** mp = static_cast (lua_touserdata (L, lua_upvalueindex (1))); Stack ::push (L, c->**mp); return 1; } template static int getWPtrProperty (lua_State* L) { boost::weak_ptr cw = luabridge::Stack >::get (L, 1); boost::shared_ptr const cp = cw.lock(); if (!cp) { return luaL_error (L, "cannot lock weak_ptr"); } C const* const c = cp.get(); if (!c) { return luaL_error (L, "weak_ptr is nil"); } T C::** mp = static_cast (lua_touserdata (L, lua_upvalueindex (1))); Stack ::push (L, c->**mp); return 1; } template static int setPtrProperty (lua_State* L) { boost::shared_ptr cp = luabridge::Stack >::get (L, 1); C* const c = cp.get(); if (!c) { return luaL_error (L, "shared_ptr is nil"); } T C::** mp = static_cast (lua_touserdata (L, lua_upvalueindex (1))); c->**mp = Stack ::get (L, 2); return 0; } template static int setWPtrProperty (lua_State* L) { boost::weak_ptr cw = luabridge::Stack >::get (L, 1); boost::shared_ptr cp = cw.lock(); if (!cp) { return luaL_error (L, "cannot lock weak_ptr"); } C* const c = cp.get(); if (!c) { return luaL_error (L, "weak_ptr is nil"); } T C::** mp = static_cast (lua_touserdata (L, lua_upvalueindex (1))); c->**mp = Stack ::get (L, 2); return 0; } template ::ReturnType> struct CallMemberWPtr { typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); boost::weak_ptr* const tw = Userdata::get > (L, 1, false); boost::shared_ptr const t = tw->lock(); if (!t) { return luaL_error (L, "cannot lock weak_ptr"); } T* const tt = t.get(); if (!tt) { return luaL_error (L, "weak_ptr is nil"); } MemFnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args (L); Stack ::push (L, FuncTraits ::call (tt, fnptr, args)); return 1; } }; /** lua_CFunction to calls for function references. */ template ::ReturnType> struct CallMemberRef { typedef typename FuncTraits ::ClassType T; typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); T* const t = Userdata::get (L, 1, false); MemFnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args (L); Stack ::push (L, FuncTraits ::call (t, fnptr, args)); LuaRef v (newTable (L)); FuncArgs ::refs (v, args); v.push(L); return 2; } }; template ::ReturnType> struct CallConstMemberRef { typedef typename FuncTraits ::ClassType T; typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); T const* const t = Userdata::get (L, 1, true); MemFnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args(L); Stack ::push (L, FuncTraits ::call (t, fnptr, args)); LuaRef v (newTable (L)); FuncArgs ::refs (v, args); v.push(L); return 2; } }; template ::ReturnType> struct CallMemberRefPtr { typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); boost::shared_ptr* const t = Userdata::get > (L, 1, false); T* const tt = t->get(); if (!tt) { return luaL_error (L, "shared_ptr is nil"); } MemFnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args (L); Stack ::push (L, FuncTraits ::call (tt, fnptr, args)); LuaRef v (newTable (L)); FuncArgs ::refs (v, args); v.push(L); return 2; } }; template ::ReturnType> struct CallMemberRefWPtr { typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); boost::weak_ptr* const tw = Userdata::get > (L, 1, false); boost::shared_ptr const t = tw->lock(); if (!t) { return luaL_error (L, "cannot lock weak_ptr"); } T* const tt = t.get(); if (!tt) { return luaL_error (L, "weak_ptr is nil"); } MemFnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args (L); Stack ::push (L, FuncTraits ::call (tt, fnptr, args)); LuaRef v (newTable (L)); FuncArgs ::refs (v, args); v.push(L); return 2; } }; //---------------------------------------------------------------------------- /** lua_CFunction to call a class member function with no return value. The member function pointer is in the first upvalue. The class userdata object is at the top of the Lua stack. */ template struct CallMember { typedef typename FuncTraits ::ClassType T; typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); T* const t = Userdata::get (L, 1, false); MemFnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args (L); FuncTraits ::call (t, fnptr, args); return 0; } }; template struct CallConstMember { typedef typename FuncTraits ::ClassType T; typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); T const* const t = Userdata::get (L, 1, true); MemFnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args (L); FuncTraits ::call (t, fnptr, args); return 0; } }; template struct CallMemberPtr { typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); boost::shared_ptr* const t = Userdata::get > (L, 1, false); T* const tt = t->get(); MemFnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args (L); FuncTraits ::call (tt, fnptr, args); return 0; } }; template struct CallMemberWPtr { typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); boost::weak_ptr* const tw = Userdata::get > (L, 1, false); boost::shared_ptr const t = tw->lock(); if (!t) { return luaL_error (L, "cannot lock weak_ptr"); } T* const tt = t.get(); if (!tt) { return luaL_error (L, "weak_ptr is nil"); } MemFnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args (L); FuncTraits ::call (tt, fnptr, args); return 0; } }; template struct CallMemberRef { typedef typename FuncTraits ::ClassType T; typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); T* const t = Userdata::get (L, 1, false); MemFnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args (L); FuncTraits ::call (t, fnptr, args); LuaRef v (newTable (L)); FuncArgs ::refs (v, args); v.push(L); return 1; } }; template struct CallConstMemberRef { typedef typename FuncTraits ::ClassType T; typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); T const* const t = Userdata::get (L, 1, true); MemFnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args (L); FuncTraits ::call (t, fnptr, args); LuaRef v (newTable (L)); FuncArgs ::refs (v, args); v.push(L); return 1; } }; template struct CallMemberRefPtr { typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); boost::shared_ptr* const t = Userdata::get > (L, 1, false); T* const tt = t->get(); if (!tt) { return luaL_error (L, "shared_ptr is nil"); } MemFnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args (L); FuncTraits ::call (tt, fnptr, args); LuaRef v (newTable (L)); FuncArgs ::refs (v, args); v.push(L); return 1; } }; template struct CallMemberRefWPtr { typedef typename FuncTraits ::Params Params; static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); boost::weak_ptr* const tw = Userdata::get > (L, 1, false); boost::shared_ptr const t = tw->lock(); if (!t) { return luaL_error (L, "cannot lock weak_ptr"); } T* const tt = t.get(); if (!tt) { return luaL_error (L, "weak_ptr is nil"); } MemFnPtr const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); ArgList args (L); FuncTraits ::call (tt, fnptr, args); LuaRef v (newTable (L)); FuncArgs ::refs (v, args); v.push(L); return 1; } }; //-------------------------------------------------------------------------- /** lua_CFunction to call a class member lua_CFunction. The member function pointer is in the first upvalue. The class userdata object is at the top of the Lua stack. */ template struct CallMemberCFunction { static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); typedef int (T::*MFP)(lua_State* L); T* const t = Userdata::get (L, 1, false); MFP const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); return (t->*fnptr) (L); } }; template struct CallConstMemberCFunction { static int f (lua_State* L) { assert (isfulluserdata (L, lua_upvalueindex (1))); typedef int (T::*MFP)(lua_State* L); T const* const t = Userdata::get (L, 1, true); MFP const& fnptr = *static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (fnptr != 0); return (t->*fnptr) (L); } }; //-------------------------------------------------------------------------- // SFINAE Helpers template struct CallMemberFunctionHelper { static void add (lua_State* L, char const* name, MemFnPtr mf) { new (lua_newuserdata (L, sizeof (MemFnPtr))) MemFnPtr (mf); lua_pushcclosure (L, &CallConstMember ::f, 1); lua_pushvalue (L, -1); rawsetfield (L, -5, name); // const table rawsetfield (L, -3, name); // class table } }; template struct CallMemberFunctionHelper { static void add (lua_State* L, char const* name, MemFnPtr mf) { new (lua_newuserdata (L, sizeof (MemFnPtr))) MemFnPtr (mf); lua_pushcclosure (L, &CallMember ::f, 1); rawsetfield (L, -3, name); // class table } }; template struct CallMemberPtrFunctionHelper { typedef typename FuncTraits ::ClassType T; static void add (lua_State* L, char const* name, MemFnPtr mf) { new (lua_newuserdata (L, sizeof (MemFnPtr))) MemFnPtr (mf); lua_pushcclosure (L, &CallMemberPtr ::f, 1); rawsetfield (L, -3, name); // class table } }; template struct CallMemberRefPtrFunctionHelper { typedef typename FuncTraits ::ClassType T; static void add (lua_State* L, char const* name, MemFnPtr mf) { new (lua_newuserdata (L, sizeof (MemFnPtr))) MemFnPtr (mf); lua_pushcclosure (L, &CallMemberRefPtr ::f, 1); rawsetfield (L, -3, name); // class table } }; template struct CallMemberWPtrFunctionHelper { typedef typename FuncTraits ::ClassType T; static void add (lua_State* L, char const* name, MemFnPtr mf) { new (lua_newuserdata (L, sizeof (MemFnPtr))) MemFnPtr (mf); lua_pushcclosure (L, &CallMemberWPtr ::f, 1); rawsetfield (L, -3, name); // class table } }; template struct CallMemberRefWPtrFunctionHelper { typedef typename FuncTraits ::ClassType T; static void add (lua_State* L, char const* name, MemFnPtr mf) { new (lua_newuserdata (L, sizeof (MemFnPtr))) MemFnPtr (mf); lua_pushcclosure (L, &CallMemberRefWPtr ::f, 1); rawsetfield (L, -3, name); // class table } }; template struct CallMemberRefFunctionHelper { static void add (lua_State* L, char const* name, MemFnPtr mf) { new (lua_newuserdata (L, sizeof (MemFnPtr))) MemFnPtr (mf); lua_pushcclosure (L, &CallConstMemberRef ::f, 1); lua_pushvalue (L, -1); rawsetfield (L, -5, name); // const table rawsetfield (L, -3, name); // class table } }; template struct CallMemberRefFunctionHelper { static void add (lua_State* L, char const* name, MemFnPtr mf) { new (lua_newuserdata (L, sizeof (MemFnPtr))) MemFnPtr (mf); lua_pushcclosure (L, &CallMemberRef ::f, 1); rawsetfield (L, -3, name); // class table } }; //-------------------------------------------------------------------------- /** __gc metamethod for a class. */ template static int gcMetaMethod (lua_State* L) { Userdata* const ud = Userdata::getExact (L, 1); ud->~Userdata (); return 0; } static int gcNOOPMethod (lua_State* L) { return 0; } //-------------------------------------------------------------------------- /** lua_CFunction to get a class data member. The pointer-to-member is in the first upvalue. The class userdata object is at the top of the Lua stack. */ template static int getProperty (lua_State* L) { C const* const c = Userdata::get (L, 1, true); T C::** mp = static_cast (lua_touserdata (L, lua_upvalueindex (1))); Stack ::push (L, c->**mp); return 1; } //-------------------------------------------------------------------------- /** lua_CFunction to get a constant (enum) */ template static int getConst (lua_State* L) { U *v = static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (v); Stack ::push (L, *v); return 1; } //-------------------------------------------------------------------------- /** lua_CFunction to set a class data member. The pointer-to-member is in the first upvalue. The class userdata object is at the top of the Lua stack. */ template static int setProperty (lua_State* L) { C* const c = Userdata::get (L, 1, false); T C::** mp = static_cast (lua_touserdata (L, lua_upvalueindex (1))); c->**mp = Stack ::get (L, 2); return 0; } //-------------------------------------------------------------------------- // metatable callback for "array[index]" template static int array_index (lua_State* L) { T** parray = (T**) luaL_checkudata (L, 1, typeid(T).name()); int const index = luabridge::Stack::get (L, 2); assert (index > 0); luabridge::Stack::push (L, (*parray)[index-1]); return 1; } // metatable callback for "array[index] = value" template static int array_newindex (lua_State* L) { T** parray = (T**) luaL_checkudata (L, 1, typeid(T).name()); int const index = luabridge::Stack::get (L, 2); T const value = luabridge::Stack::get (L, 3); assert (index > 0); (*parray)[index-1] = value; return 0; } template static int getArray (lua_State* L) { T *v = luabridge::Stack::get (L, 1); T** parray = (T**) lua_newuserdata(L, sizeof(T**)); *parray = v; luaL_getmetatable(L, typeid(T).name()); lua_setmetatable(L, -2); return 1; } // copy complete c array to lua table template static int getTable (lua_State* L) { T *v = luabridge::Stack::get (L, 1); const int cnt = luabridge::Stack::get (L, 2); LuaRef t (L); t = newTable (L); for (int i = 0; i < cnt; ++i) { t[i + 1] = v[i]; } t.push(L); return 1; } // copy lua table to c array template static int setTable (lua_State* L) { T *v = luabridge::Stack::get (L, 1); LuaRef t (LuaRef::fromStack(L, 2)); const int cnt = luabridge::Stack::get (L, 3); for (int i = 0; i < cnt; ++i) { v[i] = t[i + 1]; } return 0; } // return same array at an offset template static int offsetArray (lua_State* L) { T *v = luabridge::Stack::get (L, 1); const unsigned int i = luabridge::Stack::get (L, 2); Stack ::push (L, &v[i]); return 1; } //-------------------------------------------------------------------------- /** C++ STL iterators */ // read lua table into C++ std::list template static int tableToListHelper (lua_State *L, C * const t) { if (!t) { return luaL_error (L, "invalid pointer to std::list<>/std::vector"); } if (!lua_istable (L, -1)) { return luaL_error (L, "argument is not a table"); } lua_pushvalue (L, -1); lua_pushnil (L); while (lua_next (L, -2)) { lua_pushvalue (L, -2); T const value = Stack::get (L, -2); t->push_back (value); lua_pop (L, 2); } lua_pop (L, 1); lua_pop (L, 2); Stack::push (L, *t); return 1; } template static int tableToList (lua_State *L) { C * const t = Userdata::get (L, 1, false); return tableToListHelper (L, t); } template static int ptrTableToList (lua_State *L) { boost::shared_ptr const* const t = Userdata::get > (L, 1, true); if (!t) { return luaL_error (L, "cannot derefencee shared_ptr"); } return tableToListHelper (L, t->get()); } //-------------------------------------------------------------------------- template static int vectorToArray (lua_State *L) { C * const t = Userdata::get (L, 1, false); T * a = &((*t)[0]); Stack ::push (L, a); return 1; } //-------------------------------------------------------------------------- template static int listIterIter (lua_State *L) { typedef typename C::const_iterator IterType; IterType * const end = static_cast (lua_touserdata (L, lua_upvalueindex (2))); IterType * const iter = static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (end); assert (iter); if ((*iter) == (*end)) { return 0; } Stack ::push (L, **iter); ++(*iter); return 1; } // generate an iterator template static int listIterHelper (lua_State *L, C const * const t) { if (!t) { return luaL_error (L, "invalid pointer to std::list<>/std::vector"); } typedef typename C::const_iterator IterType; new (lua_newuserdata (L, sizeof (IterType*))) IterType (t->begin()); new (lua_newuserdata (L, sizeof (IterType*))) IterType (t->end()); lua_pushcclosure (L, listIterIter, 2); return 1; } template static int listIter (lua_State *L) { C const * const t = Userdata::get (L, 1, true); return listIterHelper (L, t); } template static int ptrListIter (lua_State *L) { boost::shared_ptr const* const t = Userdata::get >(L, 1, true); if (!t) { return luaL_error (L, "cannot derefencee shared_ptr"); } return listIterHelper (L, t->get()); } //-------------------------------------------------------------------------- // generate table from std::list template static int listToTableHelper (lua_State *L, C const* const t) { if (!t) { return luaL_error (L, "invalid pointer to std::list<>/std::vector"); } #if 0 // direct lua api lua_createtable(L, t->size(), 0); int newTable = lua_gettop(L); int index = 1; for (typename C::const_iterator iter = t->begin(); iter != t->end(); ++iter, ++index) { Stack::push(L, (*iter)); lua_rawseti (L, newTable, index); } #else // luabridge way LuaRef v (L); v = newTable (L); int index = 1; for (typename C::const_iterator iter = t->begin(); iter != t->end(); ++iter, ++index) { v[index] = (*iter); } v.push(L); #endif return 1; } template static int listToTable (lua_State *L) { C const* const t = Userdata::get (L, 1, true); return listToTableHelper (L, t); } template static int ptrListToTable (lua_State *L) { boost::shared_ptr const* const t = Userdata::get > (L, 1, true); if (!t) { return luaL_error (L, "cannot derefencee shared_ptr"); } return listToTableHelper (L, t->get()); } //-------------------------------------------------------------------------- // push back a C-pointer to a std::list template static int pushbackptr (lua_State *L) { C * const c = Userdata::get (L, 1, false); if (!c) { return luaL_error (L, "invalid pointer to std::list<>"); } T * const v = Userdata::get (L, 2, true); if (!v) { return luaL_error (L, "invalid pointer to std::list<>::value_type"); } c->push_back (v); return 0; } //-------------------------------------------------------------------------- // generate std::map from table template static int tableToMap (lua_State *L) { typedef std::map C; C * const t = Userdata::get (L, 1, true); if (!t) { return luaL_error (L, "invalid pointer to std::map"); } if (!lua_istable (L, -1)) { return luaL_error (L, "argument is not a table"); } lua_pushvalue (L, -1); lua_pushnil (L); while (lua_next (L, -2)) { lua_pushvalue (L, -2); K const key = Stack::get (L, -1); V const value = Stack::get (L, -2); t->insert (std::pair (key, value)); //(*t)[key] = value; lua_pop (L, 2); } lua_pop (L, 1); lua_pop (L, 2); Stack::push (L, *t); return 1; } // iterate over a std::map template static int mapIterIter (lua_State *L) { typedef std::map C; typedef typename C::const_iterator IterType; IterType * const end = static_cast (lua_touserdata (L, lua_upvalueindex (2))); IterType * const iter = static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (end); assert (iter); if ((*iter) == (*end)) { return 0; } Stack ::push (L, (*iter)->first); Stack ::push (L, (*iter)->second); ++(*iter); return 2; } // generate iterator template static int mapIter (lua_State *L) { typedef std::map C; C const * const t = Userdata::get (L, 1, true); if (!t) { return luaL_error (L, "invalid pointer to std::map"); } typedef typename C::const_iterator IterType; new (lua_newuserdata (L, sizeof (IterType*))) IterType (t->begin()); new (lua_newuserdata (L, sizeof (IterType*))) IterType (t->end()); lua_pushcclosure (L, mapIterIter, 2); return 1; } // generate table from std::map template static int mapToTable (lua_State *L) { typedef std::map C; C const* const t = Userdata::get (L, 1, true); if (!t) { return luaL_error (L, "invalid pointer to std::map"); } LuaRef v (L); v = newTable (L); for (typename C::const_iterator iter = t->begin(); iter != t->end(); ++iter) { v[(*iter).first] = (*iter).second; } v.push(L); return 1; } // generate table from std::map template static int mapAt (lua_State *L) { typedef std::map C; C const* const t = Userdata::get (L, 1, true); if (!t) { return luaL_error (L, "invalid pointer to std::map"); } K const key = Stack::get (L, 2); typename C::const_iterator iter = t->find(key); if (iter == t->end()) { return 0; } Stack ::push (L, (*iter).second); return 1; } //-------------------------------------------------------------------------- // generate std::set from table keys ( table[member] = true ) // http://www.lua.org/pil/11.5.html template static int tableToSet (lua_State *L) { C * const t = Userdata::get (L, 1, true); if (!t) { return luaL_error (L, "invalid pointer to std::set"); } if (!lua_istable (L, -1)) { return luaL_error (L, "argument is not a table"); } lua_pushvalue (L, -1); lua_pushnil (L); while (lua_next (L, -2)) { lua_pushvalue (L, -2); T const member = Stack::get (L, -1); bool const v = Stack::get (L, -2); if (v) { t->insert (member); } lua_pop (L, 2); } lua_pop (L, 1); lua_pop (L, 2); Stack::push (L, *t); return 1; } // iterate over a std::set, explicit "true" value. // compare to http://www.lua.org/pil/11.5.html template static int setIterIter (lua_State *L) { typedef typename C::const_iterator IterType; IterType * const end = static_cast (lua_touserdata (L, lua_upvalueindex (2))); IterType * const iter = static_cast (lua_touserdata (L, lua_upvalueindex (1))); assert (end); assert (iter); if ((*iter) == (*end)) { return 0; } Stack ::push (L, **iter); Stack ::push (L, true); ++(*iter); return 2; } template static int setInsert (lua_State *L) { C* const t = Userdata::get (L, 1, false); T const * const v = Userdata::get (L, 2, true); auto rv = t->insert (*v); Stack ::push (L, rv.second); return 1; } // generate iterator template static int setIter (lua_State *L) { C const * const t = Userdata::get (L, 1, true); if (!t) { return luaL_error (L, "invalid pointer to std::set"); } typedef typename C::const_iterator IterType; new (lua_newuserdata (L, sizeof (IterType*))) IterType (t->begin()); new (lua_newuserdata (L, sizeof (IterType*))) IterType (t->end()); lua_pushcclosure (L, setIterIter, 2); return 1; } // generate table from std::set template static int setToTable (lua_State *L) { C const* const t = Userdata::get (L, 1, true); if (!t) { return luaL_error (L, "invalid pointer to std::set"); } LuaRef v (L); v = newTable (L); for (typename C::const_iterator iter = t->begin(); iter != t->end(); ++iter) { v[(*iter)] = true; } v.push(L); return 1; } //-------------------------------------------------------------------------- // bitset { num = true } // compare to http://www.lua.org/pil/11.5.html template static int tableToBitSet (lua_State *L) { typedef std::bitset C; C * const t = Userdata::get (L, 1, true); if (!t) { return luaL_error (L, "invalid pointer to std::bitset"); } if (!lua_istable (L, -1)) { return luaL_error (L, "argument is not a table"); } lua_pushvalue (L, -1); lua_pushnil (L); while (lua_next (L, -2)) { lua_pushvalue (L, -2); unsigned int const member = Stack::get (L, -1); bool const v = Stack::get (L, -2); if (member < T && v) { t->set (member); } lua_pop (L, 2); } lua_pop (L, 1); lua_pop (L, 2); Stack::push (L, *t); return 1; } // generate table from std::bitset template static int bitSetToTable (lua_State *L) { typedef std::bitset C; C const* const t = Userdata::get (L, 1, true); if (!t) { return luaL_error (L, "invalid pointer to std::bitset"); } LuaRef v (L); v = newTable (L); for (unsigned int i = 0; i < T; ++i) { if (t->test (i)) { v[i] = true; } } v.push(L); return 1; } }; /* vim: set et sw=2: */