Iterator class reference
[Base module]
Declaration
#include <QtLua/Iterator>
namespace QtLua {
class Iterator;
};
This class is a member of the QtLua namespace.
This abstract class contains pure virtuals.
Description
This abstract class can be subclassed to implement iterators to traverse user defined objects from both lua and C++ code.
UserData based classes can reimplement the UserData::new_iterator function to return a Ref pointer to an Iterator based class. This allows iteration over user defined objects.
Some Iterator based classes are already defined internally in the QtLua library for iteration over lua tables and other table like UserData based objects.
Iterator based classes are used by ValueBase::iterator and ValueBase::const_iterator classes, this allows iteration on lua tables and UserData based objects from C++:
// code from examples/cpp/value/iterate.cc:32
QtLua::State state;
// New lua table value
state.exec_statements("table = { a = 1, b = 2, c = 3 }");
QtLua::Value table = state.at("table");
// Iterate over lua table from C++ code
for (QtLua::Value::const_iterator i = table.begin(); i != table.end(); i++)
qDebug() << i.key().to_string_p()
<< i.value().to_string_p();
The non-const iterator can be used to modify a lua table:
// code from examples/cpp/value/iterate.cc:56
// Modify lua table from C++ code
for (QtLua::Value::iterator i = table.begin(); i != table.end(); i++)
i.value() = QtLua::Value(&state, "foo");
The lua function each returns a suitable Iterator to iterate over any UserData based object or lua table:
// code from examples/cpp/value/iterate.cc:49
state.openlib(QtLua::QtLuaLib);
// Iterate from lua code
state.exec_statements("for key, value in each(table) do print(key, value) end");
Members
Inherited members
- 24 members inherited from UserData
Types
Functions
- virtual Value get_key() const = 0;
- virtual Value get_value() const = 0;
- virtual ValueRef get_value_ref() = 0;
- virtual bool more() const = 0;
- virtual void next() = 0;
Members detail
Shortcut for Ref smart pointer class to Iterator type provided for convenience
virtual Value get_key() const = 0;
The return value is current entry key
virtual Value get_value() const = 0;
The return value is current entry value
virtual ValueRef get_value_ref() = 0;
The return value is reference to current entry value
The return value is true if more entries are available.
Jump to next entry.
Shortcut for Ref smart pointer class to Iterator type provided for convenience