nngn
Loading...
Searching...
No Matches
register.h
Go to the documentation of this file.
1
91#ifndef NNGN_LUA_REGISTER_H
92#define NNGN_LUA_REGISTER_H
93
94#include <vector>
95
96#include "utils/pp.h"
97
98#include "lua.h"
99
100struct lua_State;
101
107#define NNGN_LUA_PROXY(T, ...) \
108 NNGN_OVERLOAD(NNGN_LUA_PROXY, T __VA_OPT__(,) __VA_ARGS__)
109#define NNGN_LUA_PROXY1(T) NNGN_LUA_PROXY2(T, [](auto){})
110#define NNGN_LUA_PROXY2(T, f) \
111 static auto NNGN_ANON() = nngn::lua::static_register{ \
112 [](lua_State *nngn_L) { \
113 [[maybe_unused]] const auto mark = nngn::lua::stack_mark(nngn_L); \
114 (f)(nngn::lua::state_view{nngn_L}.new_user_type<T>()); \
115 }, \
116 };
117
124#define NNGN_LUA_DECLARE_USER_TYPE(T, ...) \
125 NNGN_OVERLOAD(NNGN_LUA_DECLARE_USER_TYPE, T __VA_OPT__(,) __VA_ARGS__)
126#define NNGN_LUA_DECLARE_USER_TYPE1(T) NNGN_LUA_DECLARE_USER_TYPE2(T, #T)
127#define NNGN_LUA_DECLARE_USER_TYPE2(T, N) \
128 template<> \
129 inline constexpr auto nngn::lua::is_user_type<T> = true; \
130 template<> \
131 inline constexpr nngn::fixed_string nngn::lua::metatable_name<T> = N;
132
133namespace nngn::lua {
134
140public:
141 using fn = void(lua_State*);
143 static void register_all(lua_State *L);
145 explicit static_register(fn *f) { registry().push_back(f); }
146private:
147 using V = std::vector<fn*>;
148 static V &registry(void) { static V v; return v; }
149};
150
151}
152
153#endif
Registers a function to be executed when the Lua state is initialized.
Definition: register.h:139
std::vector< fn * > V
Definition: register.h:147
static V & registry(void)
Definition: register.h:148
void(lua_State *) fn
Definition: register.h:141
static_register(fn *f)
Adds a function to the list.
Definition: register.h:145
static void register_all(lua_State *L)
Executes all registered function and removes them from the list.
Definition: register.cpp:5
Definition: alloc.cpp:100
function f()) end
v[1]
Definition: math.lua:19