MultiUrlRequestFactory: Use std::unique_ptr for DownloadEngine

pull/106/head
Tatsuhiro Tsujikawa 2013-07-06 19:45:01 +09:00
parent 00e27e4fa4
commit 5378ed8c43
5 changed files with 27 additions and 43 deletions

View File

@ -146,13 +146,13 @@ std::unique_ptr<EventPoll> createEventPoll(Option* op)
} }
} // namespace } // namespace
std::shared_ptr<DownloadEngine> std::unique_ptr<DownloadEngine>
DownloadEngineFactory::newDownloadEngine DownloadEngineFactory::newDownloadEngine
(Option* op, std::vector<std::shared_ptr<RequestGroup> > requestGroups) (Option* op, std::vector<std::shared_ptr<RequestGroup>> requestGroups)
{ {
const size_t MAX_CONCURRENT_DOWNLOADS = const size_t MAX_CONCURRENT_DOWNLOADS =
op->getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS); op->getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS);
auto e = std::make_shared<DownloadEngine>(createEventPoll(op)); auto e = make_unique<DownloadEngine>(createEventPoll(op));
e->setOption(op); e->setOption(op);
{ {
auto requestGroupMan = make_unique<RequestGroupMan> auto requestGroupMan = make_unique<RequestGroupMan>

View File

@ -50,9 +50,9 @@ class DownloadEngineFactory {
public: public:
DownloadEngineFactory(); DownloadEngineFactory();
std::shared_ptr<DownloadEngine> std::unique_ptr<DownloadEngine>
newDownloadEngine newDownloadEngine
(Option* op, std::vector<std::shared_ptr<RequestGroup> > requestGroups); (Option* op, std::vector<std::shared_ptr<RequestGroup>> requestGroups);
}; };
} // namespace aria2 } // namespace aria2

View File

