forked from elonafoobar/elonafoobar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcat.hpp
289 lines (211 loc) · 5.94 KB
/
cat.hpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#pragma once
#include <memory>
#include <unordered_map>
#include <lua.hpp>
#include "filesystem.hpp"
#include "lib/noncopyable.hpp"
#include "optional.hpp"
namespace elona
{
namespace cat
{
using ref = int;
class engine : lib::noncopyable
{
public:
engine() = default;
void initialize();
void load(const fs::path& filepath);
void register_function(const char* name, lua_CFunction);
lua_State* ptr()
{
return L.get();
}
template <typename T, typename... Args>
T call(ref func, Args&&... args)
{
lua_rawgeti(ptr(), LUA_REGISTRYINDEX, func);
using swallow = std::initializer_list<int>;
(void)swallow{(void(push(args)), 0)...};
lua_pcall(ptr(), sizeof...(Args), 1 /* TODO */, 0);
return to_cpp_type<T>(-1);
}
// FIXME: DRY
// -2 is the called function.
// -1 is the first argument, i.e., self.
template <typename T, typename... Args>
T call_method(Args&&... args)
{
using swallow = std::initializer_list<int>;
(void)swallow{(void(push(args)), 0)...};
lua_pcall(ptr(), sizeof...(Args) + 1 /* for self */, 1 /* TODO */, 0);
return to_cpp_type<T>(-1);
}
// FIXME: rename
template <typename T, typename... Args>
T call_with_self(ref self, ref func, Args&&... args)
{
lua_rawgeti(ptr(), LUA_REGISTRYINDEX, func);
lua_rawgeti(ptr(), LUA_REGISTRYINDEX, self);
using swallow = std::initializer_list<int>;
(void)swallow{(void(push(args)), 0)...};
lua_pcall(ptr(), sizeof...(Args) + 1 /* for self */, 1 /* TODO */, 0);
return to_cpp_type<T>(-1);
}
private:
std::unique_ptr<lua_State, decltype(&lua_close)> L{nullptr, lua_close};
void push(int n)
{
lua_pushinteger(ptr(), n);
}
void push(const char* s)
{
lua_pushstring(ptr(), s);
}
void push(const std::string s)
{
lua_pushstring(ptr(), s.c_str());
}
void push(double d)
{
lua_pushnumber(ptr(), d);
}
void push(std::nullptr_t)
{
lua_pushnil(ptr());
}
template <
typename T,
std::enable_if_t<
std::is_same<T, std::nullptr_t>::value,
std::nullptr_t> = nullptr>
T to_cpp_type(int index)
{
(void)index;
return nullptr;
}
#define ELONA_DEFINE_TO_CPP_TYPE(type, function) \
template < \
typename T, \
std::enable_if_t<std::is_same<T, type>::value, std::nullptr_t> = \
nullptr> \
T to_cpp_type(int index) \
{ \
return function(ptr(), index); \
}
ELONA_DEFINE_TO_CPP_TYPE(std::string, luaL_checkstring)
ELONA_DEFINE_TO_CPP_TYPE(int, luaL_checkinteger)
ELONA_DEFINE_TO_CPP_TYPE(double, luaL_checknumber)
#undef ELONA_DEFINE_TO_CPP_TYPE
};
extern engine global;
template <typename T>
struct userdata
{
static void push_new(lua_State* L, T* ptr)
{
auto self = lua_newuserdata(L, sizeof(userdata<T>));
(static_cast<userdata<T>*>(self))->_ptr = ptr;
}
T* ptr() noexcept
{
return _ptr;
}
private:
T* _ptr;
};
template <typename>
struct cat_db_traits;
template <class T>
class cat_db : public lib::noncopyable
{
using traits_type = cat_db_traits<T>;
public:
using id_type = typename traits_type::id_type;
using data_type = typename traits_type::data_type;
using map_type = std::unordered_map<id_type, data_type>;
struct iterator
{
private:
using base_iterator_type = typename map_type::const_iterator;
public:
using value_type = const data_type;
using difference_type = typename base_iterator_type::difference_type;
using pointer = value_type*;
using reference = value_type&;
using iterator_category =
typename base_iterator_type::iterator_category;
iterator(const typename map_type::const_iterator& itr)
: itr(itr)
{
}
reference operator*() const
{
return itr->second;
}
pointer operator->() const
{
return itr.operator->();
}
void operator++()
{
++itr;
}
bool operator!=(const iterator& other) const
{
return itr != other.itr;
}
private:
typename map_type::const_iterator itr;
};
iterator begin() const
{
return iterator{std::begin(storage)};
}
iterator end() const
{
return iterator{std::end(storage)};
}
void initialize()
{
cat::global.load(filesystem::path(u8"data") / traits_type::filename);
auto L = cat::global.ptr();
lua_getglobal(L, traits_type::table_name);
lua_getfield(L, -1, u8"__storage__");
lua_pushnil(L);
while (lua_next(L, -2))
{
static_cast<T&>(*this).define(L);
lua_pop(L, 1);
}
lua_pop(L, 2);
}
optional_ref<data_type> operator[](const id_type& id) const
{
const auto itr = storage.find(id);
if (itr == std::end(storage))
return none;
else
return itr->second;
}
protected:
map_type storage;
};
#define ELONA_CAT_DB_FIELD_INTEGER(name, default_value) \
lua_getfield(L, -1, #name); \
int name = lua_isnil(L, -1) ? (default_value) : luaL_checkinteger(L, -1); \
lua_pop(L, 1);
#define ELONA_CAT_DB_FIELD_STRING(name, default_value) \
lua_getfield(L, -1, #name); \
const char* name = \
lua_isnil(L, -1) ? (default_value) : luaL_checkstring(L, -1); \
lua_pop(L, 1);
#define ELONA_CAT_DB_FIELD_BOOLEAN(name, default_value) \
lua_getfield(L, -1, #name); \
bool name = lua_isnil(L, -1) ? (default_value) : lua_toboolean(L, -1); \
lua_pop(L, 1);
#define ELONA_CAT_DB_FIELD_REF(name) \
lua_getfield(L, -1, #name); \
cat::ref name = luaL_ref(L, LUA_REGISTRYINDEX);
} // namespace cat
} // namespace elona