mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-31 04:25:35 +08:00
Ref T280, clients as callsign map as this is faster
This commit is contained in:
@@ -9,6 +9,8 @@
|
||||
|
||||
#include "clientlist.h"
|
||||
|
||||
using namespace BlackMisc::Aviation;
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace Network
|
||||
@@ -28,5 +30,15 @@ namespace BlackMisc
|
||||
if (this->isEmpty()) { return static_cast<CClient::Capabilities>(CClient::None); }
|
||||
return this->findFirstByCallsign(callsign).getCapabilities();
|
||||
}
|
||||
|
||||
QMap<CCallsign, CClient> CClientList::asMap() const
|
||||
{
|
||||
QMap<CCallsign, CClient> map;
|
||||
for (const CClient &client : *this)
|
||||
{
|
||||
map.insert(client.getCallsign(), client);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "blackmisc/network/client.h"
|
||||
#include "blackmisc/sequence.h"
|
||||
#include "blackmisc/variant.h"
|
||||
#include <QMap>
|
||||
#include <QMetaType>
|
||||
|
||||
namespace BlackMisc
|
||||
@@ -44,6 +45,9 @@ namespace BlackMisc
|
||||
|
||||
//! Capabilities of client for callsign
|
||||
CClient::Capabilities getCapabilities(const Aviation::CCallsign &callsign) const;
|
||||
|
||||
//! As map
|
||||
QMap<Aviation::CCallsign, CClient> asMap() const;
|
||||
};
|
||||
} //namespace
|
||||
} // namespace
|
||||
|
||||
@@ -18,14 +18,19 @@ namespace BlackMisc
|
||||
{
|
||||
CClientList CClientProvider::getClients() const
|
||||
{
|
||||
QReadLocker l(&m_lockClient);
|
||||
return m_clients;
|
||||
QList<CClient> clients;
|
||||
{
|
||||
QReadLocker l(&m_lockClient);
|
||||
clients = m_clients.values();
|
||||
}
|
||||
return CClientList(clients);
|
||||
}
|
||||
|
||||
void CClientProvider::setClients(const CClientList &clients)
|
||||
{
|
||||
const QMap<CCallsign, CClient> map = clients.asMap();
|
||||
QWriteLocker l(&m_lockClient);
|
||||
m_clients = clients;
|
||||
m_clients = map;
|
||||
}
|
||||
|
||||
void CClientProvider::clearClients()
|
||||
@@ -41,20 +46,21 @@ namespace BlackMisc
|
||||
|
||||
CClient CClientProvider::getClientOrDefaultForCallsign(const CCallsign &callsign) const
|
||||
{
|
||||
const CClientList clients(this->getClients());
|
||||
return clients.findFirstByCallsign(callsign);
|
||||
QReadLocker l(&m_lockClient);
|
||||
return m_clients.value(callsign);
|
||||
}
|
||||
|
||||
bool CClientProvider::setOtherClient(const CClient &client)
|
||||
{
|
||||
QWriteLocker l(&m_lockClient);
|
||||
m_clients.replaceOrAddObjectByCallsign(client);
|
||||
m_clients[client.getCallsign()] = client;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CClientProvider::hasClientInfo(const CCallsign &callsign) const
|
||||
{
|
||||
return this->getClients().containsCallsign(callsign);
|
||||
QReadLocker l(&m_lockClient);
|
||||
return m_clients.contains(callsign);
|
||||
}
|
||||
|
||||
bool CClientProvider::addNewClient(const CClient &client)
|
||||
@@ -63,7 +69,7 @@ namespace BlackMisc
|
||||
Q_ASSERT_X(!callsign.isEmpty(), Q_FUNC_INFO, "invalid callsign");
|
||||
if (this->hasClientInfo(callsign)) { return false; }
|
||||
QWriteLocker l(&m_lockClient);
|
||||
m_clients.push_back(client);
|
||||
m_clients[callsign] = client;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -71,16 +77,18 @@ namespace BlackMisc
|
||||
{
|
||||
Q_ASSERT_X(!callsign.isEmpty(), Q_FUNC_INFO, "Missing callsign");
|
||||
int c = 0;
|
||||
if (!this->hasClientInfo(callsign))
|
||||
if (this->hasClientInfo(callsign))
|
||||
{
|
||||
CClient client(callsign);
|
||||
c = client.apply(vm).size();
|
||||
this->addNewClient(client);
|
||||
QWriteLocker l(&m_lockClient);
|
||||
CClient &client = m_clients[callsign];
|
||||
c = client.apply(vm, skipEqualValues).size();
|
||||
}
|
||||
else
|
||||
{
|
||||
CClient client(callsign);
|
||||
c = client.apply(vm).size();
|
||||
QWriteLocker l(&m_lockClient);
|
||||
c = m_clients.applyIfCallsign(callsign, vm, skipEqualValues);
|
||||
m_clients[callsign] = client;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
@@ -88,7 +96,7 @@ namespace BlackMisc
|
||||
int CClientProvider::removeClient(const CCallsign &callsign)
|
||||
{
|
||||
QWriteLocker l(&m_lockClient);
|
||||
return m_clients.removeByCallsign(callsign);
|
||||
return m_clients.remove(callsign);
|
||||
}
|
||||
|
||||
bool CClientProvider::autoAdjustCientGndCapability(const CAircraftSituation &situation)
|
||||
@@ -120,7 +128,7 @@ namespace BlackMisc
|
||||
client.removeCapability(CClient::FsdWithGroundFlag);
|
||||
}
|
||||
QWriteLocker l(&m_lockClient);
|
||||
m_clients.replaceOrAddObjectByCallsign(client);
|
||||
m_clients[callsign] = client;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "blackmisc/aviation/callsignset.h"
|
||||
#include "blackmisc/provider.h"
|
||||
#include <QReadWriteLock>
|
||||
#include <QMap>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
@@ -101,7 +102,7 @@ namespace BlackMisc
|
||||
//! @}
|
||||
|
||||
private:
|
||||
CClientList m_clients;
|
||||
QMap<BlackMisc::Aviation::CCallsign, BlackMisc::Network::CClient> m_clients;
|
||||
mutable QReadWriteLock m_lockClient; //!< lock clients: m_clients
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user