summaryrefslogtreecommitdiff
path: root/source/external/Luax/luax_class.hpp
blob: 12eb2689139430d2573a7d48d8ea98fe357e6e03 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#ifndef __LUAX_CLASS_H__
#define __LUAX_CLASS_H__

#include "luax_config.h"

#if LUAX_PROFILER 
#include <unordered_set>
#endif

#include <vector>

#include "luax_ref.h"
#include "luax_memberref.h"
#include "luax_cfunctions.h"
#include "luax_watchdog.h"
#include "luax_utility.h"

namespace Luax
{

	class LuaxVM;

	///
	/// ࣬Ϊʵֶ̬ҪЩӿڵⲿҪ̳д֮࣬оͻ
	/// öӦʵķע̳дʱʵķʵLuaxNativeClassУʵֻ
	/// ¶ԡ
	/// 
	/// Effective C++40ڱʹvirtual base£Ӧþܱз
	/// ݳԱݳԱʼɵһЩ⡣һ㣬vpbӽӽC#Java
	/// InterfaceԣIͷʶһӿڡ
	///
	class LuaxObject
	{
	public:
		LuaxObject() {};
		virtual ~LuaxObject() {};

		///
		/// Աùʵref tableáȡ
		///
		virtual bool PushLuaxMemberRef(LuaxState& state, int refID) = 0;
		virtual bool PushLuaxUserdata(LuaxState& state) = 0;
		virtual bool PushLuaxMemberTable(LuaxState& state) = 0;
		virtual bool PushLuaxRefTable(LuaxState& state) = 0;

		///
		/// LuaxNativeClassʵֺ֡ͷnativeԴ
		///
		virtual void Retain() = 0;
		virtual void Release() = 0;

	};

	// TODO: ȡҪظɴ
	//class LuaxNativeClassBase
	//{
	//}

	///
	/// Ҫ¶luanative classҪ̳дࡣͨluaʵҪȷüȷԣڶ߳Ҫȷ
	/// ͷš
	///
	template<class TYPE, class BASE = LuaxObject>
	class LuaxNativeClass : public BASE
	{
	public:

		///
		/// userdataΪkeyref tableuserdataһãάuserdataڡ
		/// Ƚmember refʵᱻαͬʵõƵЩʵ壬
		/// luaƵĵgc⡣
		///
		template<class DATATYPE> void LuaxRetain(LuaxState& state, DATATYPE* userdata);

		///
		/// userdataһref tableԳԻuserdata
		/// 
		template<class DATATYPE> void LuaxRelease(LuaxState& state, DATATYPE* userdata);

		///
		/// userdata pushջûгʼmUserdataʼúԪѳʼõ
		/// userdataջһáһnativeȨƽluaƵķ
		///
		bool PushLuaxMemberRef(LuaxState& state, int refID) override;
		bool PushLuaxUserdata(LuaxState& state) override;
		bool PushLuaxMemberTable(LuaxState& state) override;
		bool PushLuaxRefTable(LuaxState& state) override;

		///
		/// Watch dog һnativeáluaVMòṩⲿӿڡ̳д಻ֱʹ
		/// deleteӦʹReleaseͷšһ__gcУû
		/// nativeиͷţҪʹRelease
		/// 
		/// nativeӿڡ
		///
		void Retain() override final;
		void Release() override final;

#if LUAX_PROFILER
		// Զϴʵdeleteռ
		static void operator delete(void* pdead, size_t size);
#endif

	protected:

		LuaxNativeClass();
		virtual ~LuaxNativeClass();

		///
		/// Աùʵref tableáȡ
		///
		void SetLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef, int idx);
		bool PushLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef);
		void ClearLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef);

	private:

		friend class LuaxState;

		static void RegisterLuaxClassShared(LuaxState& state);
		static void RegisterLuaxFactoryClass(LuaxState& state);
		static void RegisterLuaxSingletonClass(LuaxState& state);

		static void SetLuaxClassTableRef(LuaxState& state, int idx);
		static void PushLuaxClassTable(LuaxState& state);

		///
		/// userdataʵstate
		///
		void BindToLua(LuaxState& state);

		//------------------------------------------------------------------------------//

		// 
		static int __tostring       (lua_State*);
		static int _GetClass        (lua_State*);
		static int _GetClassName    (lua_State*);
														   
		// 
		static int __gc             (lua_State*);
		static int _GetRefTable     (lua_State*);
		static int _New             (lua_State*);

#if LUAX_ENABLE_NATIVE_EXTEND
		static int _ExtendFactory   (lua_State*);
		static int _ExtendSingleton (lua_State*);
#endif

		//--------------------------------------------------------------------------------//

		///
		/// class table͵С
		///
		static LuaxStrongRef mClassTable;

		///
		/// ǵsingletonùϵԱ֤ᱻͨref table
		/// ijԱȫڵģֱ_LUAX_STRONGREF_TABLEuserdata
		/// LuaxRetain\LuaxReleasemember refʱ͹ʵͬǴref table
		/// ģtable_LUAX_STRONGREF_TABLE
		///
		static LuaxStrongRef mSingletonRefTable;

		///
		/// ͨuserdataõ:
		///     1: ref table 
		///     2: member table 
		///     3: class table
		///
		LuaxWeakRef mUserdata;

		/// ͨɾ
		LuaxWatchDog mWatchDog;

#if LUAX_PROFILER
		// йܴ˶
		std::unordered_set<LuaxVM*> mRefVMs;
		// գ಻ⲿʹdeleteֱɾӦʹRelease
		bool mSafer;
#endif

	};

#if LUAX_ENABLE_PLAIN_CLASS
	///
	/// lua
	///
	class LuaxPlainClass
	{
	public:

		///
		/// עںͨregistry()עࡣ
		///
		static int registry(lua_State* L);

		LUAX_DECL_METHOD(__tostring);
		LUAX_DECL_METHOD(_Extend);
		LUAX_DECL_METHOD(_New);
		LUAX_DECL_METHOD(_TypeOf);

	};
#endif

}

#endif