Browse Source

DownloadEngine: Use std::unique_ptr for eventPoll_

Tatsuhiro Tsujikawa 12 years ago
parent
commit
00e27e4fa4

+ 2 - 2
src/DownloadEngine.cc

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

+ 2 - 2
src/DownloadEngine.h

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

+ 25 - 19
src/DownloadEngineFactory.cc

@@ -84,30 +84,27 @@ namespace aria2 {
 
 DownloadEngineFactory::DownloadEngineFactory() {}
 
-std::shared_ptr<DownloadEngine>
-DownloadEngineFactory::newDownloadEngine
-(Option* op, std::vector<std::shared_ptr<RequestGroup> > requestGroups)
+namespace {
+std::unique_ptr<EventPoll> createEventPoll(Option* op)
 {
-  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);
 #ifdef HAVE_LIBUV
   if (pollMethod == V_LIBUV) {
-    std::shared_ptr<LibuvEventPoll> ep(new LibuvEventPoll());
-    if (!ep->good()) {
+    auto ep = make_unique<LibuvEventPoll>();
+    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<EpollEventPoll> ep(new EpollEventPoll());
+    auto ep = make_unique<EpollEventPoll>();
     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<KqueueEventPoll> kp(new KqueueEventPoll());
+      auto kp = make_unique<KqueueEventPoll>();
       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<PortEventPoll> pp(new PortEventPoll());
+        auto pp = make_unique<PortEventPoll>();
         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<PollEventPoll>();
         } else
 #endif // HAVE_POLL
           if(pollMethod == V_SELECT) {
-            eventPoll.reset(new SelectEventPoll());
+            return make_unique<SelectEventPoll>();
           } 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);
   {
     auto requestGroupMan = make_unique<RequestGroupMan>

+ 1 - 1
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<DownloadEngine>(std::make_shared<SelectEventPoll>());
+    e_ = make_unique<DownloadEngine>(make_unique<SelectEventPoll>());
     e_->setOption(option_.get());
     auto rgman = make_unique<RequestGroupMan>
       (std::vector<std::shared_ptr<RequestGroup>>{}, 3, option_.get());

+ 1 - 1
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<DownloadEngine>(std::make_shared<SelectEventPoll>());
+    e_ = make_unique<DownloadEngine>(make_unique<SelectEventPoll>());
     e_->setOption(option_.get());
     e_->setRequestGroupMan
       (make_unique<RequestGroupMan>

+ 1 - 1
test/SessionSerializerTest.cc

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