2025-07-23 10:19:42 -07:00
|
|
|
/* Copyright (C) 2025 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2009-04-18 10:00:33 -07:00
|
|
|
* 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.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2009-04-18 10:00:33 -07:00
|
|
|
* 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
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2007-05-07 09:33:24 -07:00
|
|
|
#ifndef INCLUDED_SINGLETON
|
|
|
|
|
#define INCLUDED_SINGLETON
|
2003-09-21 14:24:53 -07:00
|
|
|
|
2025-07-23 10:19:42 -07:00
|
|
|
#include "lib/code_annotation.h"
|
2005-08-07 14:58:36 -07:00
|
|
|
#include "lib/debug.h"
|
|
|
|
|
|
2019-01-13 07:11:40 -08: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 14:24:53 -07:00
|
|
|
template<typename T>
|
|
|
|
|
class Singleton
|
|
|
|
|
{
|
2019-01-13 07:11:40 -08:00
|
|
|
NONCOPYABLE(Singleton);
|
2005-08-09 09:02:15 -07:00
|
|
|
public:
|
|
|
|
|
Singleton()
|
|
|
|
|
{
|
2019-01-13 07:11:40 -08:00
|
|
|
ENSURE(!ms_singleton);
|
2007-01-20 06:20:31 -08:00
|
|
|
ms_singleton = static_cast<T*>(this);
|
2005-08-09 09:02:15 -07:00
|
|
|
}
|
2003-09-21 14:24:53 -07:00
|
|
|
|
2005-08-09 09:02:15 -07:00
|
|
|
~Singleton()
|
|
|
|
|
{
|
2019-01-13 07:11:40 -08:00
|
|
|
ENSURE(ms_singleton);
|
|
|
|
|
ms_singleton = nullptr;
|
2005-08-09 09:02:15 -07:00
|
|
|
}
|
2003-09-21 14:24:53 -07:00
|
|
|
|
2005-08-09 09:02:15 -07:00
|
|
|
static T& GetSingleton()
|
|
|
|
|
{
|
2019-01-13 07:11:40 -08:00
|
|
|
ENSURE(ms_singleton);
|
2005-08-09 09:02:15 -07:00
|
|
|
return *ms_singleton;
|
|
|
|
|
}
|
2003-09-21 14:24:53 -07:00
|
|
|
|
2005-08-09 09:02:15 -07:00
|
|
|
static T* GetSingletonPtr()
|
|
|
|
|
{
|
2019-01-13 07:11:40 -08:00
|
|
|
ENSURE(ms_singleton);
|
2005-08-09 09:02:15 -07:00
|
|
|
return ms_singleton;
|
|
|
|
|
}
|
2004-08-15 13:47:25 -07:00
|
|
|
|
2005-08-09 09:02:15 -07:00
|
|
|
static bool IsInitialised()
|
|
|
|
|
{
|
2019-01-13 07:11:40 -08:00
|
|
|
return ms_singleton != nullptr;
|
2005-08-09 09:02:15 -07:00
|
|
|
}
|
2019-01-13 07:11:40 -08:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static T* ms_singleton;
|
2003-09-21 14:24:53 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2019-01-13 07:11:40 -08:00
|
|
|
T* Singleton<T>::ms_singleton = nullptr;
|
2003-09-21 14:24:53 -07:00
|
|
|
|
2019-01-13 07:11:40 -08:00
|
|
|
#endif // INCLUDED_SINGLETON
|