From dadfb44824f2dbfaa82c21a231cc80b47724646e Mon Sep 17 00:00:00 2001 From: Bill Somerville Date: Wed, 15 Apr 2015 19:24:41 +0000 Subject: [PATCH] Abandon using C++ std::unordered_map :( Apple Clang++ doesn't seem cope with the necessary hash template class specializations. So have degraded to Qt QHash. git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@5227 ab8295b8-cf94-4d9e-aec4-7959e3be5d79 --- MessageAggregator.cpp | 9 ++++----- MessageServer.cpp | 28 +++++++++++----------------- qt_helpers.hpp | 18 ------------------ 3 files changed, 15 insertions(+), 40 deletions(-) diff --git a/MessageAggregator.cpp b/MessageAggregator.cpp index 77a22b347..05250e989 100644 --- a/MessageAggregator.cpp +++ b/MessageAggregator.cpp @@ -33,7 +33,6 @@ #include #include -#include #include #include @@ -42,6 +41,7 @@ #include #include #include +#include #include "MessageServer.hpp" #include "NetworkMessage.hpp" @@ -416,9 +416,9 @@ private: auto iter = dock_widgets_.find (id); if (iter != std::end (dock_widgets_)) { - (*iter).second->close (); + (*iter)->close (); + dock_widgets_.erase (iter); } - dock_widgets_.erase (iter); } QStandardItemModel * log_; @@ -429,8 +429,7 @@ private: QTableView * log_table_view_; // maps client id to widgets - using client_map = std::unordered_map; - client_map dock_widgets_; + QHash dock_widgets_; }; #include "MessageAggregator.moc" diff --git a/MessageServer.cpp b/MessageServer.cpp index 7b2ee9240..4dbf3f0de 100644 --- a/MessageServer.cpp +++ b/MessageServer.cpp @@ -1,9 +1,8 @@ #include "MessageServer.hpp" -#include - #include #include +#include #include "NetworkMessage.hpp" #include "qt_helpers.hpp" @@ -50,8 +49,7 @@ public: port_type sender_port_; QDateTime last_activity_; }; - using client_hash = std::unordered_map; // maps id to Client - client_hash clients_; + QHash clients_; // maps id to Client QTimer * clock_; }; @@ -109,7 +107,7 @@ void MessageServer::impl::parse_message (QHostAddress const& sender, port_type s auto id = in.id (); bool new_client {false}; - if (std::end (clients_) == clients_.find (id)) + if (!clients_.contains (id)) { new_client = true; } @@ -196,7 +194,7 @@ void MessageServer::impl::parse_message (QHostAddress const& sender, port_type s if (check_status (in)) { Q_EMIT self_->client_closed (id); - clients_.erase (id); + clients_.remove (id); } break; @@ -209,17 +207,13 @@ void MessageServer::impl::parse_message (QHostAddress const& sender, port_type s void MessageServer::impl::tick () { auto now = QDateTime::currentDateTime (); - for (auto iter = std::begin (clients_); iter != std::end (clients_);) + for (auto iter = std::begin (clients_); iter != std::end (clients_); ++iter) { - if (now > (*iter).second.last_activity_.addSecs (NetworkMessage::pulse)) + if (now > (*iter).last_activity_.addSecs (NetworkMessage::pulse)) { - Q_EMIT self_->clear_decodes ((*iter).first); - Q_EMIT self_->client_closed ((*iter).first); - iter = clients_.erase (iter); - } - else - { - ++iter; + Q_EMIT self_->clear_decodes (iter.key ()); + Q_EMIT self_->client_closed (iter.key ()); + clients_.erase (iter); // safe while iterating as doesn't rehash } } } @@ -288,7 +282,7 @@ void MessageServer::reply (QString const& id, QTime time, qint32 snr, float delt out << time << snr << delta_time << delta_frequency << mode.toUtf8 () << message_text.toUtf8 (); if (m_->check_status (out)) { - m_->writeDatagram (message, (*iter).second.sender_address_, (*iter).second.sender_port_); + m_->writeDatagram (message, iter.value ().sender_address_, (*iter).sender_port_); } } } @@ -302,7 +296,7 @@ void MessageServer::replay (QString const& id) NetworkMessage::Builder out {&message, NetworkMessage::Replay, id}; if (m_->check_status (out)) { - m_->writeDatagram (message, (*iter).second.sender_address_, (*iter).second.sender_port_); + m_->writeDatagram (message, iter.value ().sender_address_, (*iter).sender_port_); } } } diff --git a/qt_helpers.hpp b/qt_helpers.hpp index 3a830183f..1b57acb85 100644 --- a/qt_helpers.hpp +++ b/qt_helpers.hpp @@ -64,24 +64,6 @@ return QString {mo.enumerator (mo.indexOfEnumerator (#ENUM)).valueToKey (m)}; \ } -namespace std -{ - /* - * std::hash specialization for QString so it can be used - * as a key in std::unordered_map - */ - template struct hash; - template<> struct hash - { - typedef QString Key; - typedef uint result_type; - inline uint operator () (const QString &s) const - { - return qHash (s); - } - }; -} - inline void throw_qstring (QString const& qs) {