msg.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #ifndef AFUN_MSG_HPP
  2. #define AFUN_MSG_HPP
  3. #include "tool.hpp"
  4. #include "aFunCoreExport.h"
  5. namespace aFuncore {
  6. class MessageStream;
  7. class Message;
  8. class NormalMessage;
  9. class UpMessage;
  10. class DownMessage;
  11. }
  12. #include "value.hpp"
  13. namespace aFuncore {
  14. class Message {
  15. friend class MessageStream;
  16. friend class UpMessage;
  17. friend class DownMessage;
  18. Message *next; // 下一条消息
  19. public:
  20. const std::string type; // 消息类型标注
  21. AFUN_CORE_EXPORT explicit Message(const std::string &type_) : type {type_}, next {nullptr} {}
  22. AFUN_CORE_EXPORT virtual ~Message() = default;
  23. };
  24. class TopMessage : public Message {
  25. public:
  26. explicit TopMessage(const std::string &type_) : Message(type_) {}
  27. virtual void topProgress()=0;
  28. };
  29. class NormalMessage : public TopMessage {
  30. Object *obj;
  31. public:
  32. AFUN_CORE_EXPORT explicit NormalMessage(Object *obj);
  33. AFUN_CORE_EXPORT ~NormalMessage() override;
  34. void topProgress() override;
  35. };
  36. class MessageStream {
  37. protected:
  38. Message *stream;
  39. [[nodiscard]] AFUN_CORE_EXPORT virtual Message *_getMessage(const std::string &type) const;
  40. public:
  41. AFUN_CORE_EXPORT MessageStream();
  42. AFUN_CORE_EXPORT virtual ~MessageStream();
  43. template<class T>
  44. [[nodiscard]] T *getMessage(const std::string &type) const {
  45. Message *msg = this->_getMessage(type);
  46. T *ret = dynamic_cast<T*>(msg);
  47. return ret;
  48. }
  49. virtual AFUN_CORE_EXPORT Message *popMessage(const std::string &type);
  50. AFUN_CORE_EXPORT void pushMessage(Message *msg);
  51. template <typename T>
  52. AFUN_CORE_EXPORT void forEach(void (*func)(Message *, T), T arg) {
  53. for (Message *msg = stream; msg != nullptr; msg = msg->next) {
  54. func(msg, arg);
  55. }
  56. }
  57. };
  58. class UpMessage : public MessageStream {
  59. protected:
  60. Message *old;
  61. public:
  62. AFUN_CORE_EXPORT explicit UpMessage(const UpMessage *old=nullptr);
  63. AFUN_CORE_EXPORT ~UpMessage() override;
  64. AFUN_CORE_EXPORT Message *popMessage(const std::string &type) override;
  65. };
  66. class DownMessage : public MessageStream {
  67. public:
  68. AFUN_CORE_EXPORT void joinMsg(DownMessage *msg);
  69. };
  70. }
  71. #endif //AFUN_MSG_HPP