From 88e45886b288dcca3663e8a646b6f2b0b191536c Mon Sep 17 00:00:00 2001 From: "Timur A. Fatkhullin" Date: Mon, 9 Jun 2025 18:24:28 +0300 Subject: [PATCH] ... --- cxx/mcc_finite_state_machine.h | 60 +++++++++++++++------------------- cxx/tests/fsm_test.cpp | 12 +++---- 2 files changed, 33 insertions(+), 39 deletions(-) diff --git a/cxx/mcc_finite_state_machine.h b/cxx/mcc_finite_state_machine.h index 4d9ab7c..4a77eb9 100644 --- a/cxx/mcc_finite_state_machine.h +++ b/cxx/mcc_finite_state_machine.h @@ -1,7 +1,9 @@ #pragma once -#include -#include +/* MOUNT CONTROL COMPONENTS LIBRARY */ + +/* FINITE-STATE MACHINE IMPLEMENTATION */ + #include #include @@ -17,6 +19,8 @@ namespace mcc::fsm { +/* error codes enum definition */ + enum class MccFiniteStateMachineErrorCode : int { ERROR_OK, ERROR_UNREGISTERED_EVENT_TYPE, ERROR_UNHANDLED_TRANSITION }; } // namespace mcc::fsm @@ -24,10 +28,12 @@ enum class MccFiniteStateMachineErrorCode : int { ERROR_OK, ERROR_UNREGISTERED_E namespace std { + template <> class is_error_code_enum : public true_type { }; + } // namespace std @@ -35,6 +41,8 @@ namespace mcc::fsm { +/* error category definition */ + // error category struct MccFiniteStateMachineCategory : public std::error_category { MccFiniteStateMachineCategory() : std::error_category() {} @@ -85,7 +93,7 @@ concept fsm_event_c = requires { requires std::same_as concept fsm_state_c = std::is_default_constructible_v && requires { @@ -111,7 +119,10 @@ concept fsm_tuple_of_events_c = } // namespace traits -/* Event-to-State transition table definition */ +/* + * Event-to-State transition table definition + * (I do not use here concepts from the above traits to avoid possible recursive concept problem) + */ template struct fsm_transition_table_t; @@ -181,6 +192,11 @@ public: }; +/* + * Finite-state machine definition + * (an idea is from https://codeberg.org/cmargiotta/compile-time-fsm) + */ + class MccFiniteStateMachine { protected: @@ -298,10 +314,10 @@ protected: std::vector> _destroyFunc{}; std::string_view _currentStateID; - std::vector _stateID; - std::vector _eventID; + std::vector _stateID{}; + std::vector _eventID{}; - std::mutex _transitionMutex; + std::mutex _transitionMutex{}; static MccFiniteStateMachine& copyInstance(const MccFiniteStateMachine* from, MccFiniteStateMachine* to) @@ -350,30 +366,6 @@ public: return std::vector({STs::ID...}); }(*states); - int status; - char* aa = abi::__cxa_demangle(typeid(states_t).name(), NULL, NULL, &status); - std::cout << "deduced states_t = " << aa << '\n'; - free(aa); - - aa = abi::__cxa_demangle(typeid(typename InitStateT::transition_t::event_state_pair_t).name(), NULL, NULL, - &status); - std::cout << "event_state_pair_t = " << aa << '\n'; - free(aa); - - aa = abi::__cxa_demangle(typeid(typename InitStateT::transition_t::events_t).name(), NULL, NULL, &status); - std::cout << "events_t = " << aa << '\n'; - free(aa); - - aa = - abi::__cxa_demangle(typeid(typename InitStateT::transition_t::unique_states_t).name(), NULL, NULL, &status); - std::cout << "unique_states_t = " << aa << '\n'; - free(aa); - - aa = abi::__cxa_demangle(typeid(deduce_events_t).name(), NULL, NULL, &status); - std::cout << "deduced events_t = " << aa << '\n'; - free(aa); - - // setup dispatch event functions @@ -446,8 +438,6 @@ public: (_eventID.emplace_back(std::tuple_element_t::ID), ...); }(std::make_index_sequence>()); - - std::cout << "MOVE VEC: " << _moveFunc.size() << "\n"; } MccFiniteStateMachine(const MccFiniteStateMachine& other) @@ -501,6 +491,8 @@ public: } + // returns IDs of all deduced unique states + template R stateIDs() const { @@ -518,6 +510,8 @@ public: } + // returns IDs of all deduced events + template R eventIDs() const { diff --git a/cxx/tests/fsm_test.cpp b/cxx/tests/fsm_test.cpp index 37c1444..3fde96d 100644 --- a/cxx/tests/fsm_test.cpp +++ b/cxx/tests/fsm_test.cpp @@ -190,14 +190,14 @@ int main() // fsmach.dispatchEvent(); - // using tab_t = fsm::fsm_transition_table_t, std::pair>; + using tab_t = fsm::fsm_transition_table_t, std::pair>; - // using st_t = tab_t::find_state_by_event_t; + using st_t = tab_t::find_state_by_event_t; - // int status; - // char* aa = abi::__cxa_demangle(typeid(st_t).name(), NULL, NULL, &status); - // std::cout << "aa = " << aa << "\n"; - // free(aa); + int status; + char* aa = abi::__cxa_demangle(typeid(st_t).name(), NULL, NULL, &status); + std::cout << "aa = " << aa << "\n"; + free(aa); return 0;