Added ForEach to RegionPagerBase and RegionPager's API

This commit is contained in:
2016-11-25 18:22:29 +11:00
parent 288b112623
commit 4f9b8695bd
3 changed files with 30 additions and 0 deletions
+24
View File
@@ -24,6 +24,8 @@
#include "region_pager_lua.hpp"
#include "region.hpp"
#include <stdexcept>
//DOCS: These glue functions simply wrap RegionPagerLua's methods
//NOTE: zero indexing is used here, but not in the region API
@@ -197,6 +199,26 @@ static int setOnUnload(lua_State* L) {
return 0;
}
static int forEach(lua_State* L) {
//get the global pager
lua_getglobal(L, REGION_PAGER_NAME);
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, -1));
lua_pop(L, 1);
//run the given closure on each region
pager->ForEach([L](Region& r) -> void {
lua_pushvalue(L, 1); //copy the closure passed
lua_pushlightuserdata(L, &r); //push this region
//call the function, catching any errors
if (lua_pcall(L, 1, 0, 0)) {
throw(std::runtime_error( lua_tostring(L, -1) ));
}
});
return 0;
}
//debugging
static int containerSize(lua_State* L) {
//get the global pager
@@ -229,6 +251,8 @@ static const luaL_Reg regionPagerLib[] = {
{"SetOnCreate",setOnCreate},
{"SetOnUnload",setOnUnload},
{"ForEach", forEach},
//debugging
{"ContainerSize", containerSize},
+4
View File
@@ -101,6 +101,10 @@ void RegionPagerBase::UnloadAll() {
regionList.clear();
}
void RegionPagerBase::ForEach(std::function<void(Region&)> fn) {
std::for_each(regionList.begin(), regionList.end(), fn);
}
std::list<Region>* RegionPagerBase::GetContainer() {
return &regionList;
}
+2
View File
@@ -51,6 +51,8 @@ public:
virtual void UnloadIf(std::function<bool(Region const&)> fn);
virtual void UnloadAll();
virtual void ForEach(std::function<void(Region&)> fn);
//accessors & mutators
std::list<Region>* GetContainer();
protected: