singletonmodule.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. Copyright (C) 2001-2006, William Joseph.
  3. All Rights Reserved.
  4. This file is part of GtkRadiant.
  5. GtkRadiant is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. GtkRadiant is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GtkRadiant; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #if !defined(INCLUDED_MODULESYSTEM_SINGLETONMODULE_H)
  18. #define INCLUDED_MODULESYSTEM_SINGLETONMODULE_H
  19. #include "modulesystem.h"
  20. #include <cstddef>
  21. #include "debugging/debugging.h"
  22. #include "modulesystem/moduleregistry.h"
  23. #include "generic/reference.h"
  24. template<typename API, typename Dependencies>
  25. class DefaultAPIConstructor
  26. {
  27. public:
  28. const char* getName()
  29. {
  30. return typename API::Name();
  31. }
  32. API* constructAPI(Dependencies& dependencies)
  33. {
  34. return new API;
  35. }
  36. void destroyAPI(API* api)
  37. {
  38. delete api;
  39. }
  40. };
  41. template<typename API, typename Dependencies>
  42. class DependenciesAPIConstructor
  43. {
  44. public:
  45. const char* getName()
  46. {
  47. return typename API::Name();
  48. }
  49. API* constructAPI(Dependencies& dependencies)
  50. {
  51. return new API(dependencies);
  52. }
  53. void destroyAPI(API* api)
  54. {
  55. delete api;
  56. }
  57. };
  58. class NullDependencies
  59. {
  60. };
  61. template<typename API, typename Dependencies = NullDependencies, typename APIConstructor = DefaultAPIConstructor<API, Dependencies> >
  62. class SingletonModule : public APIConstructor, public Module, public ModuleRegisterable
  63. {
  64. Dependencies* m_dependencies;
  65. API* m_api;
  66. std::size_t m_refcount;
  67. bool m_dependencyCheck;
  68. bool m_cycleCheck;
  69. public:
  70. typedef typename API::Type Type;
  71. SingletonModule()
  72. : m_dependencies(0), m_api(0), m_refcount(0), m_dependencyCheck(false), m_cycleCheck(false)
  73. {
  74. }
  75. explicit SingletonModule(const APIConstructor& constructor)
  76. : APIConstructor(constructor), m_dependencies(0), m_api(0), m_refcount(0), m_dependencyCheck(false), m_cycleCheck(false)
  77. {
  78. }
  79. ~SingletonModule()
  80. {
  81. ASSERT_MESSAGE(m_refcount == 0, "module still referenced at shutdown");
  82. }
  83. void selfRegister()
  84. {
  85. globalModuleServer().registerModule(typename Type::Name(), typename Type::Version(), APIConstructor::getName(), *this);
  86. }
  87. Dependencies& getDependencies()
  88. {
  89. return *m_dependencies;
  90. }
  91. void* getTable()
  92. {
  93. if(m_api != 0)
  94. {
  95. return m_api->getTable();
  96. }
  97. return 0;
  98. }
  99. void capture()
  100. {
  101. if(++m_refcount == 1)
  102. {
  103. globalOutputStream() << "Module Initialising: '" << typename Type::Name() << "' '" << APIConstructor::getName() << "'\n";
  104. m_dependencies = new Dependencies();
  105. m_dependencyCheck = !globalModuleServer().getError();
  106. if(m_dependencyCheck)
  107. {
  108. m_api = APIConstructor::constructAPI(*m_dependencies);
  109. globalOutputStream() << "Module Ready: '" << typename Type::Name() << "' '" << APIConstructor::getName() << "'\n";
  110. }
  111. else
  112. {
  113. globalOutputStream() << "Module Dependencies Failed: '" << typename Type::Name() << "' '" << APIConstructor::getName() << "'\n";
  114. }
  115. m_cycleCheck = true;
  116. }
  117. ASSERT_MESSAGE(m_cycleCheck, "cyclic dependency detected");
  118. }
  119. void release()
  120. {
  121. if(--m_refcount == 0)
  122. {
  123. if(m_dependencyCheck)
  124. {
  125. APIConstructor::destroyAPI(m_api);
  126. }
  127. delete m_dependencies;
  128. }
  129. }
  130. };
  131. #endif