Re: return multiple return values

Nigel Atkinson <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <1279969888.1982.8.camel@ankh>
Hi Markus,

This is how I do multiple results... I'm not sure if this is in the docs
or where I picked this up from.  This is with version 0.9 of Luabind,
with its rather cool replacement for the "raw" policy.  Personally I
prefer the first function, as it is slightly more clear about the order
the values are returned.

#include <lua.hpp>
#include <luabind/luabind.hpp>
#include <iostream>

void multi_return( lua_State *L, int a, int b, int c )
{
    lua_pushnumber( L, a );
    lua_pushnumber( L, b );
    lua_pushnumber( L, c );

    return;
}

int another_way( lua_State *L, int a, int b, int c )
{
    lua_pushnumber( L, a );
    lua_pushnumber( L, b );
    return c;
}

int main()
{
    using namespace luabind;

    lua_State *L = lua_open();

    luaL_openlibs( L );

    open( L );

    module(L)
    [
        def( "func1", &multi_return ),
        def( "func2", &another_way )
    ];

    const char *code = 
        "print( func1( 3, 5, 7 ) ) "
        "print( func2( 3, 5, 7 ) ) ";

    if( luaL_dostring( L, code ) )
    {
        std::cout << "Oh no! " << lua_tostring( L, -1 ) << std::endl;
    }

    lua_close( L );

    return 0;
}

Easier than building table and then using that table in Lua for results!

Nigel Atkinson.

On Fri, 2010-07-23 at 12:09 +0200, Markus Klotzbuecher wrote:
> Hi,
> 
> Is there a way to return multiple return values from C++ function to
> Lua? Using 'pure_out_value' will not work in my case because the
> number of arguments can change for the same call. The workaround I'm
> using right now is to return a table which stores the arguments.
> 
> Markus
> 
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Sprint
> What will you do first with EVO, the first 4G phone?
> Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
> _______________________________________________
> luabind-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/luabind-user



------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.