hoa_utils::Singleton< T > Class Template Reference

Used for transforming a standard class into a singleton class. More...

#include <utils.h>

Inheritance diagram for hoa_utils::Singleton< T >:

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

Collaboration graph
[legend]
List of all members.

Public Member Functions

virtual bool SingletonInitialize ()=0
 A method for the inheriting class to implement, which initializes the class.

Static Public Member Functions

static T * SingletonCreate ()
 Creates and returns an instance of the singleton class.
static void SingletonDestroy ()
 Destroys the singleton class instance.
static const T * SingletonGetReference ()
 Returns a pointer to the singleton class instance (or NULL if the class is not instantiated).

Protected Member Functions

 Singleton ()
virtual ~Singleton ()

Static Protected Attributes

static T * _singleton_reference
 A reference to the singleton class instance itself.

Private Member Functions

Singletonoperator= (const Singleton &s)
 Singleton (const Singleton &s)

Detailed Description

template<typename T>
class hoa_utils::Singleton< T >

Used for transforming a standard class into a singleton class.

****************************************************************************

This is a templated abstract class which classes may derive from to become singleton classes. To create a new singleton type class, follow the steps below. It is assumed that the desired class is called "ClassName".

  1. In the header file, define the class as follows: class ClassName : public hoa_utils::Singleton<ClassName>
  2. Make hoa_utils::Singleton<ClassName> a friend of ClassName in the header file
  3. Put the ClassName() constructor in the private section of the class, and the destructor in the public section
  4. Define the following function in the public section of the class and implement it: bool SingletonInitialize()
  5. In the source file, set the static template member like so: template<> ClassName* Singleton<ClassName>::_singleton_reference = NULL

With this done, your new class should be ready to go. To create and use a singleton class, do the following:

  1. Call ClassName* SingletonCreate() to get a pointer to the new singleton class instance
  2. After the singleton object has been created, next call bool SingletonInitialize() to initialize the class
  3. If you ever need to retreive a pointer for a singleton you've created, just call the static method const ClassName* SingletonGetReference()
  4. Call SingletonDestroy to destroy the class and its underlying singleton. You can then create and initialize the singleton once more if you'd like.

Note:
The creation steps listed above are the only way to properly construct a singleton class object. Don't try to circumvent it, and never attempt to modify the protected static member Singleton::_singleton_reference directly, except as instructed above.

Sometimes singleton classes need to refer to each other to initialize themselves, particularly with game engine components. That is the purpose of the SingletonInitialize() method, so that all the singleton objects can be created and then reference each other when this method is invoked. It can be viewed as a helper function to the class constructor.

For engine singleton classes, SingletonCreate(), SingletonDestroy(), and SingletonInitialize() should only be called in main.cpp before the main game loop. There may be qualified exceptions to this practice, however.

Most of our singleton classes also define a pointer to their singleton object inside the source file of the class. For example, the GameAudio singleton contains the AudioManager class object name inside the hoa_audio namespace. Therefore you do not need to call the SingletonGetReference() function when this object is made available.

Definition at line 280 of file utils.h.


Constructor & Destructor Documentation

template<typename T>
hoa_utils::Singleton< T >::Singleton (  )  [inline, protected]

Definition at line 285 of file utils.h.

template<typename T>
virtual hoa_utils::Singleton< T >::~Singleton (  )  [inline, protected, virtual]

Definition at line 288 of file utils.h.

template<typename T>
hoa_utils::Singleton< T >::Singleton ( const Singleton< T > &  s  )  [private]


Member Function Documentation

template<typename T>
Singleton& hoa_utils::Singleton< T >::operator= ( const Singleton< T > &  s  )  [private]

template<typename T>
static T* hoa_utils::Singleton< T >::SingletonCreate (  )  [inline, static]

Creates and returns an instance of the singleton class.

Definition at line 293 of file utils.h.

Referenced by main(), and hoa_main::PrintSystemInformation().

template<typename T>
static void hoa_utils::Singleton< T >::SingletonDestroy (  )  [inline, static]

Destroys the singleton class instance.

Definition at line 305 of file utils.h.

Referenced by hoa_main::PrintSystemInformation(), and hoa_video::GameVideo::~GameVideo().

template<typename T>
static const T* hoa_utils::Singleton< T >::SingletonGetReference (  )  [inline, static]

Returns a pointer to the singleton class instance (or NULL if the class is not instantiated).

Definition at line 317 of file utils.h.

template<typename T>
virtual bool hoa_utils::Singleton< T >::SingletonInitialize (  )  [pure virtual]

A method for the inheriting class to implement, which initializes the class.

Returns:
True if initialization was successful, false if it was not

Implemented in hoa_audio::GameAudio, hoa_input::GameInput, hoa_mode_manager::GameModeManager, hoa_script::GameScript, hoa_system::GameSystem, hoa_video::private_video::GUISupervisor, hoa_video::GameVideo, and hoa_global::GameGlobal.


Member Data Documentation

template<typename T>
T* hoa_utils::Singleton< T >::_singleton_reference [static, protected]

A reference to the singleton class instance itself.

Definition at line 283 of file utils.h.

Referenced by hoa_utils::Singleton< hoa_mode_manager::GameModeManager >::SingletonCreate(), hoa_utils::Singleton< hoa_mode_manager::GameModeManager >::SingletonDestroy(), and hoa_utils::Singleton< hoa_mode_manager::GameModeManager >::SingletonGetReference().


The documentation for this class was generated from the following file:
Generated on Fri Jul 6 23:16:11 2007 for Hero of Allacrost by  doxygen 1.5.1