From 7d2074bc046c38cf6947da77e1b9610c1809217d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Mon, 2 May 2011 22:05:08 +0200 Subject: Add a generic erase() algorithm. diff --git a/Swift/Controllers/Chat/ChatController.cpp b/Swift/Controllers/Chat/ChatController.cpp index eaf3316..d8c9e31 100644 --- a/Swift/Controllers/Chat/ChatController.cpp +++ b/Swift/Controllers/Chat/ChatController.cpp @@ -11,6 +11,7 @@ #include #include +#include #include "Swiften/Avatars/AvatarManager.h" #include "Swiften/Chat/ChatStateNotifier.h" #include "Swiften/Chat/ChatStateTracker.h" @@ -134,13 +135,7 @@ void ChatController::postSendMessage(const std::string& body, boost::shared_ptr< boost::shared_ptr replace = sentStanza->getPayload(); if (replace) { chatWindow_->replaceMessage(body, myLastMessageUIID_, boost::posix_time::microsec_clock::universal_time()); - for (std::map, std::string>::iterator it = unackedStanzas_.begin(); it != unackedStanzas_.end(); ) { - if ((*it).second == myLastMessageUIID_) { - unackedStanzas_.erase(it++); - } else { - ++it; - } - } + eraseIf(unackedStanzas_, PairSecondEquals, std::string>(myLastMessageUIID_)); } else { myLastMessageUIID_ = addMessage(body, QT_TRANSLATE_NOOP("", "me"), true, labelsEnabled_ ? chatWindow_->getSelectedSecurityLabel().getLabel() : boost::shared_ptr(), std::string(avatarManager_->getAvatarPath(selfJID_).string()), boost::posix_time::microsec_clock::universal_time()); } diff --git a/Swiften/Base/Algorithm.h b/Swiften/Base/Algorithm.h new file mode 100644 index 0000000..f0c6b48 --- /dev/null +++ b/Swiften/Base/Algorithm.h @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2011 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + +#include +#include +#include +#include + +namespace Swift { + /* + * Generic erase() + */ + namespace Detail { + struct VectorCategory {}; + struct ListCategory {}; + struct MapCategory {}; + + template + struct ContainerTraits {}; + + template + struct ContainerTraits< std::vector > { + typedef VectorCategory Category; + }; + + template<> + struct ContainerTraits< std::string > { + typedef VectorCategory Category; + }; + + template + struct ContainerTraits< std::list > { + typedef ListCategory Category; + }; + + template + struct ContainerTraits< std::map > { + typedef MapCategory Category; + }; + + template + void eraseImpl(C& c, const V& v, VectorCategory) { + c.erase(std::remove(c.begin(), c.end(), v), c.end()); + } + + template + void eraseImpl(C& c, const V& v, ListCategory) { + c.remove(v); + } + + template + void eraseImpl(C& c, const V& v, MapCategory) { + for (typename C::iterator it = c.begin(); it != c.end(); ) { + if (it->second == v) { + c.erase(it++); + } + else { + ++it; + } + } + } + + template + void eraseIfImpl(C& c, const P& p, MapCategory) { + for (typename C::iterator it = c.begin(); it != c.end(); ) { + if (p(*it)) { + c.erase(it++); + } + else { + ++it; + } + } + } + } + + template + void erase(C& container, const V& value) { + Detail::eraseImpl(container, value, typename Detail::ContainerTraits::Category()); + } + + template + void eraseIf(C& container, const P& predicate) { + Detail::eraseIfImpl(container, predicate, typename Detail::ContainerTraits::Category()); + } + + /* + * Functors + */ + template + class PairSecondEquals { + public: + PairSecondEquals(const V& value) : value(value) { + } + + bool operator()(const std::pair& pair) const { + return pair.second == value; + } + + private: + V value; + }; +} diff --git a/Swiften/Jingle/JingleSessionManager.cpp b/Swiften/Jingle/JingleSessionManager.cpp index 58e90c8..f9a94f3 100644 --- a/Swiften/Jingle/JingleSessionManager.cpp +++ b/Swiften/Jingle/JingleSessionManager.cpp @@ -8,6 +8,7 @@ #include #include #include +#include namespace Swift { @@ -29,7 +30,7 @@ void JingleSessionManager::addIncomingSessionHandler(IncomingJingleSessionHandle } void JingleSessionManager::removeIncomingSessionHandler(IncomingJingleSessionHandler* handler) { - incomingSessionHandlers.erase(std::remove(incomingSessionHandlers.begin(), incomingSessionHandlers.end(), handler), incomingSessionHandlers.end()); + erase(incomingSessionHandlers, handler); } void JingleSessionManager::handleIncomingSession(const JID& from, JingleSessionImpl::ref session, const std::vector& contents) { diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.cpp b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.cpp index 06f175c..5331964 100644 --- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.cpp +++ b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.cpp @@ -15,6 +15,7 @@ #include #include #include +#include namespace Swift { @@ -59,8 +60,7 @@ void BonjourQuerier::addRunningQuery(boost::shared_ptr query) { void BonjourQuerier::removeRunningQuery(boost::shared_ptr query) { { boost::lock_guard lock(runningQueriesMutex); - runningQueries.erase(std::remove( - runningQueries.begin(), runningQueries.end(), query), runningQueries.end()); + erase(runningQueries, query); } } diff --git a/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.cpp b/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.cpp index ff2b03c..1ddeffa 100644 --- a/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.cpp +++ b/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -74,8 +75,7 @@ void FakeDNSSDQuerier::addRunningQuery(boost::shared_ptr query) } void FakeDNSSDQuerier::removeRunningQuery(boost::shared_ptr query) { - runningQueries.erase(std::remove( - runningQueries.begin(), runningQueries.end(), query), runningQueries.end()); + erase(runningQueries, query); } void FakeDNSSDQuerier::addService(const DNSSDServiceID& id) { diff --git a/Swiften/MUC/MUCRegistry.cpp b/Swiften/MUC/MUCRegistry.cpp index 6776be9..f4d061e 100644 --- a/Swiften/MUC/MUCRegistry.cpp +++ b/Swiften/MUC/MUCRegistry.cpp @@ -6,7 +6,7 @@ #include -#include +#include namespace Swift { @@ -22,7 +22,7 @@ void MUCRegistry::addMUC(const JID& j) { } void MUCRegistry::removeMUC(const JID& j) { - mucs.erase(std::remove(mucs.begin(), mucs.end(), j), mucs.end()); + erase(mucs, j); } diff --git a/Swiften/QA/DNSSDTest/DNSSDTest.cpp b/Swiften/QA/DNSSDTest/DNSSDTest.cpp index 65daa6e..2409f66 100644 --- a/Swiften/QA/DNSSDTest/DNSSDTest.cpp +++ b/Swiften/QA/DNSSDTest/DNSSDTest.cpp @@ -9,9 +9,8 @@ #include #include -#include - #include +#include #include #include #include @@ -112,7 +111,7 @@ class DNSSDTest : public CppUnit::TestFixture { void handleServiceRemoved(const DNSSDServiceID& id) { CPPUNIT_ASSERT(std::find(toRemove.begin(), toRemove.end(), id) != toRemove.end()); - toRemove.erase(std::remove(toRemove.begin(), toRemove.end(), id)); + erase(toRemove, id); } void handleRegisterFinished(boost::optional id) { diff --git a/Swiften/Queries/IQRouter.cpp b/Swiften/Queries/IQRouter.cpp index 4e23389..65f9d7f 100644 --- a/Swiften/Queries/IQRouter.cpp +++ b/Swiften/Queries/IQRouter.cpp @@ -6,9 +6,9 @@ #include -#include #include +#include #include #include #include @@ -54,7 +54,7 @@ void IQRouter::handleIQ(boost::shared_ptr iq) { void IQRouter::processPendingRemoves() { foreach(boost::shared_ptr handler, queuedRemoves_) { - handlers_.erase(std::remove(handlers_.begin(), handlers_.end(), handler), handlers_.end()); + erase(handlers_, handler); } queuedRemoves_.clear(); } @@ -76,7 +76,7 @@ void IQRouter::removeHandler(boost::shared_ptr handler) { queuedRemoves_.push_back(handler); } else { - handlers_.erase(std::remove(handlers_.begin(), handlers_.end(), handler), handlers_.end()); + erase(handlers_, handler); } } diff --git a/Swiften/Serializer/PayloadSerializers/FormSerializer.cpp b/Swiften/Serializer/PayloadSerializers/FormSerializer.cpp index eae4c02..7a6bb79 100644 --- a/Swiften/Serializer/PayloadSerializers/FormSerializer.cpp +++ b/Swiften/Serializer/PayloadSerializers/FormSerializer.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -154,7 +155,7 @@ boost::shared_ptr FormSerializer::fieldToXML(boost::shared_ptr element) const { std::string unRdText(text); - unRdText.erase(std::remove(unRdText.begin(), unRdText.end(), '\r'), unRdText.end()); + erase(unRdText, '\r'); std::vector lines = String::split(unRdText, '\n'); foreach (std::string line, lines) { boost::shared_ptr lineElement(new XMLElement(elementName)); diff --git a/Swiften/Server/ServerStanzaRouter.cpp b/Swiften/Server/ServerStanzaRouter.cpp index 7cd8ed3..1725359 100644 --- a/Swiften/Server/ServerStanzaRouter.cpp +++ b/Swiften/Server/ServerStanzaRouter.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -67,7 +68,7 @@ void ServerStanzaRouter::addClientSession(ServerSession* clientSession) { } void ServerStanzaRouter::removeClientSession(ServerSession* clientSession) { - clientSessions_.erase(std::remove(clientSessions_.begin(), clientSessions_.end(), clientSession), clientSessions_.end()); + erase(clientSessions_, clientSession); } } diff --git a/Swiften/StringCodecs/Base64.cpp b/Swiften/StringCodecs/Base64.cpp index 214a425..a77fc93 100644 --- a/Swiften/StringCodecs/Base64.cpp +++ b/Swiften/StringCodecs/Base64.cpp @@ -5,9 +5,11 @@ */ #include + #include #include +#include namespace Swift { @@ -48,7 +50,7 @@ std::string Base64::encode(const ByteArray &s) { ByteArray Base64::decode(const std::string& input) { std::string inputWithoutNewlines(input); - inputWithoutNewlines.erase(std::remove(inputWithoutNewlines.begin(), inputWithoutNewlines.end(), '\n'), inputWithoutNewlines.end()); + erase(inputWithoutNewlines, '\n'); const std::string& s = inputWithoutNewlines; ByteArray p; -- cgit v0.10.2-6-g49f6