diff --git a/region_pager_api.cpp b/region_pager_api.cpp index 84ab33c..06392f6 100644 --- a/region_pager_api.cpp +++ b/region_pager_api.cpp @@ -24,6 +24,8 @@ #include "region_pager_lua.hpp" #include "region.hpp" +#include + //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(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}, diff --git a/region_pager_base.cpp b/region_pager_base.cpp index 5446c66..3b98508 100644 --- a/region_pager_base.cpp +++ b/region_pager_base.cpp @@ -101,6 +101,10 @@ void RegionPagerBase::UnloadAll() { regionList.clear(); } +void RegionPagerBase::ForEach(std::function fn) { + std::for_each(regionList.begin(), regionList.end(), fn); +} + std::list* RegionPagerBase::GetContainer() { return ®ionList; } \ No newline at end of file diff --git a/region_pager_base.hpp b/region_pager_base.hpp index ef43e52..bfb277b 100644 --- a/region_pager_base.hpp +++ b/region_pager_base.hpp @@ -51,6 +51,8 @@ public: virtual void UnloadIf(std::function fn); virtual void UnloadAll(); + virtual void ForEach(std::function fn); + //accessors & mutators std::list* GetContainer(); protected: