msg.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. public:
  13. AFUN_CORE_EXPORT explicit Message(const std::string &type);
  14. AFUN_CORE_EXPORT virtual ~Message() = default;
  15. [[nodiscard]] const std::string &getType() const {return type;}
  16. };
  17. class MessageStream {
  18. protected:
  19. Message *stream;
  20. [[nodiscard]] AFUN_CORE_EXPORT virtual Message *_getMessage(const std::string &type) const;
  21. public:
  22. AFUN_CORE_EXPORT MessageStream();
  23. AFUN_CORE_EXPORT virtual ~MessageStream();
  24. template<class T>
  25. [[nodiscard]] T *getMessage(const std::string &type) const {
  26. Message *msg = this->_getMessage(type);
  27. T *ret = dynamic_cast<T*>(msg);
  28. return ret;
  29. }
  30. virtual AFUN_CORE_EXPORT Message *popMessage(const std::string &type);
  31. AFUN_CORE_EXPORT void pushMessage(Message *msg);
  32. };
  33. class UpMessage : public MessageStream {
  34. protected:
  35. Message *old;
  36. public:
  37. AFUN_CORE_EXPORT explicit UpMessage(const UpMessage *old);
  38. AFUN_CORE_EXPORT ~UpMessage() override;
  39. AFUN_CORE_EXPORT Message *popMessage(const std::string &type) override;
  40. };
  41. class DownMessage : public MessageStream {};
  42. }
  43. #endif //AFUN_MSG_HPP