From 6b397c812520016f7b2b22544f51bfddbcca8dfe Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 6 Jul 2013 00:17:44 +0900 Subject: [PATCH] Use std::unique_ptr for CheckIntegrityEntry and FileAllocationEntry --- src/AbstractCommand.cc | 5 ++- src/AbstractCommand.h | 3 +- src/BtCheckIntegrityEntry.cc | 18 +++++---- src/CheckIntegrityCommand.cc | 14 +++---- src/CheckIntegrityCommand.h | 4 +- src/CheckIntegrityDispatcherCommand.cc | 8 ++-- src/CheckIntegrityDispatcherCommand.h | 3 +- src/CheckIntegrityEntry.cc | 4 +- src/CheckIntegrityEntry.h | 2 +- src/ChecksumCheckIntegrityEntry.cc | 7 ++-- src/ConsoleStatCalc.cc | 6 +-- src/DownloadCommand.cc | 6 +-- src/FileAllocationCommand.cc | 16 ++++---- src/FileAllocationCommand.h | 4 +- src/FileAllocationDispatcherCommand.cc | 4 +- src/FileAllocationDispatcherCommand.h | 3 +- src/FtpNegotiationCommand.cc | 19 +++++----- src/HttpResponseCommand.cc | 12 +++--- src/RequestGroup.cc | 51 +++++++++++--------------- src/RequestGroup.h | 4 +- src/SequentialDispatcherCommand.h | 12 +++--- src/SequentialPicker.h | 19 +++++----- src/StreamCheckIntegrityEntry.cc | 9 +++-- test/SequentialPickerTest.cc | 12 +++--- 24 files changed, 118 insertions(+), 127 deletions(-) diff --git a/src/AbstractCommand.cc b/src/AbstractCommand.cc index 6aa149de..9d154c60 100644 --- a/src/AbstractCommand.cc +++ b/src/AbstractCommand.cc @@ -770,10 +770,11 @@ std::string AbstractCommand::resolveHostname } void AbstractCommand::prepareForNextAction -(const std::shared_ptr& checkEntry) +(std::unique_ptr checkEntry) { std::vector> commands; - requestGroup_->processCheckIntegrityEntry(commands, checkEntry, e_); + requestGroup_->processCheckIntegrityEntry(commands, std::move(checkEntry), + e_); e_->addCommand(std::move(commands)); e_->setNoWait(true); } diff --git a/src/AbstractCommand.h b/src/AbstractCommand.h index 51e38edd..8230a040 100644 --- a/src/AbstractCommand.h +++ b/src/AbstractCommand.h @@ -179,8 +179,7 @@ public: void setTimeout(time_t timeout) { timeout_ = timeout; } - void prepareForNextAction - (const std::shared_ptr& checkEntry); + void prepareForNextAction(std::unique_ptr checkEntry); // Check if socket is connected. If socket is not connected and // there are other addresses to try, command is created using diff --git a/src/BtCheckIntegrityEntry.cc b/src/BtCheckIntegrityEntry.cc index cc28bfdb..79e57cd4 100644 --- a/src/BtCheckIntegrityEntry.cc +++ b/src/BtCheckIntegrityEntry.cc @@ -51,21 +51,22 @@ BtCheckIntegrityEntry::~BtCheckIntegrityEntry() {} void BtCheckIntegrityEntry::onDownloadIncomplete (std::vector>& commands, DownloadEngine* e) { - const std::shared_ptr& ps = getRequestGroup()->getPieceStorage(); + auto& ps = getRequestGroup()->getPieceStorage(); ps->onDownloadIncomplete(); if(getRequestGroup()->getOption()->getAsBool(PREF_HASH_CHECK_ONLY)) { return; } - const std::shared_ptr& diskAdaptor = ps->getDiskAdaptor(); + const auto& diskAdaptor = ps->getDiskAdaptor(); if(diskAdaptor->isReadOnlyEnabled()) { // Now reopen DiskAdaptor with read only disabled. diskAdaptor->closeFile(); diskAdaptor->disableReadOnly(); diskAdaptor->openFile(); } - std::shared_ptr entry - (new BtFileAllocationEntry(getRequestGroup())); - proceedFileAllocation(commands, entry, e); + proceedFileAllocation(commands, + make_unique + (getRequestGroup()), + e); } void BtCheckIntegrityEntry::onDownloadFinished @@ -77,9 +78,10 @@ void BtCheckIntegrityEntry::onDownloadFinished // behavior. if(!getRequestGroup()->getOption()->getAsBool(PREF_HASH_CHECK_ONLY) && getRequestGroup()->getOption()->getAsBool(PREF_BT_HASH_CHECK_SEED)) { - std::shared_ptr entry - (new BtFileAllocationEntry(getRequestGroup())); - proceedFileAllocation(commands, entry, e); + proceedFileAllocation(commands, + make_unique + (getRequestGroup()), + e); } } diff --git a/src/CheckIntegrityCommand.cc b/src/CheckIntegrityCommand.cc index ddc9b59f..4e0557fb 100644 --- a/src/CheckIntegrityCommand.cc +++ b/src/CheckIntegrityCommand.cc @@ -50,22 +50,23 @@ namespace aria2 { CheckIntegrityCommand::CheckIntegrityCommand (cuid_t cuid, RequestGroup* requestGroup, DownloadEngine* e, - const std::shared_ptr& entry): - RealtimeCommand(cuid, requestGroup, e), - entry_(entry) + CheckIntegrityEntry* entry) + : RealtimeCommand{cuid, requestGroup, e}, + entry_{entry} {} -CheckIntegrityCommand::~CheckIntegrityCommand() {} +CheckIntegrityCommand::~CheckIntegrityCommand() +{ + getDownloadEngine()->getCheckIntegrityMan()->dropPickedEntry(); +} bool CheckIntegrityCommand::executeInternal() { if(getRequestGroup()->isHaltRequested()) { - getDownloadEngine()->getCheckIntegrityMan()->dropPickedEntry(); return true; } entry_->validateChunk(); if(entry_->finished()) { - getDownloadEngine()->getCheckIntegrityMan()->dropPickedEntry(); // Enable control file saving here. See also // RequestGroup::processCheckIntegrityEntry() to know why this is // needed. @@ -95,7 +96,6 @@ bool CheckIntegrityCommand::executeInternal() bool CheckIntegrityCommand::handleException(Exception& e) { - getDownloadEngine()->getCheckIntegrityMan()->dropPickedEntry(); A2_LOG_ERROR_EX(fmt(MSG_FILE_VALIDATION_FAILURE, getCuid()), e); diff --git a/src/CheckIntegrityCommand.h b/src/CheckIntegrityCommand.h index 3160d8d4..0ce87c40 100644 --- a/src/CheckIntegrityCommand.h +++ b/src/CheckIntegrityCommand.h @@ -45,12 +45,12 @@ class CheckIntegrityEntry; class CheckIntegrityCommand : public RealtimeCommand { private: - std::shared_ptr entry_; + CheckIntegrityEntry* entry_; public: CheckIntegrityCommand(cuid_t cuid, RequestGroup* requestGroup, DownloadEngine* e, - const std::shared_ptr& entry); + CheckIntegrityEntry* entry); virtual ~CheckIntegrityCommand(); diff --git a/src/CheckIntegrityDispatcherCommand.cc b/src/CheckIntegrityDispatcherCommand.cc index 7a5517e9..0ecf6334 100644 --- a/src/CheckIntegrityDispatcherCommand.cc +++ b/src/CheckIntegrityDispatcherCommand.cc @@ -47,17 +47,17 @@ CheckIntegrityDispatcherCommand::CheckIntegrityDispatcherCommand (cuid_t cuid, const std::shared_ptr& fileAllocMan, DownloadEngine* e) - : SequentialDispatcherCommand(cuid, fileAllocMan, e) + : SequentialDispatcherCommand{cuid, fileAllocMan, e} { setStatusRealtime(); } std::unique_ptr CheckIntegrityDispatcherCommand::createCommand -(const std::shared_ptr& entry) +(CheckIntegrityEntry* entry) { cuid_t newCUID = getDownloadEngine()->newCUID(); - A2_LOG_INFO(fmt("CUID#%" PRId64 " - Dispatching CheckIntegrityCommand CUID#%" PRId64 ".", - getCuid(), newCUID)); + A2_LOG_INFO(fmt("CUID#%" PRId64 " - Dispatching CheckIntegrityCommand " + "CUID#%" PRId64 ".", getCuid(), newCUID)); return make_unique (newCUID, entry->getRequestGroup(), getDownloadEngine(), entry); } diff --git a/src/CheckIntegrityDispatcherCommand.h b/src/CheckIntegrityDispatcherCommand.h index 40753452..93c455e7 100644 --- a/src/CheckIntegrityDispatcherCommand.h +++ b/src/CheckIntegrityDispatcherCommand.h @@ -50,8 +50,7 @@ public: const std::shared_ptr& checkMan, DownloadEngine* e); protected: - virtual std::unique_ptr createCommand - (const std::shared_ptr& entry); + virtual std::unique_ptr createCommand(CheckIntegrityEntry* entry); }; } // namespace aria2 diff --git a/src/CheckIntegrityEntry.cc b/src/CheckIntegrityEntry.cc index a06b468e..3f23a7fa 100644 --- a/src/CheckIntegrityEntry.cc +++ b/src/CheckIntegrityEntry.cc @@ -86,11 +86,11 @@ void CheckIntegrityEntry::cutTrailingGarbage() void CheckIntegrityEntry::proceedFileAllocation (std::vector>& commands, - const std::shared_ptr& entry, + std::unique_ptr entry, DownloadEngine* e) { if(getRequestGroup()->needsFileAllocation()) { - e->getFileAllocationMan()->pushEntry(entry); + e->getFileAllocationMan()->pushEntry(std::move(entry)); } else { entry->prepareForNextAction(commands, e); } diff --git a/src/CheckIntegrityEntry.h b/src/CheckIntegrityEntry.h index cb2576c0..106fbd9c 100644 --- a/src/CheckIntegrityEntry.h +++ b/src/CheckIntegrityEntry.h @@ -56,7 +56,7 @@ protected: void setValidator(std::unique_ptr validator); void proceedFileAllocation(std::vector>& commands, - const std::shared_ptr& entry, + std::unique_ptr entry, DownloadEngine* e); public: CheckIntegrityEntry(RequestGroup* requestGroup, diff --git a/src/ChecksumCheckIntegrityEntry.cc b/src/ChecksumCheckIntegrityEntry.cc index 89ee08fd..12b5dfbe 100644 --- a/src/ChecksumCheckIntegrityEntry.cc +++ b/src/ChecksumCheckIntegrityEntry.cc @@ -78,9 +78,10 @@ ChecksumCheckIntegrityEntry::onDownloadIncomplete (std::vector>& commands, DownloadEngine* e) { if(redownload_) { - std::shared_ptr entry - (new StreamFileAllocationEntry(getRequestGroup(), popNextCommand())); - proceedFileAllocation(commands, entry, e); + proceedFileAllocation(commands, + make_unique + (getRequestGroup(), popNextCommand()), + e); } } diff --git a/src/ConsoleStatCalc.cc b/src/ConsoleStatCalc.cc index b91e5410..55f8d8ea 100644 --- a/src/ConsoleStatCalc.cc +++ b/src/ConsoleStatCalc.cc @@ -330,8 +330,7 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e) } { - const std::shared_ptr& entry = - e->getFileAllocationMan()->getPickedEntry(); + auto& entry = e->getFileAllocationMan()->getPickedEntry(); if(entry) { o << " [FileAlloc:#" << GroupId::toAbbrevHex(entry->getRequestGroup()->getGID()) << " " @@ -350,8 +349,7 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e) } #ifdef ENABLE_MESSAGE_DIGEST { - const std::shared_ptr& entry = - e->getCheckIntegrityMan()->getPickedEntry(); + auto& entry = e->getCheckIntegrityMan()->getPickedEntry(); if(entry) { o << " [Checksum:#" << GroupId::toAbbrevHex(entry->getRequestGroup()->getGID()) << " " diff --git a/src/DownloadCommand.cc b/src/DownloadCommand.cc index f4324b5f..4665efa8 100644 --- a/src/DownloadCommand.cc +++ b/src/DownloadCommand.cc @@ -327,12 +327,12 @@ bool DownloadCommand::prepareForNextSegment() { } #ifdef ENABLE_MESSAGE_DIGEST if(getDownloadContext()->getPieceHashType().empty()) { - std::shared_ptr entry - (new ChecksumCheckIntegrityEntry(getRequestGroup())); + auto entry = make_unique(getRequestGroup()); if(entry->isValidationReady()) { entry->initValidator(); entry->cutTrailingGarbage(); - getDownloadEngine()->getCheckIntegrityMan()->pushEntry(entry); + getDownloadEngine()->getCheckIntegrityMan()->pushEntry + (std::move(entry)); } } #endif // ENABLE_MESSAGE_DIGEST diff --git a/src/FileAllocationCommand.cc b/src/FileAllocationCommand.cc index b76357cc..a36f44cf 100644 --- a/src/FileAllocationCommand.cc +++ b/src/FileAllocationCommand.cc @@ -54,16 +54,19 @@ namespace aria2 { FileAllocationCommand::FileAllocationCommand (cuid_t cuid, RequestGroup* requestGroup, DownloadEngine* e, - const std::shared_ptr& fileAllocationEntry): - RealtimeCommand(cuid, requestGroup, e), - fileAllocationEntry_(fileAllocationEntry) {} + FileAllocationEntry* fileAllocationEntry) + : RealtimeCommand{cuid, requestGroup, e}, + fileAllocationEntry_{fileAllocationEntry} +{} -FileAllocationCommand::~FileAllocationCommand() {} +FileAllocationCommand::~FileAllocationCommand() +{ + getDownloadEngine()->getFileAllocationMan()->dropPickedEntry(); +} bool FileAllocationCommand::executeInternal() { if(getRequestGroup()->isHaltRequested()) { - getDownloadEngine()->getFileAllocationMan()->dropPickedEntry(); return true; } fileAllocationEntry_->allocateChunk(); @@ -72,8 +75,6 @@ bool FileAllocationCommand::executeInternal() (fmt(MSG_ALLOCATION_COMPLETED, static_cast(timer_.difference(global::wallclock())), getRequestGroup()->getTotalLength())); - getDownloadEngine()->getFileAllocationMan()->dropPickedEntry(); - std::vector> commands; fileAllocationEntry_->prepareForNextAction(commands, getDownloadEngine()); getDownloadEngine()->addCommand(std::move(commands)); @@ -87,7 +88,6 @@ bool FileAllocationCommand::executeInternal() bool FileAllocationCommand::handleException(Exception& e) { - getDownloadEngine()->getFileAllocationMan()->dropPickedEntry(); A2_LOG_ERROR_EX(fmt(MSG_FILE_ALLOCATION_FAILURE, getCuid()), e); diff --git a/src/FileAllocationCommand.h b/src/FileAllocationCommand.h index cad28e4c..d8f6bbf7 100644 --- a/src/FileAllocationCommand.h +++ b/src/FileAllocationCommand.h @@ -47,12 +47,12 @@ class FileAllocationEntry; class FileAllocationCommand : public RealtimeCommand { private: - std::shared_ptr fileAllocationEntry_; + FileAllocationEntry* fileAllocationEntry_; Timer timer_; public: FileAllocationCommand(cuid_t cuid, RequestGroup* requestGroup, DownloadEngine* e, - const std::shared_ptr& fileAllocationEntry); + FileAllocationEntry* fileAllocationEntry); virtual ~FileAllocationCommand(); diff --git a/src/FileAllocationDispatcherCommand.cc b/src/FileAllocationDispatcherCommand.cc index 4a56b569..4cc9c43d 100644 --- a/src/FileAllocationDispatcherCommand.cc +++ b/src/FileAllocationDispatcherCommand.cc @@ -47,11 +47,11 @@ FileAllocationDispatcherCommand::FileAllocationDispatcherCommand (cuid_t cuid, const std::shared_ptr& fileAllocMan, DownloadEngine* e) - : SequentialDispatcherCommand(cuid, fileAllocMan, e) + : SequentialDispatcherCommand{cuid, fileAllocMan, e} {} std::unique_ptr FileAllocationDispatcherCommand::createCommand -(const std::shared_ptr& entry) +(FileAllocationEntry* entry) { cuid_t newCUID = getDownloadEngine()->newCUID(); A2_LOG_INFO(fmt(MSG_FILE_ALLOCATION_DISPATCH, newCUID)); diff --git a/src/FileAllocationDispatcherCommand.h b/src/FileAllocationDispatcherCommand.h index 6d374433..ce1fe05c 100644 --- a/src/FileAllocationDispatcherCommand.h +++ b/src/FileAllocationDispatcherCommand.h @@ -50,8 +50,7 @@ public: const std::shared_ptr& fileAllocMan, DownloadEngine* e); protected: - virtual std::unique_ptr createCommand - (const std::shared_ptr& entry); + virtual std::unique_ptr createCommand(FileAllocationEntry* entry); }; } // namespace aria2 diff --git a/src/FtpNegotiationCommand.cc b/src/FtpNegotiationCommand.cc index ef943859..285ca489 100644 --- a/src/FtpNegotiationCommand.cc +++ b/src/FtpNegotiationCommand.cc @@ -400,11 +400,12 @@ bool FtpNegotiationCommand::onFileSizeDetermined(int64_t totalLength) getRequestGroup()->initPieceStorage(); if(getDownloadContext()->isChecksumVerificationNeeded()) { A2_LOG_DEBUG("Zero length file exists. Verify checksum."); - std::shared_ptr entry - (new ChecksumCheckIntegrityEntry(getRequestGroup())); + auto entry = make_unique + (getRequestGroup()); entry->initValidator(); getPieceStorage()->getDiskAdaptor()->openExistingFile(); - getDownloadEngine()->getCheckIntegrityMan()->pushEntry(entry); + getDownloadEngine()->getCheckIntegrityMan()->pushEntry + (std::move(entry)); sequence_ = SEQ_EXIT; } else #endif // ENABLE_MESSAGE_DIGEST @@ -434,10 +435,11 @@ bool FtpNegotiationCommand::onFileSizeDetermined(int64_t totalLength) // HttpResponseCommand::handleOtherEncoding() if(getDownloadContext()->isChecksumVerificationNeeded()) { A2_LOG_DEBUG("Verify checksum for zero-length file"); - std::shared_ptr entry - (new ChecksumCheckIntegrityEntry(getRequestGroup())); + auto entry = make_unique + (getRequestGroup()); entry->initValidator(); - getDownloadEngine()->getCheckIntegrityMan()->pushEntry(entry); + getDownloadEngine()->getCheckIntegrityMan()->pushEntry + (std::move(entry)); sequence_ = SEQ_EXIT; } else #endif // ENABLE_MESSAGE_DIGEST @@ -465,8 +467,7 @@ bool FtpNegotiationCommand::onFileSizeDetermined(int64_t totalLength) return false; } - std::shared_ptr checkIntegrityEntry = - getRequestGroup()->createCheckIntegrityEntry(); + auto checkIntegrityEntry = getRequestGroup()->createCheckIntegrityEntry(); if(!checkIntegrityEntry) { sequence_ = SEQ_DOWNLOAD_ALREADY_COMPLETED; poolConnection(); @@ -478,7 +479,7 @@ bool FtpNegotiationCommand::onFileSizeDetermined(int64_t totalLength) // AbstractCommand::execute() getSegmentMan()->getSegmentWithIndex(getCuid(), 0); - prepareForNextAction(checkIntegrityEntry); + prepareForNextAction(std::move(checkIntegrityEntry)); disableReadCheckSocket(); } diff --git a/src/HttpResponseCommand.cc b/src/HttpResponseCommand.cc index b6027f24..4b232d42 100644 --- a/src/HttpResponseCommand.cc +++ b/src/HttpResponseCommand.cc @@ -377,7 +377,7 @@ bool HttpResponseCommand::handleDefaultEncoding getFileEntry()->poolRequest(getRequest()); } - prepareForNextAction(checkEntry); + prepareForNextAction(std::move(checkEntry)); if(getRequest()->getMethod() == Request::METHOD_HEAD) { poolConnection(); @@ -421,11 +421,11 @@ bool HttpResponseCommand::handleOtherEncoding // See also FtpNegotiationCommand::onFileSizeDetermined() if(getDownloadContext()->isChecksumVerificationNeeded()) { A2_LOG_DEBUG("Zero length file exists. Verify checksum."); - std::shared_ptr entry - (new ChecksumCheckIntegrityEntry(getRequestGroup())); + auto entry = make_unique + (getRequestGroup()); entry->initValidator(); getPieceStorage()->getDiskAdaptor()->openExistingFile(); - getDownloadEngine()->getCheckIntegrityMan()->pushEntry(entry); + getDownloadEngine()->getCheckIntegrityMan()->pushEntry(std::move(entry)); } else #endif // ENABLE_MESSAGE_DIGEST { @@ -455,10 +455,10 @@ bool HttpResponseCommand::handleOtherEncoding #ifdef ENABLE_MESSAGE_DIGEST if(getDownloadContext()->isChecksumVerificationNeeded()) { A2_LOG_DEBUG("Verify checksum for zero-length file"); - auto entry = std::make_shared + auto entry = make_unique (getRequestGroup()); entry->initValidator(); - getDownloadEngine()->getCheckIntegrityMan()->pushEntry(entry); + getDownloadEngine()->getCheckIntegrityMan()->pushEntry(std::move(entry)); } else #endif // ENABLE_MESSAGE_DIGEST { diff --git a/src/RequestGroup.cc b/src/RequestGroup.cc index c4f16233..ff0ff5bb 100644 --- a/src/RequestGroup.cc +++ b/src/RequestGroup.cc @@ -216,18 +216,16 @@ void RequestGroup::closeFile() // TODO The function name is not intuitive at all.. it does not convey // that this function open file. -std::shared_ptr RequestGroup::createCheckIntegrityEntry() +std::unique_ptr RequestGroup::createCheckIntegrityEntry() { - std::shared_ptr infoFile - (new DefaultBtProgressInfoFile(downloadContext_, pieceStorage_, - option_.get())); - std::shared_ptr checkEntry; + auto infoFile = std::make_shared + (downloadContext_, pieceStorage_, option_.get()); if(option_->getAsBool(PREF_CHECK_INTEGRITY) && downloadContext_->isPieceHashVerificationAvailable()) { // When checking piece hash, we don't care file is downloaded and // infoFile exists. loadAndOpenFile(infoFile); - checkEntry.reset(new StreamCheckIntegrityEntry(this)); + return make_unique(this); } else if(isPreLocalFileCheckEnabled() && (infoFile->exists() || (File(getFirstFilePath()).exists() && @@ -242,10 +240,9 @@ std::shared_ptr RequestGroup::createCheckIntegrityEntry() #ifdef ENABLE_MESSAGE_DIGEST if(downloadContext_->isChecksumVerificationNeeded()) { A2_LOG_INFO(MSG_HASH_CHECK_NOT_DONE); - ChecksumCheckIntegrityEntry* tempEntry = - new ChecksumCheckIntegrityEntry(this); + auto tempEntry = make_unique(this); tempEntry->setRedownload(true); - checkEntry.reset(tempEntry); + return std::move(tempEntry); } else #endif // ENABLE_MESSAGE_DIGEST { @@ -253,9 +250,10 @@ std::shared_ptr RequestGroup::createCheckIntegrityEntry() A2_LOG_NOTICE(fmt(MSG_DOWNLOAD_ALREADY_COMPLETED, gid_->toHex().c_str(), downloadContext_->getBasePath().c_str())); + return nullptr; } } else { - checkEntry.reset(new StreamCheckIntegrityEntry(this)); + return make_unique(this); } } else #ifdef ENABLE_MESSAGE_DIGEST @@ -263,17 +261,15 @@ std::shared_ptr RequestGroup::createCheckIntegrityEntry() downloadContext_->isChecksumVerificationAvailable()) { pieceStorage_->markAllPiecesDone(); loadAndOpenFile(infoFile); - ChecksumCheckIntegrityEntry* tempEntry = - new ChecksumCheckIntegrityEntry(this); + auto tempEntry = make_unique(this); tempEntry->setRedownload(true); - checkEntry.reset(tempEntry); + return std::move(tempEntry); } else #endif // ENABLE_MESSAGE_DIGEST { loadAndOpenFile(infoFile); - checkEntry.reset(new StreamCheckIntegrityEntry(this)); + return make_unique(this); } - return checkEntry; } void RequestGroup::createInitialCommand @@ -379,11 +375,7 @@ void RequestGroup::createInitialCommand A2_LOG_NOTICE(_("For BitTorrent Magnet URI, enabling DHT is strongly" " recommended. See --enable-dht option.")); } - - std::shared_ptr entry - (new BtCheckIntegrityEntry(this)); - entry->onDownloadIncomplete(commands, e); - + BtCheckIntegrityEntry{this}.onDownloadIncomplete(commands, e); return; } removeDefunctControlFile(progressInfoFile); @@ -454,14 +446,14 @@ void RequestGroup::createInitialCommand e->addCommand(std::move(command)); } } - std::shared_ptr entry(new BtCheckIntegrityEntry(this)); + auto entry = make_unique(this); // --bt-seed-unverified=true is given and download has completed, skip // validation for piece hashes. if(option_->getAsBool(PREF_BT_SEED_UNVERIFIED) && pieceStorage_->downloadFinished()) { entry->onDownloadFinished(commands, e); } else { - processCheckIntegrityEntry(commands, entry, e); + processCheckIntegrityEntry(commands, std::move(entry), e); } return; } @@ -485,10 +477,9 @@ void RequestGroup::createInitialCommand (downloadContext_, std::shared_ptr(), option_.get())); adjustFilename(progressInfoFile); initPieceStorage(); - std::shared_ptr checkEntry = - createCheckIntegrityEntry(); + auto checkEntry = createCheckIntegrityEntry(); if(checkEntry) { - processCheckIntegrityEntry(commands, checkEntry, e); + processCheckIntegrityEntry(commands, std::move(checkEntry), e); } } } else { @@ -538,15 +529,15 @@ void RequestGroup::createInitialCommand } } progressInfoFile_ = progressInfoFile; - std::shared_ptr checkIntegrityEntry - (new StreamCheckIntegrityEntry(this)); - processCheckIntegrityEntry(commands, checkIntegrityEntry, e); + processCheckIntegrityEntry(commands, + make_unique(this), + e); } } void RequestGroup::processCheckIntegrityEntry (std::vector>& commands, - const std::shared_ptr& entry, + std::unique_ptr entry, DownloadEngine* e) { int64_t actualFileSize = pieceStorage_->getDiskAdaptor()->size(); @@ -565,7 +556,7 @@ void RequestGroup::processCheckIntegrityEntry // enableSaveControlFile() will be called after hash checking is // done. See CheckIntegrityCommand. disableSaveControlFile(); - e->getCheckIntegrityMan()->pushEntry(entry); + e->getCheckIntegrityMan()->pushEntry(std::move(entry)); } else #endif // ENABLE_MESSAGE_DIGEST { diff --git a/src/RequestGroup.h b/src/RequestGroup.h index 42aacce6..412db256 100644 --- a/src/RequestGroup.h +++ b/src/RequestGroup.h @@ -210,7 +210,7 @@ public: return segmentMan_; } - std::shared_ptr createCheckIntegrityEntry(); + std::unique_ptr createCheckIntegrityEntry(); // Returns first bootstrap commands to initiate a download. // If this is HTTP/FTP download and file size is unknown, only 1 command @@ -387,7 +387,7 @@ public: void processCheckIntegrityEntry (std::vector>& commands, - const std::shared_ptr& entry, + std::unique_ptr entry, DownloadEngine* e); // Initializes pieceStorage_ and segmentMan_. We guarantee that diff --git a/src/SequentialDispatcherCommand.h b/src/SequentialDispatcherCommand.h index 0b5a1580..8680629d 100644 --- a/src/SequentialDispatcherCommand.h +++ b/src/SequentialDispatcherCommand.h @@ -59,10 +59,11 @@ protected: return e_; } public: - SequentialDispatcherCommand(cuid_t cuid, - const std::shared_ptr >& picker, - DownloadEngine* e): - Command(cuid), picker_(picker), e_(e) + SequentialDispatcherCommand + (cuid_t cuid, + const std::shared_ptr>& picker, + DownloadEngine* e) + : Command{cuid}, picker_{picker}, e_{e} { setStatusRealtime(); } @@ -83,8 +84,7 @@ public: } protected: - virtual std::unique_ptr createCommand - (const std::shared_ptr& entry) = 0; + virtual std::unique_ptr createCommand(T* entry) = 0; }; } // namespace aria2 diff --git a/src/SequentialPicker.h b/src/SequentialPicker.h index 6fbb984e..790fa2e1 100644 --- a/src/SequentialPicker.h +++ b/src/SequentialPicker.h @@ -45,15 +45,15 @@ namespace aria2 { template class SequentialPicker { private: - std::deque > entries_; - std::shared_ptr pickedEntry_; + std::deque> entries_; + std::unique_ptr pickedEntry_; public: bool isPicked() const { return pickedEntry_.get(); } - const std::shared_ptr& getPickedEntry() const + const std::unique_ptr& getPickedEntry() const { return pickedEntry_; } @@ -68,20 +68,19 @@ public: return !entries_.empty(); } - std::shared_ptr pickNext() + T* pickNext() { - std::shared_ptr r; if(hasNext()) { - r = entries_.front(); + pickedEntry_ = std::move(entries_.front()); entries_.pop_front(); - pickedEntry_ = r; + return pickedEntry_.get(); } - return r; + return nullptr; } - void pushEntry(const std::shared_ptr& entry) + void pushEntry(std::unique_ptr entry) { - entries_.push_back(entry); + entries_.push_back(std::move(entry)); } size_t countEntryInQueue() const diff --git a/src/StreamCheckIntegrityEntry.cc b/src/StreamCheckIntegrityEntry.cc index e245f18e..aa345806 100644 --- a/src/StreamCheckIntegrityEntry.cc +++ b/src/StreamCheckIntegrityEntry.cc @@ -52,14 +52,15 @@ StreamCheckIntegrityEntry::~StreamCheckIntegrityEntry() {} void StreamCheckIntegrityEntry::onDownloadIncomplete (std::vector>& commands, DownloadEngine* e) { - const std::shared_ptr& ps = getRequestGroup()->getPieceStorage(); + auto& ps = getRequestGroup()->getPieceStorage(); ps->onDownloadIncomplete(); if(getRequestGroup()->getOption()->getAsBool(PREF_HASH_CHECK_ONLY)) { return; } - std::shared_ptr entry - (new StreamFileAllocationEntry(getRequestGroup(), popNextCommand())); - proceedFileAllocation(commands, entry, e); + proceedFileAllocation(commands, + make_unique + (getRequestGroup(), popNextCommand()), + e); } void StreamCheckIntegrityEntry::onDownloadFinished diff --git a/test/SequentialPickerTest.cc b/test/SequentialPickerTest.cc index af7b948c..f0990d50 100644 --- a/test/SequentialPickerTest.cc +++ b/test/SequentialPickerTest.cc @@ -2,9 +2,9 @@ #include -namespace aria2 { +#include "a2functional.h" -typedef std::shared_ptr Integer; +namespace aria2 { class SequentialPickerTest:public CppUnit::TestFixture { @@ -26,8 +26,8 @@ void SequentialPickerTest::testPick() CPPUNIT_ASSERT(!picker.hasNext()); CPPUNIT_ASSERT_EQUAL((size_t)0, picker.countEntryInQueue()); - picker.pushEntry(Integer(new int(1))); - picker.pushEntry(Integer(new int(2))); + picker.pushEntry(make_unique(1)); + picker.pushEntry(make_unique(2)); CPPUNIT_ASSERT(picker.hasNext()); CPPUNIT_ASSERT_EQUAL((size_t)2, picker.countEntryInQueue()); @@ -35,7 +35,7 @@ void SequentialPickerTest::testPick() picker.pickNext(); CPPUNIT_ASSERT(picker.isPicked()); - CPPUNIT_ASSERT_EQUAL(*Integer(new int(1)), *picker.getPickedEntry()); + CPPUNIT_ASSERT_EQUAL(1, *picker.getPickedEntry()); picker.dropPickedEntry(); @@ -44,7 +44,7 @@ void SequentialPickerTest::testPick() picker.pickNext(); - CPPUNIT_ASSERT_EQUAL(*Integer(new int(2)), *picker.getPickedEntry()); + CPPUNIT_ASSERT_EQUAL(2, *picker.getPickedEntry()); CPPUNIT_ASSERT(!picker.hasNext()); }