a generic c++ lua stack crawler

Sometimes one needs to walk the Lua stack on the C++ side, for example, if passing tables to C++ via the new extended LuaBridge functionality.
For that I’ve started luastackcrawler (see on github). The small test shows that it works at least in principle.
A possible use case – configuration. If the layout of a stack entry, such as a table is known, one can write own wrappers around those to easily access the values. Right now one can iterate through stack and Lua table values, which can be of several types, represented by a boost::variant.

Example, lua:

assert( ArraySize { 1, 2, 5, bla='7' } == 3 )

and the c++ implementation of ArraySize:

int ArraySize(boost::shared_ptr<LuaTable> T)
{
    if (!T)
        return 0;

    return std::count_if(
        T->begin(),
        T->end(),
        [](std::pair<LuaMultiValue,LuaMultiValue> const& entry) {
            return GetType(entry.first) == LuaType::NUMBER;
        });
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.