BsEvent.h 7.2 KB

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