From e791eb9ca334e7746bc0735cebcda09e30fa65df Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> Date: Sat, 22 Jun 2013 17:53:39 +0900 Subject: [PATCH] Cleanup a2functional.h Remove mem_fun_sh in favor of std::mem_fun. Remove unused functions. Use std::mem_fn instead of mem_fun_sh --- src/DHTBucket.cc | 3 +- src/DHTTaskExecutor.cc | 2 +- src/DefaultBtMessageDispatcher.cc | 2 +- src/DefaultBtRequestFactory.cc | 4 +- src/DownloadContext.cc | 4 +- src/Metalink2RequestGroup.cc | 2 +- src/MetalinkEntry.cc | 2 +- src/MetalinkParserController.cc | 6 +- src/MultiDiskAdaptor.cc | 4 +- src/RequestGroupMan.cc | 2 +- src/a2functional.h | 123 ------------------------------ test/a2functionalTest.cc | 48 ------------ 12 files changed, 17 insertions(+), 185 deletions(-) diff --git a/src/DHTBucket.cc b/src/DHTBucket.cc index 16292ea4..f6433bc7 100644 --- a/src/DHTBucket.cc +++ b/src/DHTBucket.cc @@ -222,7 +222,8 @@ void DHTBucket::getGoodNodes { goodNodes.insert(goodNodes.end(), nodes_.begin(), nodes_.end()); goodNodes.erase(std::remove_if(goodNodes.begin(), goodNodes.end(), - mem_fun_sh(&DHTNode::isBad)), goodNodes.end()); + std::mem_fn(&DHTNode::isBad)), + goodNodes.end()); } std::shared_ptr<DHTNode> DHTBucket::getNode(const unsigned char* nodeID, const std::string& ipaddr, uint16_t port) const diff --git a/src/DHTTaskExecutor.cc b/src/DHTTaskExecutor.cc index 9179dbe4..afe5a889 100644 --- a/src/DHTTaskExecutor.cc +++ b/src/DHTTaskExecutor.cc @@ -53,7 +53,7 @@ DHTTaskExecutor::~DHTTaskExecutor() {} void DHTTaskExecutor::update() { execTasks_.erase(std::remove_if(execTasks_.begin(), execTasks_.end(), - mem_fun_sh(&DHTTask::finished)), + std::mem_fn(&DHTTask::finished)), execTasks_.end()); int r; if(static_cast<size_t>(numConcurrent_) > execTasks_.size()) { diff --git a/src/DefaultBtMessageDispatcher.cc b/src/DefaultBtMessageDispatcher.cc index ac7d4d8a..9165ec9a 100644 --- a/src/DefaultBtMessageDispatcher.cc +++ b/src/DefaultBtMessageDispatcher.cc @@ -408,7 +408,7 @@ void DefaultBtMessageDispatcher::addOutstandingRequest size_t DefaultBtMessageDispatcher::countOutstandingUpload() { return std::count_if(messageQueue_.begin(), messageQueue_.end(), - mem_fun_sh(&BtMessage::isUploading)); + std::mem_fn(&BtMessage::isUploading)); } void DefaultBtMessageDispatcher::setPeer(const std::shared_ptr<Peer>& peer) diff --git a/src/DefaultBtRequestFactory.cc b/src/DefaultBtRequestFactory.cc index 337c740d..eb797ad2 100644 --- a/src/DefaultBtRequestFactory.cc +++ b/src/DefaultBtRequestFactory.cc @@ -86,7 +86,7 @@ void DefaultBtRequestFactory::removeCompletedPiece() { std::for_each(pieces_.begin(), pieces_.end(), AbortCompletedPieceRequest(dispatcher_)); pieces_.erase(std::remove_if(pieces_.begin(), pieces_.end(), - mem_fun_sh(&Piece::pieceComplete)), + std::mem_fn(&Piece::pieceComplete)), pieces_.end()); } @@ -259,7 +259,7 @@ void DefaultBtRequestFactory::getTargetPieceIndexes (std::vector<size_t>& indexes) const { std::transform(pieces_.begin(), pieces_.end(), std::back_inserter(indexes), - mem_fun_sh(&Piece::getIndex)); + std::mem_fn(&Piece::getIndex)); } void DefaultBtRequestFactory::setPieceStorage diff --git a/src/DownloadContext.cc b/src/DownloadContext.cc index 7bb307e7..7fba6d55 100644 --- a/src/DownloadContext.cc +++ b/src/DownloadContext.cc @@ -125,9 +125,11 @@ void DownloadContext::setFilePathWithIndex void DownloadContext::setFileFilter(SegList<int>& sgl) { + using namespace std::placeholders; + if(!sgl.hasNext() || fileEntries_.size() == 1) { std::for_each(fileEntries_.begin(), fileEntries_.end(), - std::bind2nd(mem_fun_sh(&FileEntry::setRequested), true)); + std::bind(&FileEntry::setRequested, _1, true)); return; } assert(sgl.peek() >= 1); diff --git a/src/Metalink2RequestGroup.cc b/src/Metalink2RequestGroup.cc index 696f7c85..0dd594f9 100644 --- a/src/Metalink2RequestGroup.cc +++ b/src/Metalink2RequestGroup.cc @@ -201,7 +201,7 @@ Metalink2RequestGroup::createRequestGroup } } std::for_each(selectedEntries.begin(), selectedEntries.end(), - mem_fun_sh(&MetalinkEntry::reorderMetaurlsByPriority)); + std::mem_fn(&MetalinkEntry::reorderMetaurlsByPriority)); std::vector<std::pair<std::string, std::vector<std::shared_ptr<MetalinkEntry> > > > entryGroups; metalink::groupEntryByMetaurlName(entryGroups, selectedEntries); diff --git a/src/MetalinkEntry.cc b/src/MetalinkEntry.cc index 2ebcb144..86e47344 100644 --- a/src/MetalinkEntry.cc +++ b/src/MetalinkEntry.cc @@ -196,7 +196,7 @@ void MetalinkEntry::toFileEntry { std::transform(metalinkEntries.begin(), metalinkEntries.end(), std::back_inserter(fileEntries), - mem_fun_sh(&MetalinkEntry::getFile)); + std::mem_fn(&MetalinkEntry::getFile)); } void MetalinkEntry::setSignature(const std::shared_ptr<Signature>& signature) diff --git a/src/MetalinkParserController.cc b/src/MetalinkParserController.cc index 187e6cc1..00ddbbb9 100644 --- a/src/MetalinkParserController.cc +++ b/src/MetalinkParserController.cc @@ -493,12 +493,12 @@ void MetalinkParserController::commitChunkChecksumTransaction() if(!tEntry_->chunkChecksum || MessageDigest::isStronger(tChunkChecksum_->getHashType(), tEntry_->chunkChecksum->getHashType())) { - std::sort(tempChunkChecksums_.begin(), tempChunkChecksums_.end(), - Ascend1st<std::pair<size_t, std::string> >()); + std::sort(tempChunkChecksums_.begin(), tempChunkChecksums_.end()); std::vector<std::string> pieceHashes; std::transform(tempChunkChecksums_.begin(), tempChunkChecksums_.end(), std::back_inserter(pieceHashes), - select2nd<std::pair<size_t, std::string> >()); + [](const std::pair<size_t, std::string>& p) + { return p.second; }); tChunkChecksum_->setPieceHashes(pieceHashes); tEntry_->chunkChecksum = tChunkChecksum_; } diff --git a/src/MultiDiskAdaptor.cc b/src/MultiDiskAdaptor.cc index 4bdbab03..7e537be2 100644 --- a/src/MultiDiskAdaptor.cc +++ b/src/MultiDiskAdaptor.cc @@ -300,7 +300,7 @@ void MultiDiskAdaptor::openExistingFile() void MultiDiskAdaptor::closeFile() { std::for_each(diskWriterEntries_.begin(), diskWriterEntries_.end(), - mem_fun_sh(&DiskWriterEntry::closeFile)); + std::mem_fn(&DiskWriterEntry::closeFile)); } namespace { @@ -520,7 +520,7 @@ void MultiDiskAdaptor::writeCache(const WrDiskCacheEntry* entry) bool MultiDiskAdaptor::fileExists() { return std::find_if(getFileEntries().begin(), getFileEntries().end(), - mem_fun_sh(&FileEntry::exists)) != + std::mem_fn(&FileEntry::exists)) != getFileEntries().end(); } diff --git a/src/RequestGroupMan.cc b/src/RequestGroupMan.cc index c4b3c683..437cc69b 100644 --- a/src/RequestGroupMan.cc +++ b/src/RequestGroupMan.cc @@ -801,7 +801,7 @@ bool RequestGroupMan::isSameFileBeingDownloaded(RequestGroup* requestGroup) cons rg->getDownloadContext()->getFileEntries(); std::transform(entries.begin(), entries.end(), std::back_inserter(files), - mem_fun_sh(&FileEntry::getPath)); + std::mem_fn(&FileEntry::getPath)); } } std::sort(files.begin(), files.end()); diff --git a/src/a2functional.h b/src/a2functional.h index 871db2de..34b0fda5 100644 --- a/src/a2functional.h +++ b/src/a2functional.h @@ -46,129 +46,6 @@ namespace aria2 { -// mem_fun_t for SharedHandle -template <class ReturnType, typename ClassType> -class mem_fun_sh_t:public std::unary_function< std::shared_ptr<ClassType>, ReturnType> -{ -private: - ReturnType (ClassType::*f)(); - -public: - mem_fun_sh_t(ReturnType (ClassType::*f)()):f(f) {} - - ReturnType operator()(const std::shared_ptr<ClassType>& x) const - { - return (x.get()->*f)(); - } -}; - -// const_mem_fun_t for SharedHandle -template <class ReturnType, typename ClassType> -class const_mem_fun_sh_t:public std::unary_function< std::shared_ptr<ClassType>, ReturnType> -{ -private: - ReturnType (ClassType::*f)() const; - -public: - const_mem_fun_sh_t(ReturnType (ClassType::*f)() const):f(f) {} - - ReturnType operator()(const std::shared_ptr<ClassType>& x) const - { - return (x.get()->*f)(); - } -}; - -template <class ReturnType, typename ClassType> -mem_fun_sh_t<ReturnType, ClassType> -mem_fun_sh(ReturnType (ClassType::*f)()) -{ - return mem_fun_sh_t<ReturnType, ClassType>(f); -}; - -template <class ReturnType, typename ClassType> -const_mem_fun_sh_t<ReturnType, ClassType> -mem_fun_sh(ReturnType (ClassType::*f)() const) -{ - return const_mem_fun_sh_t<ReturnType, ClassType>(f); -}; - -// mem_fun1_t for SharedHandle -template<typename ReturnType, typename ClassType, typename ArgType> -class mem_fun1_sh_t:public std::binary_function<std::shared_ptr<ClassType>, - ArgType, - ReturnType> -{ -private: - ReturnType (ClassType::*f)(ArgType); - -public: - mem_fun1_sh_t(ReturnType (ClassType::*f)(ArgType)):f(f) {} - - ReturnType operator()(const std::shared_ptr<ClassType>& x, ArgType a) const - { - return (x.get()->*f)(a); - } -}; - -template<typename ReturnType, typename ClassType, typename ArgType> -mem_fun1_sh_t<ReturnType, ClassType, ArgType> -mem_fun_sh(ReturnType (ClassType::*f)(ArgType)) -{ - return mem_fun1_sh_t<ReturnType, ClassType, ArgType>(f); -}; - -template<class BinaryOp, class UnaryOp> -class adopt2nd_t:public std::binary_function<typename BinaryOp::first_argument_type, - typename UnaryOp::argument_type, - typename BinaryOp::result_type> { -private: - BinaryOp binaryOp_; - UnaryOp unaryOp_; -public: - adopt2nd_t(const BinaryOp& b, const UnaryOp& u): - binaryOp_(b), unaryOp_(u) {} - - typename BinaryOp::result_type - operator()(const typename BinaryOp::first_argument_type& x, - const typename UnaryOp::argument_type& y) - { - return binaryOp_(x, unaryOp_(y)); - } - -}; - -template <class BinaryOp, class UnaryOp> -inline adopt2nd_t<BinaryOp, UnaryOp> -adopt2nd(const BinaryOp& binaryOp, const UnaryOp& unaryOp) -{ - return adopt2nd_t<BinaryOp, UnaryOp>(binaryOp, unaryOp); -}; - -template<typename Pair> -class Ascend1st:public std::binary_function<Pair, Pair, bool> -{ -public: - bool operator()(const Pair& p1, const Pair& p2) const - { - return p1.first < p2.first; - } -}; - -template<typename Pair> -class select2nd -{ -public: - typename Pair::second_type operator()(const Pair& p) const - { - return p.second; - } - - typename Pair::second_type operator()(Pair& p) const - { - return p.second; - } -}; - class Deleter { public: template<class T> diff --git a/test/a2functionalTest.cc b/test/a2functionalTest.cc index 85e12404..2fcca3d5 100644 --- a/test/a2functionalTest.cc +++ b/test/a2functionalTest.cc @@ -11,42 +11,13 @@ namespace aria2 { class a2functionalTest:public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(a2functionalTest); - CPPUNIT_TEST(testMemFunSh); - CPPUNIT_TEST(testAdopt2nd); CPPUNIT_TEST(testStrjoin); CPPUNIT_TEST(testLeastRecentAccess); CPPUNIT_TEST_SUITE_END(); public: - void testMemFunSh(); - void testAdopt2nd(); void testStrjoin(); void testLeastRecentAccess(); - class Greeting { - public: - virtual ~Greeting() {} - - virtual std::string sayGreeting() = 0; - - virtual std::string sayGreetingConst() const = 0; - }; - - typedef std::shared_ptr<Greeting> GreetingHandle; - - class JapaneseGreeting:public Greeting - { - virtual std::string sayGreeting() - { - return "HAROO WAARUDO"; - } - - virtual std::string sayGreetingConst() const - { - return "HAROO WAARUDO"; - } - - }; - struct LastAccess { time_t lastAccess_; LastAccess(time_t lastAccess):lastAccess_(lastAccess) {} @@ -58,27 +29,8 @@ public: }; }; - CPPUNIT_TEST_SUITE_REGISTRATION(a2functionalTest); -void a2functionalTest::testMemFunSh() -{ - GreetingHandle greeting(new JapaneseGreeting()); - - CPPUNIT_ASSERT_EQUAL(std::string("HAROO WAARUDO"), mem_fun_sh(&Greeting::sayGreeting)(greeting)); - - CPPUNIT_ASSERT_EQUAL(std::string("HAROO WAARUDO"), mem_fun_sh(&Greeting::sayGreetingConst)(greeting)); - -} - -void a2functionalTest::testAdopt2nd() -{ - GreetingHandle greeting(new JapaneseGreeting()); - - CPPUNIT_ASSERT_EQUAL(std::string("A Japanese said:HAROO WAARUDO"), - adopt2nd(std::plus<std::string>(), mem_fun_sh(&Greeting::sayGreeting))("A Japanese said:", greeting)); -} - void a2functionalTest::testStrjoin() { std::vector<std::string> v;