Iterate over std::vector from LUA.

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

Iterate over std::vector from LUA.

Valery-8
Hello,

I would like to iterate over std::vector from LUA. The vector contains my
simple class. I tryed many ways, but no success. My last simple version
returns "No such operator defined". I tryed return_stl_iterator, but it is
not clear for me how to use it, because my VectorOfA is typedef and not an
other class with the member.

Can anybody help?
Thanks,
Valery.

.cpp

class A
{
    int _i;
public:
    A() : _i(0){}
    A(int i) : _i(i){}
    int get() {return _i;}
};
typedef std::vector< A > VectorOfA;

module(L)
    [
        class_<A>("A")
            .def(constructor<>())
            .def(constructor<int>())
            .def("get", &A::get),
        class_<VectorOfA>("VectorOfA")
            .def(constructor<>())
    ];

.lua

function test_iter()
    local x = VectorOfA()
    for i in x do
        print(i:get())
    end
end


------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises
looking to deploy the next generation of Solaris that includes the latest
innovations from Sun and the OpenSource community. Download a copy and
enjoy capabilities such as Networking, Storage and Virtualization.
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Iterate over std::vector from LUA.

Iliya Trendafilov
Hi,

You should use the return_stl_iterator policy
There's an example in the documentation:
http://www.rasterbar.com/products/luabind/docs.html#return-stl-iterator

Ilia


On Thu, Jun 4, 2009 at 10:25 AM, Valery <[hidden email]> wrote:

> Hello,
>
> I would like to iterate over std::vector from LUA. The vector contains my
> simple class. I tryed many ways, but no success. My last simple version
> returns "No such operator defined". I tryed return_stl_iterator, but it is
> not clear for me how to use it, because my VectorOfA is typedef and not an
> other class with the member.
>
> Can anybody help?
> Thanks,
> Valery.
>
> .cpp
>
> class A
> {
>    int _i;
> public:
>    A() : _i(0){}
>    A(int i) : _i(i){}
>    int get() {return _i;}
> };
> typedef std::vector< A > VectorOfA;
>
> module(L)
>    [
>        class_<A>("A")
>            .def(constructor<>())
>            .def(constructor<int>())
>            .def("get", &A::get),
>        class_<VectorOfA>("VectorOfA")
>            .def(constructor<>())
>    ];
>
> .lua
>
> function test_iter()
>    local x = VectorOfA()
>    for i in x do
>        print(i:get())
>    end
> end
>
>
> ------------------------------------------------------------------------------
> OpenSolaris 2009.06 is a cutting edge operating system for enterprises
> looking to deploy the next generation of Solaris that includes the latest
> innovations from Sun and the OpenSource community. Download a copy and
> enjoy capabilities such as Networking, Storage and Virtualization.
> Go to: http://p.sf.net/sfu/opensolaris-get
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>

------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises
looking to deploy the next generation of Solaris that includes the latest
innovations from Sun and the OpenSource community. Download a copy and
enjoy capabilities such as Networking, Storage and Virtualization.
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Iterate over std::vector from LUA.

Valery-8
Ilia,

I read it many times. First it is not clear for me what class A is. There is
no its declaration not in module[]. Second, I would like to use iteration
with typedef declaration (typedef std::vector<> X), but in the sample,
std::vector is a member of class.

Thanks,
Valery.

----- Original Message -----
From: "Ilia Trendafilov" <[hidden email]>
To: <[hidden email]>
Sent: Thursday, June 04, 2009 12:18 PM
Subject: Re: [luabind] Iterate over std::vector from LUA.


Hi,

You should use the return_stl_iterator policy
There's an example in the documentation:
http://www.rasterbar.com/products/luabind/docs.html#return-stl-iterator

Ilia


On Thu, Jun 4, 2009 at 10:25 AM, Valery <[hidden email]> wrote:

> Hello,
>
> I would like to iterate over std::vector from LUA. The vector contains my
> simple class. I tryed many ways, but no success. My last simple version
> returns "No such operator defined". I tryed return_stl_iterator, but it is
> not clear for me how to use it, because my VectorOfA is typedef and not an
> other class with the member.
>
> Can anybody help?
> Thanks,
> Valery.
>
> .cpp
>
> class A
> {
> int _i;
> public:
> A() : _i(0){}
> A(int i) : _i(i){}
> int get() {return _i;}
> };
> typedef std::vector< A > VectorOfA;
>
> module(L)
> [
> class_<A>("A")
> .def(constructor<>())
> .def(constructor<int>())
> .def("get", &A::get),
> class_<VectorOfA>("VectorOfA")
> .def(constructor<>())
> ];
>
> .lua
>
> function test_iter()
> local x = VectorOfA()
> for i in x do
> print(i:get())
> end
> end
>
>
> ------------------------------------------------------------------------------
> OpenSolaris 2009.06 is a cutting edge operating system for enterprises
> looking to deploy the next generation of Solaris that includes the latest
> innovations from Sun and the OpenSource community. Download a copy and
> enjoy capabilities such as Networking, Storage and Virtualization.
> Go to: http://p.sf.net/sfu/opensolaris-get
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>

------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises
looking to deploy the next generation of Solaris that includes the latest
innovations from Sun and the OpenSource community. Download a copy and
enjoy capabilities such as Networking, Storage and Virtualization.
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises
looking to deploy the next generation of Solaris that includes the latest
innovations from Sun and the OpenSource community. Download a copy and
enjoy capabilities such as Networking, Storage and Virtualization.
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Iterate over std::vector from LUA.

James Porter-2
On Thu, Jun 4, 2009 at 6:27 AM, Valery<[hidden email]> wrote:
> I read it many times. First it is not clear for me what class A is. There is
> no its declaration not in module[].

In that example, replace "struct X" with "struct A" and it will be correct.

> Second, I would like to use iteration
> with typedef declaration (typedef std::vector<> X), but in the sample,
> std::vector is a member of class.

Rather than binding an STL container to Lua, you need to make a
function with return_stl_iterator. This means you can't just have Lua
hold an STL container and make it iterable (unless you write a
wrapper). It's not ideal, but a better solution isn't clear to me.

- Jim

------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises
looking to deploy the next generation of Solaris that includes the latest
innovations from Sun and the OpenSource community. Download a copy and
enjoy capabilities such as Networking, Storage and Virtualization.
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user