Lua iterators

I’ve been trying (albeit not very hard) to write a Lua iterator for a std::map. I didn’t think it should be particularly complex. I have a typedef std::map<int, Unit*> UNIT_INDEX; and a structure with UNIT_INDEX m_units;.

What I want to write is, in essence

function allUnits(atCP)
  local iterator =  atCP.m_units:begin()
  return function ()
    if iterator == atCP.m_units:end() then
      return nil
    end
    local unitToReturn = iterator.second
    iterator++
    return unitToReturn
  end
end

I’m using tolua++ for bindings and I’ve tried doing this in both Lua and C++, but Lua and tolua don’t understand the std::map and I’m not sure how to tell it about the std::map<int,Unit*>::iterator – specifically, I’ve tried dozens of combinations of expressing it that either the compiler or tolua just don’t like. I’m fairly sure when you know how to write it its obvious, but it isn’t obvious to me yet.

I may have been taking too direct an approach – trying to expose the data as directly as possible from the class. I think I may need to conceed defeat and just go ahead and write some glue functions and then write the iterator itself in Lua.

2 Comments

Finally! After posting this I decided to give it another 15 minutes of consideration – I’ve probably given it 2-3 hours across as many months.
It finally dawned on me that “end” is indeed a reserved word in Lua, so the normal iterator terminology (‘begin()’ and ‘end()’) was actually the problem. Who knows how many versions ago I had it working.
It’s still not 100%, there is some casting weirdness going on in there but I can work around it and get functional glue functions going.
FWIW: Relevance to TOEs? I’m trying to write test units and automation scripts for brigades and in the past I’ve just looked up a specific brigade by name. Now I want it to test <i>all</i> of them.

Unit tests and automated scripts for testing the aggregatges, are why you’re my favorite Rat. :)

I dream of a day when the “wind tunneling” of airframes won’t be an audit-based activity (as I understand it to be right now, correct me if I’m wrong), but something that a dedicated automated test box will be running every single day on every single patch, and sudden variances flagged for investigation.

Leave a Reply

Name and email address are required. Your email address will not be published.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

You may use these HTML tags and attributes:

<a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <pre> <q cite=""> <s> <strike> <strong> 

%d bloggers like this: