Singleton< T > Class Template Reference

singleton design pattern implementation More...

#include <singleton.h>

Inherited by UniversalManager< Font, FontManager >, and UniversalManager< Texture, TextureManager >.

Inheritance diagram for Singleton< T >:

Inheritance graph
[legend]
Collaboration diagram for Singleton< T >:

Collaboration graph
[legend]
List of all members.

Public Member Functions

virtual ~Singleton (void)

Static Public Member Functions

static T * getInstance (void)
 returns instance pointer
static T * getPtr (void)
 returns instance pointer
static T & getRef (void)
 returns instance reference

Protected Member Functions

 Singleton (void)
 protected ctor due to singleton DP
 Singleton (const Singleton &)
 protected ctor due to singleton DP
Singletonoperator= (const Singleton &)
 protected operator= due to singleton DP

Static Private Member Functions

static void createInstance (void)
 creates a new instance of singleton class T
static void destroyInstance (void)
 destroys the instance of singleton class T

Static Private Attributes

static T * m_InstancePtr = 0
 static pointer to the instance of the singleton class T
static Singleton< T >::InstanceDestruction m_InstanceDestruction
 static class that destroys singleton instance at the program exit.

Classes

class  InstanceDestruction
 class that will destroy the singleton instance at the program exit. More...

Detailed Description

template<class T>
class Singleton< T >

singleton design pattern implementation

        // Singleton<T> usage example:

        class OneClass: public ClassA, ClassB, Singleton<OneClass>
        {

        // I. Singleton stuffs
        // only the Singleton<OneClass> can call OneClass::OneClass()
        friend class Singleton<OneClass>;
        protected:
                OneClass();  // protected ctor (singleton)
                ~OneClass(); // protected dtor (singleton)

        // II. OneClass definition - methods, attributes
        public:
                some_OneClass_method()
        };
        
        ...

        // Equivalent usage of the OneClass singleton:
        OneClass::getInstance()->some_OneClass_method();
        OneClass::getPtr()->some_OneClass_method();
        OneClass::getRef().some_OneClass_method();

Remarks:
Singleton variables are created at the first instance request. But it is STRONGLY recommended DO NOT using any Singleton class at static-variables initialization time (before entering the main()).

Singleton classes are destroyed at the program exit (at static variables destruction). Therefore, using other classes and global or static variables (other singletons as well) inside the destructor-body of some singleton class could be unsafe (the variables that we are attempting to use might be already destroyed)!

Sometimes, it can be possible to destroy the singleton directly by using delete SomeSingleton::getPtr(). It isn't recommended, however it shouldn't cause any program's crash...

Definition at line 75 of file singleton.h.


Constructor & Destructor Documentation

template<class T>
virtual Singleton< T >::~Singleton void   )  [inline, virtual]
 

Note:
sets internal instance pointer to NULL due to safety reasons.

Definition at line 81 of file singleton.h.

template<class T>
Singleton< T >::Singleton void   )  [inline, protected]
 

protected ctor due to singleton DP

Definition at line 121 of file singleton.h.

template<class T>
Singleton< T >::Singleton const Singleton< T > &   )  [inline, protected]
 

protected ctor due to singleton DP

Definition at line 124 of file singleton.h.


Member Function Documentation

template<class T>
static void Singleton< T >::createInstance void   )  [inline, static, private]
 

creates a new instance of singleton class T

Definition at line 133 of file singleton.h.

Referenced by Singleton< T >::getInstance().

template<class T>
static void Singleton< T >::destroyInstance void   )  [inline, static, private]
 

destroys the instance of singleton class T

Definition at line 141 of file singleton.h.

Referenced by Singleton< T >::InstanceDestruction::~InstanceDestruction().

template<class T>
T * Singleton< T >::getInstance void   )  [inline, static]
 

returns instance pointer

The instance of T is created at the first getInstance() call.

Returns:
pointer to instace of type T
Note:
getInstance, getPtr and getRef are equivalent

Definition at line 165 of file singleton.h.

References Singleton< T >::createInstance(), and Singleton< T >::m_InstancePtr.

Referenced by SurfaceFactory::create(), Singleton< FontManager >::getPtr(), Singleton< FontManager >::getRef(), TerrainClient::loadGlobalTexture(), and TerrainClient::loadLightmap().

Here is the call graph for this function:

template<class T>
static T* Singleton< T >::getPtr void   )  [inline, static]
 

returns instance pointer

The instance of T is created at the first getInstance() call.

Returns:
pointer to instace of type T
Note:
getInstance, getPtr and getRef are equivalent

Definition at line 105 of file singleton.h.

Referenced by GFX::clean().

template<class T>
static T& Singleton< T >::getRef void   )  [inline, static]
 

returns instance reference

The instance of T is created at the first getInstance() call.

Returns:
reference to instace of type T
Note:
getInstance, getPtr and getRef are equivalent

Definition at line 116 of file singleton.h.

Referenced by Messages::init(), Console::init(), and ApplicationClient::init().

template<class T>
Singleton& Singleton< T >::operator= const Singleton< T > &   )  [inline, protected]
 

protected operator= due to singleton DP

Definition at line 127 of file singleton.h.


Member Data Documentation

template<class T>
Singleton< T >::InstanceDestruction Singleton< T >::m_InstanceDestruction [static, private]
 

static class that destroys singleton instance at the program exit.

Note:
it doesn't fail if the instance was destroyed before somehow and set to null

Definition at line 161 of file singleton.h.

template<class T>
T * Singleton< T >::m_InstancePtr = 0 [static, private]
 

static pointer to the instance of the singleton class T

Definition at line 130 of file singleton.h.

Referenced by Singleton< FontManager >::createInstance(), Singleton< FontManager >::destroyInstance(), Singleton< T >::getInstance(), and Singleton< FontManager >::~Singleton().


The documentation for this class was generated from the following file:
Generated on Wed Apr 12 14:23:31 2006 for bjs by  doxygen 1.4.5