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
std::shared_ptr<DownloadEngine>
std::unique_ptr<DownloadEngine>
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 =
op->getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS);
auto e = std::make_shared<DownloadEngine>(createEventPoll(op));
auto e = make_unique<DownloadEngine>(createEventPoll(op));
e->setOption(op);
{
auto requestGroupMan = make_unique<RequestGroupMan>

View File

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

View File

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

View File

@ -60,7 +60,7 @@ private:
std::shared_ptr<UriListParser> uriListParser_;
std::shared_ptr<DownloadEngine> e_;
std::unique_ptr<DownloadEngine> e_;
sigset_t mask_;
@ -99,7 +99,7 @@ public:
// have completed.
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.
void setUseSignalHandler(bool useSignalHandler)

View File

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