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/clua.h | 64 ---------------------------------- crawl-ref/source/cluautil.h | 81 +++++++++++++++++++++++++++++++++++++++++++- crawl-ref/source/dlua.h | 14 -------- crawl-ref/source/l_mapgrd.cc | 2 +- crawl-ref/source/l_mons.cc | 2 +- 5 files changed, 82 insertions(+), 81 deletions(-) (limited to 'crawl-ref') diff --git a/crawl-ref/source/clua.h b/crawl-ref/source/clua.h index daca3922aa..092e551384 100644 --- a/crawl-ref/source/clua.h +++ b/crawl-ref/source/clua.h @@ -232,72 +232,8 @@ extern CLua clua; void lua_set_exclusive_item(const item_def *item = NULL); -#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) - -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); -} - std::string quote_lua_string(const std::string &s); -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 ); -} - void print_clua_stack(); #endif 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); diff --git a/crawl-ref/source/dlua.h b/crawl-ref/source/dlua.h index fcc1978604..8d5feaa119 100644 --- a/crawl-ref/source/dlua.h +++ b/crawl-ref/source/dlua.h @@ -79,20 +79,6 @@ dungeon_feature_type dungeon_feature_by_name(const std::string &name); std::vector dungeon_feature_matches(const std::string &name); const char *dungeon_feature_name(dungeon_feature_type feat); -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); -} - void print_dlua_stack(); ////////////////////////////////////////////////////////////////////////// diff --git a/crawl-ref/source/l_mapgrd.cc b/crawl-ref/source/l_mapgrd.cc index a1ea880594..efefb583c5 100644 --- a/crawl-ref/source/l_mapgrd.cc +++ b/crawl-ref/source/l_mapgrd.cc @@ -1,6 +1,6 @@ #include "AppHdr.h" -#include "dlua.h" +#include "cluautil.h" #include "l_libs.h" #include "mapdef.h" diff --git a/crawl-ref/source/l_mons.cc b/crawl-ref/source/l_mons.cc index 459c037c60..3a4aa2af94 100644 --- a/crawl-ref/source/l_mons.cc +++ b/crawl-ref/source/l_mons.cc @@ -1,6 +1,6 @@ #include "AppHdr.h" -#include "clua.h" +#include "cluautil.h" #include "l_libs.h" #include "delay.h" -- cgit v1.2.3-54-g00ecf