From a06523dd0f46c2536e070e58c9288419bbcbf22e Mon Sep 17 00:00:00 2001 From: dshaligram Date: Tue, 18 Nov 2008 20:07:42 +0000 Subject: Added lua_datum to wrap the common task of retaining references to Lua objects in the Lua registry. git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@7477 c06c8d41-db1a-0410-9941-cceddc491573 --- crawl-ref/source/clua.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'crawl-ref/source/clua.h') diff --git a/crawl-ref/source/clua.h b/crawl-ref/source/clua.h index f5a0050cca..ae465aa3a0 100644 --- a/crawl-ref/source/clua.h +++ b/crawl-ref/source/clua.h @@ -57,6 +57,36 @@ private: static lua_clua_map lua_map; }; +class lua_shutdown_listener +{ +public: + virtual void shutdown(CLua &lua) = 0; +}; + +// A convenience class to keep a reference to a lua object on the stack. +// This is useful to hang on to things that cannot be directly retrieved by +// C++ code, such as Lua function references. +class lua_datum : public lua_shutdown_listener +{ +public: + lua_datum(CLua &lua, int stackpos = -1, bool pop = true); + lua_datum(const lua_datum &other); + + void shutdown(CLua &lua); + + ~lua_datum(); + + // Push the datum onto the Lua stack. + void push() const; + +private: + bool need_cleanup; + CLua &lua; + +private: + void cleanup(); +}; + class CLua { public: @@ -95,6 +125,9 @@ public: void fnreturns(const char *params, ...); bool runhook(const char *hook, const char *params, ...); + void add_shutdown_listener(lua_shutdown_listener *); + void remove_shutdown_listener(lua_shutdown_listener *); + static int file_write(lua_State *ls); static int loadfile(lua_State *ls, const char *file, bool trusted = false, bool die_on_fail = false); @@ -128,6 +161,8 @@ private: sfset sourced_files; unsigned long uniqindex; + std::vector shutdown_listeners; + private: void init_lua(); void set_error(int err, lua_State *ls = NULL); -- cgit v1.2.3-54-g00ecf