summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/mlua_metatable.h
blob: eb8ee722d280643d41908e324a7f20dc75f94e97 (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
#ifndef _LUA_METATABLE_H_
#define _LUA_METATABLE_H_

#define LUA_TINTEGER LUA_NUMTAGS + 1
#define LUA_TSTRINGA LUA_NUMTAGS + 2
#define LUA_TSTRINGW LUA_NUMTAGS + 3

struct MTField
{
	size_t offset;
	size_t size;
	int type;

	MTField(size_t offset, size_t size, int type)
		: offset(offset), size(size), type(type) { }
};

template<typename T>
class MT
{
private:
	lua_State *L;

	static const char *name;
	static std::map<std::string, MTField*> fields;

	template<typename R>
	static R GetValue(const T *obj, size_t offset, size_t size)
	{
		R res = NULL;
		memcpy(&res, ((char*)obj) + offset, size);
		return res;
	}

	static void Init(lua_State *L, T **obj)
	{
		luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
		T *udata = (T*)lua_touserdata(L, 1);
		memcpy(*obj, udata, sizeof(T));
	}
	
	static void Free(T **obj)
	{
		*obj = NULL;
	}

	static int lua__new(lua_State *L)
	{
		T *udata = (T*)lua_newuserdata(L, sizeof(T));
		memset(udata, 0, sizeof(T));
		Init(L, &udata);
		if (udata == NULL)
		{
			lua_pushnil(L);
			return 1;
		}
		luaL_setmetatable(L, MT::name);

		return 1;
	}

	static int lua__index(lua_State *L)
	{
		T *obj = (T*)luaL_checkudata(L, 1, MT::name);
		const char *key = lua_tostring(L, 2);

		auto it = fields.find(key);
		if (it == fields.end())
		{
			lua_pushnil(L);
			return 1;
		}

		MTField *field = it->second;
		size_t offset = field->offset;
		size_t size = field->size;
		
		switch (field->type)
		{
		case LUA_TBOOLEAN:
			lua_pushboolean(L, GetValue<bool>(obj, offset, size));
			break;
		case LUA_TINTEGER:
			lua_pushinteger(L, GetValue<long long>(obj, offset, size));
			break;
		case LUA_TNUMBER:
			lua_pushnumber(L, GetValue<double>(obj, offset, size));
			break;
		case LUA_TSTRING:
			lua_pushstring(L, GetValue<char*>(obj, offset, size));
			break;
		case LUA_TSTRINGA:
			lua_pushstring(L, ptrA(mir_utf8encode(GetValue<char*>(obj, offset, size))));
			break;
		case LUA_TSTRINGW:
			lua_pushstring(L, ptrA(mir_utf8encodeW(GetValue<wchar_t*>(obj, offset, size))));
			break;
		case LUA_TLIGHTUSERDATA:
			lua_pushlightuserdata(L, GetValue<void*>(obj, offset, size));
			break;
		default:
			lua_pushnil(L);
		}

		return 1;
	}

	static int lua__gc(lua_State *L)
	{
		T *obj = (T*)luaL_checkudata(L, 1, MT::name);
		MT::Free(&obj);
		return 0;
	}

public:
	MT(lua_State *L, const char *tname) : L(L)
	{
		MT::name = tname;

		lua_pushcfunction(L, lua__new);
		lua_setglobal(L, MT::name);

		luaL_newmetatable(L, MT::name);
		lua_pushcfunction(L, lua__new);
		lua_setfield(L, -2, "__call");
		lua_pushcfunction(L, lua__index);
		lua_setfield(L, -2, "__index");
		lua_pushcfunction(L, lua__gc);
		lua_setfield(L, -2, "__gc");
	}

	template<typename R>
	MT& Field(R T::*M, const char *name, int type, size_t size = sizeof(R))
	{
		size_t offset = reinterpret_cast<size_t>(&(((T*)0)->*M));
		if (type != LUA_TNONE)
			fields[name] = new MTField(offset, size, type);
		return *this;
	}

	MT& Method(lua_CFunction func, const char *name)
	{
		luaL_getmetatable(L, MT::name);
		lua_pushcfunction(L, func);
		lua_setfield(L, -2, name);
		lua_pop(L, 1);
		return *this;
	}

	static void Set(lua_State *L, T *obj)
	{
		if (obj == NULL)
		{
			lua_pushnil(L);
			return;
		}

		T *udata = (T*)lua_newuserdata(L, sizeof(T));
		memcpy(udata, obj, sizeof(T));
		luaL_setmetatable(L, MT::name);
	}
};

template<typename T>
const char *MT<T>::name;

template<typename T>
std::map<std::string, MTField*> MT<T>::fields;

#endif //_LUA_METATABLE_H_