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.