msg.h 2.8 KB

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