2019-01-13 16:11:40 +01:00
|
|
|
/* Copyright (C) 2019 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2007-05-07 18:33:24 +02:00
|
|
|
#ifndef INCLUDED_SINGLETON
|
|
|
|
#define INCLUDED_SINGLETON
|
2003-09-21 23:24:53 +02:00
|
|
|
|
2005-08-07 23:58:36 +02:00
|
|
|
#include "lib/debug.h"
|
|
|
|
|
2019-01-13 16:11:40 +01:00
|
|
|
/**
|
|
|
|
* Template base class for singletons.
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* class MyClass : public Singleton<MyClass> {};
|
|
|
|
* MyClass::GetSingleton().MyMethod();
|
|
|
|
*
|
|
|
|
* Modified from http://gamedev.net/reference/articles/article1954.asp
|
|
|
|
*/
|
2003-09-21 23:24:53 +02:00
|
|
|
template<typename T>
|
|
|
|
class Singleton
|
|
|
|
{
|
2019-01-13 16:11:40 +01:00
|
|
|
NONCOPYABLE(Singleton);
|
2005-08-09 18:02:15 +02:00
|
|
|
public:
|
|
|
|
Singleton()
|
|
|
|
{
|
2019-01-13 16:11:40 +01:00
|
|
|
ENSURE(!ms_singleton);
|
2007-01-20 15:20:31 +01:00
|
|
|
ms_singleton = static_cast<T*>(this);
|
2005-08-09 18:02:15 +02:00
|
|
|
}
|
2003-09-21 23:24:53 +02:00
|
|
|
|
2005-08-09 18:02:15 +02:00
|
|
|
~Singleton()
|
|
|
|
{
|
2019-01-13 16:11:40 +01:00
|
|
|
ENSURE(ms_singleton);
|
|
|
|
ms_singleton = nullptr;
|
2005-08-09 18:02:15 +02:00
|
|
|
}
|
2003-09-21 23:24:53 +02:00
|
|
|
|
2005-08-09 18:02:15 +02:00
|
|
|
static T& GetSingleton()
|
|
|
|
{
|
2019-01-13 16:11:40 +01:00
|
|
|
ENSURE(ms_singleton);
|
2005-08-09 18:02:15 +02:00
|
|
|
return *ms_singleton;
|
|
|
|
}
|
2003-09-21 23:24:53 +02:00
|
|
|
|
2005-08-09 18:02:15 +02:00
|
|
|
static T* GetSingletonPtr()
|
|
|
|
{
|
2019-01-13 16:11:40 +01:00
|
|
|
ENSURE(ms_singleton);
|
2005-08-09 18:02:15 +02:00
|
|
|
return ms_singleton;
|
|
|
|
}
|
2004-08-15 22:47:25 +02:00
|
|
|
|
2005-08-09 18:02:15 +02:00
|
|
|
static bool IsInitialised()
|
|
|
|
{
|
2019-01-13 16:11:40 +01:00
|
|
|
return ms_singleton != nullptr;
|
2005-08-09 18:02:15 +02:00
|
|
|
}
|
2019-01-13 16:11:40 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
static T* ms_singleton;
|
2003-09-21 23:24:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2019-01-13 16:11:40 +01:00
|
|
|
T* Singleton<T>::ms_singleton = nullptr;
|
2003-09-21 23:24:53 +02:00
|
|
|
|
2019-01-13 16:11:40 +01:00
|
|
|
#endif // INCLUDED_SINGLETON
|