rework ForEachClient to use concepts instead of SFINAE

This commit is contained in:
Lion Kortlepel
2022-11-13 13:35:25 +01:00
parent d7369c3bc5
commit 844b64f5d9
3 changed files with 49 additions and 29 deletions

View File

@@ -546,27 +546,20 @@ sol::table TLuaEngine::StateThreadData::Lua_GetPlayerIdentifiers(int ID) {
sol::table TLuaEngine::StateThreadData::Lua_GetPlayers() {
sol::table Result = mStateView.create_table();
mEngine->Server().ForEachClientWeak([&](std::weak_ptr<TClient> Client) -> bool {
if (!Client.expired()) {
auto locked = Client.lock();
Result[locked->GetID()] = locked->GetName();
}
return true;
mEngine->Server().ForEachClient([&](const auto& Client) {
Result[Client->GetID()] = Client->GetName();
});
return Result;
}
int TLuaEngine::StateThreadData::Lua_GetPlayerIDByName(const std::string& Name) {
int Id = -1;
mEngine->mServer->ForEachClientWeak([&Id, &Name](std::weak_ptr<TClient> Client) -> bool {
if (!Client.expired()) {
auto locked = Client.lock();
if (locked->GetName() == Name) {
Id = locked->GetID();
return false;
}
mEngine->mServer->ForEachClient([&Id, &Name](const auto& Client) -> IterationDecision {
if (Client->GetName() == Name) {
Id = Client->GetID();
return Break;
}
return true;
return Continue;
});
return Id;
}

View File

@@ -203,20 +203,6 @@ void TServer::ForEachClientWeak(const std::function<bool(std::weak_ptr<TClient>)
}
}
void TServer::ForEachClient(const std::function<IterationDecision(const std::shared_ptr<TClient>&)>& Fn) {
decltype(mClients) Clients;
{
ReadLock lock(mClientsMutex);
Clients = mClients;
}
for (auto& Client : Clients) {
auto Decision = Fn(Client);
if (Decision == IterationDecision::Break) {
break;
}
}
}
size_t TServer::ClientCount() const {
ReadLock Lock(mClientsMutex);
return mClients.size();