00001 #ifndef _CEGUI_SINGLETON_X_H_ 00002 #define _CEGUI_SINGLETON_X_H_ 00003 00004 #if defined(_MSC_VER) 00005 # pragma warning(push) 00006 # pragma warning(disable : 4786) 00007 #endif 00008 00009 namespace CEGUI 00010 { 00011 /************************************************************************* 00012 Class: CEGUISingletonX 00013 Alternative for the existing CEGUI Singleton class. This one doesn't 00014 require the Singleton to be explicitly instantiated by 'new'. 00015 *************************************************************************/ 00019 template<class T> 00020 class CEGUISingletonX 00021 { 00022 private: 00023 class _InstPtr 00024 { 00025 public: 00026 _InstPtr() : m_ptr(0) {} 00027 ~_InstPtr() { delete m_ptr; } 00028 T* get() { return m_ptr; } 00029 void Set(T* p) 00030 { 00031 if(p!= 0) 00032 { 00033 delete m_ptr; 00034 m_ptr = p; 00035 } 00036 } 00037 private: 00038 T* m_ptr; 00039 }; 00040 00041 static _InstPtr sm_ptr; 00042 CEGUISingletonX(); 00043 CEGUISingletonX(const CEGUISingletonX&); 00044 CEGUISingletonX& operator=(const CEGUISingletonX&); 00045 00046 public: 00047 static T& getSingleton() 00048 { 00049 if(sm_ptr.get() == 0) 00050 { 00051 sm_ptr.Set(new T()); 00052 } 00053 return *sm_ptr.get(); 00054 } 00055 00056 static T* getSingletonPtr() 00057 { 00058 if(sm_ptr.get() == 0) 00059 { 00060 sm_ptr.Set(new T()); 00061 } 00062 return sm_ptr.get(); 00063 } 00064 }; 00065 } 00066 #endif