2
0

BsModule.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisitesUtil.h"
  5. #include "BsException.h"
  6. namespace bs
  7. {
  8. /** @addtogroup General
  9. * @{
  10. */
  11. /**
  12. * Represents one engine module. Essentially it is a specialized type of singleton. Module must be manually started up
  13. * and shut down before and after use.
  14. */
  15. template <class T>
  16. class Module
  17. {
  18. public:
  19. /**
  20. * Returns a reference to the module instance. Module has to have been started up first otherwise an exception will
  21. * be thrown.
  22. */
  23. static T& instance()
  24. {
  25. if (!isStartedUp())
  26. {
  27. BS_EXCEPT(InternalErrorException,
  28. "Trying to access a module but it hasn't been started up yet.");
  29. }
  30. if (isDestroyed())
  31. {
  32. BS_EXCEPT(InternalErrorException,
  33. "Trying to access a destroyed module.");
  34. }
  35. return *_instance();
  36. }
  37. /**
  38. * Returns a pointer to the module instance. Module has to have been started up first otherwise an exception will
  39. * be thrown.
  40. */
  41. static T* instancePtr()
  42. {
  43. if (!isStartedUp())
  44. {
  45. BS_EXCEPT(InternalErrorException,
  46. "Trying to access a module but it hasn't been started up yet.");
  47. }
  48. if (isDestroyed())
  49. {
  50. BS_EXCEPT(InternalErrorException,
  51. "Trying to access a destroyed module.");
  52. }
  53. return _instance();
  54. }
  55. /** Constructs and starts the module using the specified parameters. */
  56. template<class ...Args>
  57. static void startUp(Args &&...args)
  58. {
  59. if (isStartedUp())
  60. BS_EXCEPT(InternalErrorException, "Trying to start an already started module.");
  61. _instance() = bs_new<T>(std::forward<Args>(args)...);
  62. isStartedUp() = true;
  63. ((Module*)_instance())->onStartUp();
  64. }
  65. /**
  66. * Constructs and starts a specialized type of the module. Provided type must derive from type the Module is
  67. * initialized with.
  68. */
  69. template<class SubType, class ...Args>
  70. static void startUp(Args &&...args)
  71. {
  72. static_assert(std::is_base_of<T, SubType>::value, "Provided type is not derived from type the Module is initialized with.");
  73. if (isStartedUp())
  74. BS_EXCEPT(InternalErrorException, "Trying to start an already started module.");
  75. _instance() = bs_new<SubType>(std::forward<Args>(args)...);
  76. isStartedUp() = true;
  77. ((Module*)_instance())->onStartUp();
  78. }
  79. /** Shuts down this module and frees any resources it is using. */
  80. static void shutDown()
  81. {
  82. if (isDestroyed())
  83. {
  84. BS_EXCEPT(InternalErrorException,
  85. "Trying to shut down an already shut down module.");
  86. }
  87. if (!isStartedUp())
  88. {
  89. BS_EXCEPT(InternalErrorException,
  90. "Trying to shut down a module which was never started.");
  91. }
  92. ((Module*)_instance())->onShutDown();
  93. bs_delete(_instance());
  94. isDestroyed() = true;
  95. }
  96. /** Query if the module has been started. */
  97. static bool isStarted()
  98. {
  99. return isStartedUp() && !isDestroyed();
  100. }
  101. protected:
  102. Module()
  103. {
  104. }
  105. virtual ~Module()
  106. {
  107. _instance() = nullptr;
  108. isDestroyed() = true;
  109. }
  110. Module(const Module&) { }
  111. Module& operator=(const Module&) { return *this; }
  112. /**
  113. * Override if you want your module to be notified once it has been constructed and started.
  114. *
  115. * @note Useful when your module is polymorphic and you cannot perform some implementation specific
  116. * initialization in constructor itself.
  117. */
  118. virtual void onStartUp() {}
  119. /**
  120. * Override if you want your module to be notified just before it is deleted.
  121. *
  122. * @note Useful when your module is polymorphic and you might want to perform some kind of clean up perhaps
  123. * overriding that of a base class.
  124. */
  125. virtual void onShutDown() {}
  126. /** Returns a singleton instance of this module. */
  127. static T*& _instance()
  128. {
  129. static T* inst = nullptr;
  130. return inst;
  131. }
  132. /**
  133. * Checks has the Module been shut down.
  134. *
  135. * @note If module was never even started, this will return false.
  136. */
  137. static bool& isDestroyed()
  138. {
  139. static bool inst = false;
  140. return inst;
  141. }
  142. /** Checks has the Module been started up. */
  143. static bool& isStartedUp()
  144. {
  145. static bool inst = false;
  146. return inst;
  147. }
  148. };
  149. /** @} */
  150. }