msg.hpp 2.9 KB

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