/*
    Copyright (C) 2004  Erik Hjortsberg
    
    Taken from the file OgreSingleton.h from the Ogre project, release 0.14
    (www.ogre3d.org)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef _EMBER_SINGLETON_H__
#define _EMBER_SINGLETON_H__

#include <cassert>

namespace Ember {

    /** Template class for creating single-instance global classes.
    */
    template <typename T> class Singleton
    {
    protected:

		static T* ms_Singleton;

    public:
		Singleton( void )
		{
			assert( !ms_Singleton );
			ms_Singleton = static_cast< T* >( this );
		}
		
		~Singleton( void )
		{  
			assert( ms_Singleton );  ms_Singleton = 0;  
		}
		
		static T& getSingleton( void )
		{	
			assert( ms_Singleton );  
			return ( *ms_Singleton ); 
		}
		
		static T* getSingletonPtr( void )
		{ 
			assert( ms_Singleton ); 
			return ms_Singleton; 
		}
		
		static T& instance( void )
		{
			return getSingleton();
		}
	};
}
#endif

