aboutsummaryrefslogtreecommitdiff
path: root/src/lua/common/je_lua_shared.hpp
blob: d9d9a0e9b37543e7fd1ccfb5ecb38f16de6dc1e7 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef __JIN_COMMON_SHARED_H__
#define __JIN_COMMON_SHARED_H__

#include <map>
#include <vector>
#include <functional>

#include "libjin/jin.h"

namespace JinEngine
{
    namespace Lua
    {

        class LuaObject;

        ///
        /// ̰߳ȫĹģ塣
        ///
        class Shared
        {
        public:
            Shared(Object* obj, const char* type)
                : mCount(0)
                , mObject(obj)
                , mType(type)
            {
            }

            Object* operator->()
            {
                return static_cast<Object*>(mObject);
            }

            Object* getObject()
            {
                return static_cast<Object*>(mObject);
            }

            inline const char* getType()
            {
                return mType;
            }

            inline bool isType(const char* t)
            {
                return strcmp(mType, t) == 0;
            }

            template<class T>
            T* getObject()
            {
                return static_cast<T*>(mObject);
            }

        private:
            friend class LuaObject;

            // Disable copy.
            Shared(const Shared& shared);

            ///
            /// Sharedֻڶ
            ///
            ~Shared()
            {
                delete mObject;
            }

            ///
            /// ͬһ̵߳lua_StateУLuaObjectEngineObjectӦһһӦLuaObject(lua runtime)ü
            /// ͻաEngine-sideüΪά̵ͬ߳lua_StateͬһEngineObjectÿLuaObjectһ
            /// Sharedʱһüͬһ߳УһEngineObjectֻһLuaObject󶨣new 
            /// instanceУômCountͱEngineObjectĹ߳
            ///
            inline void Shared::retain()
            {
                ++mCount;
            }

            inline void Shared::release()
            {
                if (--mCount <= 0)
                    delete this;
            }

            Object* mObject;
            int mCount;
            const char* mType;
        };
        
    } // namespace Lua
} // namespace JinEngine

#endif