BsEvent.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsModule.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Data common to all event connections.
  8. */
  9. class BaseConnectionData
  10. {
  11. public:
  12. BaseConnectionData()
  13. :prev(nullptr), next(nullptr), isActive(true),
  14. hasHandleLink(true)
  15. {
  16. }
  17. virtual ~BaseConnectionData()
  18. {
  19. assert(!hasHandleLink && !isActive);
  20. }
  21. virtual void deactivate()
  22. {
  23. isActive = false;
  24. }
  25. BaseConnectionData* prev;
  26. BaseConnectionData* next;
  27. bool isActive;
  28. bool hasHandleLink;
  29. };
  30. /**
  31. * @brief Internal data for an Event, storing all connections.
  32. */
  33. struct EventInternalData
  34. {
  35. EventInternalData()
  36. :mConnections(nullptr), mFreeConnections(nullptr)
  37. { }
  38. ~EventInternalData()
  39. {
  40. BaseConnectionData* conn = mConnections;
  41. while (conn != nullptr)
  42. {
  43. BaseConnectionData* next = conn->next;
  44. bs_delete(conn);
  45. conn = next;
  46. }
  47. conn = mFreeConnections;
  48. while (conn != nullptr)
  49. {
  50. BaseConnectionData* next = conn->next;
  51. bs_delete(conn);
  52. conn = next;
  53. }
  54. }
  55. /**
  56. * @brief Disconnects the connection with the specified data,
  57. * ensuring the event doesn't call its callback again.
  58. *
  59. * @note Only call this once.
  60. */
  61. void disconnect(BaseConnectionData* conn)
  62. {
  63. BS_LOCK_RECURSIVE_MUTEX(mMutex);
  64. conn->deactivate();
  65. conn->hasHandleLink = false;
  66. free(conn);
  67. }
  68. /**
  69. * @brief Disconnects all connections in the event.
  70. */
  71. void clear()
  72. {
  73. BS_LOCK_RECURSIVE_MUTEX(mMutex);
  74. BaseConnectionData* conn = mConnections;
  75. while (conn != nullptr)
  76. {
  77. BaseConnectionData* next = conn->next;
  78. conn->deactivate();
  79. if (!conn->hasHandleLink)
  80. free(conn);
  81. conn = next;
  82. }
  83. }
  84. /**
  85. * @brief Called when the event handle no longer keeps
  86. * a reference to the connection data. This means
  87. * we might be able to free (and reuse) its memory
  88. * if the event is done with it too.
  89. */
  90. void freeHandle(BaseConnectionData* conn)
  91. {
  92. BS_LOCK_RECURSIVE_MUTEX(mMutex);
  93. conn->hasHandleLink = false;
  94. if (!conn->isActive)
  95. free(conn);
  96. }
  97. /**
  98. * @brief Releases connection data and makes it
  99. * available for re-use when next connection
  100. * is formed.
  101. */
  102. void free(BaseConnectionData* conn)
  103. {
  104. if (conn->prev != nullptr)
  105. conn->prev->next = conn->next;
  106. else
  107. mConnections = conn->next;
  108. if (conn->next != nullptr)
  109. conn->next->prev = conn->prev;
  110. conn->prev = nullptr;
  111. conn->next = nullptr;
  112. if (mFreeConnections != nullptr)
  113. {
  114. conn->next = mFreeConnections;
  115. mFreeConnections->prev = conn;
  116. }
  117. mFreeConnections = conn;
  118. }
  119. BaseConnectionData* mConnections;
  120. BaseConnectionData* mFreeConnections;
  121. BS_RECURSIVE_MUTEX(mMutex);
  122. };
  123. /**
  124. * @brief Event handle. Allows you to track to which events you subscribed to and
  125. * disconnect from them when needed.
  126. */
  127. class HEvent
  128. {
  129. public:
  130. HEvent()
  131. :mConnection(nullptr)
  132. { }
  133. explicit HEvent(const SPtr<EventInternalData>& eventData, BaseConnectionData* connection)
  134. :mConnection(connection), mEventData(eventData)
  135. { }
  136. ~HEvent()
  137. {
  138. if (mConnection != nullptr)
  139. mEventData->freeHandle(mConnection);
  140. }
  141. /**
  142. * @brief Disconnect from the event you are subscribed to.
  143. */
  144. void disconnect()
  145. {
  146. if (mConnection != nullptr)
  147. {
  148. mEventData->disconnect(mConnection);
  149. mConnection = nullptr;
  150. }
  151. }
  152. struct Bool_struct
  153. {
  154. int _Member;
  155. };
  156. /**
  157. * @brief Allows direct conversion of a handle to bool.
  158. *
  159. * @note This is needed because we can't directly convert to bool
  160. * since then we can assign pointer to bool and that's wrong.
  161. */
  162. operator int Bool_struct::*() const
  163. {
  164. return (mConnection != nullptr ? &Bool_struct::_Member : 0);
  165. }
  166. private:
  167. BaseConnectionData* mConnection;
  168. SPtr<EventInternalData> mEventData;
  169. };
  170. /**
  171. * @brief Events allows you to register method callbacks that get notified
  172. * when the event is triggered.
  173. *
  174. * @note Callback method return value is ignored.
  175. */
  176. // Note: I could create a policy template argument that allows creation of
  177. // lockable and non-lockable events in the case mutex is causing too much overhead.
  178. template <class RetType, class... Args>
  179. class TEvent
  180. {
  181. struct ConnectionData : BaseConnectionData
  182. {
  183. public:
  184. void deactivate() override
  185. {
  186. func = nullptr;
  187. BaseConnectionData::deactivate();
  188. }
  189. std::function<RetType(Args...)> func;
  190. };
  191. public:
  192. TEvent()
  193. :mInternalData(bs_shared_ptr<EventInternalData>())
  194. { }
  195. ~TEvent()
  196. {
  197. clear();
  198. }
  199. /**
  200. * @brief Register a new callback that will get notified once
  201. * the event is triggered.
  202. */
  203. HEvent connect(std::function<RetType(Args...)> func)
  204. {
  205. BS_LOCK_RECURSIVE_MUTEX(mInternalData->mMutex);
  206. ConnectionData* connData = nullptr;
  207. if (mInternalData->mFreeConnections != nullptr)
  208. {
  209. connData = static_cast<ConnectionData*>(mInternalData->mFreeConnections);
  210. mInternalData->mFreeConnections = connData->next;
  211. if (connData->next != nullptr)
  212. connData->next->prev = nullptr;
  213. connData->isActive = true;
  214. connData->hasHandleLink = true;
  215. }
  216. if (connData == nullptr)
  217. connData = bs_new<ConnectionData>();
  218. connData->next = mInternalData->mConnections;
  219. if (mInternalData->mConnections != nullptr)
  220. mInternalData->mConnections->prev = connData;
  221. mInternalData->mConnections = connData;
  222. connData->func = func;
  223. return HEvent(mInternalData, connData);
  224. }
  225. /**
  226. * @brief Trigger the event, notifying all register callback methods.
  227. */
  228. void operator() (Args... args)
  229. {
  230. // Increase ref count to ensure this event data isn't destroyed if one of the callbacks
  231. // deletes the event itself.
  232. std::shared_ptr<EventInternalData> internalData = mInternalData;
  233. BS_LOCK_RECURSIVE_MUTEX(internalData->mMutex);
  234. // Hidden dependency: If any new connections are made during these callbacks they must be
  235. // inserted at the start of the linked list so that we don't trigger them here.
  236. ConnectionData* conn = static_cast<ConnectionData*>(mInternalData->mConnections);
  237. while (conn != nullptr)
  238. {
  239. // Save next here in case the callback itself disconnects this connection
  240. ConnectionData* next = static_cast<ConnectionData*>(conn->next);
  241. if (conn->func != nullptr)
  242. conn->func(args...);
  243. conn = next;
  244. }
  245. }
  246. /**
  247. * @brief Clear all callbacks from the event.
  248. */
  249. void clear()
  250. {
  251. mInternalData->clear();
  252. }
  253. /**
  254. * @brief Check if event has any callbacks registered.
  255. *
  256. * @note It is safe to trigger an event even if no callbacks are registered.
  257. */
  258. bool empty()
  259. {
  260. BS_LOCK_RECURSIVE_MUTEX(mInternalData->mMutex);
  261. return mInternalData->mConnections == nullptr;
  262. }
  263. private:
  264. SPtr<EventInternalData> mInternalData;
  265. };
  266. /************************************************************************/
  267. /* SPECIALIZATIONS */
  268. /* SO YOU MAY USE FUNCTION LIKE SYNTAX FOR DECLARING EVENT SIGNATURE */
  269. /************************************************************************/
  270. /**
  271. * @copydoc TEvent
  272. */
  273. template <typename Signature>
  274. class Event;
  275. /**
  276. * @copydoc TEvent
  277. */
  278. template <class RetType, class... Args>
  279. class Event<RetType(Args...) > : public TEvent <RetType, Args...>
  280. { };
  281. }