Main Page | Class Hierarchy | Class List | File List | Class Members | Related Pages

D:/Programming/GUI Editor (Source)/Singleton.h

00001 // CSingleton.h: interface for the CSingleton class. 00002 // 00004 00005 #if !defined(AFX_SINGLETON_H__A92F649D_846D_4119_B642_FA7730C464EF__INCLUDED_) 00006 #define AFX_SINGLETON_H__A92F649D_846D_4119_B642_FA7730C464EF__INCLUDED_ 00007 00008 #if _MSC_VER > 1000 00009 #pragma once 00010 #endif // _MSC_VER > 1000 00011 00012 #include <assert.h> 00013 00015 00018 template<typename T> 00019 class CSingleton 00020 { 00021 static T* ms_singleton; 00022 public: 00023 CSingleton() 00024 { 00025 assert(!ms_singleton); 00026 //use a cunning trick to get the singleton pointing to the start of the whole, rather than 00027 //the start of the CSingleton part of the object 00028 int offset = (int)(T*)1 - (int)(CSingleton <T>*)(T*)1; 00029 ms_singleton = (T*)((int)this + offset); 00030 } 00031 ~CSingleton() 00032 { 00033 assert(ms_singleton); 00034 ms_singleton = 0; 00035 } 00039 static inline T& GetSingleton() 00040 { 00041 assert(ms_singleton); 00042 return *ms_singleton; 00043 } 00044 00048 static inline T* GetSingletonPtr() 00049 { 00050 //assert(ms_singleton); 00051 return ms_singleton; 00052 } 00053 }; 00054 00055 template <typename T> T* CSingleton <T>::ms_singleton = 0; 00056 00057 #endif // !defined(AFX_SINGLETON_H__A92F649D_846D_4119_B642_FA7730C464EF__INCLUDED_)

Generated on Sun Jul 17 21:34:28 2005 for OpenGL GUI by doxygen 1.3.8