BsModule.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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(isShutDown())
  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 (isShutDown())
  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 (!isShutDown())
  60. BS_EXCEPT(InternalErrorException, "Trying to start an already started module.");
  61. _instance() = bs_new<T>(std::forward<Args>(args)...);
  62. isShutDown() = false;
  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 (!isShutDown())
  74. BS_EXCEPT(InternalErrorException, "Trying to start an already started module.");
  75. _instance() = bs_new<SubType>(std::forward<Args>(args)...);
  76. isShutDown() = false;
  77. ((Module*)_instance())->onStartUp();
  78. }
  79. /** Shuts down this module and frees any resources it is using. */
  80. static void shutDown()
  81. {
  82. if (isShutDown() || isDestroyed())
  83. {
  84. BS_EXCEPT(InternalErrorException,
  85. "Trying to shut down an already shut down module.");
  86. }
  87. ((Module*)_instance())->onShutDown();
  88. bs_delete(_instance());
  89. isDestroyed() = true;
  90. }
  91. /** Query if the module has been started. */
  92. static bool isStarted()
  93. {
  94. return !isShutDown() && !isDestroyed();
  95. }
  96. protected:
  97. Module()
  98. {
  99. }
  100. virtual ~Module()
  101. {
  102. _instance() = nullptr;
  103. isDestroyed() = true;
  104. }
  105. Module(const Module&) { }
  106. Module& operator=(const Module&) { return *this; }
  107. /**
  108. * Override if you want your module to be notified once it has been constructed and started.
  109. *
  110. * @note Useful when your module is polymorphic and you cannot perform some implementation specific
  111. * initialization in constructor itself.
  112. */
  113. virtual void onStartUp() {}
  114. /**
  115. * Override if you want your module to be notified just before it is deleted.
  116. *
  117. * @note Useful when your module is polymorphic and you might want to perform some kind of clean up perhaps
  118. * overriding that of a base class.
  119. */
  120. virtual void onShutDown() {}
  121. /** Returns a singleton instance of this module. */
  122. static T*& _instance()
  123. {
  124. static T* inst = nullptr;
  125. return inst;
  126. }
  127. /**
  128. * Checks has the Module been shut down.
  129. *
  130. * @note If module was never even started, this will return false.
  131. */
  132. static bool& isDestroyed()
  133. {
  134. static bool inst = false;
  135. return inst;
  136. }
  137. /** Checks has the Module been started up. */
  138. static bool& isShutDown()
  139. {
  140. static bool inst = true;
  141. return inst;
  142. }
  143. };
  144. /** @} */
  145. }