We are working with signed long (made this explicit) so use fromLong instead of fromInt.
[UnsignedByte.git] / src / Resource / singleton.h
blobdca13d9740bebf42c9f3d8c53d83f86405e61e75
1 #ifndef SINGLETON_H
2 #define SINGLETON_H
4 /**
5 * @file singleton.h
6 * This file contains the Singleton template.
8 * @see Singleton
9 */
11 #include "Assert.h"
13 extern bool g_shutdown; /**< Whether we should allow calls to <code>Free</code>. */
15 /**
16 * A template class that implements the Singleton pattern.
17 * Written on 08-23-2006 by Eran Ifrah.
18 * Many thanks for allowing the free use of this template!
20 template <class T> class Singleton
22 static T* ms_instance; /**< The one instance of this class. */
23 public:
24 /**
25 * Static method to access the only pointer of this instance.
26 * @return a pointer to the only instance of this class
28 static T* Get();
30 /** Release resources. */
31 static void Free();
33 protected:
34 /** Default constructor. */
35 Singleton();
37 /** Destructor. */
38 virtual ~Singleton();
41 template <class T> T* Singleton<T>::ms_instance = 0;
43 template <class T> Singleton<T>::Singleton()
47 template <class T> Singleton<T>::~Singleton()
51 template <class T> T* Singleton<T>::Get()
53 Assert(!g_shutdown);
55 if(!ms_instance)
56 ms_instance = new T();
57 return ms_instance;
60 template <class T> void Singleton<T>::Free()
62 if( ms_instance )
64 delete ms_instance;
65 ms_instance = 0;
69 #endif // SINGLETON_H