/***************************************************************************
*                                                                          *
*  Copyright (C) Golaem S.A.  All Rights Reserved.                         *
*                                                                          *
****************************************************************************/

#pragma once

#include "glmAssert.h"

namespace glm
{
	// In order to be a singleton, a class constructor should be private and give friendship to Singleton. 
	// The unique instance of the class is obtained by a call to getInstance.
	template< class T >
	class Singleton
	{
	public:

		static bool hasInstance(); // return a true if the instance exists
		static T& getInstance(); // return a reference to the unique instance of T in the application
		static void create(); // create the unique instance of T
		static void destroy(); // destroy the unique instance of T

	private:

		static T* _instance; // pointer to the unique instance of T in the application
	};

	//-------------------------------------------------------------------------
	template< class T > T* Singleton<T>::_instance = NULL;

	//-------------------------------------------------------------------------
	template< class T >
	T& Singleton<T>::getInstance()
	{
		GLM_DEBUG_ASSERT_MSG(_instance != NULL, "The Singleton must be created before calling getInstance()");
		return *_instance;
	}

	//-------------------------------------------------------------------------
	template< class T >
	bool Singleton<T>::hasInstance()
	{
		return _instance != NULL;
	}

	//-------------------------------------------------------------------------
	template< class T >
	void Singleton<T>::create()
	{
		if (!_instance)
		{
			_instance = new T();
		}
	}

	//-------------------------------------------------------------------------
	template< class T >
	void Singleton<T>::destroy()
	{
		delete _instance;
		_instance = NULL;
	}
}
