aboutsummaryrefslogtreecommitdiff
path: root/src/lua/luaopen_types.h
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-08-16 14:21:56 +0800
committerchai <chaifix@163.com>2018-08-16 14:21:56 +0800
commit8585c92b7d0744a1f1a39c872cf5096621161b6c (patch)
tree6aa02138f39f7b11ab17c7399064353092b8df0c /src/lua/luaopen_types.h
parentbe9b27dbf550093b555ab3087c11b38c89ab9fd0 (diff)
*update
Diffstat (limited to 'src/lua/luaopen_types.h')
-rw-r--r--src/lua/luaopen_types.h98
1 files changed, 63 insertions, 35 deletions
diff --git a/src/lua/luaopen_types.h b/src/lua/luaopen_types.h
index a685407..b328d44 100644
--- a/src/lua/luaopen_types.h
+++ b/src/lua/luaopen_types.h
@@ -21,59 +21,87 @@ namespace jin
{
namespace lua
{
-
- class Object
- {
- public:
- Object()
- : count(1)
- {
- }
- int getReferenceCount() const
- {
- return count;
- }
- void retain()
- {
- ++count;
- }
- void release()
- {
- if (--count <= 0)
- delete this;
- }
-
- protected:
- virtual ~Object() = 0 {}
+ class Reference
+ {
+ public:
- private:
- // reference count
- int count;
- };
+ Reference(void* obj)
+ : count(1)
+ , object(obj)
+ {
+ }
+ void retain()
+ {
+ ++count;
+ }
+ void release()
+ {
+ if (--count <= 0)
+ delete this;
+ }
+
+ protected:
+ Reference(const Reference&);
+ virtual ~Reference()
+ {
+ }
+ void* object;
+ int count;
+
+ };
+
+ template<class T>
+ class Ref : public Reference
+ {
+ public:
+ Ref(T* obj)
+ : Reference(obj)
+ {
+ }
+ ~Ref()
+ {
+ T* obj = (T*)object;
+ delete obj;
+ }
+ T& operator *()
+ {
+ T* obj = (T*)object;
+ return *obj;
+ }
+ private:
+ Ref(const Ref<T>& ref);
+ };
class Proxy
{
public:
- void bind(Object* obj, const char* t)
+ void bind(Reference* ref, const char* t)
{
- if (obj == nullptr)
+ if (ref == nullptr)
return;
- object = obj;
+ reference = ref;
type = t;
}
void release()
{
- if (object != nullptr)
+ if (reference != nullptr)
{
- object->release();
- object = nullptr;
+ reference->release();
+ reference = nullptr;
type = nullptr;
}
}
- Object* object; // acctual object binded
+ template<class T>
+ Ref<T>& getRef()
+ {
+ Ref<T>* ref = (Ref<T>*) reference;
+ return *ref;
+ }
+
+ Reference* reference; // acctual object binded
const char* type; // type name and metatable name
};