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.
pull/103/head
Tatsuhiro Tsujikawa 2013-06-26 23:56:43 +09:00
parent 47402c5f29
commit da7400ef5c
18 changed files with 35 additions and 33 deletions

View File

@ -69,7 +69,7 @@ DefaultBtAnnounce::DefaultBtAnnounce
incomplete_(0),
announceList_(bittorrent::getTorrentAttrs(downloadContext)->announceList),
option_(option),
randomizer_(SimpleRandomizer::getInstance()),
randomizer_(SimpleRandomizer::getInstance().get()),
tcpPort_(0)
{}

View File

@ -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

View File

@ -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)));

View File

@ -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;

View File

@ -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)

View File

@ -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);

View File

@ -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()->

View File

@ -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_);

View File

@ -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();

View File

@ -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()

View File

@ -56,7 +56,7 @@ private:
SimpleRandomizer();
public:
static SimpleRandomizer* getInstance();
static const std::unique_ptr<SimpleRandomizer>& getInstance();
static void init();

View File

@ -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)

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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

View File

@ -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);

View File

@ -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));
}

View File

@ -41,7 +41,6 @@ public:
messageFactory_.reset(new WrapExtBtMessageFactory());
dispatcher_.reset(new MockBtMessageDispatcher());
dctx_.reset(new DownloadContext());
std::shared_ptr<TorrentAttribute> attrs(new TorrentAttribute());
dctx_->setAttribute(CTX_ATTR_BT, make_unique<TorrentAttribute>());
peer_.reset(new Peer("host", 6880));
peer_->allocateSessionResource(0, 0);