Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/ps/Singleton.h
/* Copyright (C) 2009 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 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 | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
* GNU General Public License for more details. | * GNU General Public License for more details. | ||||
* | * | ||||
* You should have received a copy of the GNU General Public License | * 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/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
/* | |||||
* template base class for Singletons | |||||
*/ | |||||
/* | |||||
USAGE: class myClass : Singleton<myClass>{}; | |||||
Modified from http://gamedev.net/reference/articles/article1954.asp | |||||
*/ | |||||
#ifndef INCLUDED_SINGLETON | #ifndef INCLUDED_SINGLETON | ||||
#define INCLUDED_SINGLETON | #define INCLUDED_SINGLETON | ||||
#include "lib/debug.h" | #include "lib/debug.h" | ||||
/** | |||||
* Template base class for singletons. | |||||
* | |||||
* Usage: | |||||
* class MyClass : public Singleton<MyClass> {}; | |||||
* MyClass::GetSingleton().MyMethod(); | |||||
* | |||||
* Modified from http://gamedev.net/reference/articles/article1954.asp | |||||
*/ | |||||
template<typename T> | template<typename T> | ||||
class Singleton | class Singleton | ||||
{ | { | ||||
static T* ms_singleton; | NONCOPYABLE(Singleton); | ||||
public: | public: | ||||
Singleton() | Singleton() | ||||
{ | { | ||||
ENSURE( !ms_singleton ); | ENSURE(!ms_singleton); | ||||
ms_singleton = static_cast<T*>(this); | ms_singleton = static_cast<T*>(this); | ||||
} | } | ||||
~Singleton() | ~Singleton() | ||||
{ | { | ||||
ENSURE( ms_singleton ); | ENSURE(ms_singleton); | ||||
ms_singleton = 0; | ms_singleton = nullptr; | ||||
} | } | ||||
static T& GetSingleton() | static T& GetSingleton() | ||||
{ | { | ||||
ENSURE( ms_singleton ); | ENSURE(ms_singleton); | ||||
return *ms_singleton; | return *ms_singleton; | ||||
} | } | ||||
static T* GetSingletonPtr() | static T* GetSingletonPtr() | ||||
{ | { | ||||
ENSURE( ms_singleton ); | ENSURE(ms_singleton); | ||||
return ms_singleton; | return ms_singleton; | ||||
} | } | ||||
static bool IsInitialised() | static bool IsInitialised() | ||||
{ | { | ||||
return (ms_singleton != 0); | return ms_singleton != nullptr; | ||||
} | } | ||||
private: | |||||
static T* ms_singleton; | |||||
}; | }; | ||||
template <typename T> | template <typename T> | ||||
T* Singleton<T>::ms_singleton = 0; | T* Singleton<T>::ms_singleton = nullptr; | ||||
#endif | #endif // INCLUDED_SINGLETON |
Wildfire Games · Phabricator