From 5ccbd7fc3de7afed6c06deb93be3967a7eb5fac2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Sun, 23 Dec 2012 17:01:45 +0100
Subject: Replace functors and for loops by boost::lambdas.

Change-Id: I6d2364dc85464f238d95978793f35953a2947799

diff --git a/Swift/Controllers/StatusCache.cpp b/Swift/Controllers/StatusCache.cpp
index e77bd55..1569e26 100644
--- a/Swift/Controllers/StatusCache.cpp
+++ b/Swift/Controllers/StatusCache.cpp
@@ -9,11 +9,15 @@
 #include <boost/algorithm/string.hpp>
 #include <boost/lexical_cast.hpp>
 #include <boost/filesystem/fstream.hpp>
+#include <boost/lambda/lambda.hpp>
+#include <boost/lambda/bind.hpp>
 
 #include <Swiften/Base/foreach.h>
 #include <Swiften/Base/ByteArray.h>
 #include <SwifTools/Application/ApplicationPathProvider.h>
 
+namespace lambda = boost::lambda;
+
 namespace Swift {
 
 static const size_t MAX_ENTRIES = 200;
@@ -45,14 +49,7 @@ void StatusCache::addRecent(const std::string& text, StatusShow::Type type) {
 	if (text.empty()) {
 		return;
 	}
-	for (std::list<PreviousStatus>::iterator i = previousStatuses_.begin(); i != previousStatuses_.end(); ) {
-		if ((*i).first == text && (*i).second == type) {
-			previousStatuses_.erase(i++);
-		}
-		else {
-			++i;
-		}
-	}
+	previousStatuses_.remove_if(lambda::bind(&PreviousStatus::first, lambda::_1) == text && lambda::bind(&PreviousStatus::second, lambda::_1) == type);
 	previousStatuses_.push_front(PreviousStatus(text, type));
 	for (size_t i = previousStatuses_.size(); i > MAX_ENTRIES; i--) {
 		previousStatuses_.pop_back();
diff --git a/Swift/QtUI/QtStatusWidget.cpp b/Swift/QtUI/QtStatusWidget.cpp
index 1899b71..8cc366a 100644
--- a/Swift/QtUI/QtStatusWidget.cpp
+++ b/Swift/QtUI/QtStatusWidget.cpp
@@ -6,6 +6,10 @@
 
 #include "QtStatusWidget.h"
 
+#include <algorithm>
+#include <boost/lambda/lambda.hpp>
+#include <boost/lambda/bind.hpp>
+
 #include <QBoxLayout>
 #include <QComboBox>
 #include <QLabel>
@@ -25,6 +29,8 @@
 #include <Swift/Controllers/StatusUtil.h>
 #include <Swift/Controllers/StatusCache.h>
 
+namespace lambda = boost::lambda;
+
 namespace Swift {
 
 QtStatusWidget::QtStatusWidget(StatusCache* statusCache, QWidget *parent) : QWidget(parent), statusCache_(statusCache), editCursor_(Qt::IBeamCursor), viewCursor_(Qt::PointingHandCursor) {
@@ -144,13 +150,8 @@ void QtStatusWidget::generateList() {
 	}
 	std::vector<StatusCache::PreviousStatus> previousStatuses = statusCache_->getMatches(Q2PSTRING(text), 8);
 	foreach (StatusCache::PreviousStatus savedStatus, previousStatuses) {
-		bool breakEarly = false;
-		foreach (StatusShow::Type type, allTypes_) {
-			if (savedStatus.first.empty() || (savedStatus.second == type && savedStatus.first == statusShowTypeToFriendlyName(type))) {
-				breakEarly = true;
-			}
-		}
-		if (breakEarly) {
+		if (savedStatus.first.empty() || std::find_if(allTypes_.begin(), allTypes_.end(), 
+					savedStatus.second == lambda::_1 && savedStatus.first == lambda::bind(&statusShowTypeToFriendlyName, lambda::_1)) != allTypes_.end()) {
 			continue;
 		}
 		QListWidgetItem* item = new QListWidgetItem(P2QSTRING(savedStatus.first), menu_);
diff --git a/Swiften/EventLoop/EventLoop.cpp b/Swiften/EventLoop/EventLoop.cpp
index afb6858..0f78d96 100644
--- a/Swiften/EventLoop/EventLoop.cpp
+++ b/Swiften/EventLoop/EventLoop.cpp
@@ -12,7 +12,10 @@
 #include <cassert>
 
 #include <Swiften/Base/Log.h>
+#include <boost/lambda/lambda.hpp>
+#include <boost/lambda/bind.hpp>
 
+namespace lambda = boost::lambda;
 
 namespace Swift {
 
@@ -83,7 +86,7 @@ void EventLoop::postEvent(boost::function<void ()> callback, boost::shared_ptr<E
 
 void EventLoop::removeEventsFromOwner(boost::shared_ptr<EventOwner> owner) {
 		boost::lock_guard<boost::mutex> lock(eventsMutex_);
-		events_.remove_if(HasOwner(owner));
+		events_.remove_if(lambda::bind(&Event::owner, lambda::_1) == owner);
 }
 
 }
diff --git a/Swiften/EventLoop/EventLoop.h b/Swiften/EventLoop/EventLoop.h
index 4a602ae..587ba22 100644
--- a/Swiften/EventLoop/EventLoop.h
+++ b/Swiften/EventLoop/EventLoop.h
@@ -35,11 +35,6 @@ namespace Swift {
 			void handleEvent(const Event& event);
 
 		private:
-			struct HasOwner {
-				HasOwner(boost::shared_ptr<EventOwner> owner) : owner(owner) {}
-				bool operator()(const Event& event) const { return event.owner == owner; }
-				boost::shared_ptr<EventOwner> owner;
-			};
 			boost::mutex eventsMutex_;
 			unsigned int nextEventID_;
 			std::list<Event> events_;
diff --git a/Swiften/Network/DomainNameServiceQuery.cpp b/Swiften/Network/DomainNameServiceQuery.cpp
index da1e1ab..5b761c2 100644
--- a/Swiften/Network/DomainNameServiceQuery.cpp
+++ b/Swiften/Network/DomainNameServiceQuery.cpp
@@ -13,8 +13,11 @@
 
 #include <Swiften/Base/RandomGenerator.h>
 #include <boost/numeric/conversion/cast.hpp>
+#include <boost/lambda/lambda.hpp>
+#include <boost/lambda/bind.hpp>
 
 using namespace Swift;
+namespace lambda = boost::lambda;
 
 namespace {
 	struct ResultPriorityComparator {
@@ -22,14 +25,6 @@ namespace {
 			return a.priority < b.priority;
 		}
 	};
-
-	struct GetWeight {
-			GetWeight() {}
-
-			int operator()(const DomainNameServiceQuery::Result& result) {
-				return result.weight + 1 /* easy hack to account for '0' weights getting at least some weight */;
-			}
-	};
 }
 
 namespace Swift {
@@ -46,7 +41,9 @@ void DomainNameServiceQuery::sortResults(std::vector<DomainNameServiceQuery::Res
 		std::vector<DomainNameServiceQuery::Result>::iterator next = std::upper_bound(i, queries.end(), *i, comparator);
 		if (std::distance(i, next) > 1) {
 			std::vector<int> weights;
-			std::transform(i, next, std::back_inserter(weights), GetWeight());
+			std::transform(i, next, std::back_inserter(weights), 
+					/* easy hack to account for '0' weights getting at least some weight */
+					lambda::bind(&Result::weight, lambda::_1) + 1);
 			for (size_t j = 0; j < weights.size() - 1; ++j) {
 				std::vector<int> cumulativeWeights;
 				std::partial_sum(weights.begin() + j, weights.end(), std::back_inserter(cumulativeWeights));
diff --git a/Swiften/Parser/AttributeMap.cpp b/Swiften/Parser/AttributeMap.cpp
index 1aeaf99..d508157 100644
--- a/Swiften/Parser/AttributeMap.cpp
+++ b/Swiften/Parser/AttributeMap.cpp
@@ -8,27 +8,18 @@
 
 #include <algorithm>
 #include <boost/optional.hpp>
+#include <boost/lambda/lambda.hpp>
+#include <boost/lambda/bind.hpp>
 
 using namespace Swift;
-
-namespace {
-	struct AttributeIs {
-		AttributeIs(const Attribute& attribute) : attribute(attribute) {
-		}
-
-		bool operator()(const AttributeMap::Entry& o) const {
-			return o.getAttribute() == attribute;
-		}
-
-		Attribute attribute;
-	};
-}
+namespace lambda = boost::lambda;
 
 AttributeMap::AttributeMap() {
 }
 
 std::string AttributeMap::getAttribute(const std::string& attribute, const std::string& ns) const {
-	AttributeValueMap::const_iterator i = std::find_if(attributes.begin(), attributes.end(), AttributeIs(Attribute(attribute, ns)));
+	AttributeValueMap::const_iterator i = std::find_if(attributes.begin(), attributes.end(), 
+			lambda::bind(&AttributeMap::Entry::getAttribute, lambda::_1) == Attribute(attribute, ns));
 	if (i == attributes.end()) {
 		return "";
 	}
@@ -38,7 +29,8 @@ std::string AttributeMap::getAttribute(const std::string& attribute, const std::
 }
 
 bool AttributeMap::getBoolAttribute(const std::string& attribute, bool defaultValue) const {
-	AttributeValueMap::const_iterator i = std::find_if(attributes.begin(), attributes.end(), AttributeIs(Attribute(attribute, "")));
+	AttributeValueMap::const_iterator i = std::find_if(attributes.begin(), attributes.end(), 
+			lambda::bind(&AttributeMap::Entry::getAttribute, lambda::_1) == Attribute(attribute, ""));
 	if (i == attributes.end()) {
 		return defaultValue;
 	}
@@ -48,7 +40,8 @@ bool AttributeMap::getBoolAttribute(const std::string& attribute, bool defaultVa
 }
 
 boost::optional<std::string> AttributeMap::getAttributeValue(const std::string& attribute) const {
-	AttributeValueMap::const_iterator i = std::find_if(attributes.begin(), attributes.end(), AttributeIs(Attribute(attribute, "")));
+	AttributeValueMap::const_iterator i = std::find_if(attributes.begin(), attributes.end(), 
+			lambda::bind(&AttributeMap::Entry::getAttribute, lambda::_1) == Attribute(attribute, ""));
 	if (i == attributes.end()) {
 		return boost::optional<std::string>();
 	}
diff --git a/Swiften/Parser/Tree/ParserElement.cpp b/Swiften/Parser/Tree/ParserElement.cpp
index 9d9b9b6..e5f8bc8 100644
--- a/Swiften/Parser/Tree/ParserElement.cpp
+++ b/Swiften/Parser/Tree/ParserElement.cpp
@@ -9,8 +9,12 @@
 #include <Swiften/Parser/Tree/NullParserElement.h>
 
 #include <iostream>
+#include <boost/lambda/lambda.hpp>
+#include <boost/lambda/bind.hpp>
 
-namespace Swift{
+namespace lambda = boost::lambda;
+
+namespace Swift {
 
 ParserElement::ParserElement(const std::string& name, const std::string& xmlns, const AttributeMap& attributes) : name_(name), xmlns_(xmlns), attributes_(attributes) {
 }
@@ -28,19 +32,10 @@ void ParserElement::appendCharacterData(const std::string& data) {
 	text_ += data;
 }
 
-struct DoesntMatch {
-	public:
-		DoesntMatch(const std::string& tagName, const std::string& ns) : tagName(tagName), ns(ns) {}
-		bool operator()(ParserElement::ref element) { return element->getName() != tagName || element->getNamespace() != ns; }
-	private:
-		std::string tagName;
-		std::string ns;
-};
-
-
 std::vector<ParserElement::ref> ParserElement::getChildren(const std::string& name, const std::string& xmlns) const {
 	std::vector<ParserElement::ref> result;
-	std::remove_copy_if(children_.begin(), children_.end(), std::back_inserter(result), DoesntMatch(name, xmlns));
+	std::remove_copy_if(children_.begin(), children_.end(), std::back_inserter(result), 
+		lambda::bind(&ParserElement::getName, *lambda::_1) != name || lambda::bind(&ParserElement::getNamespace, *lambda::_1) != xmlns);
 	return result;
 }
 
-- 
cgit v0.10.2-6-g49f6