msg.h 2.7 KB

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