BsModule.h 4.0 KB

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