RUMetaState.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "RUMetaState.h"
  2. #include "RUStateMachine.h"
  3. NS_RU_BEGIN
  4. MetaState* MetaState::create(const std::string& stateName,
  5. const std::vector<std::string>& fullStatePath,
  6. kStateType stateType,
  7. const std::vector<StateActInfo>& entryActInfos,
  8. const std::vector<StateActInfo>& idleActInfos,
  9. const std::vector<StateActInfo>& exitActInfos,
  10. const std::string & stateDelegateKey,
  11. void* userObj4StateDelegate,
  12. StateMachine* stateMachine)
  13. {
  14. MetaState * ret = new (std::nothrow) MetaState();
  15. ret->_initData(stateName,
  16. fullStatePath,
  17. stateType,
  18. entryActInfos,
  19. idleActInfos,
  20. exitActInfos,
  21. stateDelegateKey,
  22. userObj4StateDelegate);
  23. ret->_stateMachine = stateMachine;
  24. return ret;
  25. }
  26. void MetaState::entry(const std::function<void(void)>& entryCb)
  27. {
  28. CCASSERT(_stateMachine != nullptr, "wtf");
  29. FSMLOG("%s进入分层状态: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str());
  30. State::entry(entryCb);
  31. }
  32. void MetaState::idle(const std::function<void(void)>& idleCb)
  33. {
  34. CCASSERT(_stateMachine != nullptr, "wtf");
  35. FSMLOG("%s执行分层状态本身: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str());
  36. auto idleCbT = [=](void){
  37. FSMLOG("%s执行分层状态机: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str());
  38. _stateMachine->runMachine(idleCb);
  39. };
  40. State::idle(idleCbT);
  41. }
  42. void MetaState::exit(const std::function<void(void)>& exitCb)
  43. {
  44. //先退出分层状态机再退出本身(退出顺序和进入刚好相反)
  45. CCASSERT(_stateMachine != nullptr, "wtf");
  46. auto stopCb = [=](void){
  47. FSMLOG("%s退出分层状态机: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str());
  48. State::exit(exitCb);
  49. };
  50. FSMLOG("%s退出分层状态本身: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str());
  51. _stateMachine->stopMachine(stopCb);
  52. }
  53. std::vector<std::string> MetaState::getBindingStatePath4Blocks()
  54. {
  55. std::vector<std::string> bindingStatePath4Blocks = _fullStatePath;
  56. std::vector<std::string> historyRunningStateNames = _stateMachine->getHistoryRunningStateNames();
  57. bindingStatePath4Blocks.insert(bindingStatePath4Blocks.end(), historyRunningStateNames.begin(),historyRunningStateNames.end());
  58. return bindingStatePath4Blocks;
  59. }
  60. std::vector<std::string> MetaState::getInitStateNames()
  61. {
  62. CCASSERT(_stateMachine != nullptr, "wtf");
  63. return _stateMachine->getInitStateNames();
  64. }
  65. void MetaState::setRunningStateNames(const std::vector<std::string>& stateNames)
  66. {
  67. CCASSERT(_stateMachine != nullptr, "wtf");
  68. _stateMachine->setRunningStateNames(stateNames);
  69. }
  70. std::vector<std::string> MetaState::getRunningStateNames()
  71. {
  72. CCASSERT(_stateMachine != nullptr, "wtf");
  73. return _stateMachine->getRunningStateNames();
  74. }
  75. std::vector<std::string> MetaState::getHistoryRunningStateNames()
  76. {
  77. CCASSERT(_stateMachine != nullptr, "wtf");
  78. return _stateMachine->getHistoryRunningStateNames();
  79. }
  80. bool MetaState::canResponseEvent(const std::string& eventName)
  81. {
  82. CCASSERT(_stateMachine != nullptr, "wtf");
  83. return _stateMachine->canResponseEvent(eventName);
  84. }
  85. void MetaState::handleEvent(const std::string& eventName, const std::map<std::string, std::string>& eventParams, const std::function<void(void)>& handleEventCb)
  86. {
  87. CCASSERT(_stateMachine != nullptr, "wtf");
  88. FSMLOG("%s%s 响应事件: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str(), eventName.c_str());
  89. _stateMachine->handleEvent(eventName, eventParams, handleEventCb);
  90. }
  91. const std::vector<StateActInfo>& MetaState::getIdleActInfosByStateFullPath(const std::vector<std::string>& stateNames)
  92. {
  93. CCASSERT(_stateMachine != nullptr, "wtf");
  94. return _stateMachine->getIdleActInfosByStateFullPath(stateNames);
  95. }
  96. void MetaState::stopMachineImmediately()
  97. {
  98. CCASSERT(_stateMachine != nullptr, "wtf");
  99. return _stateMachine->stopMachineImmediately();
  100. }
  101. void MetaState::setParam(const std::string& name, const std::string& v) {
  102. _stateMachine->setParam(name, v);
  103. }
  104. void MetaState::debugStateMachineInfo()
  105. {
  106. std::string fullStatePathStr;
  107. for (int i = 0 ; i < _fullStatePath.size(); i++) {
  108. if (i == _fullStatePath.size() - 1) {
  109. fullStatePathStr = fullStatePathStr + _fullStatePath[i];
  110. }else{
  111. fullStatePathStr = fullStatePathStr + _fullStatePath[i] + "#";
  112. }
  113. }
  114. CCLOG("打印分层状态信息: \"%s\"", fullStatePathStr.c_str());
  115. _stateMachine->debugInfo();
  116. }
  117. void MetaState::setIndent4Dbg(const std::string& indent) {
  118. _indent = indent;
  119. _stateMachine->setIndent4Dbg(" " + _indent);
  120. }
  121. void MetaState::runMachine4Dbg()
  122. {
  123. _stateMachine->runMachine4Dbg();
  124. }
  125. void MetaState::handleEvent4Dbg(const std::string& eventName)
  126. {
  127. _stateMachine->handleEvent4Dbg(eventName);
  128. }
  129. bool MetaState::chekcStateNamesValidity(std::vector<std::string> stateNames) {
  130. return _stateMachine->chekcStateNamesValidity(stateNames);
  131. }
  132. NS_RU_END