CorsixTH engine (the C++ part)
Open source implementation of Theme Hospital
Loading...
Searching...
No Matches
th_lua_internal.h
Go to the documentation of this file.
1/*
2Copyright (c) 2010 Peter "Corsix" Cawley
3
4Permission is hereby granted, free of charge, to any person obtaining a copy of
5this software and associated documentation files (the "Software"), to deal in
6the Software without restriction, including without limitation the rights to
7use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8of the Software, and to permit persons to whom the Software is furnished to do
9so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
21*/
22
23#ifndef CORSIX_TH_TH_LUA_INTERNAL_H_
24#define CORSIX_TH_TH_LUA_INTERNAL_H_
25#include "config.h"
26
27#include <string>
28
29#include "th_lua.h"
30
31enum class lua_metatable {
32 map,
33 palette,
34 sheet,
35 font,
38 layers,
39 anims,
40 anim,
42 surface,
43 bitmap,
44 cursor,
45 lfs_ext,
48 movie,
49 string,
53 line,
54 iso_fs,
56
57 count
58};
59
62 int metatables[static_cast<size_t>(lua_metatable::count)];
64 int top;
65};
66
68 int iUps);
69
70template <typename... Args>
73 lua_pushvalue(pState->L,
74 pState->metatables[static_cast<size_t>(eMetatable1)]);
75 luaT_setclosure(pState, fn, iUps + 1, args...);
76}
77
78template <typename... Args>
80 int iUps, const char* str, Args... args) {
81 lua_pushstring(pState->L, str);
82 luaT_setclosure(pState, fn, iUps + 1, args...);
83}
84
93template <typename... Args>
95 const char* name, Args... args) {
96 luaT_setclosure(pState, fn, 0, args...);
97 lua_setfield(pState->L, -2, name);
98}
99
106template <typename T>
108 public:
114
123 lua_class_binding(const lua_register_state* pState, const char* name,
125 : pState(pState),
126 class_name(name),
127 class_metatable(pState->metatables[static_cast<size_t>(mt)]) {
128 lua_settop(pState->L, pState->top);
129 /* Make metatable the environment for registered functions */
130 lua_pushvalue(pState->L, class_metatable);
132 /* Set the __gc metamethod to C++ destructor */
134 lua_setfield(pState->L, class_metatable, "__gc");
135 /* Set the depersist size */
136 lua_pushinteger(pState->L, sizeof(T));
137 lua_setfield(pState->L, class_metatable, "__depersist_size");
138 /* Create the methods table; call it -> new instance */
139 luaT_pushcclosuretable(pState->L, new_fn, 0);
140 /* Set __class_name on the methods metatable */
141 lua_getmetatable(pState->L, -1);
142 lua_pushstring(pState->L, class_name);
143 lua_setfield(pState->L, -2, "__class_name");
144 lua_pop(pState->L, 1);
145 /* Set __index to the methods table */
146 lua_pushvalue(pState->L, -1);
147 lua_setfield(pState->L, class_metatable, "__index");
148 }
149
156 lua_getmetatable(pState->L, -1);
157 lua_getfield(pState->L, pState->metatables[static_cast<size_t>(super_mt)],
158 "__index");
159 lua_setfield(pState->L, -2, "__index");
160 lua_pop(pState->L, 1);
161 /* Set metatable[1] to super_mt */
162 lua_pushvalue(pState->L, pState->metatables[static_cast<size_t>(super_mt)]);
163 lua_rawseti(pState->L, class_metatable, 1);
164 }
165
172 template <typename V>
173 void add_constant(const char* name, V value) {
174 luaT_push(pState->L, value);
175 lua_setfield(pState->L, -2, name);
176 }
177
185 template <typename... Args>
186 void add_metamethod(lua_CFunction fn, const char* name, Args... args) {
187 luaT_setclosure(pState, fn, 0, args...);
188 lua_setfield(pState->L, class_metatable,
189 std::string("__").append(name).c_str());
190 }
191
199 template <typename... Args>
200 void add_function(lua_CFunction fn, const char* name, Args... args) {
201 add_lua_function(pState, fn, name, args...);
202 }
203
208 lua_setfield(pState->L, pState->main_table, class_name);
209 }
210
211 private:
212 const lua_register_state* pState;
213 const char* class_name;
214 int class_metatable;
215};
216
217#endif
Definition th_lua_internal.h:107
void set_superclass(lua_metatable super_mt)
Definition th_lua_internal.h:155
lua_class_binding & operator=(const lua_class_binding &)=delete
lua_class_binding(const lua_register_state *pState, const char *name, lua_CFunction new_fn, lua_metatable mt)
Definition th_lua_internal.h:123
void add_metamethod(lua_CFunction fn, const char *name, Args... args)
Definition th_lua_internal.h:186
~lua_class_binding()
Definition th_lua_internal.h:207
lua_class_binding operator=(lua_class_binding &&)=delete
lua_class_binding(lua_class_binding &&)=delete
lua_class_binding()=delete
void add_function(lua_CFunction fn, const char *name, Args... args)
Definition th_lua_internal.h:200
lua_class_binding(const lua_class_binding &)=delete
void add_constant(const char *name, V value)
Definition th_lua_internal.h:173
uint32_t mt[N]
Definition random.c:61
Definition th_lua_internal.h:60
int top
Definition th_lua_internal.h:64
lua_State * L
Definition th_lua_internal.h:61
int main_table
Definition th_lua_internal.h:63
int metatables[static_cast< size_t >(lua_metatable::count)]
Definition th_lua_internal.h:62
void luaT_push(lua_State *L, lua_CFunction f)
Definition th_lua.cpp:412
void luaT_pushcclosuretable(lua_State *L, lua_CFunction fn, int n)
Push a C closure as a callable table.
Definition th_lua.cpp:143
void luaT_pushcclosure(lua_State *L, lua_CFunction f, int nups)
Definition th_lua.h:128
const int luaT_environindex
Definition th_lua.h:80
void add_lua_function(const lua_register_state *pState, lua_CFunction fn, const char *name, Args... args)
Definition th_lua_internal.h:94
lua_metatable
Definition th_lua_internal.h:31
void luaT_setclosure(const lua_register_state *pState, lua_CFunction fn, int iUps)
Definition th_lua.cpp:315