From b3b5d901b07ae16b3c1397c6e8710239516be68e Mon Sep 17 00:00:00 2001 From: Robert Vollmert Date: Wed, 21 Oct 2009 11:53:18 +0200 Subject: Move userdata templates to cluautil.h. --- crawl-ref/source/cluautil.h | 81 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) (limited to 'crawl-ref/source/cluautil.h') diff --git a/crawl-ref/source/cluautil.h b/crawl-ref/source/cluautil.h index 02e5180d7c..8e322bc584 100644 --- a/crawl-ref/source/cluautil.h +++ b/crawl-ref/source/cluautil.h @@ -15,7 +15,36 @@ extern "C" { /* * Function definitions. */ + #define LUAFN(name) static int name(lua_State *ls) +#define LUAWRAP(name, wrapexpr) \ + static int name(lua_State *ls) \ + { \ + wrapexpr; \ + return (0); \ + } +#define PLUARET(type, val) \ + lua_push##type(ls, val); \ + return (1); +#define LUARET1(name, type, val) \ + static int name(lua_State *ls) \ + { \ + lua_push##type(ls, val); \ + return (1); \ + } +#define LUARET2(name, type, val1, val2) \ + static int name(lua_State *ls) \ + { \ + lua_push##type(ls, val1); \ + lua_push##type(ls, val2); \ + return (2); \ + } + +#define ASSERT_DLUA \ + do { \ + if (CLua::get_vm(ls).managed_vm) \ + luaL_error(ls, "Operation forbidden in end-user script"); \ + } while (false) // FIXME: remove one of these. @@ -29,9 +58,59 @@ void clua_register_metatable(lua_State *ls, const char *tn, int (*gcfn)(lua_State *ls) = NULL); /* - * Passing objects from and to Lua. + * User-data templates. + * TODO: Consolidate these. */ +template +inline static T *util_get_userdata(lua_State *ls, int ndx) +{ + return (lua_islightuserdata(ls, ndx))? + static_cast( lua_touserdata(ls, ndx) ) + : NULL; +} + +template +inline static T *clua_get_userdata(lua_State *ls, const char *mt, int ndx = 1) +{ + return static_cast( luaL_checkudata( ls, ndx, mt ) ); +} + +template +static int lua_object_gc(lua_State *ls) +{ + T **pptr = static_cast( lua_touserdata(ls, 1) ); + if (pptr) + delete *pptr; + return (0); +} + +template T *clua_new_userdata( + lua_State *ls, const char *mt) +{ + void *udata = lua_newuserdata( ls, sizeof(T) ); + luaL_getmetatable(ls, mt); + lua_setmetatable(ls, -2); + return static_cast( udata ); +} + +template +inline void dlua_push_userdata(lua_State *ls, T udata, const char *meta) +{ + T *de = clua_new_userdata(ls, meta); + *de = udata; +} + +template +static void dlua_push_object_type(lua_State *ls, const char *meta, const T &data) +{ + T **ptr = clua_new_userdata(ls, meta); + *ptr = new T(data); +} + +/* + * Passing objects from and to Lua. + */ struct activity_interrupt_data; int push_activity_interrupt(lua_State *ls, activity_interrupt_data *t); -- cgit v1.2.3-54-g00ecf