map->multimap

master
const an teen 2021-12-30 09:44:01 -05:00
parent e3b04af298
commit a59f91bcaf
2 changed files with 9 additions and 8 deletions

View File

@ -1507,10 +1507,10 @@ void MessagePool::saveNewMessage(const QString &nick, const QString &text)
qint64 timestamp = QDateTime::currentMSecsSinceEpoch(); qint64 timestamp = QDateTime::currentMSecsSinceEpoch();
Message* newMessage = new Message (nick, text, timestamp); Message* newMessage = new Message (nick, text, timestamp);
connect (newMessage, SIGNAL(outDated(qint64)), this, SLOT (messageToDelete(qint64))); connect (newMessage, SIGNAL(outDated(qint64)), this, SLOT (messageToDelete(qint64)));
m_messages[timestamp] = newMessage; m_messages.insert({timestamp, newMessage});
} }
const std::map<qint64, Message *>* MessagePool::getMessages() const std::multimap<qint64, Message *>* MessagePool::getMessages()
{ {
m_lastPing = QDateTime::currentMSecsSinceEpoch(); m_lastPing = QDateTime::currentMSecsSinceEpoch();
return &m_messages; return &m_messages;
@ -1523,8 +1523,9 @@ qint64 MessagePool::getLastPing()
void MessagePool::messageToDelete(qint64 timestamp) void MessagePool::messageToDelete(qint64 timestamp)
{ {
auto it = m_messages.find(timestamp); while (m_messages.find(timestamp) != m_messages.end()) {
if (it == m_messages.end()) return; auto it = m_messages.find(timestamp);
m_messages[timestamp]->deleteLater(); it->second->deleteLater();
m_messages.erase(it); m_messages.erase(it);
}
} }

View File

@ -35,12 +35,12 @@ class MessagePool : public QObject
public: public:
MessagePool(QObject *parent = nullptr); MessagePool(QObject *parent = nullptr);
void saveNewMessage(const QString& nick, const QString& text); void saveNewMessage(const QString& nick, const QString& text);
const std::map<qint64, Message*>* getMessages(); const std::multimap<qint64, Message*>* getMessages();
qint64 getLastPing(); qint64 getLastPing();
private: private:
qint64 m_lastPing; qint64 m_lastPing;
std::map<qint64, Message*> m_messages; // timestamp, message std::multimap<qint64, Message*> m_messages; // timestamp, message
public slots: public slots:
void messageToDelete(qint64 timestamp); void messageToDelete(qint64 timestamp);