d933eae338
git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@12865 178a84e3-b1eb-0310-8ba1-8eac791a3b58
44 lines
943 B
C++
44 lines
943 B
C++
#ifndef SINGLETON_HPP
|
|
#define SINGLETON_HPP
|
|
|
|
#include <iostream>
|
|
|
|
template <typename T>
|
|
class Singleton
|
|
{
|
|
protected:
|
|
Singleton () { }
|
|
~Singleton () { std::cout << "destroying singleton." << std::endl; }
|
|
|
|
public:
|
|
static T *getInstance ()
|
|
{
|
|
if (NULL == m_singleton)
|
|
{
|
|
std::cout << "creating singleton." << std::endl;
|
|
m_singleton = new T;
|
|
}
|
|
else
|
|
{
|
|
std::cout << "singleton already created!" << std::endl;
|
|
}
|
|
return (static_cast<T*> (m_singleton));
|
|
}
|
|
|
|
static void kill ()
|
|
{
|
|
if (NULL != m_singleton)
|
|
{
|
|
delete m_singleton;
|
|
m_singleton = NULL;
|
|
}
|
|
}
|
|
|
|
private:
|
|
static T *m_singleton;
|
|
};
|
|
|
|
template <typename T> T *Singleton<T>::_singleton = NULL;
|
|
|
|
#endif // SINGLETON_HPP
|