Files

80 lines
1.1 KiB
C
Raw Permalink Normal View History

2026-03-03 13:56:44 +08:00
//{future header message}
#ifndef __Singleton_H__
#define __Singleton_H__
#include "cocos2d.h"
template <class T>
class Singleton
{
public:
static T *m_pInstance;
public:
Singleton()
{
CCASSERT(m_pInstance == NULL, "");
m_pInstance = static_cast< T * >(this);
}
~Singleton()
{
m_pInstance = 0;
}
static T &Instance()
{
if (m_pInstance == nullptr)
{
new T;
}
return *m_pInstance;
}
static T *getInstance()
{
return m_pInstance;
}
};
#define SINGLETON_STORAGE( TYPE ) \
template <> \
TYPE * Singleton< TYPE >::m_pInstance = 0;
template <class T>
class SingletonBase
{
public:
static T *m_pInstanceBase;
public:
SingletonBase()
{
CCASSERT(m_pInstanceBase == NULL, "");
m_pInstanceBase = static_cast<T *>(this);
}
~SingletonBase()
{
m_pInstanceBase = 0;
}
static T &InstanceBase()
{
CCASSERT(m_pInstanceBase != NULL, "InstanceBase");
return *m_pInstanceBase;
}
static T *getInstanceBase()
{
return m_pInstanceBase;
}
};
#define SINGLETON_STORAGEBASE( TYPE ) \
template <> \
TYPE * SingletonBase< TYPE >::m_pInstanceBase = 0;
#endif // __Singleton_H__