nngn
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1
5#ifndef NNGN_LUA_UTILS_H
6#define NNGN_LUA_UTILS_H
7
8#include <lua.hpp>
9
10#include "os/platform.h"
11#include "utils/log.h"
12#include "utils/scoped.h"
13#include "utils/utils.h"
14
15namespace nngn::lua {
16
18inline auto defer_settop(lua_State *L, int i = -1) {
19 return make_scoped(
20 std::bind_front(lua_settop, L, i == -1 ? lua_gettop(L) : i));
21}
22
24class defer_pop {
25public:
27 explicit defer_pop(lua_State *L_, int n_ = 1) : L{L_}, n{n_} {}
28 ~defer_pop(void) { if(this->n) lua_pop(L, n); }
29 void set_n(int n_) { this->n = n_; }
30private:
31 lua_State *L;
32 int n;
33};
34
42inline auto stack_mark(lua_State *L, int extra = 0) {
43 if constexpr(!Platform::debug)
44 return empty{};
45 else
46 return make_scoped([](auto L_, int t0) {
47 if(const int t1 = lua_gettop(L_); t0 != t1)
48 Log::l()
49 << "unexpected stack top: "
50 << t1 << " != " << t0 << '\n';
51 }, L, lua_gettop(L) + extra);
52}
53
54}
55
56#endif
static std::ostream & l()
Definition log.cpp:56
Pops n values from the Lua stack at scope exit.
Definition utils.h:24
void set_n(int n_)
Definition utils.h:29
int n
Definition utils.h:32
~defer_pop(void)
Definition utils.h:28
lua_State * L
Definition utils.h:31
for i
Definition font.lua:5
Definition lua_audio.cpp:19
auto stack_mark(lua_State *L, int extra=0)
Creates a scoped object which verifies the stack top at scope exit.
Definition utils.h:42
auto defer_settop(lua_State *L, int i=-1)
Resets the top of the stack at scope exit.
Definition utils.h:18
empty
Definition utils.h:89
auto make_scoped(F &&f, Ts &&...ts)
Definition scoped.h:44
#define NNGN_MOVE_ONLY(x)
Definition utils.h:39
static constexpr bool debug
Definition platform.h:26