nngn
Loading...
Searching...
No Matches
get.h
Go to the documentation of this file.
1
18#ifndef NNGN_LUA_GET_H
19#define NNGN_LUA_GET_H
20
21#include <string_view>
22#include <tuple>
23#include <utility>
24#include <vector>
25
27
28#include "lua.h"
29#include "user.h"
30
31namespace nngn::lua {
32
34template<typename T>
35struct stack_get<std::optional<T>> {
36 static std::optional<T> get(lua_State *L, int i) {
37 switch(lua_type(L, i)) {
38 case LUA_TNONE:
39 case LUA_TNIL:
40 return {};
41 default:
42 return {stack_get<T>::get(L, i)};
43 }
44 }
45};
46
48template<>
49struct stack_get<bool> {
50 static bool get(lua_State *L, int i) {
51 return lua_toboolean(L, i);
52 }
53};
54
56template<std::convertible_to<const void> T>
57struct stack_get<T*> {
58 static T* get(lua_State *L, int i) {
59 return lua_touserdata(L, i);
60 }
61};
62
69template<detail::integer T>
70struct stack_get<T> {
71 static T get(lua_State *L, int i) {
72 return static_cast<T>(lua_tointeger(L, i));
73 }
74};
75
81template<detail::number T>
82struct stack_get<T> {
83 static T get(lua_State *L, int i) {
84 return static_cast<T>(lua_tonumber(L, i));
85 }
86};
87
89template<scoped_enum T>
91struct stack_get<T> {
92 static T get(lua_State *L, int i) {
93 return static_cast<T>(stack_get<std::underlying_type_t<T>>::get(L, i));
94 }
95};
96
98template<>
99struct stack_get<const char*> {
100 static const char *get(lua_State *L, int i) {
101 return lua_tostring(L, i);
102 }
103};
104
109template<>
110struct stack_get<std::string_view> {
111 static std::string_view get(lua_State *L, int i) {
112 std::size_t n = 0;
113 const char *const s = lua_tolstring(L, i, &n);
114 return {s, n};
115 }
116};
117
119template<>
121 static std::string get(lua_State *L, int i) {
122 return std::string{stack_get<std::string_view>::get(L, i)};
123 }
124};
125
127template<>
128struct stack_get<lua_CFunction> {
129 static lua_CFunction get(lua_State *L, int i) {
130 return lua_tocfunction(L, i);
131 }
132};
133
135template<detail::stack_type T>
136struct stack_get<T> {
137 static T get(lua_State *L, int i) {
138 return {L, i};
139 }
140};
141
148template<user_type T>
149struct stack_get<T*> {
150 static T *get(lua_State *L, int i) {
151 return user_data<T*>::get(L, i);
152 }
153};
154
159template<user_type T>
160struct stack_get<T&> {
161 static T &get(lua_State *L, int i) {
162 return *stack_get<T*>::get(L, i);
163 }
164};
165
171template<typename ...Ts>
172requires(... && detail::can_get<Ts>)
173struct stack_get<std::tuple<Ts...>> {
174 static std::tuple<Ts...> get(lua_State *L, int i) {
175 constexpr auto n = static_cast<int>(sizeof...(Ts));
176 return [L, i]<int ...Is>(std::integer_sequence<int, Is...>) {
177 return std::tuple<Ts...>{stack_get<Ts>::get(L, i + Is)...};
178 }(std::make_integer_sequence<int, n>{});
179 }
180};
181
182}
183
184#endif
local class const
Definition animation.lua:7
get_type get(void) const
Pointer to the contained/referenced object.
Definition user.h:124
precision highp int
Definition common.h:14
for i
Definition font.lua:5
local n
Definition dump_lights.lua:5
#define T(f0, f1, f2)
local function bool(title, init, text)
constexpr bool can_get
Whether this library knows how to read a T value from the stack.
Definition lua.h:183
Definition lua_audio.cpp:19
Definition debug.h:13
get
Definition camera.lua:46
static T get(lua_State *L, int i)
Definition get.h:71
static T * get(lua_State *L, int i)
Definition get.h:58
static T & get(lua_State *L, int i)
Definition get.h:161
static bool get(lua_State *L, int i)
Definition get.h:50
static const char * get(lua_State *L, int i)
Definition get.h:100
static lua_CFunction get(lua_State *L, int i)
Definition get.h:129
static std::optional< T > get(lua_State *L, int i)
Definition get.h:36
static std::string get(lua_State *L, int i)
Definition get.h:121
static std::string_view get(lua_State *L, int i)
Definition get.h:111
static std::tuple< Ts... > get(lua_State *L, int i)
Definition get.h:174
Reads a value from the Lua stack.
Definition lua.h:118
std::chrono::seconds s
Definition timing.cpp:6
Operations on light/full user data values.