This commit is contained in:
Timur A. Fatkhullin 2025-06-09 21:44:11 +03:00
parent 88e45886b2
commit 47413792e5
2 changed files with 23 additions and 4 deletions

View File

@ -331,6 +331,11 @@ protected:
to->_moveFunc = from->_moveFunc;
to->_copyFunc = from->_copyFunc;
to->_destroyFunc = from->_destroyFunc;
to->_stateID = from->_stateID;
to->_eventID = from->_eventID;
to->_currentStateID = from->_currentStateID;
}
return *to;
@ -347,6 +352,11 @@ protected:
to->_moveFunc = std::move(from->_moveFunc);
to->_copyFunc = std::move(from->_copyFunc);
to->_destroyFunc = std::move(from->_destroyFunc);
to->_stateID = std::move(from->_stateID);
to->_eventID = std::move(from->_eventID);
to->_currentStateID = std::move(from->_currentStateID);
}
return *to;
@ -354,7 +364,7 @@ protected:
public:
template <traits::fsm_state_c InitStateT>
MccFiniteStateMachine(InitStateT) : _currentStateID(InitStateT::ID)
constexpr MccFiniteStateMachine(InitStateT) : _currentStateID(InitStateT::ID)
{
using states_t = deduce_states_t<InitStateT>;
auto states = std::make_shared<states_t>();
@ -367,18 +377,20 @@ public:
}(*states);
// setup dispatch event functions
using all_events_t = deduce_events_t<InitStateT>;
[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 {
[states, currentState, this]<traits::fsm_event_c EvT>(EvT& event) {
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) {
@ -478,10 +490,9 @@ public:
}
template <traits::fsm_event_c EvT>
auto dispatchEvent()
auto dispatchEvent(EvT&& event = EvT())
requires std::default_initializable<EvT>
{
static EvT event;
return dispatchEvent(event);
}

View File

@ -199,6 +199,14 @@ int main()
std::cout << "aa = " << aa << "\n";
free(aa);
std::cout << "\nAfter std::move\n";
auto fsmach1 = std::move(fsmach);
std::cout << "STATES: ";
for (auto& el : fsmach1.stateIDs()) {
std::cout << std::quoted(el) << " ";
}
std::cout << "\n";
std::cout << "current state: " << std::quoted(fsmach1.currentStateID()) << "\n";
return 0;
}