...
This commit is contained in:
parent
d502796d6b
commit
7d49c3c122
@ -347,7 +347,7 @@ protected:
|
||||
template <traits::fsm_tuple_of_events_c EvTplT, typename FT, size_t... Is>
|
||||
void setupInstanceFuncs(FT&& func, std::index_sequence<Is...>)
|
||||
{
|
||||
((_dispatchEventFunc<std::tuple_element_t<Is, EvTplT>>[this] = std::forward<FT>(func)), ...);
|
||||
(_dispatchEventFunc<std::tuple_element_t<Is, EvTplT>>.emplace(this, func), ...);
|
||||
|
||||
(_moveFunc.emplace_back([](MccFiniteStateMachine* from, MccFiniteStateMachine* to) {
|
||||
_dispatchEventFunc<std::tuple_element_t<Is, EvTplT>>[to] =
|
||||
@ -451,9 +451,9 @@ public:
|
||||
|
||||
using all_events_t = deduce_events_t<InitStateT>;
|
||||
|
||||
// for_each_event<all_events_t>([this]<traits::fsm_event_c EvT>(EvT) { _eventID.emplace_back(EvT::ID); });
|
||||
|
||||
/*
|
||||
for_each_event<all_events_t>([this]<traits::fsm_event_c EvT>(EvT) { _eventID.emplace_back(EvT::ID); });
|
||||
|
||||
for_each_event<all_events_t>([states, currentState, this]<traits::fsm_event_c EvT>(EvT) mutable {
|
||||
if constexpr (!in_tuple_v<EvT, all_events_t>) {
|
||||
throw std::system_error(MccFiniteStateMachineErrorCode::ERROR_UNREGISTERED_EVENT_TYPE);
|
||||
@ -515,7 +515,8 @@ public:
|
||||
_dispatchEventFunc<EvT>.erase(inst);
|
||||
});
|
||||
});
|
||||
*/
|
||||
|
||||
|
||||
|
||||
setupInstanceFuncs<all_events_t>([states, currentState, this]<traits::fsm_event_c EvT>(EvT& event) mutable {
|
||||
std::lock_guard lock(_transitionMutex);
|
||||
@ -561,6 +562,76 @@ public:
|
||||
},
|
||||
*currentState);
|
||||
});
|
||||
*/
|
||||
|
||||
[states, currentState, this]<size_t... Is>(std::index_sequence<Is...>) {
|
||||
((_dispatchEventFunc<std::tuple_element_t<Is, all_events_t>>[this] =
|
||||
[states, currentState, this]<traits::fsm_event_c EvT>(EvT& event) mutable {
|
||||
std::lock_guard lock(_transitionMutex);
|
||||
|
||||
std::visit(
|
||||
[&event, states, currentState, this]<traits::fsm_state_c curr_state_t>(curr_state_t*) {
|
||||
using to_state_t = curr_state_t::transition_t::template find_state_by_event_t<EvT>;
|
||||
if constexpr (!std::is_void_v<to_state_t>) {
|
||||
// exit from current
|
||||
if constexpr (requires(curr_state_t inst) {
|
||||
{ inst.exit(std::declval<EvT&>()) };
|
||||
}) {
|
||||
std::get<curr_state_t>(*states).exit(event);
|
||||
} else if constexpr (requires(curr_state_t inst) {
|
||||
{ inst.exit() };
|
||||
}) {
|
||||
std::get<curr_state_t>(*states).exit();
|
||||
}
|
||||
|
||||
// transit ...
|
||||
if constexpr (requires(EvT inst) {
|
||||
{ inst.onTransit() };
|
||||
}) {
|
||||
event.onTransit();
|
||||
}
|
||||
|
||||
*currentState = &std::get<to_state_t>(*states);
|
||||
_currentStateID = to_state_t::ID;
|
||||
|
||||
// enter to new
|
||||
if constexpr (requires(to_state_t inst) {
|
||||
{ inst.enter(std::declval<EvT&>()) };
|
||||
}) {
|
||||
std::get<to_state_t>(*states).enter(event);
|
||||
} else if constexpr (requires(to_state_t inst) {
|
||||
{ inst.enter() };
|
||||
}) {
|
||||
std::get<to_state_t>(*states).enter();
|
||||
}
|
||||
} else {
|
||||
throw std::system_error(MccFiniteStateMachineErrorCode::ERROR_UNHANDLED_TRANSITION);
|
||||
}
|
||||
},
|
||||
*currentState);
|
||||
}),
|
||||
...);
|
||||
|
||||
(_moveFunc.emplace_back([](MccFiniteStateMachine* from, MccFiniteStateMachine* to) {
|
||||
_dispatchEventFunc<std::tuple_element_t<Is, all_events_t>>[to] =
|
||||
std::move(_dispatchEventFunc<std::tuple_element_t<Is, all_events_t>>[from]);
|
||||
}),
|
||||
...);
|
||||
|
||||
(_copyFunc.emplace_back([](const MccFiniteStateMachine* from, MccFiniteStateMachine* to) {
|
||||
_dispatchEventFunc<std::tuple_element_t<Is, all_events_t>>[to] =
|
||||
_dispatchEventFunc<std::tuple_element_t<Is, all_events_t>>[from];
|
||||
}),
|
||||
...);
|
||||
|
||||
(_destroyFunc.emplace_back([](const MccFiniteStateMachine* inst) {
|
||||
//
|
||||
_dispatchEventFunc<std::tuple_element_t<Is, all_events_t>>.erase(inst);
|
||||
}),
|
||||
...);
|
||||
|
||||
(_eventID.emplace_back(std::tuple_element_t<Is, all_events_t>::ID), ...);
|
||||
}(std::make_index_sequence<std::tuple_size_v<all_events_t>>());
|
||||
|
||||
std::cout << "MOVE VEC: " << _moveFunc.size() << "\n";
|
||||
}
|
||||
@ -595,6 +666,10 @@ public:
|
||||
template <traits::fsm_event_c EvT>
|
||||
auto dispatchEvent(EvT& event)
|
||||
{
|
||||
if (!_dispatchEventFunc<EvT>[this]) {
|
||||
throw std::system_error(MccFiniteStateMachineErrorCode::ERROR_UNREGISTERED_EVENT_TYPE);
|
||||
}
|
||||
|
||||
_dispatchEventFunc<EvT>[this](event);
|
||||
}
|
||||
|
||||
|
||||
@ -30,27 +30,56 @@ struct EV3 {
|
||||
static constexpr std::string_view ID = "EV3";
|
||||
};
|
||||
|
||||
struct EVN {
|
||||
static constexpr std::string_view ID = "EVN";
|
||||
};
|
||||
|
||||
struct ST1;
|
||||
struct ST2;
|
||||
|
||||
struct STN {
|
||||
static constexpr std::string_view ID = "STN";
|
||||
using transition_t = fsm::fsm_transition_table_t<std::pair<EV1, STN>, std::pair<EV2, ST2>>;
|
||||
|
||||
void enter()
|
||||
{
|
||||
std::cout << "transit to " << ID << "-state\n";
|
||||
}
|
||||
|
||||
void exit()
|
||||
{
|
||||
std::cout << "transit from " << ID << "-state\n";
|
||||
}
|
||||
};
|
||||
|
||||
struct ST3 {
|
||||
static constexpr std::string_view ID = "ST3";
|
||||
using transition_t = fsm::fsm_transition_table_t<std::pair<EV1, ST1>, std::pair<EV2, ST2>>;
|
||||
|
||||
void enter(EV3& ev)
|
||||
{
|
||||
std::cout << "transit to " << ID << "-state\n";
|
||||
}
|
||||
|
||||
void exit()
|
||||
{
|
||||
std::cout << "transit from " << ID << "-state\n";
|
||||
}
|
||||
};
|
||||
|
||||
struct ST2 {
|
||||
static constexpr std::string_view ID = "ST2";
|
||||
using transition_t = fsm::fsm_transition_table_t<std::pair<EV2, STN>, std::pair<EV3, ST3>>;
|
||||
|
||||
void enter(EV1& ev)
|
||||
void enter(EV2& ev)
|
||||
{
|
||||
std::cout << "transit to " << ID << "-state\n";
|
||||
}
|
||||
|
||||
void exit()
|
||||
{
|
||||
std::cout << "transit from " << ID << "-state\n";
|
||||
}
|
||||
};
|
||||
|
||||
struct ST1 {
|
||||
@ -58,6 +87,11 @@ struct ST1 {
|
||||
using transition_t =
|
||||
fsm::fsm_transition_table_t<std::pair<EV1, ST2>, std::pair<EV2, STN>, std::pair<EV3, STN>, std::pair<EV3, STN>>;
|
||||
|
||||
void enter()
|
||||
{
|
||||
std::cout << "transit to " << ID << "-state\n";
|
||||
}
|
||||
|
||||
void exit()
|
||||
{
|
||||
std::cout << "transit from " << ID << "-state\n";
|
||||
@ -151,8 +185,10 @@ int main()
|
||||
std::cout << "\n";
|
||||
|
||||
fsmach.dispatchEvent<EV1>();
|
||||
// fsmach.dispatchEvent<EV1>();
|
||||
fsmach.dispatchEvent<EV3>();
|
||||
fsmach.dispatchEvent<EV2>();
|
||||
|
||||
// fsmach.dispatchEvent<EVN>();
|
||||
|
||||
// using tab_t = fsm::fsm_transition_table_t<std::pair<EV1, ST2>, std::pair<EV3, ST1>>;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user