aboutsummaryrefslogtreecommitdiff
path: root/src/lua/common/Reference.hpp
blob: feb96bb931a541f3fe728e3925a4d749a4a179d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef __JIN_COMMON_REFERENCE_H
#define __JIN_COMMON_REFERENCE_H

namespace jin
{
namespace lua
{

    /*abstract*/class RefBase
    {
    public:
        void retain()
        {
            ++count;
        }

        void release()
        {
            if (--count <= 0)
                delete this;
        }

        // object type string
        const char* const type;

    protected:
        RefBase(void* obj, const char* t)
            : count(1)
            , object(obj)
            , type(t)
        {
        }

        RefBase(const RefBase&);

        virtual ~RefBase()
        {
        }

        void* object;
        int count;        

    };

    template<class T>
    class Ref : public RefBase
    {
    public:
        Ref(T* obj, const char* type)
            : RefBase(obj, type)
        {
        }

        ~Ref()
        {
            T* obj = (T*)object;
            delete obj;
        }

        T* operator->()
        {
            return (T*)object;
        }

        T* getObject()
        {
            return (T*)object;
        }

    private:
        Ref(const Ref<T>& ref);

    };

}
}

#endif