96#ifndef NNGN_LUA_TABLE_H
97#define NNGN_LUA_TABLE_H
112template<
typename T,
typename ...Ks>
class table_proxy;
117template<
typename CRTP>
133 template<
typename T>
T raw_get(
auto &&k,
T &&def =
T{})
const;
139 void set(
auto &&k,
auto &&
v)
const;
144 template<
typename ...Ts>
void set(std::tuple<Ts...> &&t)
const;
146 template<op_mode mode,
typename T>
148 template<op_mode mode>
151 const CRTP &
crtp(
void)
const {
return static_cast<const CRTP&
>(*this); }
187 int push(
void)
const { lua_pushglobaltable(this->
state());
return 1; }
196template<
typename T,
typename ...Ks>
205 operator V(
void)
const {
return this->
get<V>(); }
210 const std::tuple<Ks...> &
keys(
void)
const {
return this->
m_keys; }
213 template<
typename V> V
get(V &&def = V{})
const;
214 void set(
auto &&
v)
const;
217 auto key(
void)
const {
return std::get<0>(this->
keys()); }
226template<
typename CRTP>
229 return {
static_cast<const CRTP&
>(*this),
FWD(k)};
232template<
typename CRTP>
234 return this->crtp().
state().
len(this->crtp().index());
237template<
typename CRTP>
240 return this->get_common<op_mode::raw>(
FWD(k),
FWD(def));
243template<
typename CRTP>
246 return this->get_common<op_mode::normal>(
FWD(k),
FWD(def));
249template<
typename CRTP>
251 return this->set_common<op_mode::raw>(
FWD(k),
FWD(
v));
254template<
typename CRTP>
256 return this->set_common<op_mode::normal>(
FWD(k),
FWD(
v));
259template<
typename CRTP>
260template<
typename ...Ts>
262 constexpr auto n =
sizeof...(Ts);
263 static_assert(!(
n % 2));
264 [
this, &t]<std::size_t ...I>(std::index_sequence<I...>) {
265 (...,
static_cast<const CRTP*
>(
this)->
set(
266 std::get<2 * I>(
FWD(t)),
267 std::get<2 * I + 1>(
FWD(t))));
268 }(std::make_index_sequence<
n / 2>{});
271template<
typename CRTP>
272template<op_mode mode,
typename T>
274 const auto &crtp =
static_cast<const CRTP&
>(*this);
275 const auto lua = crtp.state();
276 const int i = crtp.begin_op();
283 return lua.template
get<T>(lua.top());
289template<
typename CRTP>
290template<op_mode mode>
292 const auto &crtp =
static_cast<const CRTP&
>(*this);
293 const auto lua = crtp.state();
294 const int i = crtp.begin_op();
300 lua_settable(lua,
i);
306template<
typename T,
typename ...Ks>
311 return {this->m_table, std::tuple_cat(this->m_keys, std::tuple{
FWD(k)})};
314template<
typename T,
typename ...Ks>
317 .template get_field<detail::table_accessor>(this->key());
320template<
typename T,
typename ...Ks>
323 return [&t]<
typename ...Ks1>(std::tuple<Ks1...> &&k) {
328template<
typename T,
typename ...Ks>
331 if constexpr(
sizeof...(Ks) == 1)
332 return this->
table().template get_field<V>(this->key(),
FWD(def));
334 return this->make_sub(this->push_sub()).template
get<V>(
FWD(def));
337template<
typename T,
typename ...Ks>
339 if constexpr(
sizeof...(Ks) == 1)
342 return this->make_sub(this->push_sub()).template
set(
FWD(
v));
345template<
typename T,
typename PT,
typename ...Ks>
350template<
typename T,
typename PT,
typename ...Ks>
376 lua_createtable(*
this, narr, nrec);
377 return {this->
L, lua_gettop(*
this)};
382 using LI = lua_Integer;
383 static_assert(
sizeof...(args) <= std::numeric_limits<LI>::max());
384 constexpr auto n =
static_cast<LI
>(
sizeof...(args));
386 [&ret]<LI ...I>(std::integer_sequence<LI, I...>,
auto &&...args_) {
387 (..., ret.raw_set(I + 1, args_));
388 }(std::make_integer_sequence<LI, n>{},
FWD(args)...);
393template<std::ranges::range T>
396 if constexpr(std::ranges::sized_range<T>)
397 n =
static_cast<int>(std::ranges::size(
FWD(
r)));
401 for(
auto &&x :
FWD(
r))
411 constexpr auto n =
sizeof...(args);
413 [&ret]<std::size_t ...I>(std::index_sequence<I...>,
auto &&t) {
415 FWD(std::get<2 * I>(t)),
416 FWD(std::get<2 * I + 1>(t)))
418 }(std::make_index_sequence<
n / 2>{}, std::forward_as_tuple(
FWD(args)...));
426 static std::vector<T>
get(lua_State *L,
int i) {
428 const auto n = t.
size();
429 std::vector<T> ret = {};
430 ret.reserve(
static_cast<std::size_t
>(
n));
431 for(lua_Integer ti = 1; ti <=
n; ++ti)
432 ret.push_back(t[ti]);
437template<
typename T,
typename ...Ks>
449 std::derived_from<T, detail::table_base_tag>
450 && !std::derived_from<T, value>
451 && !std::same_as<T, global_table>)
Pops n values from the Lua stack at scope exit.
Definition utils.h:24
CRTP base for stack table types.
Definition table.h:118
T raw_get(auto &&k, T &&def=T{}) const
Gets field without meta methods.
Definition table.h:239
T get_field(auto &&k, T &&def=T{}) const
Gets field, with optional default value.
Definition table.h:245
table_seq_iter< const CRTP > iend(void) const
Sentinel for ibegin.
Definition iter.h:196
void raw_set(auto &&k, auto &&v) const
Sets a table field without meta methods.
Definition table.h:250
table_iter< const CRTP > begin(void) const
lua_next-based iteration.
Definition iter.h:181
table_seq_iter< const CRTP > ibegin(void) const
ipairs-style iteration.
Definition iter.h:186
int begin_op(void) const
Definition table.h:152
lua_Integer size(void) const
Definition table.h:233
void set(std::tuple< Ts... > &&t) const
Sets multiple table fields.
Definition table.h:261
const CRTP & crtp(void) const
Definition table.h:151
void set_common(auto &&k, auto &&v) const
Definition table.h:291
T get_common(auto &&k, T &&def=T{}) const
Definition table.h:273
void end_op(int) const
Definition table.h:153
table_proxy< CRTP, std::decay_t< K > > operator[](K &&k) const
Definition table.h:228
void set(auto &&k, auto &&v) const
Sets a table field.
Definition table.h:255
table_iter< const CRTP > end(void) const
Sentinel for begin.
Definition iter.h:191
lua_next-based table iterator.
Definition lua.h:170
ipairs-style table iterator.
Definition lua.h:171
Table interface to the global environment.
Definition table.h:181
~global_table(void)=default
int begin_op(void) const
Definition table.h:190
void end_op(int i) const
Definition table.h:191
int push(void) const
Definition table.h:187
state_view state(void) const
Definition table.h:186
state_view m_state
Definition table.h:192
lua_State * L
Definition state.h:162
table new_user_type(void) const
Registers a new user type and returns its meta table.
Definition table.h:356
global_table globals(void) const
Definition table.h:367
int top(void) const
Definition state.h:109
T len(int i) const
Definition state.h:204
table create_table(void) const
Creates a table and pushes it on the stack.
Definition table.h:371
Expression template for table assignemnts.
Definition table.h:197
table_proxy< T, Ks..., std::decay_t< K > > operator[](K &&k) const
auto make_sub(const auto &t) const
Definition table.h:322
void set(auto &&v) const
Definition table.h:338
const T & table(void) const
Definition table.h:209
std::tuple< Ks... > m_keys
Definition table.h:221
auto push_sub(void) const
Definition table.h:315
table_proxy & operator=(V &&v)
Definition table.h:203
const T & m_table
Definition table.h:220
auto key(void) const
Definition table.h:217
state_view state(void) const
Definition table.h:211
V get(V &&def=V{}) const
Definition table.h:330
table_proxy(const T &t, auto &&k)
Definition table.h:200
int push(void) const
Definition table.h:215
const std::tuple< Ks... > & keys(void) const
Definition table.h:210
Data block allocated for user data objects.
Definition user.h:73
Base, non-owning generic stack value reference.
Definition value.h:18
state_view state(void) const
Definition value.h:24
A built-in type from this library with stack manipulation operations.
Definition lua.h:199
assert
Definition debug.lua:3
for i
Definition font.lua:5
local n
Definition dump_lights.lua:5
local r
Definition gamma.lua:7
Functions for retrieving values from the stack.
constexpr bool is_optional
Definition lua.h:213
@ raw
Definition lua.h:173
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
constexpr type type_from_lua(int t)
Maps LUA_T* values to type.
Definition lua.h:109
table table_from_range(state_view lua, T &&r)
Creates a table array from a range.
Definition table.h:394
constexpr bool is_stack_ref< T >
Definition table.h:452
type
LUA_T* constants as a scoped enumeration.
Definition lua.h:77
table table_map(state_view lua, auto &&...args)
Creates a table with each successive argument pair as key/value.
Definition table.h:410
table table_array(state_view lua, auto &&...args)
Creates a table array with each argument in succession.
Definition table.h:381
constexpr empty metatable_name
Key in the global table where the meta-table for T is stored.
Definition lua.h:156
auto tuple_tail(T &&t)
Definition tuple.h:11
Functions for pushing values onto the stack.
set
Definition camera.lua:47
get
Definition camera.lua:46
v[1]
Definition math.lua:22
#define NNGN_DEFAULT_CONSTRUCT(x)
Definition utils.h:20
#define FWD(...)
Definition utils.h:18
#define NNGN_MOVE_ONLY(x)
Definition utils.h:39
#define NNGN_ANON_DECL(...)
Definition utils.h:53
Internal type used in nested table accesses.
Definition table.h:157
void end_op(int i) const
Definition table.h:160
int begin_op(void) const
Definition table.h:159
Tag to relate all table_base instantiations via inheritance.
Definition lua.h:176
Tag to relate all table_proxy instantiations via inheritance.
Definition lua.h:179
static std::vector< T > get(lua_State *L, int i)
Definition table.h:426
Reads a value from the Lua stack.
Definition lua.h:118
static int push(lua_State *L, const table_proxy< T, Ks... > &t)
Definition table.h:439
Pushes a value onto the Lua stack.
Definition lua.h:120
Non-owning reference to a table on the stack.
Definition table.h:166
table_view(value_view v)
Definition table.h:168
Owning reference to a table on the stack, popped when destroyed.
Definition table.h:172
table_view release(void)
Disowns the current reference and returns a non-owning view to it.
Definition table.h:177
Owning stack value reference.
Definition value.h:48
value_view release(void)
Definition value.h:56
value(const value &)=delete
Operations on generic stack values.