mirror of
https://github.com/wu736139669/SmallGame_Cocos2dx.git
synced 2026-08-05 06:23:09 +00:00
42 lines
710 B
C++
42 lines
710 B
C++
// 单件
|
|
|
|
#ifndef __SINGLETON_H__
|
|
#define __SINGLETON_H__
|
|
#include <assert.h>
|
|
template <typename T>
|
|
class CSingleton {
|
|
protected:
|
|
static T *m_pInstance;
|
|
|
|
public:
|
|
CSingleton(void) {
|
|
assert(!m_pInstance);
|
|
m_pInstance = static_cast<T *>(this);
|
|
}
|
|
|
|
~CSingleton(void) {
|
|
assert(m_pInstance);
|
|
m_pInstance = 0;
|
|
}
|
|
|
|
static T &sharedInstance(void) {
|
|
if (m_pInstance == 0) {
|
|
m_pInstance = new T;
|
|
}
|
|
|
|
return (*m_pInstance);
|
|
}
|
|
|
|
static T *sharedInstancePtr(void) {
|
|
if (m_pInstance == 0) {
|
|
m_pInstance = new T;
|
|
}
|
|
|
|
return (m_pInstance);
|
|
}
|
|
};
|
|
|
|
template<typename T>
|
|
T *CSingleton<T>::m_pInstance = 0;
|
|
|
|
#endif |