ctkSingleton.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkSingleton_h
  15. #define __ctkSingleton_h
  16. //
  17. /// \ingroup Core
  18. /// @{
  19. /// Singleton definition and declaration helpers
  20. //
  21. /// See http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12
  22. /// and http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Nifty_Counter
  23. //
  24. /// Inspired from VTK/Utilities/kwsys/SystemTools class
  25. //
  26. //-----------------------------------------------------------------------------
  27. /// Should be included as a class protected member
  28. #define CTK_SINGLETON_DECLARE(NAME) \
  29. static NAME* Instance; \
  30. static void classInitialize(); \
  31. static void classFinalize(); \
  32. friend class NAME##Initialize; \
  33. typedef NAME Self;
  34. //-----------------------------------------------------------------------------
  35. /// Help macro allowing to declare the utility class to make sure
  36. /// NAME is initialized before it is used.
  37. //
  38. /// Should be added at the bottom of the header file, after the class declaration
  39. //
  40. /// The instance (NAME##%Initializer) will show up in any translation unit
  41. /// that uses NAME. It will make sure NAME is initialized before it is used.
  42. ///
  43. #define CTK_SINGLETON_DECLARE_INITIALIZER(EXPORT_DIRECTIVE,NAME) \
  44. class EXPORT_DIRECTIVE NAME##Initialize \
  45. { \
  46. public: \
  47. typedef NAME##Initialize Self; \
  48. \
  49. NAME##Initialize(); \
  50. ~NAME##Initialize(); \
  51. private: \
  52. static unsigned int Count; \
  53. }; \
  54. \
  55. static NAME##Initialize NAME##Initializer;
  56. //-----------------------------------------------------------------------------
  57. //
  58. /// Implementation of %NAME##%Initialize class.
  59. //
  60. /// Note: NAME##%Initialize::%Count and NAME::%Instance Must NOT be initialized.
  61. /// Default initialization to zero is necessary.
  62. //
  63. #define CTK_SINGLETON_DEFINE_INITIALIZER(NAME) \
  64. NAME##Initialize::NAME##Initialize() \
  65. { \
  66. if(++Self::Count == 1) \
  67. { NAME::classInitialize(); } \
  68. } \
  69. \
  70. NAME##Initialize::~NAME##Initialize() \
  71. { \
  72. if(--Self::Count == 0) \
  73. { NAME::classFinalize(); } \
  74. } \
  75. \
  76. unsigned int NAME##Initialize::Count; \
  77. NAME* NAME::Instance;
  78. //----------------------------------------------------------------------------
  79. //
  80. /// This should be added at the end of the CPP file
  81. //
  82. #define CTK_SINGLETON_DEFINE(NAME) \
  83. void NAME::classInitialize() \
  84. { \
  85. Self::Instance = new NAME; \
  86. } \
  87. \
  88. void NAME::classFinalize() \
  89. { \
  90. delete Self::Instance; \
  91. } \
  92. \
  93. CTK_SINGLETON_DEFINE_INITIALIZER(NAME)
  94. ///@}
  95. #endif //__ctkSingleton_h