LCOV - code coverage report
Current view: top level - source/ps - Singleton.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 13 13 100.0 %
Date: 2026-12-03 01:19:39 Functions: 24 24 100.0 %

          Line data    Source code
       1             : /* Copyright (C) 2025 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #ifndef INCLUDED_SINGLETON
      19             : #define INCLUDED_SINGLETON
      20             : 
      21             : #include "lib/code_annotation.h"
      22             : #include "lib/debug.h"
      23             : 
      24             : /**
      25             :  * Template base class for singletons.
      26             :  *
      27             :  * Usage:
      28             :  *      class MyClass : public Singleton<MyClass> {};
      29             :  *      MyClass::GetSingleton().MyMethod();
      30             :  *
      31             :  * Modified from http://gamedev.net/reference/articles/article1954.asp
      32             :  */
      33             : template<typename T>
      34             : class Singleton
      35             : {
      36             :     NONCOPYABLE(Singleton);
      37             : public:
      38         156 :     Singleton()
      39             :     {
      40         156 :         ENSURE(!ms_singleton);
      41         156 :         ms_singleton = static_cast<T*>(this);
      42         156 :     }
      43             : 
      44         156 :     ~Singleton()
      45             :     {
      46         156 :         ENSURE(ms_singleton);
      47         156 :         ms_singleton = nullptr;
      48         156 :     }
      49             : 
      50      159832 :     static T& GetSingleton()
      51             :     {
      52      159832 :         ENSURE(ms_singleton);
      53      159832 :         return *ms_singleton;
      54             :     }
      55             : 
      56             :     static T* GetSingletonPtr()
      57             :     {
      58             :         ENSURE(ms_singleton);
      59             :         return ms_singleton;
      60             :     }
      61             : 
      62       41099 :     static bool IsInitialised()
      63             :     {
      64       41103 :         return ms_singleton != nullptr;
      65             :     }
      66             : 
      67             : private:
      68             :     static T* ms_singleton;
      69             : };
      70             : 
      71             : template <typename T>
      72             : T* Singleton<T>::ms_singleton = nullptr;
      73             : 
      74             : #endif // INCLUDED_SINGLETON

Generated by: LCOV version 1.16