Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
refactor: singleton template class
Browse files Browse the repository at this point in the history
  • Loading branch information
gaowanlu committed Sep 29, 2023
1 parent 0904972 commit 9e6cd93
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
4 changes: 2 additions & 2 deletions bin/config/main.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ port = 20023
threads = 3
max_conn = 20000
wait_time = 10
#task_type = HTTP_TASK
task_type = STREAM_TASK
task_type = HTTP_TASK
#task_type = STREAM_TASK
# now support HTTP_TASK or STREAM_TASK
daemon = 1

Expand Down
22 changes: 17 additions & 5 deletions src/utility/singleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,31 @@ namespace tubekit
class singleton
{
public:
static T *instance()
template <typename... Args>
static T *instance(Args &&...args)
{
static std::shared_ptr<T> instance = nullptr;
if (instance == nullptr)
if (m_instance == nullptr)
{
instance = std::make_shared<T>();
m_instance = std::make_shared<T>(std::forward<Args>(args)...);
}
return instance.get();
return m_instance.get();
}
static T *get_instance()
{
return m_instance.get();
}
static void destory_instance()
{
m_instance.reset();
}

protected:
singleton() = default;
~singleton() = default;
static std::shared_ptr<T> m_instance;
};

template <typename T>
std::shared_ptr<T> singleton<T>::m_instance = nullptr;
}
}

0 comments on commit 9e6cd93

Please sign in to comment.