From e92caa97329016d012b46b9d37e1b2c3b613a8f2 Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 10 Nov 2018 20:13:55 +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 | 2 +- src/lua/common/je_lua_proxy.h | 30 +++++------ src/lua/common/je_lua_reference.hpp | 88 --------------------------------- src/lua/common/je_lua_shared.hpp | 99 +++++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 104 deletions(-) delete mode 100644 src/lua/common/je_lua_reference.hpp create mode 100644 src/lua/common/je_lua_shared.hpp (limited to 'src/lua/common') diff --git a/src/lua/common/je_lua_common.h b/src/lua/common/je_lua_common.h index 1d772a5..82c550a 100644 --- a/src/lua/common/je_lua_common.h +++ b/src/lua/common/je_lua_common.h @@ -3,7 +3,7 @@ #include "je_lua_port.h" #include "je_lua_proxy.h" -#include "je_lua_reference.hpp" +#include "je_lua_shared.hpp" #include "je_lua_error.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 b428dc9..56e5fee 100644 --- a/src/lua/common/je_lua_proxy.h +++ b/src/lua/common/je_lua_proxy.h @@ -1,7 +1,7 @@ #ifndef __JIN_COMMON_PROXY_H #define __JIN_COMMON_PROXY_H -#include "je_lua_reference.hpp" +#include "je_lua_shared.hpp" namespace JinEngine { @@ -11,53 +11,53 @@ namespace JinEngine class Proxy { public: - void bind(RefBase* ref) + void bind(SharedBase* ref) { if (ref == nullptr) return; - reference = ref; + shared = ref; } 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 - Ref& getRef() + Shared& getShared() { - return *(Ref*) reference; + return *(static_cast*>(shared)); } template T* getObject() { - Ref& ref = getRef(); + Shared& ref = getShared(); return ref.getObject(); } const char* getObjectType() { - return reference->type; + return shared->type; } - RefBase* reference; + SharedBase* shared; }; diff --git a/src/lua/common/je_lua_reference.hpp b/src/lua/common/je_lua_reference.hpp deleted file mode 100644 index ba918bb..0000000 --- a/src/lua/common/je_lua_reference.hpp +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef __JIN_COMMON_REFERENCE_H -#define __JIN_COMMON_REFERENCE_H - -namespace JinEngine -{ - namespace Lua - { - - /*abstract*/class RefBase - { - public: - void retain() - { - ++count; - } - - void release() - { - if (--count <= 0) - delete this; - } - - // object type string - const char* const type; - - void setUserdata(void* data) - { - userdata = data; - } - - void* getUserdata() - { - return userdata; - } - - protected: - RefBase(void* obj, const char* t) - : count(1) - , object(obj) - , type(t) - { - } - - RefBase(const RefBase&); - - virtual ~RefBase() - { - } - - void* object; - int count; - void* userdata; - }; - - template - class Ref : public RefBase - { - public: - Ref(T* obj, const char* type) - : RefBase(obj, type) - { - } - - ~Ref() - { - T* obj = static_cast(object); - delete obj; - } - - T* operator->() - { - return (T*)object; - } - - T* getObject() - { - return (T*)object; - } - - private: - Ref(const Ref& ref); - - }; - - } -} - -#endif \ No newline at end of file diff --git a/src/lua/common/je_lua_shared.hpp b/src/lua/common/je_lua_shared.hpp new file mode 100644 index 0000000..7ad7c1d --- /dev/null +++ b/src/lua/common/je_lua_shared.hpp @@ -0,0 +1,99 @@ +#ifndef __JIN_COMMON_REFERENCE_H +#define __JIN_COMMON_REFERENCE_H + +namespace JinEngine +{ + namespace Lua + { + + /*abstract*/class SharedBase + { + public: + void retain() + { + ++(*count); + } + + void release() + { + if (--(*count) <= 0) + delete this; + } + + // object type string + const char* const type; + + void setUserdata(void* data) + { + userdata = data; + } + + void* getUserdata() + { + return userdata; + } + + protected: + SharedBase(void* obj, const char* t) + : object(obj) + , type(t) + { + count = new int(1); + } + + SharedBase(const SharedBase&); + + virtual ~SharedBase() + { + + } + + void* object; + int* count; + void* userdata; + }; + + template + class Shared : public SharedBase + { + public: + Shared(T* obj, const char* type) + : SharedBase(obj, type) + { + } + + ~Shared() + { + T* obj = static_cast(object); + delete obj; + } + + T* operator->() + { + return static_cast(object); + } + + T* getObject() + { + return static_cast(object); + } + + T* operator &() + { + return static_cast(object); + } + + T& operator *() + { + return *(static_cast(object)); + } + + private: + Shared(const Shared& ref); + + }; + + } +} + +#endif \ No newline at end of file -- cgit v1.1-26-g67d0