nngn
Loading...
Searching...
No Matches
iter.h
Go to the documentation of this file.
1
41#ifndef NNGN_LUA_ITER_H
42#define NNGN_LUA_ITER_H
43
44#include <iterator>
45
46#include "table.h"
47
48namespace nngn::lua {
49
50namespace detail {
51
52template<typename CRTP, typename T>
53bool operator==(
54 const table_iter_base<CRTP, T> &lhs,
55 const table_iter_base<CRTP, T> &rhs);
56
58template<typename CRTP, typename T>
59class table_iter_base : std::input_iterator_tag {
60public:
61 using difference_type = std::ptrdiff_t;
62 table_iter_base(void) = default;
63 table_iter_base(T *table_) : table{table_} {}
66 friend bool operator==<>(
67 const table_iter_base &lhs,
68 const table_iter_base &rhs);
69protected:
70 CRTP &pre_inc(void) { return static_cast<CRTP&>(++(*this)); }
71 CRTP post_inc(void) { return static_cast<CRTP&>((*this)++); }
72 T *table = nullptr;
73};
74
76template<typename T>
77class table_iter : public table_iter_base<table_iter<T>, T> {
79public:
80 using base::base;
81 using value_type = std::pair<value_view, value_view>;
82 value_type operator*(void) const;
83 table_iter(T *table);
84 table_iter &operator++(void) { return this->pre_inc(); }
85 table_iter operator++(int) { return this->post_inc(); }
86private:
87 friend base;
88 table_iter &next(void);
89 int key_idx = 0;
90};
91
93template<typename T>
94class table_seq_iter : public table_iter_base<table_seq_iter<T>, T> {
96public:
97 using base::base;
98 using value_type = std::pair<lua_Integer, value_view>;
100 value_type operator*(void) const;
101 table_seq_iter &operator++(void) { return this->pre_inc(); }
102 table_seq_iter operator++(int) { return this->post_inc(); }
103private:
104 friend base;
105 table_seq_iter &next(void);
106 lua_Integer i = 0;
107 int value_idx = 0;
108};
109
110template<typename CRTP, typename T>
112 return static_cast<CRTP*>(this)->next();
113}
114
115template<typename CRTP, typename T>
117 auto ret = static_cast<CRTP>(*this);
118 this->next();
119 return ret;
120}
121
122template<typename T>
124 const auto lua = this->table->state();
125 this->key_idx = lua.top() + 1;
126 lua.push(std::tuple{nil, nil});
127 this->next();
128}
130template<typename T>
132 const auto lua = this->table->state();
133 assert(this->key_idx == lua.top() - 1);
134 lua.pop(1);
135 if(!lua_next(lua, this->table->index()))
136 this->table = nullptr;
137 return *this;
138}
139
140template<typename T>
142 const auto lua = this->table->state();
143 return {{lua, this->key_idx}, {lua, this->key_idx + 1}};
144}
145
146template<typename T>
148 const auto lua = this->table->state();
149 lua.push(nil);
150 this->value_idx = lua.top();
151 this->next();
152}
153
154template<typename T>
156 const auto lua = this->table->state();
157 assert(this->value_idx == lua.top());
158 lua.pop(1);
159 if(lua.push((*this->table)[++this->i]).is_nil()) {
160 lua.pop(1);
161 this->table = nullptr;
162 }
163 return *this;
164}
165
166template<typename T>
168 const auto lua = this->table->state();
169 return {this->i, {lua, this->value_idx}};
170}
171
172template<typename CRTP, typename T>
174 const table_iter_base<CRTP, T> &lhs,
175 const table_iter_base<CRTP, T> &rhs)
176{
177 return lhs.table == rhs.table;
178}
179
180template<typename CRTP>
182 return {static_cast<const CRTP*>(this)};
183}
184
185template<typename CRTP>
187 return {static_cast<const CRTP*>(this)};
188}
189
190template<typename CRTP>
192 return {};
193}
194
195template<typename CRTP>
197 return {};
198}
199
200static_assert(std::input_iterator<table_iter<table_view>>);
201static_assert(std::input_iterator<table_seq_iter<table_view>>);
202static_assert(
203 std::sentinel_for<
206static_assert(
207 std::sentinel_for<
210
211}
212
226template<typename T>
227auto ipairs(const T &table) {
228 struct {
229 auto begin(void) const { return this->t.ibegin(); }
230 auto end(void) const { return this->t.iend(); }
231 const T &t;
232 } ret = {table};
233 return ret;
234}
235
236}
237
238#endif
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
table_iter_base & operator++(void)
Definition: iter.h:111
std::ptrdiff_t difference_type
Definition: iter.h:61
T * table
Definition: iter.h:72
table_iter_base operator++(int)
Definition: iter.h:116
table_iter_base(T *table_)
Definition: iter.h:63
lua_next-based table iterator.
Definition: iter.h:77
table_iter & next(void)
Definition: iter.h:131
table_iter operator++(int)
Definition: iter.h:85
table_iter & operator++(void)
Definition: iter.h:84
value_type operator*(void) const
Definition: iter.h:141
int key_idx
Definition: iter.h:89
friend base
Definition: iter.h:87
std::pair< value_view, value_view > value_type
Definition: iter.h:81
table_iter(T *table)
Definition: iter.h:123
ipairs-style table iterator.
Definition: iter.h:94
friend base
Definition: iter.h:104
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
lua_Integer i
Definition: iter.h:106
table_seq_iter(T *table)
Definition: iter.h:147
table_seq_iter & next(void)
Definition: iter.h:155
value_type operator*(void) const
Definition: iter.h:167
T push(auto &&x) const
Pushes a value onto the stack.
Definition: state.h:235
int index(void) const
Lua stack index.
Definition: value.h:28
state_view state(void) const
Definition: value.h:24
assert
Definition: debug.lua:3
end
Definition: entities.lua:9
bool operator==(const table_iter_base< CRTP, T > &lhs, const table_iter_base< CRTP, T > &rhs)
Definition: iter.h:173
Definition: alloc.cpp:100
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
Owning reference to a table on the stack, popped when destroyed.
Definition: table.h:172
Operations on table values.