#ifndef _SINGLETON_H_
#define _SINGLETON_H_
/*
*@brief: 单例类
*@auther: seven
*@date: 2024-05-22
*@version: 1.0.0
*@CopyRight: 1392019713@qq.com
*/
#include "ThreadMutex.h"
/*
*@note 放在(.h)头文件中使用
*/
#define DECLARE_UNMANGED_SINGLETON(classType) \
public:\
static classType* Instanse();\
static void Release();\
static classType* s_pInstance;\
static CThreadMutex s_singletonLock;
/*
*@note 放在(.cpp)文件中使用
*/
#define IMPLEMENT_UNMANED_SINGLETON(classType) \
classType* classType::s_pInstance = NULL; \
CThreadMutex classType::s_singletonLock; \
classType* classType::Instanse() \
{ \
if(s_pInstance == NULL) \
{ \
s_singletonLock.Acquire();\
if (s_pInstance == NULL) \
{ \
s_pInstance = new classType;\
}\
s_singletonLock.Release();\
return s_pInstance;\
} \
}\
void classType::Release()\
{\
if (s_pInstance == NULL) \
{ \
delete s_pInstance;\
s_pInstance = NULL;\
}\
}
#endif
【C++】构造跨平台库 方便易用的单例定义
最新推荐文章于 2024-10-17 21:38:17 发布