aboutsummaryrefslogtreecommitdiff
path: root/src/lua/common/Reference.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua/common/Reference.hpp')
-rw-r--r--src/lua/common/Reference.hpp72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/lua/common/Reference.hpp b/src/lua/common/Reference.hpp
new file mode 100644
index 0000000..f330323
--- /dev/null
+++ b/src/lua/common/Reference.hpp
@@ -0,0 +1,72 @@
+#ifndef __JIN_COMMON_REFERENCE_H
+#define __JIN_COMMON_REFERENCE_H
+
+namespace jin
+{
+namespace lua
+{
+
+ /*abstract*/class Reference
+ {
+ public:
+ void retain()
+ {
+ ++count;
+ }
+
+ void release()
+ {
+ if (--count <= 0)
+ delete this;
+ }
+
+ protected:
+ Reference(void* obj)
+ : count(1)
+ , object(obj)
+ {
+ }
+ 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->()
+ {
+ return (T*)object;
+ }
+
+ T* getObject()
+ {
+ return (T*)object;
+ }
+
+ private:
+ Ref(const Ref<T>& ref);
+
+ };
+
+}
+}
+
+#endif \ No newline at end of file