summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2013-06-26 23:56:43 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2013-06-26 23:56:43 +0900
commitda7400ef5c62ee144fc14ba7ca4745349f0b0c72 (patch)
treeb0d023eecfcfcca2a88eb3466baf878bdff9f4c1 /src
parent47402c5f29cb806f73aae2ed18aa2dde21956700 (diff)
Return std::unique_ptr member as const ref
Returning raw pointer has a risk that it may be stolen by std::shared_ptr in accident.
Diffstat (limited to 'src')
-rw-r--r--src/DefaultBtAnnounce.cc2
-rw-r--r--src/DefaultPieceStorage.cc3
-rw-r--r--src/DownloadContext.cc5
-rw-r--r--src/DownloadContext.h3
-rw-r--r--src/DownloadEngine.cc9
-rw-r--r--src/DownloadEngine.h4
-rw-r--r--src/HttpRequestCommand.cc4
-rw-r--r--src/RequestGroupEntry.cc5
-rw-r--r--src/RequestGroupEntry.h5
-rw-r--r--src/SimpleRandomizer.cc4
-rw-r--r--src/SimpleRandomizer.h2
-rw-r--r--src/SingletonHolder.h4
-rw-r--r--src/UTMetadataPostDownloadHandler.cc3
-rw-r--r--src/aria2api.cc7
-rw-r--r--src/bittorrent_helper.cc2
-rw-r--r--src/bittorrent_helper.h3
-rw-r--r--src/util.cc2
17 files changed, 35 insertions, 32 deletions
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>& 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<ContextAttribute>& DownloadContext::getAttribute
+(ContextAttributeType key)
{
assert(key < MAX_CTX_ATTR);
const std::unique_ptr<ContextAttribute>& 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<ContextAttribute> value);
- ContextAttribute* getAttribute(ContextAttributeType key);
+ const std::unique_ptr<ContextAttribute>& 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<AuthConfigFactory>&
+DownloadEngine::getAuthConfigFactory() const
{
- return authConfigFactory_.get();
+ return authConfigFactory_;
}
-CookieStorage* DownloadEngine::getCookieStorage() const
+const std::unique_ptr<CookieStorage>& 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<CookieStorage>& getCookieStorage() const;
#ifdef ENABLE_BITTORRENT
const std::shared_ptr<BtRegistry>& getBtRegistry() const
@@ -333,7 +333,7 @@ public:
void setAuthConfigFactory(std::unique_ptr<AuthConfigFactory> factory);
- AuthConfigFactory* getAuthConfigFactory() const;
+ const std::unique_ptr<AuthConfigFactory>& 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<Request>& 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<Command>& RequestGroupEntry::getNextCommand() const
+{
+ return nextCommand_;
+}
+
std::unique_ptr<Command> 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<Command>& getNextCommand() const;
std::unique_ptr<Command> 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> SimpleRandomizer::randomizer_;
-SimpleRandomizer* SimpleRandomizer::getInstance()
+const std::unique_ptr<SimpleRandomizer>& 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<SimpleRandomizer>& 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<T>& instance()
{
- return instance_.get();
+ return instance_;
}
static void instance(std::unique_ptr<T> 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<DownloadContext>& 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<TorrentAttribute> 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<TorrentAttribute> 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<TorrentAttribute*>(dctx->getAttribute(CTX_ATTR_BT));
+ return static_cast<TorrentAttribute*>(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<ValueBase>& torrent,
const std::string& overrideName = "");
// Parses BitTorrent Magnet URI and returns
-// std::shared_ptr<TorrentAttribute> which includes infoHash, name and
+// std::unique_ptr<TorrentAttribute> 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<size_t>& 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<DownloadContext>& 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<std::pair<size_t, std::string> > 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<unsigned long>(rd->getRandomNumber(256));
}