-
Notifications
You must be signed in to change notification settings - Fork 38
/
DMMultSingletonT.h
89 lines (81 loc) · 1.88 KB
/
DMMultSingletonT.h
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//-------------------------------------------------------
// Copyright (c) DuiMagic
// All rights reserved.
//
// File Name: DMSingletonT.h
// File Des: 单例模式
// File Summary:
// Cur Version: 1.0
// Author:
// Create Data:
// History:
// <Author> <Time> <Version> <Des>
// guoyou 2014-9-12 1.0
// guoyou 2015-01-09 1.1 增加帮助chm注释
//-------------------------------------------------------
#pragma once
#include <assert.h>
#include "DMMapT.h"
__pragma(warning(disable: 4661))
namespace DM
{
/// <summary>
/// 多线程单例模式模板类
/// </summary>
template <typename T>
class DMSingletonT
{
public:
static T* ms_Singleton;
public:
DMSingletonT(void)
{
if (m_Map.IsKeyExist(::GetCurrentThreadId()))
{
DMASSERT_EXPR(false,L"不要初始化多次DMApp对象");
}
T* pObj = static_cast<T*>(this);
m_Map.AddKey(::GetCurrentThreadId(),pObj);
}
virtual ~DMSingletonT(void)
{
m_Map.RemoveKey(::GetCurrentThreadId());
}
/// <summary>
/// 获取单例模式的对象
/// </summary>
/// <returns>单例模式的对象</returns>
static T& getSingleton()
{
T* pObj = NULL;
if (!m_Map.GetObjByKey(::GetCurrentThreadId(),pObj))
{
DMASSERT_EXPR(false,L"当前线程未创建DMApp对象");
}
return *pObj;
}
/// <summary>
/// 获取单例模式的对象指针
/// </summary>
/// <returns>单例模式的对象指针</returns>
static T* getSingletonPtr()
{
T* pObj = NULL;
if (!m_Map.GetObjByKey(::GetCurrentThreadId(),pObj))
{
DMASSERT_EXPR(false,L"当前线程未创建DMApp对象");
}
return pObj;
}
private:
DMSingletonT& operator=(const DMSingletonT&)
{
return *this;
}
DMSingletonT(const DMSingletonT&) {}
public:
static DMMapT<DWORD, T*> m_Map;
};
template<class T> typename DMMapT<DWORD, T*> DMSingletonT<T>::m_Map;
}//namespace DM