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