SuperTuxKart
Loading...
Searching...
No Matches
singleton.hpp
Go to the documentation of this file.
1//
2// SuperTuxKart - a fun racing game with go-kart
3// Copyright (C) 2013-2015 SuperTuxKart-Team
4//
5// This program is free software; you can redistribute it and/or
6// modify it under the terms of the GNU General Public License
7// as published by the Free Software Foundation; either version 3
8// of the License, or (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
22#ifndef SINGLETON_HPP
23#define SINGLETON_HPP
24
25#include "utils/log.hpp"
26
33template <typename T>
35{
36 protected:
38 AbstractSingleton() { m_singleton = NULL; }
41 {
42 Log::info("Singleton", "Destroyed singleton.");
43 }
44
45 public:
52 template<typename S>
53 static S *getInstance ()
54 {
55 if (m_singleton == NULL)
56 m_singleton = new S;
57
58 S* result = (dynamic_cast<S*> (m_singleton));
59 if (result == NULL)
60 Log::debug("Singleton", "THE SINGLETON HAS NOT BEEN REALOCATED, IT IS NOT OF THE REQUESTED TYPE.");
61 return result;
62 }
64 static T *getInstance()
65 {
66 return m_singleton;
67 }
68
70 static void kill ()
71 {
72 if (m_singleton)
73 {
74 delete m_singleton;
75 m_singleton = NULL;
76 }
77 }
78
79 private:
80 static T *m_singleton;
81};
82
83template <typename T> T *AbstractSingleton<T>::m_singleton = NULL;
84
85template <typename T>
87{
88protected:
90 Singleton() { m_singleton = NULL; }
92 virtual ~Singleton()
93 {
94 Log::info("Singleton", "Destroyed singleton.");
95 }
96
97public:
99 static T *getInstance()
100 {
101 if (m_singleton == NULL)
102 m_singleton = new T;
103 return m_singleton;
104 }
105
107 static void kill()
108 {
109 if (m_singleton)
110 {
111 delete m_singleton;
112 m_singleton = NULL;
113 }
114 }
115
116private:
117 static T *m_singleton;
118};
119
120template <typename T> T *Singleton<T>::m_singleton = NULL;
121
122
123#endif // SINGLETON_HPP
Manages the abstract singleton at runtime. This has been designed to allow multi-inheritance....
Definition: singleton.hpp:35
virtual ~AbstractSingleton()
Destructor.
Definition: singleton.hpp:40
static void kill()
Used to kill the singleton, if needed.
Definition: singleton.hpp:70
AbstractSingleton()
Constructor.
Definition: singleton.hpp:38
static T * getInstance()
Used to get the instance.
Definition: singleton.hpp:64
static S * getInstance()
Used to get the instance, after a dynamic cast. This is important when making a double-inheritance of...
Definition: singleton.hpp:53
Definition: singleton.hpp:87
Singleton()
Constructor.
Definition: singleton.hpp:90
static void kill()
Used to kill the singleton, if needed.
Definition: singleton.hpp:107
virtual ~Singleton()
Destructor.
Definition: singleton.hpp:92
static T * getInstance()
Used to get the instance.
Definition: singleton.hpp:99