소스 검색

Use std::unique_ptr in SingletonHolder

Tatsuhiro Tsujikawa 12 년 전
부모
커밋
5cb7ae0b86
3개의 변경된 파일10개의 추가작업 그리고 10개의 파일을 삭제
  1. 1 2
      src/MultiUrlRequestInfo.cc
  2. 6 6
      src/SingletonHolder.h
  3. 3 2
      test/SingletonHolderTest.cc

+ 1 - 2
src/MultiUrlRequestInfo.cc

@@ -137,8 +137,7 @@ int MultiUrlRequestInfo::prepare()
 {
   global::globalHaltRequested = 0;
   try {
-    std::shared_ptr<Notifier> notifier(new Notifier());
-    SingletonHolder<Notifier>::instance(notifier);
+    SingletonHolder<Notifier>::instance(make_unique<Notifier>());
 
 #ifdef ENABLE_SSL
     if(option_->getAsBool(PREF_ENABLE_RPC) &&

+ 6 - 6
src/SingletonHolder.h

@@ -44,20 +44,20 @@ namespace aria2 {
 template<typename T>
 class SingletonHolder {
 private:
-  static std::shared_ptr<T> instance_;
+  static std::unique_ptr<T> instance_;
 
   SingletonHolder() {}
 public:
   ~SingletonHolder() {}
 
-  static const std::shared_ptr<T>& instance()
+  static T* instance()
   {
-    return instance_;
+    return instance_.get();
   }
 
-  static void instance(const std::shared_ptr<T>& instance)
+  static void instance(std::unique_ptr<T>&& ptr)
   {
-    instance_ = instance;
+    instance_ = std::move(ptr);
   }
 
   static void clear()
@@ -67,7 +67,7 @@ public:
 };
 
 template<typename T>
-std::shared_ptr<T> SingletonHolder<T>::instance_;
+std::unique_ptr<T> SingletonHolder<T>::instance_;
 
 } // namespace aria2
 

+ 3 - 2
test/SingletonHolderTest.cc

@@ -4,6 +4,8 @@
 
 #include <cppunit/extensions/HelperMacros.h>
 
+#include "a2functional.h"
+
 namespace aria2 {
 
 class SingletonHolderTest : public CppUnit::TestFixture {
@@ -38,8 +40,7 @@ public:
 
 void SingletonHolderTest::testInstance()
 {
-  std::shared_ptr<M> m(new M("Hello world."));
-  SingletonHolder<M>::instance(m);
+  SingletonHolder<M>::instance(make_unique<M>("Hello world."));
   CPPUNIT_ASSERT_EQUAL(std::string("Hello world."),
                        SingletonHolder<M>::instance()->greeting());