singleton.h 759 B

12345678910111213141516171819202122232425262728293031
  1. #ifndef SINGLETON_H
  2. #define SINGLETON_H
  3. #define DISABLE_COPY(Class) \
  4. Class(const Class &) = delete; \
  5. Class &operator=(const Class &) = delete;
  6. #define DCLR_SINGLETON(Class) \
  7. public: \
  8. static Class* instance(); \
  9. static void exitInstance(); \
  10. private: \
  11. DISABLE_COPY(Class) \
  12. static Class* s_pInstance;
  13. #define IMPL_SINGLETON(Class) \
  14. Class* Class::s_pInstance = NULL; \
  15. Class* Class::instance(){ \
  16. if (s_pInstance == NULL){ \
  17. s_pInstance = new Class; \
  18. } \
  19. return s_pInstance; \
  20. } \
  21. void Class::exitInstance(){ \
  22. if (s_pInstance){ \
  23. delete s_pInstance; \
  24. s_pInstance = NULL; \
  25. } \
  26. }
  27. #endif // SINGLETON_H