tb_msg.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #ifndef TB_MSG_H
  6. #define TB_MSG_H
  7. #include "tb_core.h"
  8. #include "tb_linklist.h"
  9. #include "tb_value.h"
  10. #include "tb_object.h"
  11. #include "tb_id.h"
  12. namespace tb {
  13. class TBMessageHandler;
  14. /** TB_NOT_SOON is returned from TBMessageHandler::GetNextMessageFireTime
  15. and means that there is currently no more messages to process. */
  16. #define TB_NOT_SOON 0xffffffff
  17. /** TBMessageData holds custom data to send with a posted message. */
  18. class TBMessageData : public TBTypedObject
  19. {
  20. public:
  21. TBMessageData() {}
  22. TBMessageData(int v1, int v2) : v1(v1), v2(v2) {}
  23. virtual ~TBMessageData() {}
  24. public:
  25. TBValue v1; ///< Use for anything
  26. TBValue v2; ///< Use for anything
  27. TBID id1; ///< Use for anything
  28. TBID id2; ///< Use for anything
  29. };
  30. /** TBMessageLink should never be created or subclassed anywhere except in TBMessage.
  31. It's only purpose is to add a extra typed link for TBMessage, since it needs to be
  32. added in multiple lists. */
  33. class TBMessageLink : public TBLinkOf<TBMessageLink> { };
  34. /** TBMessage is a message created and owned by TBMessageHandler.
  35. It carries a message id, and may also carry a TBMessageData with
  36. additional parameters. */
  37. class TBMessage : public TBLinkOf<TBMessage>, public TBMessageLink
  38. {
  39. private:
  40. TBMessage(TBID message, TBMessageData *data, double fire_time_ms, TBMessageHandler *mh);
  41. ~TBMessage();
  42. public:
  43. TBID message; ///< The message id
  44. TBMessageData *data; ///< The message data, or nullptr if no data is set
  45. /** The time which a delayed message should have fired (0 for non delayed messages) */
  46. double GetFireTime() { return fire_time_ms; }
  47. private:
  48. friend class TBMessageHandler;
  49. double fire_time_ms;
  50. TBMessageHandler *mh;
  51. };
  52. /** TBMessageHandler handles a list of pending messages posted to itself.
  53. Messages can be delivered immediately or after a delay.
  54. Delayed message are delivered as close as possible to the time they should fire.
  55. Immediate messages are put on a queue and delivered as soon as possible, after any delayed
  56. messages that has passed their delivery time. This queue is global (among all TBMessageHandlers) */
  57. class TBMessageHandler
  58. {
  59. public:
  60. TBMessageHandler();
  61. virtual ~TBMessageHandler();
  62. /** Posts a message to the target after a delay.
  63. data may be nullptr if no extra data need to be sent. It will be deleted
  64. automatically when the message is deleted. */
  65. bool PostMessageDelayed(TBID message, TBMessageData *data, uint32 delay_in_ms);
  66. /** Posts a message to the target at the given time (relative to TBSystem::GetTimeMS()).
  67. data may be nullptr if no extra data need to be sent. It will be deleted
  68. automatically when the message is deleted. */
  69. bool PostMessageOnTime(TBID message, TBMessageData *data, double fire_time);
  70. /** Posts a message to the target.
  71. data may be nullptr if no extra data need to be sent. It will be deleted
  72. automatically when the message is deleted. */
  73. bool PostMessage(TBID message, TBMessageData *data);
  74. /** Check if this messagehandler has a pending message with the given id.
  75. Returns the message if found, or nullptr.
  76. If you want to delete the message, call DeleteMessage. */
  77. TBMessage *GetMessageByID(TBID message);
  78. /** Delete the message from this message handler. */
  79. void DeleteMessage(TBMessage *msg);
  80. /** Delete all messages from this message handler. */
  81. void DeleteAllMessages();
  82. /** Called when a message is delivered.
  83. This message won't be found using GetMessageByID. It is already removed from the list.
  84. You should not call DeleteMessage on this message. That is done automatically after this method exit. */
  85. virtual void OnMessageReceived(TBMessage *msg) {}
  86. // == static methods to handle the queue of messages ====================================================
  87. /** Process any messages in queue. */
  88. static void ProcessMessages();
  89. /** Get when the time when ProcessMessages needs to be called again.
  90. Always returns 0 if there is nondelayed messages to process, which means it needs to be called asap.
  91. If there's only delayed messages to process, it returns the time that the earliest delayed message should be fired.
  92. If there's no more messages to process at the moment, it returns TB_NOT_SOON (No call to ProcessMessages is needed). */
  93. static double GetNextMessageFireTime();
  94. private:
  95. TBLinkListOf<TBMessage> m_messages;
  96. };
  97. }; // namespace tb
  98. #endif // TB_MSG_H