DownloadEngine: Use std::unique_ptr for eventPoll_

pull/106/head
Tatsuhiro Tsujikawa 2013-07-06 19:39:16 +09:00
parent f83b0fcfa3
commit 00e27e4fa4
6 changed files with 32 additions and 26 deletions

View File

@ -87,8 +87,8 @@ volatile sig_atomic_t globalHaltRequested = 0;
} // namespace global } // namespace global
DownloadEngine::DownloadEngine(const std::shared_ptr<EventPoll>& eventPoll) DownloadEngine::DownloadEngine(std::unique_ptr<EventPoll> eventPoll)
: eventPoll_(eventPoll), : eventPoll_(std::move(eventPoll)),
haltRequested_(0), haltRequested_(0),
noWait_(true), noWait_(true),
refreshInterval_(DEFAULT_REFRESH_INTERVAL), refreshInterval_(DEFAULT_REFRESH_INTERVAL),

View File

@ -80,7 +80,7 @@ private:
std::string sessionId_; std::string sessionId_;
std::shared_ptr<EventPoll> eventPoll_; std::unique_ptr<EventPoll> eventPoll_;
std::unique_ptr<StatCalc> statCalc_; std::unique_ptr<StatCalc> statCalc_;
@ -173,7 +173,7 @@ private:
std::unique_ptr<CheckIntegrityMan> checkIntegrityMan_; std::unique_ptr<CheckIntegrityMan> checkIntegrityMan_;
Option* option_; Option* option_;
public: public:
DownloadEngine(const std::shared_ptr<EventPoll>& eventPoll); DownloadEngine(std::unique_ptr<EventPoll> eventPoll);
~DownloadEngine(); ~DownloadEngine();

View File

