Singleton.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Version : 2.3.15
  5. * Author : Bruce Liang
  6. * Website : http://www.jessma.org
  7. * Project : https://github.com/ldcsaa
  8. * Blog : http://www.cnblogs.com/ldcsaa
  9. * Wiki : http://www.oschina.net/p/hp-socket
  10. * QQ Group : 75375912
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. #pragma once
  25. #define SINGLETON_THIS(ClassName) ClassName::GetThis()
  26. #define SINGLETON_INSTANCE(ClassName) ClassName::GetInstance()
  27. #define SINGLETON_OBJECT(ObjName) SINGLETON_INSTANCE(C##ObjName)
  28. #define DEFINE_SINGLETON(ClassName) \
  29. ClassName* ClassName::m_pThis = nullptr;
  30. #define DEFINE_P_THIS(ClassName) \
  31. DEFINE_SINGLETON(ClassName)
  32. #define DECLARE_SINGLETON_INTERFACE(ClassName) \
  33. public: \
  34. static ClassName* GetThis() {return m_pThis;} \
  35. static ClassName& GetInstance() {return *m_pThis;} \
  36. protected: \
  37. static ClassName* m_pThis;
  38. #define DECLARE_SINGLETON_CREATE_INSTANCE(ClassName) \
  39. public: \
  40. static BOOL CreateInstance() \
  41. { \
  42. if(!m_pThis) \
  43. m_pThis = new ClassName; \
  44. \
  45. return m_pThis != nullptr; \
  46. } \
  47. \
  48. static BOOL DeleteInstance() \
  49. { \
  50. if(m_pThis) \
  51. { \
  52. delete m_pThis; \
  53. m_pThis = nullptr; \
  54. } \
  55. \
  56. return m_pThis == nullptr; \
  57. }
  58. #define DECLARE_PRIVATE_DEFAULT_CONSTRUCTOR(ClassName) \
  59. private: \
  60. ClassName(){}
  61. #define DECLARE_PRIVATE_COPY_CONSTRUCTOR(ClassName) \
  62. private: \
  63. ClassName(const ClassName&); \
  64. ClassName& operator = (const ClassName&);
  65. #define DECLARE_NO_COPY_CLASS(className) \
  66. DECLARE_PRIVATE_COPY_CONSTRUCTOR(className)
  67. #define DECLARE_SINGLETON_IMPLEMENT_NO_CREATE_INSTANCE(ClassName) \
  68. DECLARE_SINGLETON_INTERFACE(ClassName) \
  69. DECLARE_PRIVATE_DEFAULT_CONSTRUCTOR(ClassName) \
  70. DECLARE_PRIVATE_COPY_CONSTRUCTOR(ClassName)
  71. #define DECLARE_SINGLETON_IMPLEMENT_NO_DEFAULT_CONSTRUCTOR(ClassName) \
  72. DECLARE_SINGLETON_CREATE_INSTANCE(ClassName) \
  73. DECLARE_PRIVATE_COPY_CONSTRUCTOR(ClassName)
  74. #define DECLARE_SINGLETON_IMPLEMENT(ClassName) \
  75. DECLARE_SINGLETON_IMPLEMENT_NO_DEFAULT_CONSTRUCTOR(ClassName) \
  76. DECLARE_PRIVATE_DEFAULT_CONSTRUCTOR(ClassName)
  77. #define DECLARE_SINGLETON_NO_DEFAULT_CONSTRUCTOR(ClassName) \
  78. DECLARE_SINGLETON_INTERFACE(ClassName) \
  79. DECLARE_SINGLETON_IMPLEMENT_NO_DEFAULT_CONSTRUCTOR(ClassName)
  80. #define DECLARE_SINGLETON(ClassName) \
  81. DECLARE_SINGLETON_NO_DEFAULT_CONSTRUCTOR(ClassName) \
  82. DECLARE_PRIVATE_DEFAULT_CONSTRUCTOR(ClassName)
  83. template<class T>
  84. class CSingleObject
  85. {
  86. public:
  87. CSingleObject () {T::CreateInstance();}
  88. ~CSingleObject () {T::DeleteInstance();}
  89. T* GetPointer () {return T::GetThis();}
  90. T& GetObject () {return T::GetInstance();}
  91. BOOL IsValid () {return GetPointer() != nullptr;}
  92. };
  93. #define DECLARE_SINGLE_OBJECT(ClassName) CSingleObject<ClassName> _##ClassName##_Single_Object_;