From 602c2ec1b1dbd923b61113dfe9d6f6fffd619159 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Tue, 19 Jun 2012 20:31:21 +0000 Subject: Made ForEach API safer to use, now supports Destroy()-ing objects, too ( http://forum.mc-server.org/showthread.php?tid=434&pid=3513#pid3513 ) git-svn-id: http://mc-server.googlecode.com/svn/trunk@633 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/cChunk.cpp | 15 ++++++++++----- source/cRoot.cpp | 6 ++++-- source/cWorld.cpp | 6 ++++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/source/cChunk.cpp b/source/cChunk.cpp index e185ef851..ff1f7472b 100644 --- a/source/cChunk.cpp +++ b/source/cChunk.cpp @@ -1535,8 +1535,9 @@ void cChunk::RemoveEntity(cEntity * a_Entity) bool cChunk::ForEachEntity(cEntityCallback & a_Callback) { // The entity list is locked by the parent chunkmap's CS - for (cEntityList::iterator itr = m_Entities.begin(); itr != m_Entities.end(); ++itr) + for (cEntityList::iterator itr = m_Entities.begin(), itr2 = itr; itr != m_Entities.end(); itr = itr2) { + ++itr2; if (a_Callback.Item(*itr)) { return false; @@ -1552,8 +1553,9 @@ bool cChunk::ForEachEntity(cEntityCallback & a_Callback) bool cChunk::ForEachChest(cChestCallback & a_Callback) { // The blockentity list is locked by the parent chunkmap's CS - for (cBlockEntityList::iterator itr = m_BlockEntities.begin(); itr != m_BlockEntities.end(); ++itr) + for (cBlockEntityList::iterator itr = m_BlockEntities.begin(), itr2 = itr; itr != m_BlockEntities.end(); itr = itr2) { + ++itr2; if ((*itr)->GetBlockType() != E_BLOCK_CHEST) { continue; @@ -1573,8 +1575,9 @@ bool cChunk::ForEachChest(cChestCallback & a_Callback) bool cChunk::ForEachFurnace(cFurnaceCallback & a_Callback) { // The blockentity list is locked by the parent chunkmap's CS - for (cBlockEntityList::iterator itr = m_BlockEntities.begin(); itr != m_BlockEntities.end(); ++itr) + for (cBlockEntityList::iterator itr = m_BlockEntities.begin(), itr2 = itr; itr != m_BlockEntities.end(); itr = itr2) { + ++itr2; switch ((*itr)->GetBlockType()) { case E_BLOCK_FURNACE: @@ -1602,8 +1605,9 @@ bool cChunk::ForEachFurnace(cFurnaceCallback & a_Callback) bool cChunk::DoWithChestAt(int a_BlockX, int a_BlockY, int a_BlockZ, cChestCallback & a_Callback) { // The blockentity list is locked by the parent chunkmap's CS - for (cBlockEntityList::iterator itr = m_BlockEntities.begin(); itr != m_BlockEntities.end(); ++itr) + for (cBlockEntityList::iterator itr = m_BlockEntities.begin(), itr2 = itr; itr != m_BlockEntities.end(); itr = itr2) { + ++itr2; if (((*itr)->GetPosX() != a_BlockX) || ((*itr)->GetPosY() != a_BlockY) || ((*itr)->GetPosZ() != a_BlockZ)) { continue; @@ -1633,8 +1637,9 @@ bool cChunk::DoWithChestAt(int a_BlockX, int a_BlockY, int a_BlockZ, cChestCallb bool cChunk::DoWithFurnaceAt(int a_BlockX, int a_BlockY, int a_BlockZ, cFurnaceCallback & a_Callback) { // The blockentity list is locked by the parent chunkmap's CS - for (cBlockEntityList::iterator itr = m_BlockEntities.begin(); itr != m_BlockEntities.end(); ++itr) + for (cBlockEntityList::iterator itr = m_BlockEntities.begin(), itr2 = itr; itr != m_BlockEntities.end(); itr = itr2) { + ++itr2; if (((*itr)->GetPosX() != a_BlockX) || ((*itr)->GetPosY() != a_BlockY) || ((*itr)->GetPosZ() != a_BlockZ)) { continue; diff --git a/source/cRoot.cpp b/source/cRoot.cpp index 9feff537d..f4816d79c 100644 --- a/source/cRoot.cpp +++ b/source/cRoot.cpp @@ -289,8 +289,9 @@ cWorld* cRoot::GetWorld( const AString & a_WorldName ) bool cRoot::ForEachWorld(cWorldListCallback & a_Callback) { - for( WorldMap::iterator itr = m_pState->WorldsByName.begin(); itr != m_pState->WorldsByName.end(); ++itr ) + for (WorldMap::iterator itr = m_pState->WorldsByName.begin(), itr2 = itr; itr != m_pState->WorldsByName.end(); itr = itr2) { + ++itr2; if (a_Callback.Item(itr->second)) { return false; @@ -379,8 +380,9 @@ void cRoot::SaveAllChunks(void) bool cRoot::ForEachPlayer(cPlayerListCallback & a_Callback) { - for (WorldMap::iterator itr = m_pState->WorldsByName.begin(); itr != m_pState->WorldsByName.end(); ++itr) + for (WorldMap::iterator itr = m_pState->WorldsByName.begin(), itr2 = itr; itr != m_pState->WorldsByName.end(); itr = itr2) { + ++itr2; if (!itr->second->ForEachPlayer(a_Callback)) { return false; diff --git a/source/cWorld.cpp b/source/cWorld.cpp index b9731d96c..e438760af 100644 --- a/source/cWorld.cpp +++ b/source/cWorld.cpp @@ -1515,8 +1515,9 @@ bool cWorld::ForEachPlayer(cPlayerListCallback & a_Callback) { // Calls the callback for each player in the list cCSLock Lock(m_CSPlayers); - for (cPlayerList::iterator itr = m_Players.begin(); itr != m_Players.end(); ++itr) + for (cPlayerList::iterator itr = m_Players.begin(), itr2 = itr; itr != m_Players.end(); itr = itr2) { + ++itr2; if (a_Callback.Item(*itr)) { return false; @@ -1658,8 +1659,9 @@ void cWorld::MoveEntityToChunk(cEntity * a_Entity, int a_ChunkX, int a_ChunkY, i bool cWorld::ForEachEntity(cEntityCallback & a_Callback) { cCSLock Lock(m_CSEntities); - for (cEntityList::iterator itr = m_AllEntities.begin(); itr != m_AllEntities.end(); ++itr ) + for (cEntityList::iterator itr = m_AllEntities.begin(), itr2 = itr; itr != m_AllEntities.end(); itr = itr2) { + ++itr2; if (a_Callback.Item(*itr)) { return false; -- cgit v1.2.3