diff options
author | dshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573> | 2008-11-18 20:07:42 +0000 |
---|---|---|
committer | dshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573> | 2008-11-18 20:07:42 +0000 |
commit | a06523dd0f46c2536e070e58c9288419bbcbf22e (patch) | |
tree | 1c49523bf609b18ee5e6a529e18e34438b62bfad /crawl-ref/source/clua.h | |
parent | ff00ffd9da330a94bd282bcc127157672e589a9a (diff) | |
download | crawl-ref-a06523dd0f46c2536e070e58c9288419bbcbf22e.tar.gz crawl-ref-a06523dd0f46c2536e070e58c9288419bbcbf22e.zip |
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
Diffstat (limited to 'crawl-ref/source/clua.h')
-rw-r--r-- | crawl-ref/source/clua.h | 35 |
1 files changed, 35 insertions, 0 deletions
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<lua_shutdown_listener*> shutdown_listeners; + private: void init_lua(); void set_error(int err, lua_State *ls = NULL); |