Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "any state" feature #602

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/boost/sml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,9 @@ template <class T, class, class... Ts>
transitions<Ts...> get_state_mapping_impl(state_mappings<T, aux::type_list<Ts...>> *);
template <class T, class TMappings, class TUnexpected>
struct get_state_mapping {
using type = decltype(get_state_mapping_impl<T, TUnexpected>((TMappings *)0));
using type = aux::conditional_t<aux::is_same<decltype(get_state_mapping_impl<T, TUnexpected>((TMappings *)0)), transitions<TUnexpected>>::value,
decltype(get_state_mapping_impl<_, TUnexpected>((TMappings *)0)),
decltype(get_state_mapping_impl<T, TUnexpected>((TMappings *)0))>;
};
template <class S>
transitions_sub<S> get_sub_state_mapping_impl(...);
Expand Down
46 changes: 46 additions & 0 deletions test/ft/states.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct e7 {
const auto idle = sml::state<class idle>;
const auto s1 = sml::state<class s1>;
const auto s2 = sml::state<class s2>;
const auto any = sml::state<sml::_>;

test terminate_state = [] {
struct c {
Expand Down Expand Up @@ -215,6 +216,51 @@ test states_initial_entry_actions_with_events = [] {
std::type_index(typeid(sml::anonymous))} == c_.entries);
};

test any_state = [] {
struct c {
auto operator()() {
using namespace sml;
auto action1 = [this]{ calls += "a1|"; };
auto action2 = [this]{ calls += "a2|"; };
auto action3 = [this]{ calls += "a3|"; };

// clang-format off
return make_transition_table(
any + event<e1> / action1,
any + event<e2> / action2,
any + event<e3> / action3 = idle,

*idle + event<e1> = s1,
s1 + event<e2> = s2,
s2 + event<e3> = s1
);
// clang-format on
}

std::string calls{};
};

sml::sm<c> sm{};
const c& c_ = sm;

sm.process_event(e1());
expect(sm.is(s1));
sm.process_event(e1());
expect(sm.is(s1));
sm.process_event(e2());
expect(sm.is(s2));
sm.process_event(e1());
expect(sm.is(s2));
sm.process_event(e2());
expect(sm.is(s2));
sm.process_event(e3());
expect(sm.is(s1));
sm.process_event(e3());
expect(sm.is(idle));

expect("a1|a1|a2|a3|" == c_.calls);
};

#if !defined(_MSC_VER)
test state_names = [] {
struct c {
Expand Down
Loading