0ad/source/ps/Singleton.h
janwas b755ddefda remove all author/modified by tags.
make include guards consistent.

This was SVN commit r5040.
2007-05-07 16:33:24 +00:00

60 lines
1.0 KiB
C++

/**
* =========================================================================
* File : Singleton.h
* Project : 0 A.D.
* Description : template base class for Singletons
* =========================================================================
*/
/*
USAGE: class myClass : Singleton<myClass>{};
Modified from http://gamedev.net/reference/articles/article1954.asp
*/
#ifndef INCLUDED_SINGLETON
#define INCLUDED_SINGLETON
#include "lib/debug.h"
template<typename T>
class Singleton
{
static T* ms_singleton;
public:
Singleton()
{
debug_assert( !ms_singleton );
ms_singleton = static_cast<T*>(this);
}
~Singleton()
{
debug_assert( ms_singleton );
ms_singleton = 0;
}
static T& GetSingleton()
{
debug_assert( ms_singleton );
return *ms_singleton;
}
static T* GetSingletonPtr()
{
debug_assert( ms_singleton );
return ms_singleton;
}
static bool IsInitialised()
{
return (ms_singleton != 0);
}
};
template <typename T>
T* Singleton<T>::ms_singleton = 0;
#endif