@ -84,30 +84,27 @@ namespace aria2 {
DownloadEngineFactory::DownloadEngineFactory() {} DownloadEngineFactory::DownloadEngineFactory() {}
std::shared_ptr<DownloadEngine> namespace {
DownloadEngineFactory::newDownloadEngine std::unique_ptr<EventPoll> createEventPoll(Option* op)
(Option* op, std::vector<std::shared_ptr<RequestGroup> > requestGroups)
{ {
const size_t MAX_CONCURRENT_DOWNLOADS =
op->getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS);
std::shared_ptr<EventPoll> eventPoll;
const std::string& pollMethod = op->get(PREF_EVENT_POLL); const std::string& pollMethod = op->get(PREF_EVENT_POLL);
#ifdef HAVE_LIBUV #ifdef HAVE_LIBUV
if (pollMethod == V_LIBUV) { if (pollMethod == V_LIBUV) {
std::shared_ptr<LibuvEventPoll> ep(new LibuvEventPoll()); auto ep = make_unique<LibuvEventPoll>();
if (!ep->good()) { if(ep->good()) {
return std::move(ep);
} else {
throw DL_ABORT_EX("Initializing LibuvEventPoll failed." throw DL_ABORT_EX("Initializing LibuvEventPoll failed."
" Try --event-poll=select"); " Try --event-poll=select");
} }
eventPoll = ep;
} }
else else
#endif // HAVE_LIBUV #endif // HAVE_LIBUV
#ifdef HAVE_EPOLL #ifdef HAVE_EPOLL
if(pollMethod == V_EPOLL) { if(pollMethod == V_EPOLL) {
std::shared_ptr<EpollEventPoll> ep(new EpollEventPoll()); auto ep = make_unique<EpollEventPoll>();
if(ep->good()) { if(ep->good()) {
eventPoll = ep; return std::move(ep);
} else { } else {
throw DL_ABORT_EX("Initializing EpollEventPoll failed." throw DL_ABORT_EX("Initializing EpollEventPoll failed."
" Try --event-poll=select"); " Try --event-poll=select");
@ -116,9 +113,9 @@ DownloadEngineFactory::newDownloadEngine
#endif // HAVE_EPLL #endif // HAVE_EPLL
#ifdef HAVE_KQUEUE #ifdef HAVE_KQUEUE
if(pollMethod == V_KQUEUE) { if(pollMethod == V_KQUEUE) {
std::shared_ptr<KqueueEventPoll> kp(new KqueueEventPoll()); auto kp = make_unique<KqueueEventPoll>();
if(kp->good()) { if(kp->good()) {
eventPoll = kp; return std::move(kp);
} else { } else {
throw DL_ABORT_EX("Initializing KqueueEventPoll failed." throw DL_ABORT_EX("Initializing KqueueEventPoll failed."
" Try --event-poll=select"); " Try --event-poll=select");
@ -127,9 +124,9 @@ DownloadEngineFactory::newDownloadEngine
#endif // HAVE_KQUEUE #endif // HAVE_KQUEUE
#ifdef HAVE_PORT_ASSOCIATE #ifdef HAVE_PORT_ASSOCIATE
if(pollMethod == V_PORT) { if(pollMethod == V_PORT) {
std::shared_ptr<PortEventPoll> pp(new PortEventPoll()); auto pp = make_unique<PortEventPoll>();
if(pp->good()) { if(pp->good()) {
eventPoll = pp; return std::move(pp);
} else { } else {
throw DL_ABORT_EX("Initializing PortEventPoll failed." throw DL_ABORT_EX("Initializing PortEventPoll failed."
" Try --event-poll=select"); " Try --event-poll=select");
@ -138,15 +135,24 @@ DownloadEngineFactory::newDownloadEngine
#endif // HAVE_PORT_ASSOCIATE #endif // HAVE_PORT_ASSOCIATE
#ifdef HAVE_POLL #ifdef HAVE_POLL
if(pollMethod == V_POLL) { if(pollMethod == V_POLL) {
eventPoll.reset(new PollEventPoll()); return make_unique<PollEventPoll>();
} else } else
#endif // HAVE_POLL #endif // HAVE_POLL
if(pollMethod == V_SELECT) { if(pollMethod == V_SELECT) {
eventPoll.reset(new SelectEventPoll()); return make_unique<SelectEventPoll>();
} else { } else {
abort(); assert(0);
} }
std::shared_ptr<DownloadEngine> e(new DownloadEngine(eventPoll)); }
} // namespace
std::shared_ptr<DownloadEngine>
DownloadEngineFactory::newDownloadEngine
(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));
e->setOption(op); e->setOption(op);
{ {
auto requestGroupMan = make_unique<RequestGroupMan> auto requestGroupMan = make_unique<RequestGroupMan>

View File

@ -48,7 +48,7 @@ public:
// To enable paused RequestGroup // To enable paused RequestGroup
option_->put(PREF_ENABLE_RPC, A2_V_TRUE); option_->put(PREF_ENABLE_RPC, A2_V_TRUE);
File(option_->get(PREF_DIR)).mkdirs(); File(option_->get(PREF_DIR)).mkdirs();
e_ = make_unique<DownloadEngine>(std::make_shared<SelectEventPoll>()); e_ = make_unique<DownloadEngine>(make_unique<SelectEventPoll>());
e_->setOption(option_.get()); e_->setOption(option_.get());
auto rgman = make_unique<RequestGroupMan> auto rgman = make_unique<RequestGroupMan>
(std::vector<std::shared_ptr<RequestGroup>>{}, 3, option_.get()); (std::vector<std::shared_ptr<RequestGroup>>{}, 3, option_.get());

View File

@ -89,7 +89,7 @@ public:
option_->put(PREF_PIECE_LENGTH, "1048576"); option_->put(PREF_PIECE_LENGTH, "1048576");
option_->put(PREF_MAX_DOWNLOAD_RESULT, "10"); option_->put(PREF_MAX_DOWNLOAD_RESULT, "10");
File(option_->get(PREF_DIR)).mkdirs(); File(option_->get(PREF_DIR)).mkdirs();
e_ = make_unique<DownloadEngine>(std::make_shared<SelectEventPoll>()); e_ = make_unique<DownloadEngine>(make_unique<SelectEventPoll>());
e_->setOption(option_.get()); e_->setOption(option_.get());
e_->setRequestGroupMan e_->setRequestGroupMan
(make_unique<RequestGroupMan> (make_unique<RequestGroupMan>

View File

@ -78,7 +78,7 @@ void SessionSerializerTest::testSave()
rgman.addDownloadResult(drs[i]); rgman.addDownloadResult(drs[i]);
} }
DownloadEngine e(std::shared_ptr<EventPoll>(new SelectEventPoll())); DownloadEngine e(make_unique<SelectEventPoll>());
e.setOption(option.get()); e.setOption(option.get());
rgman.fillRequestGroupFromReserver(&e); rgman.fillRequestGroupFromReserver(&e);
CPPUNIT_ASSERT_EQUAL((size_t)1, rgman.getRequestGroups().size()); CPPUNIT_ASSERT_EQUAL((size_t)1, rgman.getRequestGroups().size());