luabind::iterator for arrays

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

luabind::iterator for arrays

Ken Smith-2
I have a Lua table that contains array elements and I want to use
luabind::iterator to visit them in order.  Is this possible?

The input array looks like this.

document =
{
   name = 'simple',
   insert = table.insert,
}

for i = 0,9 do
   document:insert
   {
       name = 'row ' .. tostring(i)
   }
end

I read it with luabind::iterator like this.

for (luabind::iterator i(table), end; i != end; ++i)
{
   // basically just pushing back into an std::list
}

A serialization method renders this to an ostream and the result is this.

document =
{
   name = "simple",
   {
       name = "row 6",
   },
   {
       name = "row 0",
   },
   {
       name = "row 1",
   },
   {
       name = "row 3",
   },
   {
       name = "row 7",
   },
   {
       name = "row 8",
   },
   {
       name = "row 4",
   },
   {
       name = "row 9",
   },
   {
       name = "row 2",
   },
   {
       name = "row 5",
   },
}

luabind::raw_iterator behaves the same.  Is traversing the list in
array order possible with luabind::iterator or should I just use
lua_rawgeti?

  Regards,
  Ken

------------------------------------------------------------------------------
This SF.net email is sponsored by

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: luabind::iterator for arrays

Daniel Wallin
On Thu, Aug 12, 2010 at 6:27 PM, Ken Smith <[hidden email]> wrote:
> I have a Lua table that contains array elements and I want to use
> luabind::iterator to visit them in order.  Is this possible?
...
>
> luabind::raw_iterator behaves the same.  Is traversing the list in
> array order possible with luabind::iterator or should I just use
> lua_rawgeti?

No, I'm afraid not. There should definitely be an iterator that does
that though. Patches are welcome. I was going to suggest doing
something like this in the meantime:

  for (std::size_t i = 1; i != len(table); ++i)
  {
      table[i] ... ;
  }

but it seems there is no len() function either. Again, patches are welcome. ;)

--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user