nngn
Loading...
Searching...
No Matches
iter.h File Reference

Table iteration. More...

#include <iterator>
#include "table.h"
Include dependency graph for iter.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  nngn::lua::detail::table_iter_base< CRTP, T >
 CRTP base for table iterators. More...
 
class  nngn::lua::detail::table_iter< T >
 lua_next-based table iterator. More...
 
class  nngn::lua::detail::table_seq_iter< T >
 ipairs-style table iterator. More...
 

Namespaces

namespace  nngn
 
namespace  nngn::lua
 
namespace  nngn::lua::detail
 

Functions

template<typename CRTP , typename T >
bool nngn::lua::detail::operator== (const table_iter_base< CRTP, T > &lhs, const table_iter_base< CRTP, T > &rhs)
 
template<typename T >
auto nngn::lua::ipairs (const T &table)
 Simple wrapper for a table value, returns ipairs-style iterators.
 

Detailed Description

Table iteration.

Basic input iterators are available for very simple table iteration. Since it uses the Lua stack, this is only suitable for simple range for loops which do not leave values on the stack at the end of each iteration (or other cases, if you know what you are doing). The objects on the stack are automatically managed by the iterators and should not be moved. Their scope is limited to the for loop (or similar construct), (stack) references held after it will dangle.

The stack positions above the key/value can be manipulated freely inside the loop body, including before the iterators are dereferenced. In particular, nested iteration is supported.

Example lua_next-based iteration:

const auto t = lua.create_table();
for(const auto &[k, v] : …) t[k] = v;
for(auto [k, v] : t) use(k, v);
v[1]
Definition: math.lua:19

Example ipairs-style iteration:

const auto t = lua.create_table();
for(const auto &[i, x] : …) t[i] = x;
for(auto [i, x] : ipairs(t)) use(i, x);

Nested example:

for(auto [i, x] : ipairs(t))
for(auto [k, v] : nngn::lua::table_view{x})
ret[static_cast<std::size_t>(i) - 1].emplace_back(k, v);
Non-owning reference to a table on the stack.
Definition: table.h:166