diff options
author | Remko Tronçon <git@el-tramo.be> | 2012-12-23 16:01:45 (GMT) |
---|---|---|
committer | Swift Review <review@swift.im> | 2012-12-23 16:48:15 (GMT) |
commit | 5ccbd7fc3de7afed6c06deb93be3967a7eb5fac2 (patch) | |
tree | d57334bf18662d6753a2cb14bff3b192ff3e7a76 /Swiften | |
parent | fe5b5e25eadbfe9f8bdaa152b987273ab77d4711 (diff) | |
download | swift-5ccbd7fc3de7afed6c06deb93be3967a7eb5fac2.zip swift-5ccbd7fc3de7afed6c06deb93be3967a7eb5fac2.tar.bz2 |
Replace functors and for loops by boost::lambdas.
Change-Id: I6d2364dc85464f238d95978793f35953a2947799
Diffstat (limited to 'Swiften')
-rw-r--r-- | Swiften/EventLoop/EventLoop.cpp | 5 | ||||
-rw-r--r-- | Swiften/EventLoop/EventLoop.h | 5 | ||||
-rw-r--r-- | Swiften/Network/DomainNameServiceQuery.cpp | 15 | ||||
-rw-r--r-- | Swiften/Parser/AttributeMap.cpp | 25 | ||||
-rw-r--r-- | Swiften/Parser/Tree/ParserElement.cpp | 19 |
5 files changed, 26 insertions, 43 deletions
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; } |