FSM: working compile-time deducing states from initial one (i hope ...)

This commit is contained in:
Timur A. Fatkhullin 2025-05-30 17:31:39 +03:00
parent fbe82fc56c
commit 10dd92baab
2 changed files with 56 additions and 24 deletions

View File

@ -165,9 +165,9 @@ namespace traits
template <typename T>
concept fsm_trans_table_c = requires {
// []<fsm_event_state_pair_c... PTs>(std::type_identity<mcc::fsm::MccFsmAbstractState::trans_table_t<PTs...>>) {
// }(std::type_identity<T>());
requires true;
[]<fsm_event_state_pair_c... PTs>(std::type_identity<mcc::fsm::MccFsmAbstractState::trans_table_t<PTs...>>) {
}(std::type_identity<T>());
// requires true;
};
@ -195,7 +195,7 @@ public:
// initFromInitial<ST::transition_t::evst_pairs_t>(typeid(ST));
// }
MccFiniteStateMachine() {}
MccFiniteStateMachine() : _currentStateID(InitStateT::ID), _currentStateVariant(&std::get<InitStateT>(_states)) {}
template <traits::fsm_event_c EvT>
void dispatchEvent(EvT event)
@ -325,34 +325,52 @@ protected:
template <typename... Ts>
struct variant_maker_t<std::tuple<Ts...>> {
// using variant_t = std::variant<Ts*...>;
using variant_t = std::variant<std::reference_wrapper<Ts>...>;
using variant_t = std::variant<Ts*...>;
};
template <typename... TplTs>
struct collect_states_t;
// template <typename InTplT, typename OutTplT = std::tuple<>>
// struct deduce_states_t;
// template <traits::fsm_state_c... StTs, typename OutTplT>
// struct deduce_states_t<std::tuple<StTs...>, OutTplT> {
// using curr_collection_t =
// merge_tuples_res_t<OutTplT, std::tuple<StTs...>, typename StTs::transition_t::unique_states_t...>;
// using states_t = std::conditional_t<
// std::tuple_size_v<OutTplT> == std::tuple_size_v<curr_collection_t>,
// curr_collection_t,
// merge_tuples_res_t<curr_collection_t,
// typename deduce_states_t<merge_tuples_res_t<
// typename StTs::transition_t::unique_states_t...>>::curr_collection_t>>;
// };
template <traits::fsm_state_c StT, typename... TplTs>
struct collect_states_t<std::tuple<StT>, TplTs...> {
using states_t =
merge_tuples_res_t<std::tuple<StT>,
typename collect_states_t<typename StT::transition_t::unique_states_t>::states_t,
TplTs...>;
template <bool stop, typename ResTplT, typename... InTplTs>
struct deduce_states_t;
template <typename ResTplT, typename... InTplTs>
struct deduce_states_t<true, ResTplT, InTplTs...> {
using states_t = ResTplT;
};
template <traits::fsm_state_c... StTs, typename... TplTs>
struct collect_states_t<std::tuple<StTs...>, TplTs...> {
using states_t =
merge_tuples_res_t<std::tuple<StTs...>,
typename collect_states_t<typename StTs::transition_t::unique_states_t...>::states_t,
TplTs...>;
template <typename ResTplT, traits::fsm_state_c... StTs, typename... InTplTs>
struct deduce_states_t<false, ResTplT, std::tuple<StTs...>, InTplTs...> {
using curr_collection_t =
merge_tuples_res_t<ResTplT, std::tuple<StTs...>, typename StTs::transition_t::unique_states_t...>;
using states_t = typename deduce_states_t<std::tuple_size_v<ResTplT> == std::tuple_size_v<curr_collection_t>,
curr_collection_t,
typename StTs::transition_t::unique_states_t...>::states_t;
};
using states_t = typename collect_states_t<std::tuple<InitStateT>>::states_t;
public:
// using states_t = typename deduce_states_t<std::tuple<InitStateT>>::states_t;
using states_t = typename deduce_states_t<false, std::tuple<>, std::tuple<InitStateT>>::states_t;
protected:
states_t _states;
typename variant_maker_t<states_t>::variant_t _currentStateVariant;
};

View File

@ -23,15 +23,26 @@ struct EV2 {
static constexpr std::string_view ID = "EV2";
};
struct EV3 {
static constexpr std::string_view ID = "EV3";
};
struct ST1;
struct ST2;
struct STN : fsm::MccFsmAbstractState {
static constexpr std::string_view ID = "STN";
using transition_t = trans_table_t<std::pair<EV1, STN>, std::pair<EV2, STN>>;
using transition_t = trans_table_t<std::pair<EV1, STN>, std::pair<EV2, ST2>>;
};
struct ST3 : fsm::MccFsmAbstractState {
static constexpr std::string_view ID = "ST3";
using transition_t = trans_table_t<std::pair<EV1, ST1>, std::pair<EV2, ST2>>;
};
struct ST2 : fsm::MccFsmAbstractState {
static constexpr std::string_view ID = "ST2";
using transition_t = trans_table_t<std::pair<EV2, STN>>;
using transition_t = trans_table_t<std::pair<EV2, STN>, std::pair<EV3, ST3>>;
};
struct ST1 : fsm::MccFsmAbstractState {
@ -43,7 +54,7 @@ struct ST1 : fsm::MccFsmAbstractState {
// static_assert(fsm::traits::fsm_trans_table_c<tr_t>, "!!!!!!!!!!!!!");
// fsm::MccFiniteStateMachine<ST1> fsm;
fsm::MccFiniteStateMachine<ST1> fsm;
int main()
{
@ -87,5 +98,8 @@ int main()
free(aa);
free(uu);
aa = abi::__cxa_demangle(typeid(fsm::MccFiniteStateMachine<ST1>::states_t).name(), NULL, NULL, &status);
std::cout << "states = " << aa << "\n";
return 0;
}