Advertisement
AI_UBI

Singletone template

Mar 8th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #pragma once
  2.  
  3. template<typename T>
  4. class Singleton
  5. {
  6. public:
  7.  
  8.     Singleton() = delete;
  9.     Singleton(const Singleton<T> &) = delete;
  10.     Singleton<T> & operator =(const Singleton<T> &) = delete;
  11.     ~Singleton() = delete;
  12.  
  13.     static T * getInstance();
  14.  
  15.     static bool delInstance();
  16.    
  17. private:
  18.  
  19.     static T *m_Self;
  20. };
  21.  
  22. template<typename T>
  23. T *Singleton<T>::m_Self = nullptr;
  24.  
  25. template<typename T>
  26. inline T * Singleton<T>::getInstance()
  27. {
  28.     if (!m_Self)
  29.     {
  30.         m_Self = new T();
  31.     }
  32.  
  33.     return m_Self;
  34. }
  35.  
  36. template<typename T>
  37. inline bool Singleton<T>::delInstance()
  38. {
  39.     if (m_Self)
  40.     {
  41.         delete m_Self;
  42.  
  43.         m_Self = nullptr;
  44.        
  45.         return true;
  46.     }
  47.  
  48.     return false;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement