diff --git a/src/DownloadEngine.cc b/src/DownloadEngine.cc index 62fb9d66e..348efa827 100644 --- a/src/DownloadEngine.cc +++ b/src/DownloadEngine.cc @@ -87,8 +87,8 @@ volatile sig_atomic_t globalHaltRequested = 0; } // namespace global -DownloadEngine::DownloadEngine(const std::shared_ptr& eventPoll) - : eventPoll_(eventPoll), +DownloadEngine::DownloadEngine(std::unique_ptr eventPoll) + : eventPoll_(std::move(eventPoll)), haltRequested_(0), noWait_(true), refreshInterval_(DEFAULT_REFRESH_INTERVAL), diff --git a/src/DownloadEngine.h b/src/DownloadEngine.h index efc9fcc4f..dad7773a0 100644 --- a/src/DownloadEngine.h +++ b/src/DownloadEngine.h @@ -80,7 +80,7 @@ private: std::string sessionId_; - std::shared_ptr eventPoll_; + std::unique_ptr eventPoll_; std::unique_ptr statCalc_; @@ -173,7 +173,7 @@ private: std::unique_ptr checkIntegrityMan_; Option* option_; public: - DownloadEngine(const std::shared_ptr& eventPoll); + DownloadEngine(std::unique_ptr eventPoll); ~DownloadEngine(); diff --git a/src/DownloadEngineFactory.cc b/src/DownloadEngineFactory.cc index 49e9a4bfb..0edb3ccc4 100644 --- a/src/DownloadEngineFactory.cc +++ b/src/DownloadEngineFactory.cc @@ -84,30 +84,27 @@ namespace aria2 { DownloadEngineFactory::DownloadEngineFactory() {} -std::shared_ptr -DownloadEngineFactory::newDownloadEngine -(Option* op, std::vector > requestGroups) +namespace { +std::unique_ptr createEventPoll(Option* op) { - const size_t MAX_CONCURRENT_DOWNLOADS = - op->getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS); - std::shared_ptr eventPoll; const std::string& pollMethod = op->get(PREF_EVENT_POLL); #ifdef HAVE_LIBUV if (pollMethod == V_LIBUV) { - std::shared_ptr ep(new LibuvEventPoll()); - if (!ep->good()) { + auto ep = make_unique(); + if(ep->good()) { + return std::move(ep); + } else { throw DL_ABORT_EX("Initializing LibuvEventPoll failed." " Try --event-poll=select"); } - eventPoll = ep; } else #endif // HAVE_LIBUV #ifdef HAVE_EPOLL if(pollMethod == V_EPOLL) { - std::shared_ptr ep(new EpollEventPoll()); + auto ep = make_unique(); if(ep->good()) { - eventPoll = ep; + return std::move(ep); } else { throw DL_ABORT_EX("Initializing EpollEventPoll failed." " Try --event-poll=select"); @@ -116,9 +113,9 @@ DownloadEngineFactory::newDownloadEngine #endif // HAVE_EPLL #ifdef HAVE_KQUEUE if(pollMethod == V_KQUEUE) { - std::shared_ptr kp(new KqueueEventPoll()); + auto kp = make_unique(); if(kp->good()) { - eventPoll = kp; + return std::move(kp); } else { throw DL_ABORT_EX("Initializing KqueueEventPoll failed." " Try --event-poll=select"); @@ -127,9 +124,9 @@ DownloadEngineFactory::newDownloadEngine #endif // HAVE_KQUEUE #ifdef HAVE_PORT_ASSOCIATE if(pollMethod == V_PORT) { - std::shared_ptr pp(new PortEventPoll()); + auto pp = make_unique(); if(pp->good()) { - eventPoll = pp; + return std::move(pp); } else { throw DL_ABORT_EX("Initializing PortEventPoll failed." " Try --event-poll=select"); @@ -138,15 +135,24 @@ DownloadEngineFactory::newDownloadEngine #endif // HAVE_PORT_ASSOCIATE #ifdef HAVE_POLL if(pollMethod == V_POLL) { - eventPoll.reset(new PollEventPoll()); + return make_unique(); } else #endif // HAVE_POLL if(pollMethod == V_SELECT) { - eventPoll.reset(new SelectEventPoll()); + return make_unique(); } else { - abort(); + assert(0); } - std::shared_ptr e(new DownloadEngine(eventPoll)); +} +} // namespace + +std::shared_ptr +DownloadEngineFactory::newDownloadEngine +(Option* op, std::vector > requestGroups) +{ + const size_t MAX_CONCURRENT_DOWNLOADS = + op->getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS); + auto e = std::make_shared(createEventPoll(op)); e->setOption(op); { auto requestGroupMan = make_unique diff --git a/test/RequestGroupManTest.cc b/test/RequestGroupManTest.cc index 47f229599..5701a7b97 100644 --- a/test/RequestGroupManTest.cc +++ b/test/RequestGroupManTest.cc @@ -48,7 +48,7 @@ public: // To enable paused RequestGroup option_->put(PREF_ENABLE_RPC, A2_V_TRUE); File(option_->get(PREF_DIR)).mkdirs(); - e_ = make_unique(std::make_shared()); + e_ = make_unique(make_unique()); e_->setOption(option_.get()); auto rgman = make_unique (std::vector>{}, 3, option_.get()); diff --git a/test/RpcMethodTest.cc b/test/RpcMethodTest.cc index 7c12db432..0e6e0f053 100644 --- a/test/RpcMethodTest.cc +++ b/test/RpcMethodTest.cc @@ -89,7 +89,7 @@ public: option_->put(PREF_PIECE_LENGTH, "1048576"); option_->put(PREF_MAX_DOWNLOAD_RESULT, "10"); File(option_->get(PREF_DIR)).mkdirs(); - e_ = make_unique(std::make_shared()); + e_ = make_unique(make_unique()); e_->setOption(option_.get()); e_->setRequestGroupMan (make_unique diff --git a/test/SessionSerializerTest.cc b/test/SessionSerializerTest.cc index 1019eb2f4..9d996d4cc 100644 --- a/test/SessionSerializerTest.cc +++ b/test/SessionSerializerTest.cc @@ -78,7 +78,7 @@ void SessionSerializerTest::testSave() rgman.addDownloadResult(drs[i]); } - DownloadEngine e(std::shared_ptr(new SelectEventPoll())); + DownloadEngine e(make_unique()); e.setOption(option.get()); rgman.fillRequestGroupFromReserver(&e); CPPUNIT_ASSERT_EQUAL((size_t)1, rgman.getRequestGroups().size());