diff options
author | Pavel Labath <pavelo@centrum.sk> | 2011-07-07 22:24:40 (GMT) |
---|---|---|
committer | Pavel Labath <pavelo@centrum.sk> | 2011-07-08 13:36:58 (GMT) |
commit | 613df819d690dbe8cdec97457bcf538e980093ae (patch) | |
tree | 37bfb2e72e2d9ecb42487cf5f7d55fbc06378db2 /src | |
parent | 8addf3e9c6e50dc85d2a5d7840a752a56b2258da (diff) | |
download | fluxbox_pavel-613df819d690dbe8cdec97457bcf538e980093ae.zip fluxbox_pavel-613df819d690dbe8cdec97457bcf538e980093ae.tar.bz2 |
A lightweight version of lua::state::pushclosure which handles the simple cases
Diffstat (limited to 'src')
-rw-r--r-- | src/FbTk/Luamm.cc | 12 | ||||
-rw-r--r-- | src/FbTk/Luamm.hh | 3 |
2 files changed, 12 insertions, 3 deletions
diff --git a/src/FbTk/Luamm.cc b/src/FbTk/Luamm.cc index 563be9c..1ef751f 100644 --- a/src/FbTk/Luamm.cc +++ b/src/FbTk/Luamm.cc | |||
@@ -59,6 +59,7 @@ namespace lua { | |||
59 | lua_rawset(l, index); | 59 | lua_rawset(l, index); |
60 | } | 60 | } |
61 | 61 | ||
62 | template<typename Functor> | ||
62 | int closure_trampoline(lua_State *l) | 63 | int closure_trampoline(lua_State *l) |
63 | { | 64 | { |
64 | lua_checkstack(l, 2); | 65 | lua_checkstack(l, 2); |
@@ -68,7 +69,7 @@ namespace lua { | |||
68 | lua_pop(l, 1); | 69 | lua_pop(l, 1); |
69 | 70 | ||
70 | try { | 71 | try { |
71 | Slot *fn = static_cast<Slot *>( L->touserdata(lua_upvalueindex(1)) ); | 72 | Functor *fn = reinterpret_cast<Functor *>( L->touserdata(lua_upvalueindex(1)) ); |
72 | assert(fn); | 73 | assert(fn); |
73 | return (*fn)(L); | 74 | return (*fn)(L); |
74 | } | 75 | } |
@@ -390,7 +391,14 @@ namespace lua { | |||
390 | setmetatable(-2); | 391 | setmetatable(-2); |
391 | 392 | ||
392 | insert(-n-1); | 393 | insert(-n-1); |
393 | lua_pushcclosure(cobj, &closure_trampoline, n+1); | 394 | lua_pushcclosure(cobj, &closure_trampoline<Slot>, n+1); |
395 | } | ||
396 | |||
397 | void state::pushclosure(int (*fn)(state *), int n) | ||
398 | { | ||
399 | pushlightuserdata(reinterpret_cast<void *>(fn)); | ||
400 | insert(-n-1); | ||
401 | lua_pushcclosure(cobj, &closure_trampoline<int (state *)>, n+1); | ||
394 | } | 402 | } |
395 | 403 | ||
396 | void state::rawgetfield(int index, const char *k) throw(std::bad_alloc) | 404 | void state::rawgetfield(int index, const char *k) throw(std::bad_alloc) |
diff --git a/src/FbTk/Luamm.hh b/src/FbTk/Luamm.hh index 05cb569..96a5eea 100644 --- a/src/FbTk/Luamm.hh +++ b/src/FbTk/Luamm.hh | |||
@@ -237,9 +237,10 @@ namespace lua { | |||
237 | void newtable() { lua_newtable(cobj); } | 237 | void newtable() { lua_newtable(cobj); } |
238 | void *newuserdata(size_t size) { return lua_newuserdata(cobj, size); } | 238 | void *newuserdata(size_t size) { return lua_newuserdata(cobj, size); } |
239 | // Functor can be anything that FbTk::Slot can handle, everything else remains | 239 | // Functor can be anything that FbTk::Slot can handle, everything else remains |
240 | // identical | 240 | // identical. We also provide a specialized, lightweight version for simple functors |
241 | template<typename Functor> | 241 | template<typename Functor> |
242 | void pushclosure(const Functor &fn, int n); | 242 | void pushclosure(const Functor &fn, int n); |
243 | void pushclosure(int (*fn)(state *), int n); | ||
243 | template<typename Functor> | 244 | template<typename Functor> |
244 | void pushfunction(const Functor &fn) { pushclosure(fn, 0); } | 245 | void pushfunction(const Functor &fn) { pushclosure(fn, 0); } |
245 | void pushstring(const char *s) { lua_pushstring(cobj, s); } | 246 | void pushstring(const char *s) { lua_pushstring(cobj, s); } |