diff --git a/src/DefaultBtAnnounce.cc b/src/DefaultBtAnnounce.cc index fb4800c0..2219ef24 100644 --- a/src/DefaultBtAnnounce.cc +++ b/src/DefaultBtAnnounce.cc @@ -69,7 +69,7 @@ DefaultBtAnnounce::DefaultBtAnnounce incomplete_(0), announceList_(bittorrent::getTorrentAttrs(downloadContext)->announceList), option_(option), - randomizer_(SimpleRandomizer::getInstance()), + randomizer_(SimpleRandomizer::getInstance().get()), tcpPort_(0) {} diff --git a/src/DefaultPieceStorage.cc b/src/DefaultPieceStorage.cc index 811c68b7..cee96df9 100644 --- a/src/DefaultPieceStorage.cc +++ b/src/DefaultPieceStorage.cc @@ -478,8 +478,7 @@ void DefaultPieceStorage::completePiece(const std::shared_ptr& piece) } #ifdef ENABLE_BITTORRENT if(downloadContext_->hasAttribute(CTX_ATTR_BT)) { - auto torrentAttrs = bittorrent::getTorrentAttrs(downloadContext_); - if(!torrentAttrs->metadata.empty()) { + if(!bittorrent::getTorrentAttrs(downloadContext_)->metadata.empty()) { #ifdef __MINGW32__ // On Windows, if aria2 opens files with GENERIC_WRITE access // right, some programs cannot open them aria2 is seeding. To diff --git a/src/DownloadContext.cc b/src/DownloadContext.cc index 0a76bbf5..6591b3ea 100644 --- a/src/DownloadContext.cc +++ b/src/DownloadContext.cc @@ -156,12 +156,13 @@ void DownloadContext::setAttribute attrs_[key] = std::move(value); } -ContextAttribute* DownloadContext::getAttribute(ContextAttributeType key) +const std::unique_ptr& DownloadContext::getAttribute +(ContextAttributeType key) { assert(key < MAX_CTX_ATTR); const std::unique_ptr& attr = attrs_[key]; if(attr) { - return attr.get(); + return attr; } else { throw DL_ABORT_EX(fmt("No attribute named %s", strContextAttributeType(key))); diff --git a/src/DownloadContext.h b/src/DownloadContext.h index e1d9020a..89a8c8e7 100644 --- a/src/DownloadContext.h +++ b/src/DownloadContext.h @@ -206,7 +206,8 @@ public: void setAttribute (ContextAttributeType key, std::unique_ptr value); - ContextAttribute* getAttribute(ContextAttributeType key); + const std::unique_ptr& getAttribute + (ContextAttributeType key); bool hasAttribute(ContextAttributeType key) const; diff --git a/src/DownloadEngine.cc b/src/DownloadEngine.cc index d845ff1d..8de6a316 100644 --- a/src/DownloadEngine.cc +++ b/src/DownloadEngine.cc @@ -549,14 +549,15 @@ void DownloadEngine::setAuthConfigFactory authConfigFactory_ = std::move(factory); } -AuthConfigFactory* DownloadEngine::getAuthConfigFactory() const +const std::unique_ptr& +DownloadEngine::getAuthConfigFactory() const { - return authConfigFactory_.get(); + return authConfigFactory_; } -CookieStorage* DownloadEngine::getCookieStorage() const +const std::unique_ptr& DownloadEngine::getCookieStorage() const { - return cookieStorage_.get(); + return cookieStorage_; } void DownloadEngine::setRefreshInterval(int64_t interval) diff --git a/src/DownloadEngine.h b/src/DownloadEngine.h index 0f2e67fd..00f1aab2 100644 --- a/src/DownloadEngine.h +++ b/src/DownloadEngine.h @@ -302,7 +302,7 @@ public: uint16_t port, const std::string& username); - CookieStorage* getCookieStorage() const; + const std::unique_ptr& getCookieStorage() const; #ifdef ENABLE_BITTORRENT const std::shared_ptr& getBtRegistry() const @@ -333,7 +333,7 @@ public: void setAuthConfigFactory(std::unique_ptr factory); - AuthConfigFactory* getAuthConfigFactory() const; + const std::unique_ptr& getAuthConfigFactory() const; void setRefreshInterval(int64_t interval); diff --git a/src/HttpRequestCommand.cc b/src/HttpRequestCommand.cc index 678bec18..34b708a1 100644 --- a/src/HttpRequestCommand.cc +++ b/src/HttpRequestCommand.cc @@ -98,8 +98,8 @@ createHttpRequest(const std::shared_ptr& req, httpRequest->setFileEntry(fileEntry); httpRequest->setSegment(segment); httpRequest->addHeader(option->get(PREF_HEADER)); - httpRequest->setCookieStorage(e->getCookieStorage()); - httpRequest->setAuthConfigFactory(e->getAuthConfigFactory()); + httpRequest->setCookieStorage(e->getCookieStorage().get()); + httpRequest->setAuthConfigFactory(e->getAuthConfigFactory().get()); httpRequest->setOption(option.get()); httpRequest->setProxyRequest(proxyRequest); httpRequest->setAcceptMetalink(rg->getDownloadContext()-> diff --git a/src/RequestGroupEntry.cc b/src/RequestGroupEntry.cc index 8241e52a..7a2c64b8 100644 --- a/src/RequestGroupEntry.cc +++ b/src/RequestGroupEntry.cc @@ -52,6 +52,11 @@ RequestGroupEntry::~RequestGroupEntry() requestGroup_->decreaseNumCommand(); } +const std::unique_ptr& RequestGroupEntry::getNextCommand() const +{ + return nextCommand_; +} + std::unique_ptr RequestGroupEntry::popNextCommand() { return std::move(nextCommand_); diff --git a/src/RequestGroupEntry.h b/src/RequestGroupEntry.h index eb151461..13fabca8 100644 --- a/src/RequestGroupEntry.h +++ b/src/RequestGroupEntry.h @@ -60,10 +60,7 @@ public: return requestGroup_; } - Command* getNextCommand() const - { - return nextCommand_.get(); - } + const std::unique_ptr& getNextCommand() const; std::unique_ptr popNextCommand(); diff --git a/src/SimpleRandomizer.cc b/src/SimpleRandomizer.cc index dc558e3d..5d304243 100644 --- a/src/SimpleRandomizer.cc +++ b/src/SimpleRandomizer.cc @@ -45,12 +45,12 @@ namespace aria2 { std::unique_ptr SimpleRandomizer::randomizer_; -SimpleRandomizer* SimpleRandomizer::getInstance() +const std::unique_ptr& SimpleRandomizer::getInstance() { if(!randomizer_) { randomizer_.reset(new SimpleRandomizer()); } - return randomizer_.get(); + return randomizer_; } void SimpleRandomizer::init() diff --git a/src/SimpleRandomizer.h b/src/SimpleRandomizer.h index 457bdcb1..f1670413 100644 --- a/src/SimpleRandomizer.h +++ b/src/SimpleRandomizer.h @@ -56,7 +56,7 @@ private: SimpleRandomizer(); public: - static SimpleRandomizer* getInstance(); + static const std::unique_ptr& getInstance(); static void init(); diff --git a/src/SingletonHolder.h b/src/SingletonHolder.h index e37cc4ac..19f4e6c9 100644 --- a/src/SingletonHolder.h +++ b/src/SingletonHolder.h @@ -50,9 +50,9 @@ private: public: ~SingletonHolder() {} - static T* instance() + static std::unique_ptr& instance() { - return instance_.get(); + return instance_; } static void instance(std::unique_ptr ptr) diff --git a/src/UTMetadataPostDownloadHandler.cc b/src/UTMetadataPostDownloadHandler.cc index 82084243..58e3002a 100644 --- a/src/UTMetadataPostDownloadHandler.cc +++ b/src/UTMetadataPostDownloadHandler.cc @@ -59,8 +59,7 @@ bool UTMetadataPostDownloadHandler::Criteria::match const std::shared_ptr& dctx = requestGroup->getDownloadContext(); if(dctx->hasAttribute(CTX_ATTR_BT)) { - auto attrs = bittorrent::getTorrentAttrs(dctx); - if(attrs->metadata.empty()) { + if(bittorrent::getTorrentAttrs(dctx)->metadata.empty()) { return true; } } diff --git a/src/aria2api.cc b/src/aria2api.cc index 6a353f70..a9a1e131 100644 --- a/src/aria2api.cc +++ b/src/aria2api.cc @@ -737,9 +737,8 @@ struct RequestGroupDH : public DownloadHandle { { #ifdef ENABLE_BITTORRENT if(group->getDownloadContext()->hasAttribute(CTX_ATTR_BT)) { - std::shared_ptr torrentAttrs = - bittorrent::getTorrentAttrs(group->getDownloadContext()); - return torrentAttrs->infoHash; + return bittorrent::getTorrentAttrs(group->getDownloadContext()) + ->infoHash; } #endif // ENABLE_BITTORRENT return A2STR::NIL; @@ -804,7 +803,7 @@ struct RequestGroupDH : public DownloadHandle { BtMetaInfoData res; #ifdef ENABLE_BITTORRENT if(group->getDownloadContext()->hasAttribute(CTX_ATTR_BT)) { - std::shared_ptr torrentAttrs = + auto torrentAttrs = bittorrent::getTorrentAttrs(group->getDownloadContext()); res.announceList = torrentAttrs->announceList; res.comment = torrentAttrs->comment; diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc index e907ab0f..6fc3fba1 100644 --- a/src/bittorrent_helper.cc +++ b/src/bittorrent_helper.cc @@ -626,7 +626,7 @@ TorrentAttribute* getTorrentAttrs TorrentAttribute* getTorrentAttrs(DownloadContext* dctx) { - return static_cast(dctx->getAttribute(CTX_ATTR_BT)); + return static_cast(dctx->getAttribute(CTX_ATTR_BT).get()); } const unsigned char* getInfoHash diff --git a/src/bittorrent_helper.h b/src/bittorrent_helper.h index 40c07e3d..19647b42 100644 --- a/src/bittorrent_helper.h +++ b/src/bittorrent_helper.h @@ -109,7 +109,7 @@ void loadFromMemory(const std::shared_ptr& torrent, const std::string& overrideName = ""); // Parses BitTorrent Magnet URI and returns -// std::shared_ptr which includes infoHash, name and +// std::unique_ptr which includes infoHash, name and // announceList. If parsing operation failed, an RecoverableException // will be thrown. infoHash and name are string and announceList is a // list of list of announce URI. @@ -149,6 +149,7 @@ void computeFastSet (std::vector& fastSet, const std::string& ipaddr, size_t numPieces, const unsigned char* infoHash, size_t fastSetSize); +// Make sure that don't recieve return value into std::shared_ptr. TorrentAttribute* getTorrentAttrs(DownloadContext* dctx); TorrentAttribute* getTorrentAttrs (const std::shared_ptr& dctx); diff --git a/src/util.cc b/src/util.cc index 07f87cad..7931bfba 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1506,7 +1506,7 @@ std::vector > createIndexPaths(std::istream& i) namespace { void generateRandomDataRandom(unsigned char* data, size_t length) { - SimpleRandomizer* rd = SimpleRandomizer::getInstance(); + const auto& rd = SimpleRandomizer::getInstance(); for(size_t i = 0; i < length; ++i) { data[i] = static_cast(rd->getRandomNumber(256)); } diff --git a/test/UTMetadataRequestExtensionMessageTest.cc b/test/UTMetadataRequestExtensionMessageTest.cc index f1b925cb..bc7007dd 100644 --- a/test/UTMetadataRequestExtensionMessageTest.cc +++ b/test/UTMetadataRequestExtensionMessageTest.cc @@ -41,7 +41,6 @@ public: messageFactory_.reset(new WrapExtBtMessageFactory()); dispatcher_.reset(new MockBtMessageDispatcher()); dctx_.reset(new DownloadContext()); - std::shared_ptr attrs(new TorrentAttribute()); dctx_->setAttribute(CTX_ATTR_BT, make_unique()); peer_.reset(new Peer("host", 6880)); peer_->allocateSessionResource(0, 0);