mirror of https://github.com/aria2/aria2
Use std::unique_ptr in SingletonHolder
parent
ca329a7ccb
commit
5cb7ae0b86
|
@ -137,8 +137,7 @@ int MultiUrlRequestInfo::prepare()
|
||||||
{
|
{
|
||||||
global::globalHaltRequested = 0;
|
global::globalHaltRequested = 0;
|
||||||
try {
|
try {
|
||||||
std::shared_ptr<Notifier> notifier(new Notifier());
|
SingletonHolder<Notifier>::instance(make_unique<Notifier>());
|
||||||
SingletonHolder<Notifier>::instance(notifier);
|
|
||||||
|
|
||||||
#ifdef ENABLE_SSL
|
#ifdef ENABLE_SSL
|
||||||
if(option_->getAsBool(PREF_ENABLE_RPC) &&
|
if(option_->getAsBool(PREF_ENABLE_RPC) &&
|
||||||
|
|
|
@ -44,20 +44,20 @@ namespace aria2 {
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class SingletonHolder {
|
class SingletonHolder {
|
||||||
private:
|
private:
|
||||||
static std::shared_ptr<T> instance_;
|
static std::unique_ptr<T> instance_;
|
||||||
|
|
||||||
SingletonHolder() {}
|
SingletonHolder() {}
|
||||||
public:
|
public:
|
||||||
~SingletonHolder() {}
|
~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()
|
static void clear()
|
||||||
|
@ -67,7 +67,7 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::shared_ptr<T> SingletonHolder<T>::instance_;
|
std::unique_ptr<T> SingletonHolder<T>::instance_;
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
#include <cppunit/extensions/HelperMacros.h>
|
#include <cppunit/extensions/HelperMacros.h>
|
||||||
|
|
||||||
|
#include "a2functional.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
class SingletonHolderTest : public CppUnit::TestFixture {
|
class SingletonHolderTest : public CppUnit::TestFixture {
|
||||||
|
@ -38,8 +40,7 @@ public:
|
||||||
|
|
||||||
void SingletonHolderTest::testInstance()
|
void SingletonHolderTest::testInstance()
|
||||||
{
|
{
|
||||||
std::shared_ptr<M> m(new M("Hello world."));
|
SingletonHolder<M>::instance(make_unique<M>("Hello world."));
|
||||||
SingletonHolder<M>::instance(m);
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("Hello world."),
|
CPPUNIT_ASSERT_EQUAL(std::string("Hello world."),
|
||||||
SingletonHolder<M>::instance()->greeting());
|
SingletonHolder<M>::instance()->greeting());
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue