msg.h 2.8 KB

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