123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- #include "RUMetaState.h"
- #include "RUStateMachine.h"
- NS_RU_BEGIN
- MetaState* MetaState::create(const std::string& stateName,
- const std::vector<std::string>& fullStatePath,
- kStateType stateType,
- const std::vector<StateActInfo>& entryActInfos,
- const std::vector<StateActInfo>& idleActInfos,
- const std::vector<StateActInfo>& exitActInfos,
- const std::string & stateDelegateKey,
- void* userObj4StateDelegate,
- StateMachine* stateMachine)
- {
- MetaState * ret = new (std::nothrow) MetaState();
- ret->_initData(stateName,
- fullStatePath,
- stateType,
- entryActInfos,
- idleActInfos,
- exitActInfos,
- stateDelegateKey,
- userObj4StateDelegate);
- ret->_stateMachine = stateMachine;
- return ret;
- }
- void MetaState::entry(const std::function<void(void)>& entryCb)
- {
- CCASSERT(_stateMachine != nullptr, "wtf");
- FSMLOG("%s进入分层状态: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str());
- State::entry(entryCb);
- }
- void MetaState::idle(const std::function<void(void)>& idleCb)
- {
- CCASSERT(_stateMachine != nullptr, "wtf");
- FSMLOG("%s执行分层状态本身: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str());
-
- auto idleCbT = [=](void){
- FSMLOG("%s执行分层状态机: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str());
- _stateMachine->runMachine(idleCb);
- };
- State::idle(idleCbT);
- }
- void MetaState::exit(const std::function<void(void)>& exitCb)
- {
- //先退出分层状态机再退出本身(退出顺序和进入刚好相反)
- CCASSERT(_stateMachine != nullptr, "wtf");
- auto stopCb = [=](void){
- FSMLOG("%s退出分层状态机: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str());
- State::exit(exitCb);
- };
- FSMLOG("%s退出分层状态本身: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str());
- _stateMachine->stopMachine(stopCb);
- }
- std::vector<std::string> MetaState::getBindingStatePath4Blocks()
- {
- std::vector<std::string> bindingStatePath4Blocks = _fullStatePath;
- std::vector<std::string> historyRunningStateNames = _stateMachine->getHistoryRunningStateNames();
- bindingStatePath4Blocks.insert(bindingStatePath4Blocks.end(), historyRunningStateNames.begin(),historyRunningStateNames.end());
- return bindingStatePath4Blocks;
- }
- std::vector<std::string> MetaState::getInitStateNames()
- {
- CCASSERT(_stateMachine != nullptr, "wtf");
- return _stateMachine->getInitStateNames();
- }
- void MetaState::setRunningStateNames(const std::vector<std::string>& stateNames)
- {
- CCASSERT(_stateMachine != nullptr, "wtf");
- _stateMachine->setRunningStateNames(stateNames);
- }
- std::vector<std::string> MetaState::getRunningStateNames()
- {
- CCASSERT(_stateMachine != nullptr, "wtf");
- return _stateMachine->getRunningStateNames();
- }
- std::vector<std::string> MetaState::getHistoryRunningStateNames()
- {
- CCASSERT(_stateMachine != nullptr, "wtf");
- return _stateMachine->getHistoryRunningStateNames();
- }
- bool MetaState::canResponseEvent(const std::string& eventName)
- {
- CCASSERT(_stateMachine != nullptr, "wtf");
- return _stateMachine->canResponseEvent(eventName);
- }
- void MetaState::handleEvent(const std::string& eventName, const std::map<std::string, std::string>& eventParams, const std::function<void(void)>& handleEventCb)
- {
- CCASSERT(_stateMachine != nullptr, "wtf");
- FSMLOG("%s%s 响应事件: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str(), eventName.c_str());
- _stateMachine->handleEvent(eventName, eventParams, handleEventCb);
- }
- const std::vector<StateActInfo>& MetaState::getIdleActInfosByStateFullPath(const std::vector<std::string>& stateNames)
- {
- CCASSERT(_stateMachine != nullptr, "wtf");
- return _stateMachine->getIdleActInfosByStateFullPath(stateNames);
- }
- void MetaState::stopMachineImmediately()
- {
- CCASSERT(_stateMachine != nullptr, "wtf");
- return _stateMachine->stopMachineImmediately();
- }
- void MetaState::setParam(const std::string& name, const std::string& v) {
- _stateMachine->setParam(name, v);
- }
- void MetaState::debugStateMachineInfo()
- {
- std::string fullStatePathStr;
- for (int i = 0 ; i < _fullStatePath.size(); i++) {
- if (i == _fullStatePath.size() - 1) {
- fullStatePathStr = fullStatePathStr + _fullStatePath[i];
- }else{
- fullStatePathStr = fullStatePathStr + _fullStatePath[i] + "#";
- }
- }
- CCLOG("打印分层状态信息: \"%s\"", fullStatePathStr.c_str());
- _stateMachine->debugInfo();
- }
- void MetaState::setIndent4Dbg(const std::string& indent) {
- _indent = indent;
- _stateMachine->setIndent4Dbg(" " + _indent);
- }
- void MetaState::runMachine4Dbg()
- {
- _stateMachine->runMachine4Dbg();
- }
- void MetaState::handleEvent4Dbg(const std::string& eventName)
- {
- _stateMachine->handleEvent4Dbg(eventName);
- }
- bool MetaState::chekcStateNamesValidity(std::vector<std::string> stateNames) {
- return _stateMachine->chekcStateNamesValidity(stateNames);
- }
- NS_RU_END
|