return multiple return values

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

return multiple return values

Markus Klotzbuecher
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: return multiple return values

Nigel Atkinson-2
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
> [hidden email]
> 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
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: return multiple return values

Markus Klotzbuecher
Hi Nigel,

On Sat, Jul 24, 2010 at 01:11:28PM +0200, Nigel Atkinson wrote:

> 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.
...

Thanks a lot! I completely forgot I could push data on the stack
directly. But I have to return non-primitive objects (bound with
luabind) too. Is there an equivalent of 'lua_pushluabindobject' ?

Thanks in advance!
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: return multiple return values

Markus Klotzbuecher
On Sun, Jul 25, 2010 at 10:40:30PM +0200, Markus Klotzbuecher wrote:

> Hi Nigel,
>
> On Sat, Jul 24, 2010 at 01:11:28PM +0200, Nigel Atkinson wrote:
>
> > 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.
> ...
>
> Thanks a lot! I completely forgot I could push data on the stack
> directly. But I have to return non-primitive objects (bound with
> luabind) too. Is there an equivalent of 'lua_pushluabindobject' ?

I just found the answer to my own question: there is in form of the
luabind::object::push() method.

Problem solved, thanks!
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user