00001
00002
00003
00004
00005
00006
00007 #if !defined(AFX_SINGLETON_H__202E8DC7_873E_47D1_AF46_DEC9051E4BE7__INCLUDED_)
00008 #define AFX_SINGLETON_H__202E8DC7_873E_47D1_AF46_DEC9051E4BE7__INCLUDED_
00009
00010 #if _MSC_VER > 1000
00011 #pragma once
00012 #endif // _MSC_VER > 1000
00013
00014 #include <cassert>
00015
00017 template <typename T>
00018 class Singleton
00019 {
00020 private:
00021 static T* ms_Singleton;
00022
00023 public:
00024 Singleton()
00025 {
00026 assert(!ms_Singleton && "Pointer to ms_Singleton is NULL");
00027 int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
00028 ms_Singleton = (T*)((int)this + offset);
00029 }
00030
00031 ~Singleton()
00032 {
00033 assert(ms_Singleton && "Nothing to destroy");
00034 ms_Singleton = 0;
00035 }
00036
00037 static T& GetSingleton()
00038 {
00039 assert(ms_Singleton && "Singleton not initialized correctly");
00040 return (*ms_Singleton);
00041 }
00042
00043 static T* GetSingletonPtr()
00044 {
00045 return (ms_Singleton);
00046 }
00047 };
00048
00049 template <typename T>
00050 T* Singleton <T>::ms_Singleton = 0;
00051
00052 #endif // !defined(AFX_SINGLETON_H__202E8DC7_873E_47D1_AF46_DEC9051E4BE7__INCLUDED_)