1: #ifndef __SINGTMPL_H__ 
   2: #define __SINGTMPL_H__ 
   3: 
   4: 
   5: 
   6: #if defined ( DEBUG ) 
   7: #define DEBUG_CODE( code_to_put_into_source ) { code_to_put_into_source } 
   8: #else 
   9: #define DEBUG_CODE( code_to_put_into_source ) 
  10: #endif 
  11: 
  12: 
  13: /**
  14: *
  15: * class CSingleton
  16: *
  17: * Class CSingleton is a template class that makes shure that only one instance of type T exists.
  18: * You can also reach this instance from any class easily by calling it's static method getInstance().
  19: *
  20: * Attention:
  21: * There are two ways of using the constructin/destructing the class:
  22: *
  23: * 1. Create an instance of class Singleton<MyClass>:
  24: *    Works similar to an autopointer: 
  25: *    When instance is deleted also MyClass will be also be deleted 
  26: *    since it is created on the stack:
  27: *
  28: *    e.g.:
  29: *    typedef Singleton<MyClass> MySingleton;
  30: *    MySingleton mySingle;
  31: *    MyClass * Pointer2MyClass_2 = mySingle.getInstance();
  32: *    // do something with the pointer
  33: *
  34: * 2. Never create an instance of class Singleton<MyClass>,
  35: *    but always only use the static getInstance() and destroyInstance() methods.
  36: *    Warning: You have to destroy the class manually with destroyInstance(),
  37: *    since the object is created on the heap.
  38: *
  39: *    e.g.:
  40: *    typedef Singleton<MyClass> MySingleton;
  41: *    MyClass * Pointer2MyClass_2 = MySingleton::getInstance();
  42: *    // do something with the pointer
  43: *    Pointer2MyClass_2->doSomethingMethod();
  44: *    // Destroy object trough static method:
  45: *    MySingleton::destroyInstance();
  46: *
  47: * You also can mix this two ways of constructing/destructing
  48: * the singleton class up if you know what you are doing.
  49: *
  50: *
  51: * @author Arno Wilhelm
  52: * @date 2002-02-20
  53: */
  55: template <class T> class CSingleton
  56: {
  57: public:
  58: 
  59: 	/**
  60: 	* Constructor
  61: 	*/
  63: 	CSingleton()
  64:       {
  65:       DEBUG_CODE( cerr << "CSingleton()" << endl; )
  66:       }
  67: 
  68: 	/**
  69: 	* Destructor: deletes the instance of type T and
  70: 	* 			  resets the pointer to it ( m_instance ) to zero.
  71: 	*/
  73: 	~CSingleton()
  74:       {
  75:       if ( 0 != m_instance )
  76:           {
  77:           delete m_instance;
  78:           m_instance = 0;
  79:           }
  80:       DEBUG_CODE( cerr << "~CSingleton()" << endl; )
  81:       }
  82: 
  83: 	/**
  84: 	* getInstance returns a pointer to type T.
  85: 	* If it does not already exist it is created.
  86: 	* The pointer is stored in member m_instance.
  87: 	* Since getInstance is static it can by reached from anywhere
  88: 	* ( e.g.: T * = CSingleton<T>::getInstance() )
  89: 	* @return returns a pointer to type T
  90: 	* @see CSingleton
  91: 	*/
  93:     static T* const getInstance()
  94:       {
  95:       if ( !m_instance )
  96:           {
  97:           m_instance = new T;
  98:           }
  99: 
 100:       return m_instance;
 101:       }
 102: 
 103: 	/**
 104: 	* destroyInstance deletes the instance of type T it exists.
 105: 	* The pointer m_instance is then reset to zero.
 106: 	* Since destroyInstance is static it can by reached from anywhere
 107: 	* ( e.g.: CSingleton<T>::destroyInstance() )
 108: 	* Use this method when you never have called the constructor of Singleton<T>.
 109: 	* @see CSingleton
 110: 	*/
 112:     static void destroyInstance()
 113:       {
 114:       if ( m_instance )
 115:           {
 116:           delete m_instance;
 117:           m_instance = 0;
 118:           }
 119:       }
 120: 
 121: private:
 122: 	/**
 123: 	* pointer to type T. Exists only once since it is static.
 124: 	*/
 126:     static T* m_instance;
 127: };
 128: 
 129: template<class T> T* CSingleton<T>::m_instance=0;
 130: 
 131: #endif 
 132: 
 133: