Planeshift
singleton.h
Go to the documentation of this file.
1 /*
2  * singleton.h - Author: Andrew Dai
3  *
4  * Copyright (C) 2004 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
5  *
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation (version 2 of the License)
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  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 
19 #ifndef __SINGLETON_H__
20 #define __SINGLETON_H__
21 
26 template <typename T> class Singleton
27 {
28  static T* ptrSingleton;
29 
30 public:
31  Singleton(T* ptr)
32  {
33  CS_ASSERT(ptrSingleton == 0);
34  ptrSingleton = ptr;
35  }
36 
37  // Use this constructor only when the derived class is only deriving from Singleton
38  Singleton(void)
39  {
40  CS_ASSERT(ptrSingleton == 0);
41  ptrSingleton = (T*) (this);
42  }
43 
45  {
46  CS_ASSERT(ptrSingleton != 0);
47  ptrSingleton = NULL;
48  }
49  static T& GetSingleton(void)
50  {
51  return *ptrSingleton;
52  }
53  static T* GetSingletonPtr(void)
54  {
55  return ptrSingleton;
56  }
57 };
58 template <typename T> T* Singleton<T>::ptrSingleton = 0;
59 
62 #endif
63 
~Singleton()
Definition: singleton.h:44
Singleton(T *ptr)
Definition: singleton.h:31
static T & GetSingleton(void)
Definition: singleton.h:49
Singleton(void)
Definition: singleton.h:38
static T * GetSingletonPtr(void)
Definition: singleton.h:53