Просмотр исходного кода

DownloadEngine:: Use std::unique_ptr for webSocketSessionMan_

Notifier::addDownloadEventListener now takes pointer to
DownloadEventListener. Session holds unique_ptr to
ApiCallbackDownloadEventListener object.
Tatsuhiro Tsujikawa 12 лет назад
Родитель
Сommit
9130dc6776
7 измененных файлов с 25 добавлено и 33 удалено
  1. 2 2
      src/DownloadEngine.cc
  2. 4 4
      src/DownloadEngine.h
  3. 2 4
      src/MultiUrlRequestInfo.cc
  4. 3 5
      src/Notifier.cc
  5. 2 3
      src/Notifier.h
  6. 10 15
      src/aria2api.cc
  7. 2 0
      src/aria2api.h

+ 2 - 2
src/DownloadEngine.cc

@@ -610,9 +610,9 @@ void DownloadEngine::setAsyncDNSServers(ares_addr_node* asyncDNSServers)
 
 #ifdef ENABLE_WEBSOCKET
 void DownloadEngine::setWebSocketSessionMan
-(const std::shared_ptr<rpc::WebSocketSessionMan>& wsman)
+(std::unique_ptr<rpc::WebSocketSessionMan> wsman)
 {
-  webSocketSessionMan_ = wsman;
+  webSocketSessionMan_ = std::move(wsman);
 }
 #endif // ENABLE_WEBSOCKET
 

+ 4 - 4
src/DownloadEngine.h

@@ -150,7 +150,7 @@ private:
   std::unique_ptr<AuthConfigFactory> authConfigFactory_;
 
 #ifdef ENABLE_WEBSOCKET
-  std::shared_ptr<rpc::WebSocketSessionMan> webSocketSessionMan_;
+  std::unique_ptr<rpc::WebSocketSessionMan> webSocketSessionMan_;
 #endif // ENABLE_WEBSOCKET
 
   /**
@@ -352,9 +352,9 @@ public:
 #endif // HAVE_ARES_ADDR_NODE
 
 #ifdef ENABLE_WEBSOCKET
-  void setWebSocketSessionMan
-  (const std::shared_ptr<rpc::WebSocketSessionMan>& wsman);
-  const std::shared_ptr<rpc::WebSocketSessionMan>& getWebSocketSessionMan() const
+  void setWebSocketSessionMan(std::unique_ptr<rpc::WebSocketSessionMan> wsman);
+  const std::unique_ptr<rpc::WebSocketSessionMan>& getWebSocketSessionMan()
+    const
   {
     return webSocketSessionMan_;
   }

+ 2 - 4
src/MultiUrlRequestInfo.cc

@@ -166,11 +166,9 @@ int MultiUrlRequestInfo::prepare()
 
 #ifdef ENABLE_WEBSOCKET
     if(option_->getAsBool(PREF_ENABLE_RPC)) {
-      std::shared_ptr<rpc::WebSocketSessionMan> wsSessionMan
-        (new rpc::WebSocketSessionMan());
-      e_->setWebSocketSessionMan(wsSessionMan);
+      e_->setWebSocketSessionMan(make_unique<rpc::WebSocketSessionMan>());
       SingletonHolder<Notifier>::instance()->addDownloadEventListener
-        (wsSessionMan);
+        (e_->getWebSocketSessionMan().get());
     }
 #endif // ENABLE_WEBSOCKET
 

+ 3 - 5
src/Notifier.cc

@@ -42,8 +42,7 @@ Notifier::Notifier() {}
 
 Notifier::~Notifier() {}
 
-void Notifier::addDownloadEventListener
-(const std::shared_ptr<DownloadEventListener>& listener)
+void Notifier::addDownloadEventListener(DownloadEventListener* listener)
 {
   listeners_.push_back(listener);
 }
@@ -51,9 +50,8 @@ void Notifier::addDownloadEventListener
 void Notifier::notifyDownloadEvent
 (DownloadEvent event, const RequestGroup* group)
 {
-  for(std::vector<std::shared_ptr<DownloadEventListener> >::const_iterator i =
-        listeners_.begin(), eoi = listeners_.end(); i != eoi; ++i) {
-    (*i)->onEvent(event, group);
+  for(auto listener : listeners_) {
+    listener->onEvent(event, group);
   }
 }
 

+ 2 - 3
src/Notifier.h

@@ -55,8 +55,7 @@ class Notifier {
 public:
   Notifier();
   ~Notifier();
-  void addDownloadEventListener
-  (const std::shared_ptr<DownloadEventListener>& listener);
+  void addDownloadEventListener(DownloadEventListener* listener);
   // Notifies the download event to all listeners.
   void notifyDownloadEvent(DownloadEvent event, const RequestGroup* group);
 
@@ -66,7 +65,7 @@ public:
     notifyDownloadEvent(event, group.get());
   }
 private:
-  std::vector<std::shared_ptr<DownloadEventListener> > listeners_;
+  std::vector<DownloadEventListener*> listeners_;
 };
 
 } // namespace aria2

+ 10 - 15
src/aria2api.cc

@@ -108,11 +108,11 @@ int libraryDeinit()
 Session* sessionNew(const KeyVals& options, const SessionConfig& config)
 {
   int rv;
-  Session* session;
+  std::unique_ptr<Session> session;
   try {
-    session = new Session(options);
+    session = make_unique<Session>(options);
   } catch(RecoverableException& e) {
-    return 0;
+    return nullptr;
   }
   if(session->context->reqinfo) {
     if(!config.useSignalHandler) {
@@ -120,29 +120,24 @@ Session* sessionNew(const KeyVals& options, const SessionConfig& config)
     }
     rv = session->context->reqinfo->prepare();
     if(rv != 0) {
-      delete session;
-      return 0;
+      return nullptr;
     }
-    const std::shared_ptr<DownloadEngine>& e =
-      session->context->reqinfo->getDownloadEngine();
+    auto& e = session->context->reqinfo->getDownloadEngine();
     if(config.keepRunning) {
       e->getRequestGroupMan()->setKeepRunning(true);
       // Add command to make aria2 keep event polling
       e->addCommand(make_unique<KeepRunningCommand>(e->newCUID(), e.get()));
     }
     if(config.downloadEventCallback) {
-      std::shared_ptr<DownloadEventListener> listener
-        (new ApiCallbackDownloadEventListener(session,
-                                              config.downloadEventCallback,
-                                              config.userData));
+      session->listener = make_unique<ApiCallbackDownloadEventListener>
+        (session.get(), config.downloadEventCallback, config.userData);
       SingletonHolder<Notifier>::instance()
-        ->addDownloadEventListener(listener);
+        ->addDownloadEventListener(session->listener.get());
     }
   } else {
-    delete session;
-    return 0;
+    return nullptr;
   }
-  return session;
+  return session.release();
 }
 
 int sessionFinal(Session* session)

+ 2 - 0
src/aria2api.h

@@ -44,11 +44,13 @@
 namespace aria2 {
 
 struct Context;
+class ApiCallbackDownloadEventListener;
 
 struct Session {
   Session(const KeyVals& options);
   ~Session();
   std::shared_ptr<Context> context;
+  std::unique_ptr<ApiCallbackDownloadEventListener> listener;
 };
 
 } // namespace aria2