From 9130dc6776d1706478b11f1c19b5160b24c887da Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 6 Jul 2013 18:33:59 +0900 Subject: [PATCH] DownloadEngine:: Use std::unique_ptr for webSocketSessionMan_ Notifier::addDownloadEventListener now takes pointer to DownloadEventListener. Session holds unique_ptr to ApiCallbackDownloadEventListener object. --- src/DownloadEngine.cc | 4 ++-- src/DownloadEngine.h | 8 ++++---- src/MultiUrlRequestInfo.cc | 6 ++---- src/Notifier.cc | 8 +++----- src/Notifier.h | 5 ++--- src/aria2api.cc | 25 ++++++++++--------------- src/aria2api.h | 2 ++ 7 files changed, 25 insertions(+), 33 deletions(-) diff --git a/src/DownloadEngine.cc b/src/DownloadEngine.cc index 23112f59..47426960 100644 --- a/src/DownloadEngine.cc +++ b/src/DownloadEngine.cc @@ -610,9 +610,9 @@ void DownloadEngine::setAsyncDNSServers(ares_addr_node* asyncDNSServers) #ifdef ENABLE_WEBSOCKET void DownloadEngine::setWebSocketSessionMan -(const std::shared_ptr& wsman) +(std::unique_ptr wsman) { - webSocketSessionMan_ = wsman; + webSocketSessionMan_ = std::move(wsman); } #endif // ENABLE_WEBSOCKET diff --git a/src/DownloadEngine.h b/src/DownloadEngine.h index 6dbd341c..b1b10b54 100644 --- a/src/DownloadEngine.h +++ b/src/DownloadEngine.h @@ -150,7 +150,7 @@ private: std::unique_ptr authConfigFactory_; #ifdef ENABLE_WEBSOCKET - std::shared_ptr webSocketSessionMan_; + std::unique_ptr webSocketSessionMan_; #endif // ENABLE_WEBSOCKET /** @@ -352,9 +352,9 @@ public: #endif // HAVE_ARES_ADDR_NODE #ifdef ENABLE_WEBSOCKET - void setWebSocketSessionMan - (const std::shared_ptr& wsman); - const std::shared_ptr& getWebSocketSessionMan() const + void setWebSocketSessionMan(std::unique_ptr wsman); + const std::unique_ptr& getWebSocketSessionMan() + const { return webSocketSessionMan_; } diff --git a/src/MultiUrlRequestInfo.cc b/src/MultiUrlRequestInfo.cc index 74ab10d5..546320a1 100644 --- a/src/MultiUrlRequestInfo.cc +++ b/src/MultiUrlRequestInfo.cc @@ -166,11 +166,9 @@ int MultiUrlRequestInfo::prepare() #ifdef ENABLE_WEBSOCKET if(option_->getAsBool(PREF_ENABLE_RPC)) { - std::shared_ptr wsSessionMan - (new rpc::WebSocketSessionMan()); - e_->setWebSocketSessionMan(wsSessionMan); + e_->setWebSocketSessionMan(make_unique()); SingletonHolder::instance()->addDownloadEventListener - (wsSessionMan); + (e_->getWebSocketSessionMan().get()); } #endif // ENABLE_WEBSOCKET diff --git a/src/Notifier.cc b/src/Notifier.cc index da43a8cf..d26200eb 100644 --- a/src/Notifier.cc +++ b/src/Notifier.cc @@ -42,8 +42,7 @@ Notifier::Notifier() {} Notifier::~Notifier() {} -void Notifier::addDownloadEventListener -(const std::shared_ptr& 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 >::const_iterator i = - listeners_.begin(), eoi = listeners_.end(); i != eoi; ++i) { - (*i)->onEvent(event, group); + for(auto listener : listeners_) { + listener->onEvent(event, group); } } diff --git a/src/Notifier.h b/src/Notifier.h index e4586c6c..b037f6dd 100644 --- a/src/Notifier.h +++ b/src/Notifier.h @@ -55,8 +55,7 @@ class Notifier { public: Notifier(); ~Notifier(); - void addDownloadEventListener - (const std::shared_ptr& 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 > listeners_; + std::vector listeners_; }; } // namespace aria2 diff --git a/src/aria2api.cc b/src/aria2api.cc index c3833bff..4440cd85 100644 --- a/src/aria2api.cc +++ b/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; try { - session = new Session(options); + session = make_unique(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& 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(e->newCUID(), e.get())); } if(config.downloadEventCallback) { - std::shared_ptr listener - (new ApiCallbackDownloadEventListener(session, - config.downloadEventCallback, - config.userData)); + session->listener = make_unique + (session.get(), config.downloadEventCallback, config.userData); SingletonHolder::instance() - ->addDownloadEventListener(listener); + ->addDownloadEventListener(session->listener.get()); } } else { - delete session; - return 0; + return nullptr; } - return session; + return session.release(); } int sessionFinal(Session* session) diff --git a/src/aria2api.h b/src/aria2api.h index 59985dca..eabfa6f4 100644 --- a/src/aria2api.h +++ b/src/aria2api.h @@ -44,11 +44,13 @@ namespace aria2 { struct Context; +class ApiCallbackDownloadEventListener; struct Session { Session(const KeyVals& options); ~Session(); std::shared_ptr context; + std::unique_ptr listener; }; } // namespace aria2