msg.hpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #ifndef AFUN_MSG_HPP
  2. #define AFUN_MSG_HPP
  3. #include "tool.hpp"
  4. namespace aFuncore {
  5. class MessageStream;
  6. class Message {
  7. std::string type; // 消息类型标注
  8. Message *next; // 下一条消息
  9. friend class MessageStream;
  10. friend class UpMessage;
  11. public:
  12. explicit Message(const std::string &type);
  13. virtual ~Message() = default;
  14. [[nodiscard]] const std::string &getType() const {return type;}
  15. };
  16. class MessageStream {
  17. protected:
  18. Message *stream;
  19. [[nodiscard]] virtual Message *_getMessage(const std::string &type) const;
  20. public:
  21. MessageStream();
  22. virtual ~MessageStream();
  23. template<class T>
  24. [[nodiscard]] T *getMessage(const std::string &type) const {
  25. Message *msg = this->_getMessage(type);
  26. T *ret = dynamic_cast<T*>(msg);
  27. return ret;
  28. }
  29. virtual Message *popMessage(const std::string &type);
  30. void pushMessage(Message *msg);
  31. };
  32. class UpMessage : public MessageStream {
  33. protected:
  34. Message *old;
  35. public:
  36. explicit UpMessage(const UpMessage *old);
  37. ~UpMessage() override;
  38. Message *popMessage(const std::string &type) override;
  39. };
  40. class DownMessage : public MessageStream {};
  41. }
  42. #endif //AFUN_MSG_HPP