27 lines
533 B
C++
27 lines
533 B
C++
#pragma once
|
|
|
|
#include <assert.h>
|
|
|
|
template <class T> class CSingleton
|
|
{
|
|
protected:
|
|
static T* ms_pSingleton;
|
|
|
|
public:
|
|
CSingleton()
|
|
{
|
|
assert( !ms_pSingleton );
|
|
/*
|
|
int offset = (int)(T*)1 - (int)(CSingleton <T> *)(T*)1;
|
|
ms_pSingleton = (T *)((int)this + offset);
|
|
*/
|
|
ms_pSingleton = static_cast <T*> (this);
|
|
}
|
|
~CSingleton()
|
|
{ assert( ms_pSingleton ); ms_pSingleton = NULL; }
|
|
static T* GetInstance()
|
|
{ assert( ms_pSingleton ); return ms_pSingleton; }
|
|
};
|
|
|
|
template <class T> T* CSingleton <T>::ms_pSingleton = NULL;
|