From 496c337cdfa593654018c171f6a74c28272265b5 Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Fri, 1 Sep 2017 12:04:50 +0100 Subject: Replace ItemCallbacks with lambdas (#3948) --- src/Chunk.cpp | 85 ++++++++++++++++++++++++----------------------------------- 1 file changed, 35 insertions(+), 50 deletions(-) (limited to 'src/Chunk.cpp') diff --git a/src/Chunk.cpp b/src/Chunk.cpp index 78a8461d3..3a81b9ed1 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -1,4 +1,4 @@ - + #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #ifndef _WIN32 @@ -2114,17 +2114,12 @@ bool cChunk::HasEntity(UInt32 a_EntityID) -bool cChunk::ForEachEntity(cEntityCallback & a_Callback) +bool cChunk::ForEachEntity(const cEntityCallback & a_Callback) { // The entity list is locked by the parent chunkmap's CS - for (auto itr = m_Entities.begin(), itr2 = itr; itr != m_Entities.end(); itr = itr2) + for (const auto & Entity : m_Entities) { - ++itr2; - if (!(*itr)->IsTicking()) - { - continue; - } - if (a_Callback.Item(itr->get())) + if (Entity->IsTicking() && a_Callback(*Entity)) { return false; } @@ -2136,23 +2131,22 @@ bool cChunk::ForEachEntity(cEntityCallback & a_Callback) -bool cChunk::ForEachEntityInBox(const cBoundingBox & a_Box, cEntityCallback & a_Callback) +bool cChunk::ForEachEntityInBox(const cBoundingBox & a_Box, const cEntityCallback & a_Callback) { // The entity list is locked by the parent chunkmap's CS - for (auto itr = m_Entities.begin(), itr2 = itr; itr != m_Entities.end(); itr = itr2) + for (const auto & Entity : m_Entities) { - ++itr2; - if (!(*itr)->IsTicking()) + if (!Entity->IsTicking()) { continue; } - cBoundingBox EntBox((*itr)->GetPosition(), (*itr)->GetWidth() / 2, (*itr)->GetHeight()); + cBoundingBox EntBox(Entity->GetPosition(), Entity->GetWidth() / 2, Entity->GetHeight()); if (!EntBox.DoesIntersect(a_Box)) { // The entity is not in the specified box continue; } - if (a_Callback.Item(itr->get())) + if (a_Callback(*Entity)) { return false; } @@ -2164,23 +2158,14 @@ bool cChunk::ForEachEntityInBox(const cBoundingBox & a_Box, cEntityCallback & a_ -bool cChunk::DoWithEntityByID(UInt32 a_EntityID, cEntityCallback & a_Callback, bool & a_CallbackResult) -{ - return DoWithEntityByID(a_EntityID, std::bind(&cEntityCallback::Item, &a_Callback, std::placeholders::_1), a_CallbackResult); -} - - - - - -bool cChunk::DoWithEntityByID(UInt32 a_EntityID, cLambdaEntityCallback a_Callback, bool & a_CallbackResult) +bool cChunk::DoWithEntityByID(UInt32 a_EntityID, const cEntityCallback & a_Callback, bool & a_CallbackResult) { // The entity list is locked by the parent chunkmap's CS for (const auto & Entity : m_Entities) { if ((Entity->GetUniqueID() == a_EntityID) && (Entity->IsTicking())) { - a_CallbackResult = a_Callback(Entity.get()); + a_CallbackResult = a_Callback(*Entity); return true; } } // for itr - m_Entitites[] @@ -2192,7 +2177,7 @@ bool cChunk::DoWithEntityByID(UInt32 a_EntityID, cLambdaEntityCallback a_Callbac template -bool cChunk::GenericForEachBlockEntity(cItemCallback& a_Callback) +bool cChunk::GenericForEachBlockEntity(const std::function & a_Callback) { // The blockentity list is locked by the parent chunkmap's CS for (auto & KeyPair : m_BlockEntities) @@ -2203,7 +2188,7 @@ bool cChunk::GenericForEachBlockEntity(cItemCallback& a_Callback) (IsOneOf(Block->GetBlockType())) ) { - if (a_Callback.Item(static_cast(Block))) + if (a_Callback(*static_cast(Block))) { return false; } @@ -2216,7 +2201,7 @@ bool cChunk::GenericForEachBlockEntity(cItemCallback& a_Callback) -bool cChunk::ForEachBlockEntity(cBlockEntityCallback & a_Callback) +bool cChunk::ForEachBlockEntity(const cBlockEntityCallback & a_Callback) { return GenericForEachBlockEntity(a_Callback); } @@ -2225,7 +2210,7 @@ bool cChunk::ForEachBlockEntity(cBlockEntityCallback & a_Callback) -bool cChunk::ForEachBrewingstand(cBrewingstandCallback & a_Callback) +bool cChunk::ForEachBrewingstand(const cBrewingstandCallback & a_Callback) { return GenericForEachBlockEntity -bool cChunk::GenericDoWithBlockEntityAt(int a_BlockX, int a_BlockY, int a_BlockZ, cItemCallback& a_Callback) +bool cChunk::GenericDoWithBlockEntityAt(int a_BlockX, int a_BlockY, int a_BlockZ, const std::function & a_Callback) { // The blockentity list is locked by the parent chunkmap's CS cBlockEntity * Block = GetBlockEntity(a_BlockX, a_BlockY, a_BlockZ); @@ -2309,14 +2294,14 @@ bool cChunk::GenericDoWithBlockEntityAt(int a_BlockX, int a_BlockY, int a_BlockZ { return false; // Not any of the given tBlocktypes } - return !a_Callback.Item(static_cast(Block)); + return !a_Callback(*static_cast(Block)); } -bool cChunk::DoWithBlockEntityAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBlockEntityCallback & a_Callback) +bool cChunk::DoWithBlockEntityAt(int a_BlockX, int a_BlockY, int a_BlockZ, const cBlockEntityCallback & a_Callback) { return GenericDoWithBlockEntityAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); } @@ -2324,7 +2309,7 @@ bool cChunk::DoWithBlockEntityAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBloc -bool cChunk::DoWithBeaconAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBeaconCallback & a_Callback) +bool cChunk::DoWithBeaconAt(int a_BlockX, int a_BlockY, int a_BlockZ, const cBeaconCallback & a_Callback) { return GenericDoWithBlockEntityAt