ne_ziggy

Singleton Template Class

Sep 13th, 2012
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. /*!
  2.    \file       single.h
  3.    \brief      A singleton template class. Make sure you use 'friend class Single<Derived>;' so the derived constructor can be private.
  4.    \author     ~Ziggy Stardust
  5. */
  6.  
  7. #ifndef __SINGLE_H__
  8. #define __SINGLE_H__
  9. #include <assert.h>
  10.  
  11. template <class T>
  12. class Single {
  13. protected:
  14.     static T* pInstance;
  15. public:
  16.     /*
  17.     @param  - None
  18.     @return - pointer to derived class
  19.     */
  20.     static T* Instance() {
  21.         if(!pInstance) pInstance = new T;
  22.         return pInstance;
  23.     }
  24.  
  25.     //Frees up instance
  26.     static void DestroyInstance() {
  27.         if(pInstance) delete pInstance;
  28.         pInstance = 0;
  29.     }
  30. };
  31.  
  32. template <class T>
  33. T* Single<T>::pInstance = 0;
  34.  
  35. #endif
Advertisement
Add Comment
Please, Sign In to add comment