aboutsummaryrefslogtreecommitdiff
path: root/src/lua/common/je_lua_reference.h
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-11-12 08:04:11 +0800
committerchai <chaifix@163.com>2018-11-12 08:04:11 +0800
commit72e45f0062d727cedd576d1e1251f6722454a119 (patch)
tree736594b79e71c66a668d99d96c3ce464618e50ca /src/lua/common/je_lua_reference.h
parent7c2f33bdf37de7acf9b0728a115377081344db1c (diff)
*修改代码结构
Diffstat (limited to 'src/lua/common/je_lua_reference.h')
-rw-r--r--src/lua/common/je_lua_reference.h52
1 files changed, 52 insertions, 0 deletions
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