summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2013-07-06 18:33:59 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2013-07-06 18:33:59 +0900
commit9130dc6776d1706478b11f1c19b5160b24c887da (patch)
tree3fbc0a92eab5ca6b9f8e0cfce76d4e5cc21cd509
parent81f46fbf924722e22e141119b4a0bfb3ed136fd0 (diff)
DownloadEngine:: Use std::unique_ptr for webSocketSessionMan_
Notifier::addDownloadEventListener now takes pointer to DownloadEventListener. Session holds unique_ptr to ApiCallbackDownloadEventListener object.
-rw-r--r--src/DownloadEngine.cc4
-rw-r--r--src/DownloadEngine.h8
-rw-r--r--src/MultiUrlRequestInfo.cc6
-rw-r--r--src/Notifier.cc8
-rw-r--r--src/Notifier.h5
-rw-r--r--src/aria2api.cc25
-rw-r--r--src/aria2api.h2
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<rpc::WebSocketSessionMan>& wsman)
+(std::unique_ptr<rpc::WebSocketSessionMan> 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> 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_;
}
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<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
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<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);
}
}
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<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
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> 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)
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> context;
+ std::unique_ptr<ApiCallbackDownloadEventListener> listener;
};
} // namespace aria2