msg.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "msg.h"
  2. #include "core-activation.h"
  3. #include "inter.h"
  4. #include "env-var.h"
  5. namespace aFuncore {
  6. NormalMessage::NormalMessage(Object *obj_) : obj {obj_} {
  7. obj->addReference();
  8. }
  9. NormalMessage::~NormalMessage(){
  10. if (obj != nullptr)
  11. obj->delReference();
  12. obj = nullptr;
  13. }
  14. void NormalMessage::topProgress(Inter &inter, Activation &activation){
  15. inter.getOutMessageStream().pushMessage("NORMAL", new NormalMessage(std::move(*this)));
  16. }
  17. ErrorMessage::ErrorMessage(std::string error_type_, std::string error_info_, Activation *start)
  18. : error_type{std::move(error_type_)}, error_info{std::move(error_info_)}, inter{start->inter}{
  19. for (const auto activation : inter.getStack()) {
  20. if (activation->getFileLine() != 0)
  21. trackback.push_front({activation->getFilePath(), activation->getFileLine()});
  22. }
  23. }
  24. void ErrorMessage::topProgress(Inter &inter_, Activation &activation){
  25. inter_.getOutMessageStream().pushMessage("ERROR", new ErrorMessage(std::move(*this)));
  26. }
  27. MessageStream::~MessageStream(){
  28. for (auto &msg : stream)
  29. delete msg.second;
  30. }
  31. /**
  32. * 压入 Message
  33. * @param msg Message
  34. */
  35. void MessageStream::pushMessage(const std::string &type, Message *msg){
  36. stream.emplace(type, msg);
  37. }
  38. /**
  39. * 获取 Message
  40. * @param type 类型
  41. * @return Message
  42. */
  43. Message *MessageStream::_getMessage(const std::string &type) const{
  44. auto ret = stream.find(type);
  45. if (ret == stream.end())
  46. return nullptr;
  47. return ret->second;
  48. }
  49. /**
  50. * 弹出Message (使Message脱离数据流)
  51. * @param type 类型
  52. * @return Message
  53. */
  54. Message *MessageStream::popMessage(const std::string &type){
  55. auto ret = stream.find(type);
  56. if (ret == stream.end())
  57. return nullptr;
  58. Message *msg = ret->second;
  59. stream.erase(ret);
  60. return msg;
  61. }
  62. UpMessage::UpMessage(const UpMessage *old_) : MessageStream(), old{old_} {
  63. }
  64. Message *UpMessage::_getMessage(const std::string &type) const {
  65. for (const UpMessage *up = this; up != nullptr; up = up->old) {
  66. Message *ret = up->MessageStream::_getMessage(type);
  67. if (ret != nullptr)
  68. return ret;
  69. }
  70. return nullptr;
  71. }
  72. /**
  73. * 拼接数据流 (将this合并到msg)
  74. * @param msg
  75. */
  76. void DownMessage::joinMsg(DownMessage &msg){
  77. msg.stream.merge(stream);
  78. }
  79. Message *InterMessage::popFrontMessage(std::string &type) {
  80. std::unique_lock<std::mutex> mutex{lock};
  81. if (stream.empty())
  82. return nullptr;
  83. Message *ret = stream.begin()->second;
  84. type = stream.begin()->first;
  85. stream.erase(stream.begin());
  86. return ret;
  87. }
  88. Message *InterMessage::popMessage(const std::string &type) {
  89. std::unique_lock<std::mutex> mutex{lock};
  90. return MessageStream::popMessage(type);
  91. }
  92. void InterMessage::pushMessage(const std::string &type, Message *msg) {
  93. std::unique_lock<std::mutex> mutex{lock};
  94. MessageStream::pushMessage(type, msg);
  95. }
  96. }