MyGUI  3.2.0
MyGUI_Singleton.h
Go to the documentation of this file.
00001 
00006 /*
00007     This file is part of MyGUI.
00008 
00009     MyGUI is free software: you can redistribute it and/or modify
00010     it under the terms of the GNU Lesser General Public License as published by
00011     the Free Software Foundation, either version 3 of the License, or
00012     (at your option) any later version.
00013 
00014     MyGUI is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 #ifndef __MYGUI_SINGLETON_H__
00023 #define __MYGUI_SINGLETON_H__
00024 
00025 #include "MyGUI_Diagnostic.h"
00026 
00027 namespace MyGUI
00028 {
00029 
00030 #if MYGUI_COMPILER == MYGUI_COMPILER_MSVC || MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
00031     template <class T>
00032     class Singleton
00033 #else
00034     template <class T>
00035     class MYGUI_EXPORT Singleton
00036 #endif
00037     {
00038     public:
00039         typedef Singleton<T> Base;
00040 
00041         Singleton()
00042         {
00043             MYGUI_ASSERT(nullptr == msInstance, "Singleton instance " << getClassTypeName() << " already exsist");
00044             msInstance = static_cast<T*>(this);
00045         }
00046 
00047         virtual ~Singleton()
00048         {
00049             MYGUI_ASSERT(nullptr != msInstance, "Destroying Singleton instance " << getClassTypeName() << " before constructing it.");
00050             msInstance = nullptr;
00051         }
00052 
00053         static T& getInstance()
00054         {
00055             MYGUI_ASSERT(nullptr != getInstancePtr(), "Singleton instance " << getClassTypeName() << " was not created");
00056             return (*getInstancePtr());
00057         }
00058 
00059         static T* getInstancePtr()
00060         {
00061             return msInstance;
00062         }
00063 
00064         static const char* getClassTypeName()
00065         {
00066             return mClassTypeName;
00067         }
00068 
00069     private:
00070         static T* msInstance;
00071 
00072     protected:
00073         static const char* mClassTypeName;
00074     };
00075 
00076 } // namespace MyGUI
00077 
00078 #endif // __MYGUI_SINGLETON_H__