所以我正在创建一个小的框架来创建StateMachines。该框架目前使用的TransitionTable如下所示:
typedef std::map<std::string, std::string> inner_table;
typedef std::map<std::string, inner_table> complete_table;
complete_table test = {
{
"State1",{
{ "Event1", "State2" },
{ "Event2", "State3" },
{ "Event3", "State3" }
}
},
{
"State2",{
{ "Event1", "State3" },
{ "Event2", "State1" },
{ "Event3", "State3" }
}
},
{
"State3",{
{ "Event1", "State1" },
{ "Event2", "State2" }
}
}
};
这对我来说很好。状态都是单独的类,名称带有默认的Cstr。唯一的问题是:我不喜欢您使用它的方式。 我想到这个:
TransitionTable<
Current, Event , Next
Transition<State1, Event1, State2>,
Transition<State1, Event1, State2>,
Transition<State1, Event1, State2>,
Transition<State1, Event1, State2>,
Transition<State1, Event1, State2>,
Transition<State1, Event1, State2>,
Transition<State1, Event1, State2>,
Transition<State1, Event1, State2>,
Transition<State1, Event1, State2>,
Transition<State1, Event1, State2>>
table{};
此表更具可读性,您不必为每个州都提供一个名称,该名称始终可以包含一个错字。 现在,我创建了两个模板类,但是我真的不知道如何继续。我有点卡住。
#pragma once
#include <map>
typedef std::map<std::string, std::string> inner_table;
typedef std::map<std::string, inner_table> complete_table;
template<typename CurrentState, typename Event, typename NextState>
struct Transition{
Transition();
static complete_table GetTransition();
std::string m_CurrentState;
std::string m_Event;
std::string m_NextState;
};
template <typename CurrentState, typename Event, typename NextState>
Transition<CurrentState, Event, NextState>::Transition() :
m_CurrentState(typeid(CurrentState).name()),
m_Event(typeid(Event).name()),
m_NextState(typeid(NextState).name()) {
}
template <typename CurrentState, typename Event, typename NextState>
complete_table Transition<CurrentState, Event, NextState>::GetTransition() {
return{ {m_CurrentState,{{m_Event,m_NextState}}} };
}
template<typename Transition, typename ...Args>
class TransitionTable {
public:
TransitionTable();
complete_table operator+=(const complete_table& table);
~TransitionTable() = default;
private:
complete_table m_TransitionTable;
};
//Help this is wrong.
template <typename Transition, typename ... Args>
TransitionTable<Transition, Args...>::TransitionTable() {
m_TransitionTable = Transition::GiveTransition(); // Wrong but to show the idea
}
template <typename Transition, typename ... Args>
complete_table TransitionTable<Transition, Args...>::operator+=(const complete_table& table) {
}
我的问题是:
我不需要完整的解决方案,只需提示如何进行即可。