diff --git a/cxx/mcc_finite_state_machine.h b/cxx/mcc_finite_state_machine.h index 15df69e..4d9ab7c 100644 --- a/cxx/mcc_finite_state_machine.h +++ b/cxx/mcc_finite_state_machine.h @@ -73,8 +73,6 @@ inline std::error_code make_error_code(MccFiniteStateMachineErrorCode ec) return std::error_code(static_cast(ec), MccFiniteStateMachineCategory::get()); } - - namespace traits { @@ -82,8 +80,7 @@ namespace traits The only requirement to Event-class is public-accepted static constant 'ID' */ template -concept fsm_event_c = std::is_default_constructible_v && std::is_move_constructible_v && std::movable && - requires { requires std::same_as; }; +concept fsm_event_c = requires { requires std::same_as; }; /* @@ -91,11 +88,10 @@ concept fsm_event_c = std::is_default_constructible_v && std::is_move_constru definition of type transition_table_t */ template -concept fsm_state_c = - std::is_default_constructible_v && std::is_move_constructible_v && std::movable && requires { - requires std::same_as; - typename T::transition_t; - }; +concept fsm_state_c = std::is_default_constructible_v && requires { + requires std::same_as; + typename T::transition_t; +}; // concept for std::pair @@ -282,42 +278,6 @@ protected: using variant_from_tuple_t = typename variant_from_tuple::variant_t; - // call given function for all event-types in the given std::tuple - template - static void for_each_event(FT&& func, std::index_sequence) - { - (func(std::get(EvTplT{})), ...); - }; - - template - static void for_each_event(FT&& func) - { - for_each_event(std::forward(func), std::make_index_sequence>()); - }; - - - - template - static void for_each_event_in_each_state(FT&& func) - { - for_each_event(std::forward(func)); - } - - - template - static void for_each_event_in_each_state(FT&& func, std::index_sequence) - { - (for_each_event_in_each_state>(std::forward(func)), ...); - } - - template - static void for_each_event_in_each_state(FT&& func) - { - for_each_event_in_each_state(std::forward(func), - std::make_index_sequence>()); - } - - // check if given event-type is in std::tuple of event-types template struct in_tuple; @@ -344,38 +304,6 @@ protected: std::mutex _transitionMutex; - template - void setupInstanceFuncs(FT&& func, std::index_sequence) - { - (_dispatchEventFunc>.emplace(this, func), ...); - - (_moveFunc.emplace_back([](MccFiniteStateMachine* from, MccFiniteStateMachine* to) { - _dispatchEventFunc>[to] = - std::move(_dispatchEventFunc>[from]); - }), - ...); - - (_copyFunc.emplace_back([](const MccFiniteStateMachine* from, MccFiniteStateMachine* to) { - _dispatchEventFunc>[to] = - _dispatchEventFunc>[from]; - }), - ...); - - (_destroyFunc.emplace_back([](const MccFiniteStateMachine* inst) { - // - _dispatchEventFunc>.erase(inst); - }), - ...); - - (_eventID.emplace_back(std::tuple_element_t::ID), ...); - }; - - template - void setupInstanceFuncs(FT&& func) - { - setupInstanceFuncs(std::forward(func), std::make_index_sequence>()); - }; - static MccFiniteStateMachine& copyInstance(const MccFiniteStateMachine* from, MccFiniteStateMachine* to) { if (from != to) { @@ -451,119 +379,6 @@ public: using all_events_t = deduce_events_t; - /* - for_each_event([this](EvT) { _eventID.emplace_back(EvT::ID); }); - - for_each_event([states, currentState, this](EvT) mutable { - if constexpr (!in_tuple_v) { - throw std::system_error(MccFiniteStateMachineErrorCode::ERROR_UNREGISTERED_EVENT_TYPE); - } - - _dispatchEventFunc[this] = [states, currentState, this](EvT& event) mutable { - std::lock_guard lock(_transitionMutex); - - std::visit( - [&event, states, currentState, this](curr_state_t*) { - using to_state_t = curr_state_t::transition_t::template find_state_by_event_t; - if constexpr (!std::is_void_v) { - // exit from current - if constexpr (requires(curr_state_t inst) { - { inst.exit(std::declval()) }; - }) { - std::get(*states).exit(event); - } else if constexpr (requires(curr_state_t inst) { - { inst.exit() }; - }) { - std::get(*states).exit(); - } - - // transit ... - if constexpr (requires(EvT inst) { - { inst.onTransit() }; - }) { - event.onTransit(); - } - - *currentState = &std::get(*states); - _currentStateID = to_state_t::ID; - - // enter to new - if constexpr (requires(to_state_t inst) { - { inst.enter(std::declval()) }; - }) { - std::get(*states).enter(event); - } else if constexpr (requires(to_state_t inst) { - { inst.enter() }; - }) { - std::get(*states).enter(); - } - } else { - throw std::system_error(MccFiniteStateMachineErrorCode::ERROR_UNHANDLED_TRANSITION); - } - }, - *currentState); - }; - - _moveFunc.emplace_back([](MccFiniteStateMachine* from, MccFiniteStateMachine* to) { - _dispatchEventFunc[to] = std::move(_dispatchEventFunc[from]); - }); - _copyFunc.emplace_back([](const MccFiniteStateMachine* from, MccFiniteStateMachine* to) { - _dispatchEventFunc[to] = _dispatchEventFunc[from]; - }); - _destroyFunc.emplace_back([](const MccFiniteStateMachine* inst) { - // - _dispatchEventFunc.erase(inst); - }); - }); - - - - setupInstanceFuncs([states, currentState, this](EvT& event) mutable { - std::lock_guard lock(_transitionMutex); - - std::visit( - [&event, states, currentState, this](curr_state_t*) { - using to_state_t = curr_state_t::transition_t::template find_state_by_event_t; - if constexpr (!std::is_void_v) { - // exit from current - if constexpr (requires(curr_state_t inst) { - { inst.exit(std::declval()) }; - }) { - std::get(*states).exit(event); - } else if constexpr (requires(curr_state_t inst) { - { inst.exit() }; - }) { - std::get(*states).exit(); - } - - // transit ... - if constexpr (requires(EvT inst) { - { inst.onTransit() }; - }) { - event.onTransit(); - } - - *currentState = &std::get(*states); - _currentStateID = to_state_t::ID; - - // enter to new - if constexpr (requires(to_state_t inst) { - { inst.enter(std::declval()) }; - }) { - std::get(*states).enter(event); - } else if constexpr (requires(to_state_t inst) { - { inst.enter() }; - }) { - std::get(*states).enter(); - } - } else { - throw std::system_error(MccFiniteStateMachineErrorCode::ERROR_UNHANDLED_TRANSITION); - } - }, - *currentState); - }); - */ - [states, currentState, this](std::index_sequence) { ((_dispatchEventFunc>[this] = [states, currentState, this](EvT& event) mutable { @@ -625,7 +440,6 @@ public: ...); (_destroyFunc.emplace_back([](const MccFiniteStateMachine* inst) { - // _dispatchEventFunc>.erase(inst); }), ...); @@ -675,6 +489,7 @@ public: template auto dispatchEvent() + requires std::default_initializable { static EvT event; return dispatchEvent(event);