msg.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. ~NormalMessage() override;
  32. void topProgress(Inter &inter, Activation &activation) override;
  33. inline Object *getObject();
  34. private:
  35. Object *obj;
  36. };
  37. class AFUN_CORE_EXPORT ErrorMessage : public TopMessage {
  38. public:
  39. explicit ErrorMessage(std::string error_type_, std::string error_info_, Activation *activation);
  40. void topProgress(Inter &inter_, Activation &activation) override;
  41. inline std::string getErrorType();
  42. inline std::string getErrorInfo();
  43. private:
  44. Inter &inter;
  45. std::string error_type;
  46. std::string error_info;
  47. struct TrackBack{
  48. const aFuntool::FilePath path;
  49. aFuntool::FileLine line;
  50. };
  51. std::list<TrackBack> trackback;
  52. };
  53. class AFUN_CORE_EXPORT MessageStream {
  54. public:
  55. MessageStream();
  56. virtual ~MessageStream();
  57. MessageStream &operator=(const MessageStream &)=delete;
  58. template<class T>
  59. [[nodiscard]] T *getMessage(const std::string &type) const;
  60. virtual Message *popMessage(const std::string &type);
  61. void pushMessage(Message *msg);
  62. template <typename Callable, typename...T>
  63. void forEach(Callable func, T...arg);
  64. protected:
  65. Message *stream;
  66. [[nodiscard]] virtual Message *_getMessage(const std::string &type) const;
  67. };
  68. class AFUN_CORE_EXPORT UpMessage : public MessageStream {
  69. public:
  70. explicit UpMessage(const UpMessage *old=nullptr);
  71. ~UpMessage() override;
  72. Message *popMessage(const std::string &type) override;
  73. protected:
  74. Message *old;
  75. };
  76. class AFUN_CORE_EXPORT DownMessage : public MessageStream {
  77. public:
  78. void joinMsg(DownMessage &msg);
  79. };
  80. class AFUN_CORE_EXPORT InterMessage : public MessageStream {
  81. std::mutex mutex;
  82. };
  83. }
  84. #include "msg.inline.h"
  85. #include "msg.template.h"
  86. #endif //AFUN_MSG_H