From 72e45f0062d727cedd576d1e1251f6722454a119 Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 12 Nov 2018 08:04:11 +0800 Subject: =?UTF-8?q?*=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lua/common/je_lua_reference.h | 52 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/lua/common/je_lua_reference.h (limited to 'src/lua/common/je_lua_reference.h') diff --git a/src/lua/common/je_lua_reference.h b/src/lua/common/je_lua_reference.h new file mode 100644 index 0000000..28c3bc3 --- /dev/null +++ b/src/lua/common/je_lua_reference.h @@ -0,0 +1,52 @@ +#ifndef __JIN_COMMON_REFERENCE_H +#define __JIN_COMMON_REFERENCE_H + +#include "../luax.h" + +namespace JinEngine +{ + namespace Lua + { + + /// + /// This class wraps the reference functionality built into Lua, which allows C++ code to refer to Lua + /// variables. + /// + class Reference + { + public: + Reference(lua_State* L, unsigned int i) + : mL(L) + { + luax_pushvalue(mL, i); + mIndex = luax_ref(mL, LUA_REGISTRYINDEX); + } + + ~Reference() + { + unref(); + } + + void unref() + { + luax_unref(mL, LUA_REGISTRYINDEX, mIndex); + } + + /// + /// Push value onto the stack. + /// + void push() + { + luax_rawgeti(mL, LUA_REGISTRYINDEX, mIndex); + } + + private: + lua_State* const mL; + int mIndex; + + }; + + } // namespace Lua +} // namespace JinEngine + +#endif // __JIN_COMMON_REFERENCE_H \ No newline at end of file -- cgit v1.1-26-g67d0