/*
 * This source file is part of KEOS (Free 3D Engine)
 * For the latest info, see http://www.keosengine.org/
 * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
 * http://www.gnu.org/copyleft/lesser.txt.
 *
 */

#ifndef KEOS_SINGLETON_H
#define KEOS_SINGLETON_H

#include "KeosPrerequisites.h"

namespace Keos
{
    //-----------------------------------------------------------------------
	/** Template class for creating single-instance global classes.
     *
     */
	template <class T>
    class CSingleton
    {
    public :

        /** Create instance of the class.
         * 
         */
        static void Create()
        {
            if (!ms_pInst)
                ms_pInst = new T;
        }

        /** Return single-instance of the class.
         *  @return The single-instance.
         */
        static T& Instance()
        {
            if (!ms_pInst)
                ms_pInst = new T;

            return *ms_pInst;
        }

        /** Destroy single-instance of the class.
         *
         */
        static void Destroy()
        {
            delete ms_pInst;
            ms_pInst = NULL;
        }

		/** Change the intance of the class.
		 *
		 */
		static void _SetInstance(T* pInst)
		{
			ms_pInst = pInst;
		}

    protected :

        /** Default constructor.
         *
         */
        CSingleton() {}

        /** Destructor.
         *
         */
        ~CSingleton() {}

    private :

        //! Class instance
        static T* ms_pInst; 

        /** Copy constructor.
         *
         */
        CSingleton(CSingleton&);

        /** Affectation operator.
         *
         */
        void operator =(CSingleton&);
    };

    // Instance initialisation
    template <class T> T* CSingleton<T>::ms_pInst = NULL;

} // namespace Keos

#endif // KEOS_SINGLETON_H
