41#ifndef NNGN_LUA_ITER_H
42#define NNGN_LUA_ITER_H
52template<
typename CRTP,
typename T>
54 const table_iter_base<CRTP, T> &lhs,
55 const table_iter_base<CRTP, T> &rhs);
58template<
typename CRTP,
typename T>
59class table_iter_base : std::input_iterator_tag {
66 friend bool operator==<>(
70 CRTP &
pre_inc(
void) {
return static_cast<CRTP&
>(++(*this)); }
71 CRTP
post_inc(
void) {
return static_cast<CRTP&
>((*this)++); }
77class table_iter :
public table_iter_base<table_iter<T>, T> {
110template<
typename CRTP,
typename T>
112 return static_cast<CRTP*
>(
this)->
next();
115template<
typename CRTP,
typename T>
117 auto ret =
static_cast<CRTP
>(*this);
124 const auto lua = this->
table->state();
125 this->key_idx = lua.top() + 1;
126 lua.push(std::tuple{
nil,
nil});
132 const auto lua = this->
table->state();
133 assert(this->key_idx == lua.top() - 1);
135 if(!lua_next(lua, this->
table->index()))
136 this->
table =
nullptr;
142 const auto lua = this->
table->state();
143 return {{lua, this->key_idx}, {lua, this->key_idx + 1}};
148 const auto lua = this->
table->state();
156 const auto lua = this->
table->state();
157 assert(this->value_idx == lua.top());
159 if(lua.push((*this->table)[++this->i]).is_nil()) {
161 this->
table =
nullptr;
168 const auto lua = this->
table->state();
169 return {this->
i, {lua, this->value_idx}};
172template<
typename CRTP,
typename T>
174 const table_iter_base<CRTP, T> &lhs,
175 const table_iter_base<CRTP, T> &rhs)
177 return lhs.table == rhs.table;
180template<
typename CRTP>
182 return {
static_cast<const CRTP*
>(
this)};
185template<
typename CRTP>
187 return {
static_cast<const CRTP*
>(
this)};
190template<
typename CRTP>
195template<
typename CRTP>
200static_assert(std::input_iterator<table_iter<table_view>>);
201static_assert(std::input_iterator<table_seq_iter<table_view>>);
204 table_iter<table_view>,
205 table_iter<table_view>>);
208 table_seq_iter<table_view>,
209 table_seq_iter<table_view>>);
229 auto begin(
void)
const {
return this->t.ibegin(); }
230 auto end(
void)
const {
return this->t.iend(); }
table_seq_iter< const CRTP > iend(void) const
Sentinel for ibegin.
Definition: iter.h:196
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
table_iter< const CRTP > end(void) const
Sentinel for begin.
Definition: iter.h:191
CRTP base for table iterators.
Definition: iter.h:59
CRTP post_inc(void)
Definition: iter.h:71
CRTP & pre_inc(void)
Definition: iter.h:70
T * table
Definition: iter.h:72
table_iter_base & operator++(void)
Definition: iter.h:111
std::ptrdiff_t difference_type
Definition: iter.h:61
table_iter_base(void)=default
table_iter_base operator++(int)
table_iter_base & operator++(void)
table_iter_base(T *table_)
Definition: iter.h:63
lua_next-based table iterator.
Definition: iter.h:77
value_type operator*(void) const
table_iter & next(void)
Definition: iter.h:131
table_iter operator++(int)
Definition: iter.h:85
table_iter_base< table_iter, T > base
Definition: iter.h:78
table_iter & operator++(void)
Definition: iter.h:84
value_type operator*(void) const
Definition: iter.h:141
int key_idx
Definition: iter.h:89
std::pair< value_view, value_view > value_type
Definition: iter.h:81
ipairs-style table iterator.
Definition: iter.h:94
int value_idx
Definition: iter.h:107
std::pair< lua_Integer, value_view > value_type
Definition: iter.h:98
table_seq_iter & operator++(void)
Definition: iter.h:101
table_seq_iter operator++(int)
Definition: iter.h:102
table_iter_base< table_seq_iter, T > base
Definition: iter.h:95
lua_Integer i
Definition: iter.h:106
table_seq_iter & next(void)
Definition: iter.h:155
value_type operator*(void) const
value_type operator*(void) const
Definition: iter.h:167
table_seq_iter & next(void)
function DEMO text end
Definition: demo0.lua:6
map next
Definition: demo1.lua:80
for i
Definition: font.lua:5
assert
Definition: debug.lua:3
bool operator==(const table_iter_base< CRTP, T > &lhs, const table_iter_base< CRTP, T > &rhs)
Definition: iter.h:173
Definition: lua_audio.cpp:19
constexpr struct nngn::lua::nil_type nil
auto ipairs(const T &table)
Simple wrapper for a table value, returns ipairs-style iterators.
Definition: iter.h:227
constexpr const R * begin(const T< R > &v)
Definition: vec.h:207
Operations on table values.
Owning reference to a table on the stack, popped when destroyed.
Definition: table.h:172