forked from skyfireitdiy/sflib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsf_single_instance.hpp
53 lines (43 loc) · 1.05 KB
/
sf_single_instance.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* @version 1.0.0
* @author skyfire
* @mail skyfireitdiy@hotmail.com
* @see http://github.com/skyfireitdiy/sflib
* @file sf_single_instance.hpp
* sflib第一版本发布
* 版本号1.0.0
* 发布日期:2018-10-22
*/
/*
* sf_single_instance单例支持
*/
#pragma once
#include <mutex>
#include "sf_empty_class.hpp"
#include "sf_single_instance.h"
namespace skyfire
{
template <typename ThisClass, typename BaseClass>
ThisClass* sf_single_instance<ThisClass, BaseClass>::get_instance()
{
if (instance == nullptr)
{
std::lock_guard<std::mutex> lck(mu);
if (instance == nullptr)
{
instance = new ThisClass();
}
}
return instance;
}
template <typename ThisClass, typename BaseClass>
sf_single_instance<ThisClass, BaseClass>::~sf_single_instance()
{
if (instance != nullptr)
delete instance;
}
template<typename ThisClass, typename BaseClass>
ThisClass* sf_single_instance<ThisClass, BaseClass>::instance = nullptr;
template<typename ThisClass, typename BaseClass>
std::mutex sf_single_instance<ThisClass, BaseClass>::mu;
}