Singleton.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Author : Bruce Liang
  5. * Website : https://github.com/ldcsaa
  6. * Project : https://github.com/ldcsaa/HP-Socket
  7. * Blog : http://www.cnblogs.com/ldcsaa
  8. * Wiki : http://www.oschina.net/p/hp-socket
  9. * QQ Group : 44636872, 75375912
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. #pragma once
  24. #include "hpsocket/GlobalDef.h"
  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_PUBLIC_DEFAULT_CONSTRUCTOR(ClassName) \
  59. public: \
  60. ClassName() = default;
  61. #define DECLARE_PRIVATE_DEFAULT_CONSTRUCTOR(ClassName) \
  62. private: \
  63. ClassName() = default;
  64. #define DECLARE_PRIVATE_COPY_CONSTRUCTOR(ClassName) \
  65. private: \
  66. ClassName(const ClassName&); \
  67. ClassName& operator = (const ClassName&);
  68. #define DECLARE_NO_COPY_CLASS(ClassName) \
  69. private: \
  70. ClassName(const ClassName&) = delete; \
  71. ClassName& operator = (const ClassName&) = delete;
  72. #define DECLARE_SINGLETON_IMPLEMENT_NO_CREATE_INSTANCE(ClassName) \
  73. DECLARE_SINGLETON_INTERFACE(ClassName) \
  74. DECLARE_PRIVATE_DEFAULT_CONSTRUCTOR(ClassName) \
  75. DECLARE_NO_COPY_CLASS(ClassName)
  76. #define DECLARE_SINGLETON_IMPLEMENT_NO_DEFAULT_CONSTRUCTOR(ClassName) \
  77. DECLARE_SINGLETON_CREATE_INSTANCE(ClassName) \
  78. DECLARE_NO_COPY_CLASS(ClassName)
  79. #define DECLARE_SINGLETON_IMPLEMENT(ClassName) \
  80. DECLARE_SINGLETON_IMPLEMENT_NO_DEFAULT_CONSTRUCTOR(ClassName) \
  81. DECLARE_PRIVATE_DEFAULT_CONSTRUCTOR(ClassName)
  82. #define DECLARE_SINGLETON_NO_DEFAULT_CONSTRUCTOR(ClassName) \
  83. DECLARE_SINGLETON_INTERFACE(ClassName) \
  84. DECLARE_SINGLETON_IMPLEMENT_NO_DEFAULT_CONSTRUCTOR(ClassName)
  85. #define DECLARE_SINGLETON(ClassName) \
  86. DECLARE_SINGLETON_NO_DEFAULT_CONSTRUCTOR(ClassName) \
  87. DECLARE_PRIVATE_DEFAULT_CONSTRUCTOR(ClassName)
  88. template<class T> class CSingleObject
  89. {
  90. public:
  91. CSingleObject () {T::CreateInstance();}
  92. ~CSingleObject () {T::DeleteInstance();}
  93. T* GetPointer () {return T::GetThis();}
  94. T& GetObject () {return T::GetInstance();}
  95. BOOL IsValid () {return GetPointer() != nullptr;}
  96. };
  97. #define DECLARE_SINGLE_OBJECT(ClassName) CSingleObject<ClassName> _##ClassName##_Single_Object_;