summaryrefslogtreecommitdiff
path: root/Source/3rdParty/Luax/luax_class.h
blob: bde999073d062f75a8a636c5de2759707f64414d (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
#ifndef __LUAX_CLASS_H__
#define __LUAX_CLASS_H__

#include <vector>

#include "lua.hpp"
#include "luax_config.h"
#include "luax_state.h"
#include "luax_runtime.h"

namespace Luax
{

#define LUAX_DECL_METHOD(mtd) static int mtd(lua_State*)

	///
	/// RegisterLuaxClass עķͳԱö١ȵclass table
	/// LuaxRegisterInterface עӿڷinterface table
	/// LuaxGetFactoryName ùͬʱעʱעΪsingletonͨʱ
	///
#define LUAX_DECL_FACTORY(type) \
	static void RegisterLuaxClass(LuaxState&);\
	static void RegisterLuaxInterface(LuaxState&);\
	static const char* GetLuaxFactoryName() { return #type; };\
	static const char* GetLuaxClassName() { return #type; };\
	static bool IsLuaxClassSingleton() { return false; };


	///
	/// RegisterLuaxClass עķͳԱö١ȵclass table
	/// LuaxGetSingletonName õ
	///
#define LUAX_DECL_SINGLETON(type) \
	static void RegisterLuaxClass(LuaxState&);  \
	static const char* GetLuaxSingletonName() { return #type; }; \
	static const char* GetLuaxClassName() { return #type; }; \
	static bool IsLuaxClassSingleton() { return true; };


	///
	/// Ҫ¶luaclassҪ̳дࡣͨluaʵҪȷüȷԣڶ߳Ҫȷͷš
	///
	class LuaxClass
	{
	public:

		void Retain();
		void Release();

	protected:

		LuaxClass();
		virtual ~LuaxClass();

		///
		/// userdata pushջûгʼmUserdataʼúԪѳʼõuserdataջ
		///
		bool PushLuaUserdata(LuaxState& state);

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

		///
		/// reftableĹ
		///
		void Ref();

	private:

		friend class LuaxState;

		template<class T> static void RegisterLuaxClass(LuaxState& state);
		template<class T> static void RegisterLuaxFactoryClass(LuaxState& state);
		template<class T> static void RegisterLuaxInterface(LuaxState& state);
		template<class T> static void RegisterLuaxSingletonClass(LuaxState& state);

		///
		/// ȡַҪַֻͨڶϴʵõջϺ;̬ıȡַ֤ü׼ȷ
		/// ҪãʹôݶǴݵַ
		///
		void* operator &();

		///
		/// 󶨵stateǹ
		///
		void BindFactoryToLua(LuaxState& state);

		///
		/// 󶨵stateǵ
		/// 
		void BindSingletonToLua(LuaxState& state);

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

		LuaxStrongRef mInterfaceTable; // interface table 
		LuaxStrongRef mClassTable;     // class table

		///
		/// ü̼߳乲
		///
		int mRC;

		///
		/// ȷֻͨReleaseõsaferֻҪ̳LuaxClass࣬ʹdeleteֱͻᱨ
		///
		bool mSafer; 

	public:

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

		template<class T> LUAX_DECL_METHOD(l_GetClassName);
		template<class T> LUAX_DECL_METHOD(l_GetClassName2);

		LUAX_DECL_METHOD(l_ToString); // __tostring

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

		LUAX_DECL_METHOD(l_GC); // __gc

		//------------------------------------------------------------------------------------------------------------
		// 
		
	};

	///
	/// ڳԱﴴLuaxStateԲм顣
	///
#define LUAX_SETUP(L, params) \
	LuaxRuntime& runtime = LuaxRuntime::Get(); \
	LuaxState& state = runtime[L].state;       \
	if(!state.CheckParams(1, params)) return 0

#define LUAX_STATE(L) \
	LuaxState& state = LuaxRuntime::Get().GetLuaxState(L)

#define LUAX_RUNTIME() \
	LuaxRuntime& runtime = LuaxRuntime::Get()

#include "luax_class.inl"

}

#endif