Use std::unique_ptr in SingletonHolder

pull/103/head
Tatsuhiro Tsujikawa 2013-06-22 19:36:42 +09:00
parent ca329a7ccb
commit 5cb7ae0b86
3 changed files with 10 additions and 10 deletions

View File

@ -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) &&

View File

@ -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

View File

@ -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());