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/Root.cpp | 70 ++++++++++++++++++++++-------------------------------------- 1 file changed, 25 insertions(+), 45 deletions(-) (limited to 'src/Root.cpp') diff --git a/src/Root.cpp b/src/Root.cpp index 3e30d8a07..f773ab06e 100644 --- a/src/Root.cpp +++ b/src/Root.cpp @@ -1,4 +1,4 @@ - + #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "Root.h" @@ -352,22 +352,16 @@ void cRoot::Start(std::unique_ptr a_OverridesRepo) void cRoot::StopServer() { // Kick all players from the server with custom disconnect message - class cPlayerCallback : public cPlayerListCallback - { - AString m_ShutdownMessage; - virtual bool Item(cPlayer * a_Player) + + bool SentDisconnect = false; + cRoot::Get()->ForEachPlayer([&](cPlayer & a_Player) { - a_Player->GetClientHandlePtr()->Kick(m_ShutdownMessage); - m_HasSentDisconnect = true; + a_Player.GetClientHandlePtr()->Kick(m_Server->GetShutdownMessage()); + SentDisconnect = true; return false; } - public: - bool m_HasSentDisconnect; - cPlayerCallback(AString a_ShutdownMessage) : m_ShutdownMessage(a_ShutdownMessage) { m_HasSentDisconnect = false; } - } PlayerCallback(m_Server->GetShutdownMessage()); - - cRoot::Get()->ForEachPlayer(PlayerCallback); - if (PlayerCallback.m_HasSentDisconnect) + ); + if (SentDisconnect) { std::this_thread::sleep_for(std::chrono::seconds(1)); } @@ -584,14 +578,13 @@ cWorld * cRoot::GetWorld(const AString & a_WorldName) -bool cRoot::ForEachWorld(cWorldListCallback & a_Callback) +bool cRoot::ForEachWorld(const cWorldListCallback & a_Callback) { - for (WorldMap::iterator itr = m_WorldsByName.begin(), itr2 = itr; itr != m_WorldsByName.end(); itr = itr2) + for (auto & World : m_WorldsByName) { - ++itr2; - if (itr->second != nullptr) + if (World.second != nullptr) { - if (a_Callback.Item(itr->second)) + if (a_Callback(*World.second)) { return false; } @@ -750,7 +743,7 @@ void cRoot::BroadcastChat(const cCompositeChat & a_Message) -bool cRoot::ForEachPlayer(cPlayerListCallback & a_Callback) +bool cRoot::ForEachPlayer(const cPlayerListCallback & a_Callback) { for (WorldMap::iterator itr = m_WorldsByName.begin(), itr2 = itr; itr != m_WorldsByName.end(); itr = itr2) { @@ -767,20 +760,22 @@ bool cRoot::ForEachPlayer(cPlayerListCallback & a_Callback) -bool cRoot::FindAndDoWithPlayer(const AString & a_PlayerName, cPlayerListCallback & a_Callback) +bool cRoot::FindAndDoWithPlayer(const AString & a_PlayerName, const cPlayerListCallback & a_Callback) { - class cCallback : public cPlayerListCallback + class cCallback { size_t m_BestRating; size_t m_NameLength; const AString m_PlayerName; - virtual bool Item (cPlayer * a_pPlayer) + public: + + bool operator () (cPlayer & a_Player) { - size_t Rating = RateCompareString (m_PlayerName, a_pPlayer->GetName()); + size_t Rating = RateCompareString (m_PlayerName, a_Player.GetName()); if ((Rating > 0) && (Rating >= m_BestRating)) { - m_BestMatch = a_pPlayer->GetName(); + m_BestMatch = a_Player.GetName(); if (Rating > m_BestRating) { m_NumMatches = 0; @@ -795,7 +790,6 @@ bool cRoot::FindAndDoWithPlayer(const AString & a_PlayerName, cPlayerListCallbac return false; } - public: cCallback (const AString & a_CBPlayerName) : m_BestRating(0), m_NameLength(a_CBPlayerName.length()), @@ -820,7 +814,7 @@ bool cRoot::FindAndDoWithPlayer(const AString & a_PlayerName, cPlayerListCallbac -bool cRoot::DoWithPlayerByUUID(const cUUID & a_PlayerUUID, cPlayerListCallback & a_Callback) +bool cRoot::DoWithPlayerByUUID(const cUUID & a_PlayerUUID, const cPlayerListCallback & a_Callback) { for (WorldMap::iterator itr = m_WorldsByName.begin(); itr != m_WorldsByName.end(); ++itr) { @@ -836,7 +830,7 @@ bool cRoot::DoWithPlayerByUUID(const cUUID & a_PlayerUUID, cPlayerListCallback & -bool cRoot::DoWithPlayer(const AString & a_PlayerName, cPlayerListCallback & a_Callback) +bool cRoot::DoWithPlayer(const AString & a_PlayerName, const cPlayerListCallback & a_Callback) { for (auto World : m_WorldsByName) { @@ -1028,25 +1022,11 @@ int cRoot::GetFurnaceFuelBurnTime(const cItem & a_Fuel) AStringVector cRoot::GetPlayerTabCompletionMultiWorld(const AString & a_Text) { AStringVector Results; - class cWorldCallback : public cWorldListCallback - { - public: - cWorldCallback(AStringVector & a_Results, const AString & a_Search) : - m_Results(a_Results), - m_Search(a_Search) + ForEachWorld([&](cWorld & a_World) { - } - - virtual bool Item(cWorld * a_World) override - { - a_World->TabCompleteUserName(m_Search, m_Results); + a_World.TabCompleteUserName(a_Text, Results); return false; } - private: - AStringVector & m_Results; - const AString & m_Search; - } WC(Results, a_Text); - - Get()->ForEachWorld(WC); + ); return Results; } -- cgit v1.2.3