From e225b7f8262df48ad4d7094bc295add3007b0649 Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Mon, 11 Sep 2017 22:20:49 +0100 Subject: Replace ItemCallbacks with lambdas (#3993) --- src/Chunk.cpp | 83 ++++++++++++++++++++++++----------------------------------- 1 file changed, 34 insertions(+), 49 deletions(-) (limited to 'src/Chunk.cpp') diff --git a/src/Chunk.cpp b/src/Chunk.cpp index 3effcb690..8289d2a77 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -2114,17 +2114,12 @@ bool cChunk::HasEntity(UInt32 a_EntityID) -bool cChunk::ForEachEntity(cEntityCallback & a_Callback) +bool cChunk::ForEachEntity(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, 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, 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(cFunctionRef 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(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(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, cFunctionRef 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, 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, cBeaconCallback a_Callback) { return GenericDoWithBlockEntityAt