Advertisement
robn

Wrap any Lua function with a sigc++ slot (untested)

Mar 22nd, 2014
174
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) {
  4.         lua_getfield(l, LUA_REGISTRYINDEX, "PiUISlot");
  5.         if (lua_isnil(l, -1)) {
  6.             lua_newtable(l);
  7.             lua_pushvalue(l, -1);
  8.             lua_setfield(l, LUA_REGISTRYINDEX, "PiUISlot");
  9.         }
  10.         lua_pushvalue(l, -2);
  11.         int ref = luaL_ref(l, -2);
  12.         lua_pop(l, 1);
  13.         return sigc::bind(sigc::ptr_fun(&trampoline), l, ref);
  14.     }
  15.  
  16. private:
  17.     static inline void trampoline(lua_State *l, int ref) {
  18.         lua_getfield(l, LUA_REGISTRYINDEX, "PiUISlot");
  19.         lua_rawgeti(l, -1, ref);
  20.         luaL_unref(l, -2, ref);
  21.         lua_remove(l, -2);
  22.         lua_call(l, -1, 0);
  23.     }
  24. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement