diff --git a/cxx/mcc_fsm.h b/cxx/mcc_fsm.h index e43f2d9..d455221 100644 --- a/cxx/mcc_fsm.h +++ b/cxx/mcc_fsm.h @@ -25,12 +25,15 @@ namespace traits template concept fsm_event_c = std::is_default_constructible_v && std::is_move_constructible_v && std::movable && requires { - { T::ID } -> std::same_as; // static constant member of event identificator + // { T::ID } -> std::same_as; // static constant member of event identificator + requires std::same_as; }; template concept fsm_event_state_pair_c = requires { - [] StT>(std::type_identity>) { + [](std::type_identity>) { + // [] StT>(std::type_identity>) { }(std::type_identity()); }; @@ -80,7 +83,7 @@ public: template struct trans_table_t { using events_t = std::tuple; - using states_t = std::tuple; + using states_t = std::tuple; using unique_states_t = states_t; using evst_pairs_t = std::tuple; @@ -89,7 +92,7 @@ public: template using find_state_by_event_t = - std::conditional_t, typename PT::first_second, std::nullptr_t>; + std::conditional_t, typename PT::second_type, std::nullptr_t>; }; template @@ -152,6 +155,8 @@ public: template using find_state_by_event_t = find_state_by_event::res_t; }; // end of 'trans_table_t' + + using transition_t = trans_table_t<>; }; // end of 'MccFsmAbstractState' @@ -160,8 +165,9 @@ namespace traits template concept fsm_trans_table_c = requires { - [](std::type_identity>) { - }(std::type_identity()); + // [](std::type_identity>) { + // }(std::type_identity()); + requires true; }; @@ -182,12 +188,14 @@ template class MccFiniteStateMachine { public: - template - MccFiniteStateMachine(ST initial_state) : _currentStateID(ST::ID), _currentStateIDX(typeid(ST)) - { - // setStateFunc(); - initFromInitial(typeid(ST)); - } + // template + // MccFiniteStateMachine(ST initial_state) : _currentStateID(ST::ID), _currentStateIDX(typeid(ST)) + // { + // // setStateFunc(); + // initFromInitial(typeid(ST)); + // } + + MccFiniteStateMachine() {} template void dispatchEvent(EvT event) @@ -209,8 +217,8 @@ protected: template inline static std::unordered_map> exit_state_func_umap_t{}; - std::string_view _currentStateID; - std::type_index _currentStateIDX; + std::string_view _currentStateID{InitStateT::ID}; + std::type_index _currentStateIDX{typeid(InitStateT)}; std::function _exitCurrentStateFunc; @@ -277,18 +285,15 @@ protected: // } - // template<> - - // merge N std::tuple types with filtering dublicates // (NOTE: the first std::tuple type must contain unique types!!!) template struct merge_tuples_t; - template <> - struct merge_tuples_t<> { - using res_t = std::tuple<>; - }; + // template <> + // struct merge_tuples_t> { + // using res_t = std::tuple<>; + // }; template struct merge_tuples_t { @@ -328,10 +333,14 @@ protected: template struct collect_states_t; - // template - // struct collect_states_t { - // using states_t = TplT; - // }; + + template + struct collect_states_t, TplTs...> { + using states_t = + merge_tuples_res_t, + typename collect_states_t::states_t, + TplTs...>; + }; template struct collect_states_t, TplTs...> { @@ -342,7 +351,7 @@ protected: }; - using states_t = typename collect_states_t::states_t; + using states_t = typename collect_states_t>::states_t; states_t _states; typename variant_maker_t::variant_t _currentStateVariant; diff --git a/cxx/tests/fsm_test.cpp b/cxx/tests/fsm_test.cpp index f9b9fec..82a907b 100644 --- a/cxx/tests/fsm_test.cpp +++ b/cxx/tests/fsm_test.cpp @@ -1,15 +1,52 @@ +#include #include #include #include "../mcc_fsm.h" +using namespace mcc; + + struct S { }; + +struct EV1 { + static constexpr std::string_view ID = "EV1"; + + // EV1() = default; + // EV1(EV1&&) = default; + // EV1& operator=(EV1&&) = default; +}; + +struct EV2 { + static constexpr std::string_view ID = "EV2"; +}; + + +struct STN : fsm::MccFsmAbstractState { + static constexpr std::string_view ID = "STN"; + using transition_t = trans_table_t, std::pair>; +}; + +struct ST2 : fsm::MccFsmAbstractState { + static constexpr std::string_view ID = "ST2"; + using transition_t = trans_table_t>; +}; + +struct ST1 : fsm::MccFsmAbstractState { + static constexpr std::string_view ID = "ST1"; + using transition_t = trans_table_t, std::pair>; +}; + +// using tr_t = fsm::MccFsmAbstractState::trans_table_t>; + +// static_assert(fsm::traits::fsm_trans_table_c, "!!!!!!!!!!!!!"); + +// fsm::MccFiniteStateMachine fsm; + int main() { - using namespace mcc; - typedef fsm::unique_tuple_t ut_t; std::cout << "type index: " << fsm::unique_tuple_type_index_t::index << " (double)\n"; @@ -37,5 +74,18 @@ int main() using t2_t = fsm::pair_holder_second_t::second_t; std::cout << "second types: " << typeid(t2_t).name() << "\n"; + + using tp1_t = std::tuple; + using tp2_t = std::tuple; + + int status; + char* aa = abi::__cxa_demangle(typeid(STN::transition_t::states_t).name(), NULL, NULL, &status); + std::cout << "aa = " << aa << "\n"; + char* uu = abi::__cxa_demangle(typeid(STN::transition_t::unique_states_t).name(), NULL, NULL, &status); + std::cout << "uu = " << uu << "\n"; + + free(aa); + free(uu); + return 0; }