msg.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. std::string type; // 消息类型标注
  9. Message *next; // 下一条消息
  10. friend class MessageStream;
  11. friend class UpMessage;
  12. friend class DownMessage;
  13. public:
  14. AFUN_CORE_EXPORT explicit Message(const std::string &type_) : type {type_}, next {nullptr} {};
  15. AFUN_CORE_EXPORT virtual ~Message() = default;
  16. [[nodiscard]] const std::string &getType() const {return type;}
  17. };
  18. class MessageStream {
  19. protected:
  20. Message *stream;
  21. [[nodiscard]] AFUN_CORE_EXPORT virtual Message *_getMessage(const std::string &type) const;
  22. public:
  23. AFUN_CORE_EXPORT MessageStream();
  24. AFUN_CORE_EXPORT virtual ~MessageStream();
  25. template<class T>
  26. [[nodiscard]] T *getMessage(const std::string &type) const {
  27. Message *msg = this->_getMessage(type);
  28. T *ret = dynamic_cast<T*>(msg);
  29. return ret;
  30. }
  31. virtual AFUN_CORE_EXPORT Message *popMessage(const std::string &type);
  32. AFUN_CORE_EXPORT void pushMessage(Message *msg);
  33. };
  34. class UpMessage : public MessageStream {
  35. protected:
  36. Message *old;
  37. public:
  38. AFUN_CORE_EXPORT explicit UpMessage(const UpMessage *old=nullptr);
  39. AFUN_CORE_EXPORT ~UpMessage() override;
  40. AFUN_CORE_EXPORT Message *popMessage(const std::string &type) override;
  41. };
  42. class DownMessage : public MessageStream {
  43. public:
  44. AFUN_CORE_EXPORT void joinMsg(DownMessage *msg);
  45. };
  46. }
  47. #endif //AFUN_MSG_HPP