Advertisement
robn

Wrap any Lua function with a sigc++ slot

Mar 23rd, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class LuaSlot {
  2. public:
  3.     static inline sigc::slot<void> Wrap(lua_State *l, int idx) {
  4.         idx = lua_absindex(l, idx);
  5.         lua_getfield(l, LUA_REGISTRYINDEX, "PiUISlot");
  6.         if (lua_isnil(l, -1)) {
  7.             lua_pop(l, 1);
  8.             lua_newtable(l);
  9.             lua_pushvalue(l, -1);
  10.             lua_setfield(l, LUA_REGISTRYINDEX, "PiUISlot");
  11.         }
  12.         lua_pushvalue(l, idx);
  13.         int ref = luaL_ref(l, -2);
  14.         lua_pop(l, 1);
  15.         return sigc::bind(sigc::ptr_fun(&trampoline), l, ref);
  16.     }
  17.  
  18. private:
  19.     static inline void trampoline(lua_State *l, int ref) {
  20.         lua_getfield(l, LUA_REGISTRYINDEX, "PiUISlot");
  21.         lua_rawgeti(l, -1, ref);
  22.         luaL_unref(l, -2, ref);
  23.         lua_call(l, 0, 0);
  24.         lua_pop(l, 1);
  25.     }
  26. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement