msg.hpp 2.3 KB

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