diff options
Diffstat (limited to 'src/lua/common/je_lua_reference.h')
-rw-r--r-- | src/lua/common/je_lua_reference.h | 52 |
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 |