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_common.h | 1 + src/lua/common/je_lua_proxy.h | 29 ++++++++++++---------- src/lua/common/je_lua_reference.h | 52 +++++++++++++++++++++++++++++++++++++++ src/lua/common/je_lua_shared.hpp | 9 ++++--- 4 files changed, 74 insertions(+), 17 deletions(-) create mode 100644 src/lua/common/je_lua_reference.h (limited to 'src/lua/common') diff --git a/src/lua/common/je_lua_common.h b/src/lua/common/je_lua_common.h index 82c550a..5b217a2 100644 --- a/src/lua/common/je_lua_common.h +++ b/src/lua/common/je_lua_common.h @@ -5,5 +5,6 @@ #include "je_lua_proxy.h" #include "je_lua_shared.hpp" #include "je_lua_error.h" +#include "je_lua_reference.h" #endif \ No newline at end of file diff --git a/src/lua/common/je_lua_proxy.h b/src/lua/common/je_lua_proxy.h index b476aa6..96b2093 100644 --- a/src/lua/common/je_lua_proxy.h +++ b/src/lua/common/je_lua_proxy.h @@ -13,40 +13,43 @@ namespace JinEngine class Proxy { public: - void bind(SharedBase* shared) + void bind(SharedBase* s) { - if (shared == nullptr) + if (s == nullptr) return; - reference = shared; + shared = s; } void release() { - if (reference != nullptr) + if (shared != nullptr) { - reference->release(); - reference = nullptr; + shared->release(); + shared = nullptr; } } void retain() { - if (reference != nullptr) - reference->retain(); + if (shared != nullptr) + shared->retain(); } void setUserdata(void* data) { - if (reference != nullptr) - reference->setUserdata(data); + if (shared != nullptr) + shared->setUserdata(data); } template Shared& getShared() { - return *(Shared*) reference; + return *(Shared*)shared; } + /// + /// For convenience. + /// template T* getObject() { @@ -56,10 +59,10 @@ namespace JinEngine const char* getObjectType() { - return reference->type; + return shared->type; } - SharedBase* reference; + SharedBase* shared; }; 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 diff --git a/src/lua/common/je_lua_shared.hpp b/src/lua/common/je_lua_shared.hpp index 53f557a..7ac8923 100644 --- a/src/lua/common/je_lua_shared.hpp +++ b/src/lua/common/je_lua_shared.hpp @@ -1,5 +1,5 @@ -#ifndef __JIN_COMMON_REFERENCE_H -#define __JIN_COMMON_REFERENCE_H +#ifndef __JIN_COMMON_SHARED_H +#define __JIN_COMMON_SHARED_H namespace JinEngine { @@ -75,6 +75,7 @@ namespace JinEngine // Disable copy constructor. Shared(const Shared& shared); + // Make shared only be able created with new. ~Shared() { T* obj = static_cast(object); @@ -83,7 +84,7 @@ namespace JinEngine }; - } -} + } // namespace Lua +} // namespace JinEngine #endif \ No newline at end of file -- cgit v1.1-26-g67d0