00001 /*********************************************************************** 00002 filename: CEGUISingleton.h 00003 created: 22/2/2004 00004 author: Paul D Turner 00005 00006 purpose: Singleton Base Class 00007 *************************************************************************/ 00008 /*************************************************************************** 00009 * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team 00010 * 00011 * Permission is hereby granted, free of charge, to any person obtaining 00012 * a copy of this software and associated documentation files (the 00013 * "Software"), to deal in the Software without restriction, including 00014 * without limitation the rights to use, copy, modify, merge, publish, 00015 * distribute, sublicense, and/or sell copies of the Software, and to 00016 * permit persons to whom the Software is furnished to do so, subject to 00017 * the following conditions: 00018 * 00019 * The above copyright notice and this permission notice shall be 00020 * included in all copies or substantial portions of the Software. 00021 * 00022 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00023 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00024 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00025 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 00026 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00027 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00028 * OTHER DEALINGS IN THE SOFTWARE. 00029 ***************************************************************************/ 00030 /************************************************************************* 00031 00032 The code in this file is taken from article 1.3 in the the book: 00033 Game Programming Gems from Charles River Media 00034 00035 *************************************************************************/ 00036 #ifndef _CEGUISingleton_h_ 00037 #define _CEGUISingleton_h_ 00038 00039 #include "CEGUIBase.h" 00040 #include <cassert> 00041 00042 // Start of CEGUI namespace section 00043 namespace CEGUI 00044 { 00045 /* Copyright (C) Scott Bilas, 2000. 00046 * All rights reserved worldwide. 00047 * 00048 * This software is provided "as is" without express or implied 00049 * warranties. You may freely copy and compile this source into 00050 * applications you distribute provided that the copyright text 00051 * below is included in the resulting source code, for example: 00052 * "Portions Copyright (C) Scott Bilas, 2000" 00053 */ 00054 00055 00056 template <typename T> class CEGUIEXPORT Singleton 00057 { 00058 protected: 00059 // TODO: Come up with something better than this! 00060 // TODO: 00061 // TODO: This super-nasty piece of nastiness was put in for continued 00062 // TODO: compatability with MSVC++ and MinGW - the latter apparently 00063 // TODO: needs this. 00064 static 00065 #ifdef __MINGW32__ 00066 CEGUIEXPORT 00067 #endif 00068 T* ms_Singleton; 00069 00070 public: 00071 Singleton( void ) 00072 { 00073 assert( !ms_Singleton ); 00074 ms_Singleton = static_cast<T*>(this); 00075 } 00076 ~Singleton( void ) 00077 { assert( ms_Singleton ); ms_Singleton = 0; } 00078 static T& getSingleton( void ) 00079 { assert( ms_Singleton ); return ( *ms_Singleton ); } 00080 static T* getSingletonPtr( void ) 00081 { return ( ms_Singleton ); } 00082 }; 00083 00084 } // End of CEGUI namespace section 00085 00086 00087 00088 #endif // end of guard _CEGUISingleton_h_