msg.h 2.7 KB

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