@ -391,7 +391,7 @@ void MultiUrlRequestInfo::resetSignalHandlers()
#endif // SIGPIPE #endif // SIGPIPE
} }
const std::shared_ptr<DownloadEngine>& const std::unique_ptr<DownloadEngine>&
MultiUrlRequestInfo::getDownloadEngine() const MultiUrlRequestInfo::getDownloadEngine() const
{ {
return e_; return e_;

View File

@ -60,7 +60,7 @@ private:
std::shared_ptr<UriListParser> uriListParser_; std::shared_ptr<UriListParser> uriListParser_;
std::shared_ptr<DownloadEngine> e_; std::unique_ptr<DownloadEngine> e_;
sigset_t mask_; sigset_t mask_;
@ -99,7 +99,7 @@ public:
// have completed. // have completed.
error_code::Value getResult(); error_code::Value getResult();
const std::shared_ptr<DownloadEngine>& getDownloadEngine() const; const std::unique_ptr<DownloadEngine>& getDownloadEngine() const;
// Signal handlers are not prepared if false is given. // Signal handlers are not prepared if false is given.
void setUseSignalHandler(bool useSignalHandler) void setUseSignalHandler(bool useSignalHandler)

View File

@ -149,15 +149,13 @@ int sessionFinal(Session* session)
int run(Session* session, RUN_MODE mode) int run(Session* session, RUN_MODE mode)
{ {
const std::shared_ptr<DownloadEngine>& e = auto& e = session->context->reqinfo->getDownloadEngine();
session->context->reqinfo->getDownloadEngine();
return e->run(mode == RUN_ONCE); return e->run(mode == RUN_ONCE);
} }
int shutdown(Session* session, bool force) int shutdown(Session* session, bool force)
{ {
const std::shared_ptr<DownloadEngine>& e = auto& e = session->context->reqinfo->getDownloadEngine();
session->context->reqinfo->getDownloadEngine();
if(force) { if(force) {
e->requestForceHalt(); e->requestForceHalt();
} else { } else {
@ -254,7 +252,7 @@ void apiGatherChangeableGlobalOption
namespace { namespace {
void addRequestGroup(const std::shared_ptr<RequestGroup>& group, void addRequestGroup(const std::shared_ptr<RequestGroup>& group,
const std::shared_ptr<DownloadEngine>& e, DownloadEngine* e,
int position) int position)
{ {
if(position >= 0) { if(position >= 0) {
@ -271,8 +269,7 @@ int addUri(Session* session,
const KeyVals& options, const KeyVals& options,
int position) int position)
{ {
const std::shared_ptr<DownloadEngine>& e = auto& e = session->context->reqinfo->getDownloadEngine();
session->context->reqinfo->getDownloadEngine();
std::shared_ptr<Option> requestOption(new Option(*e->getOption())); std::shared_ptr<Option> requestOption(new Option(*e->getOption()));
try { try {
apiGatherRequestOption(requestOption.get(), options, apiGatherRequestOption(requestOption.get(), options,
@ -286,7 +283,7 @@ int addUri(Session* session,
/* ignoreForceSeq = */ true, /* ignoreForceSeq = */ true,
/* ignoreLocalPath = */ true); /* ignoreLocalPath = */ true);
if(!result.empty()) { if(!result.empty()) {
addRequestGroup(result.front(), e, position); addRequestGroup(result.front(), e.get(), position);
if(gid) { if(gid) {
*gid = result.front()->getGID(); *gid = result.front()->getGID();
} }
@ -301,8 +298,7 @@ int addMetalink(Session* session,
int position) int position)
{ {
#ifdef ENABLE_METALINK #ifdef ENABLE_METALINK
const std::shared_ptr<DownloadEngine>& e = auto& e = session->context->reqinfo->getDownloadEngine();
session->context->reqinfo->getDownloadEngine();
std::shared_ptr<Option> requestOption(new Option(*e->getOption())); std::shared_ptr<Option> requestOption(new Option(*e->getOption()));
std::vector<std::shared_ptr<RequestGroup> > result; std::vector<std::shared_ptr<RequestGroup> > result;
try { try {
@ -341,8 +337,7 @@ int addTorrent(Session* session,
int position) int position)
{ {
#ifdef ENABLE_BITTORRENT #ifdef ENABLE_BITTORRENT
const std::shared_ptr<DownloadEngine>& e = auto& e = session->context->reqinfo->getDownloadEngine();
session->context->reqinfo->getDownloadEngine();
std::shared_ptr<Option> requestOption(new Option(*e->getOption())); std::shared_ptr<Option> requestOption(new Option(*e->getOption()));
std::vector<std::shared_ptr<RequestGroup> > result; std::vector<std::shared_ptr<RequestGroup> > result;
try { try {
@ -356,7 +351,7 @@ int addTorrent(Session* session,
return -1; return -1;
} }
if(!result.empty()) { if(!result.empty()) {
addRequestGroup(result.front(), e, position); addRequestGroup(result.front(), e.get(), position);
if(gid) { if(gid) {
*gid = result.front()->getGID(); *gid = result.front()->getGID();
} }
@ -379,8 +374,7 @@ int addTorrent(Session* session,
int removeDownload(Session* session, A2Gid gid, bool force) int removeDownload(Session* session, A2Gid gid, bool force)
{ {
const std::shared_ptr<DownloadEngine>& e = auto& e = session->context->reqinfo->getDownloadEngine();
session->context->reqinfo->getDownloadEngine();
std::shared_ptr<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid); std::shared_ptr<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid);
if(group) { if(group) {
if(group->getState() == RequestGroup::STATE_ACTIVE) { if(group->getState() == RequestGroup::STATE_ACTIVE) {
@ -405,8 +399,7 @@ int removeDownload(Session* session, A2Gid gid, bool force)
int pauseDownload(Session* session, A2Gid gid, bool force) int pauseDownload(Session* session, A2Gid gid, bool force)
{ {
const std::shared_ptr<DownloadEngine>& e = auto& e = session->context->reqinfo->getDownloadEngine();
session->context->reqinfo->getDownloadEngine();
std::shared_ptr<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid); std::shared_ptr<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid);
if(group) { if(group) {
bool reserved = group->getState() == RequestGroup::STATE_WAITING; bool reserved = group->getState() == RequestGroup::STATE_WAITING;
@ -420,8 +413,7 @@ int pauseDownload(Session* session, A2Gid gid, bool force)
int unpauseDownload(Session* session, A2Gid gid) int unpauseDownload(Session* session, A2Gid gid)
{ {
const std::shared_ptr<DownloadEngine>& e = auto& e = session->context->reqinfo->getDownloadEngine();
session->context->reqinfo->getDownloadEngine();
std::shared_ptr<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid); std::shared_ptr<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid);
if(!group || if(!group ||
group->getState() != RequestGroup::STATE_WAITING || group->getState() != RequestGroup::STATE_WAITING ||
@ -436,8 +428,7 @@ int unpauseDownload(Session* session, A2Gid gid)
int changePosition(Session* session, A2Gid gid, int pos, OffsetMode how) int changePosition(Session* session, A2Gid gid, int pos, OffsetMode how)
{ {
const std::shared_ptr<DownloadEngine>& e = auto& e = session->context->reqinfo->getDownloadEngine();
session->context->reqinfo->getDownloadEngine();
try { try {
return e->getRequestGroupMan()->changeReservedGroupPosition(gid, pos, how); return e->getRequestGroupMan()->changeReservedGroupPosition(gid, pos, how);
} catch(RecoverableException& e) { } catch(RecoverableException& e) {
@ -448,8 +439,7 @@ int changePosition(Session* session, A2Gid gid, int pos, OffsetMode how)
int changeOption(Session* session, A2Gid gid, const KeyVals& options) int changeOption(Session* session, A2Gid gid, const KeyVals& options)
{ {
const std::shared_ptr<DownloadEngine>& e = auto& e = session->context->reqinfo->getDownloadEngine();
session->context->reqinfo->getDownloadEngine();
std::shared_ptr<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid); std::shared_ptr<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid);
if(group) { if(group) {
Option option; Option option;
@ -474,8 +464,7 @@ int changeOption(Session* session, A2Gid gid, const KeyVals& options)
const std::string& getGlobalOption(Session* session, const std::string& name) const std::string& getGlobalOption(Session* session, const std::string& name)
{ {
const std::shared_ptr<DownloadEngine>& e = auto& e = session->context->reqinfo->getDownloadEngine();
session->context->reqinfo->getDownloadEngine();
const Pref* pref = option::k2p(name); const Pref* pref = option::k2p(name);
if(OptionParser::getInstance()->find(pref)) { if(OptionParser::getInstance()->find(pref)) {
return e->getOption()->get(pref); return e->getOption()->get(pref);
@ -486,8 +475,7 @@ const std::string& getGlobalOption(Session* session, const std::string& name)
KeyVals getGlobalOptions(Session* session) KeyVals getGlobalOptions(Session* session)
{ {
const std::shared_ptr<DownloadEngine>& e = auto& e = session->context->reqinfo->getDownloadEngine();
session->context->reqinfo->getDownloadEngine();
const std::shared_ptr<OptionParser>& optionParser = OptionParser::getInstance(); const std::shared_ptr<OptionParser>& optionParser = OptionParser::getInstance();
const Option* option = e->getOption(); const Option* option = e->getOption();
KeyVals options; KeyVals options;
@ -502,8 +490,7 @@ KeyVals getGlobalOptions(Session* session)
int changeGlobalOption(Session* session, const KeyVals& options) int changeGlobalOption(Session* session, const KeyVals& options)
{ {
const std::shared_ptr<DownloadEngine>& e = auto& e = session->context->reqinfo->getDownloadEngine();
session->context->reqinfo->getDownloadEngine();
Option option; Option option;
try { try {
apiGatherChangeableGlobalOption(&option, options, apiGatherChangeableGlobalOption(&option, options,
@ -518,8 +505,7 @@ int changeGlobalOption(Session* session, const KeyVals& options)
GlobalStat getGlobalStat(Session* session) GlobalStat getGlobalStat(Session* session)
{ {
const std::shared_ptr<DownloadEngine>& e = auto& e = session->context->reqinfo->getDownloadEngine();
session->context->reqinfo->getDownloadEngine();
auto& rgman = e->getRequestGroupMan(); auto& rgman = e->getRequestGroupMan();
TransferStat ts = rgman->calculateStat(); TransferStat ts = rgman->calculateStat();
GlobalStat res; GlobalStat res;
@ -533,8 +519,7 @@ GlobalStat getGlobalStat(Session* session)
std::vector<A2Gid> getActiveDownload(Session* session) std::vector<A2Gid> getActiveDownload(Session* session)
{ {
const std::shared_ptr<DownloadEngine>& e = auto& e = session->context->reqinfo->getDownloadEngine();
session->context->reqinfo->getDownloadEngine();
const RequestGroupList& groups = e->getRequestGroupMan()->getRequestGroups(); const RequestGroupList& groups = e->getRequestGroupMan()->getRequestGroups();
std::vector<A2Gid> res; std::vector<A2Gid> res;
for(RequestGroupList::const_iterator i = groups.begin(), for(RequestGroupList::const_iterator i = groups.begin(),
@ -938,8 +923,7 @@ struct DownloadResultDH : public DownloadHandle {
DownloadHandle* getDownloadHandle(Session* session, A2Gid gid) DownloadHandle* getDownloadHandle(Session* session, A2Gid gid)
{ {
const std::shared_ptr<DownloadEngine>& e = auto& e = session->context->reqinfo->getDownloadEngine();
session->context->reqinfo->getDownloadEngine();
auto& rgman = e->getRequestGroupMan(); auto& rgman = e->getRequestGroupMan();
std::shared_ptr<RequestGroup> group = rgman->findGroup(gid); std::shared_ptr<RequestGroup> group = rgman->findGroup(gid);
if(group) { if(group) {