msg.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #ifndef AFUN_MSG_H
  2. #define AFUN_MSG_H
  3. #include <list>
  4. #include <mutex>
  5. #include <map>
  6. #include "aFuntool.h"
  7. #include "aFunCoreExport.h"
  8. namespace aFuncore {
  9. class AFUN_CORE_EXPORT Message {
  10. public:
  11. explicit inline Message() = default;
  12. virtual ~Message() = default;
  13. Message &operator=(const Message &)=delete;
  14. };
  15. class Object;
  16. class Activation;
  17. class Inter;
  18. class TopMessage : public virtual Message {
  19. public:
  20. virtual void topProgress(Inter &inter, Activation &activation) = 0;
  21. };
  22. class AFUN_CORE_EXPORT NormalMessage : public TopMessage {
  23. public:
  24. explicit NormalMessage(Object *obj_);
  25. inline NormalMessage(NormalMessage &&msg) noexcept;
  26. ~NormalMessage() override;
  27. void topProgress(Inter &inter, Activation &activation) override;
  28. inline Object *getObject();
  29. private:
  30. Object *obj;
  31. };
  32. class AFUN_CORE_EXPORT ErrorMessage : public TopMessage {
  33. public:
  34. struct TrackBack {
  35. const aFuntool::FilePath path;
  36. aFuntool::FileLine line;
  37. };
  38. explicit ErrorMessage(std::string error_type_, std::string error_info_, Activation *start);
  39. inline ErrorMessage(ErrorMessage &&msg) noexcept;
  40. void topProgress(Inter &inter_, Activation &activation) override;
  41. [[nodiscard]] inline std::string getErrorType() const;
  42. [[nodiscard]] inline std::string getErrorInfo() const;
  43. [[nodiscard]] inline const std::list<TrackBack> &getTrackBack() const;
  44. private:
  45. Inter &inter;
  46. std::string error_type;
  47. std::string error_info;
  48. std::list<TrackBack> trackback;
  49. };
  50. class AFUN_CORE_EXPORT MessageStream {
  51. public:
  52. MessageStream() = default;
  53. virtual ~MessageStream();
  54. MessageStream(const MessageStream &)=delete;
  55. MessageStream &operator=(const MessageStream &)=delete;
  56. template<class T>
  57. [[nodiscard]] T *getMessage(const std::string &type) const;
  58. Message *popMessage(const std::string &type);
  59. void pushMessage(const std::string &type, Message *msg);
  60. template <typename Callable, typename...T>
  61. void forEach(Callable func, T...arg);
  62. protected:
  63. std::map<std::string, Message *> stream;
  64. [[nodiscard]] virtual Message *_getMessage(const std::string &type) const;
  65. };
  66. class AFUN_CORE_EXPORT UpMessage : public MessageStream {
  67. public:
  68. explicit UpMessage(const UpMessage *old=nullptr);
  69. ~UpMessage() override = default;
  70. template <typename Callable, typename...T>
  71. void forEachAll(Callable func, T...arg);
  72. protected:
  73. const UpMessage *old;
  74. [[nodiscard]] Message *_getMessage(const std::string &type) const override;
  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. public:
  82. Message *popFrontMessage(std::string &type);
  83. Message *popMessage(const std::string &type);
  84. void pushMessage(const std::string &type, Message *msg);
  85. template <typename Callable, typename...T>
  86. void forEach(Callable func, T...arg);
  87. template <typename Callable, typename...T>
  88. void forEachLock(Callable func, T...arg);
  89. private:
  90. std::mutex lock;
  91. };
  92. class InterOutMessage : public InterMessage {
  93. public:
  94. template<class T>
  95. [[nodiscard]] T *getMessage(const std::string &type) const = delete; // 对外不设置 getMessage 以避免线程问题
  96. };
  97. class InterInMessage : public InterMessage {
  98. };
  99. }
  100. #include "msg.inline.h"
  101. #include "msg.template.h"
  102. #endif //AFUN_MSG_H