msg.h 3.1 KB

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