# CEGUISingleton.h

    00001 /************************************************************************
    00002         filename:       CEGUISingleton.h
    00003         created:        22/2/2004
    00004         author:         Paul D Turner
    00005
    00006         purpose:        Singleton Base Class
    00007 *************************************************************************/
    00008 /*************************************************************************
    00009     Crazy Eddie's GUI System (http://www.cegui.org.uk)
    00010     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
    00011
    00012     This library is free software; you can redistribute it and/or
    00013     modify it under the terms of the GNU Lesser General Public
    00014     License as published by the Free Software Foundation; either
    00015     version 2.1 of the License, or (at your option) any later version.
    00016
    00017     This library is distributed in the hope that it will be useful,
    00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00020     Lesser General Public License for more details.
    00021
    00022     You should have received a copy of the GNU Lesser General Public
    00023     License along with this library; if not, write to the Free Software
    00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00025 *************************************************************************/
    00026 /*************************************************************************
    00027
    00028         The code in this file is taken from article 1.3 in the the book:
    00029         Game Programming Gems from Charles River Media
    00030
    00031 *************************************************************************/
    00032 #ifndef _CEGUISingleton_h_
    00033 #define _CEGUISingleton_h_
    00034
    00035 #include "CEGUIBase.h"
    00036 #include <cassert>
    00037
    00038 // Start of CEGUI namespace section
    00039 namespace CEGUI
    00040 {
    00041 /* Copyright (C) Scott Bilas, 2000.
    00042  * All rights reserved worldwide.
    00043  *
    00044  * This software is provided "as is" without express or implied
    00045  * warranties. You may freely copy and compile this source into
    00046  * applications you distribute provided that the copyright text
    00047  * below is included in the resulting source code, for example:
    00048  * "Portions Copyright (C) Scott Bilas, 2000"
    00049  */
    00050
    00051
    00052 template <typename T> class CEGUIEXPORT Singleton
    00053 {
    00054 protected:
    00055     static T* ms_Singleton;
    00056
    00057 public:
    00058     Singleton( void )
    00059     {
    00060         assert( !ms_Singleton );
    00061         ms_Singleton = static_cast<T*>(this);
    00062     }
    00063    ~Singleton( void )
    00064         {  assert( ms_Singleton );  ms_Singleton = 0;  }
    00065     static T& getSingleton( void )
    00066         {  assert( ms_Singleton );  return ( *ms_Singleton );  }
    00067     static T* getSingletonPtr( void )
    00068         {  return ( ms_Singleton );  }
    00069 };
    00070
    00071 } // End of  CEGUI namespace section
    00072
    00073
    00074
    00075 #endif  // end of guard _CEGUISingleton_h_