diff --git a/src/DefaultPieceStorage.cc b/src/DefaultPieceStorage.cc index 9fc33367..8a236e59 100644 --- a/src/DefaultPieceStorage.cc +++ b/src/DefaultPieceStorage.cc @@ -83,7 +83,7 @@ DefaultPieceStorage::DefaultPieceStorage endGamePieceNum_(END_GAME_PIECE_NUM), option_(option), pieceStatMan_(new PieceStatMan(downloadContext->getNumPieces(), true)), - pieceSelector_(new RarestPieceSelector(pieceStatMan_)), + pieceSelector_(make_unique(pieceStatMan_)), wrDiskCache_(0) { const std::string& pieceSelectorOpt = @@ -866,4 +866,15 @@ void DefaultPieceStorage::onDownloadIncomplete() streamPieceSelector_->onBitfieldInit(); } +void DefaultPieceStorage::setPieceSelector +(std::unique_ptr pieceSelector) +{ + pieceSelector_ = std::move(pieceSelector); +} + +std::unique_ptr DefaultPieceStorage::popPieceSelector() +{ + return std::move(pieceSelector_); +} + } // namespace aria2 diff --git a/src/DefaultPieceStorage.h b/src/DefaultPieceStorage.h index b0d6e316..d6094d2e 100644 --- a/src/DefaultPieceStorage.h +++ b/src/DefaultPieceStorage.h @@ -90,7 +90,7 @@ private: std::shared_ptr pieceStatMan_; - std::shared_ptr pieceSelector_; + std::unique_ptr pieceSelector_; std::shared_ptr streamPieceSelector_; WrDiskCache* wrDiskCache_; @@ -301,16 +301,15 @@ public: return pieceStatMan_; } - void setPieceSelector(const std::shared_ptr& pieceSelector) - { - pieceSelector_ = pieceSelector; - } + void setPieceSelector(std::unique_ptr pieceSelector); - const std::shared_ptr& getPieceSelector() const + const std::unique_ptr& getPieceSelector() const { return pieceSelector_; } + std::unique_ptr popPieceSelector(); + void setWrDiskCache(WrDiskCache* wrDiskCache) { wrDiskCache_ = wrDiskCache; diff --git a/src/RequestGroup.cc b/src/RequestGroup.cc index 6efe900b..c4f16233 100644 --- a/src/RequestGroup.cc +++ b/src/RequestGroup.cc @@ -596,9 +596,7 @@ void RequestGroup::initPieceStorage() // integrated downloads. Currently multi-file integrated // download is not supported. A2_LOG_DEBUG("Using LongestSequencePieceSelector"); - std::shared_ptr longestPieceSelector - (new LongestSequencePieceSelector()); - ps->setPieceSelector(longestPieceSelector); + ps->setPieceSelector(make_unique()); } if(option_->defined(PREF_BT_PRIORITIZE_PIECE)) { std::vector result; @@ -607,12 +605,12 @@ void RequestGroup::initPieceStorage() downloadContext_->getFileEntries(), downloadContext_->getPieceLength()); if(!result.empty()) { - std::random_shuffle(result.begin(), result.end(), + std::random_shuffle(std::begin(result), std::end(result), *SimpleRandomizer::getInstance()); - std::shared_ptr priSelector - (new PriorityPieceSelector(ps->getPieceSelector())); - priSelector->setPriorityPiece(result.begin(), result.end()); - ps->setPieceSelector(priSelector); + auto priSelector = make_unique + (ps->popPieceSelector()); + priSelector->setPriorityPiece(std::begin(result), std::end(result)); + ps->setPieceSelector(std::move(priSelector)); } } } diff --git a/test/DefaultPieceStorageTest.cc b/test/DefaultPieceStorageTest.cc index dc6c4879..385f8cb8 100644 --- a/test/DefaultPieceStorageTest.cc +++ b/test/DefaultPieceStorageTest.cc @@ -44,17 +44,17 @@ private: std::shared_ptr dctx_; std::shared_ptr peer; std::shared_ptr