You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.2 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* @file WSingletonHandler.h
* @brief 通用单例模式
* @remark 使用方法:
* 1> 需要做为单例类构造函数位置为private拷贝构造函数和赋值操作符只定义不实现
* 2> 该类设置WSingletonHandler<T>为友元类
* 3> 增加全局访问函数接口比如AfxGetT()返回T*函数实现中调用WSingletonHandler<T>::GetInstance()返回指针
* 当然此步可以不做每次调用WSingletonHandler<T>::GetInstance()获取指针
* 4> 内部使用智能指针保存单例指针,不需要对内存进行删除操作
* @author 沙漠乌鸦
* @time 2009-12-25
*/
#pragma once
#include "WGuardLock.h"
#include <memory>
namespace wuya
{
/** @brief 通用单例模式*/
template<class T>
class WSingletonHandler : public T
{
public:
/** @brief 获取唯一访问指针 */
static T* GetInstance()
{
if(m_InstancePtr.get() == NULL)
{
m_InstancePtr.reset(new T);
}
return m_InstancePtr.get();
}
private:
WSingletonHandler(void);
~WSingletonHandler(void);
WSingletonHandler(const WSingletonHandler& singletonHandle);
WSingletonHandler& operator=(const WSingletonHandler& singletonHandle);
private:
static auto_ptr<T> m_InstancePtr;
};
template<class T>
auto_ptr<T> WSingletonHandler<T>::m_InstancePtr;
}