Skip to content

Commit

Permalink
Added "any state" feature allowing a default reaction on events witho…
Browse files Browse the repository at this point in the history
…ut having to add them to every state
  • Loading branch information
rvdvvdw authored and kris-jusiak committed Oct 11, 2023
1 parent 067478e commit 07d1590
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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

0 comments on commit 07d1590

Please sign in to comment.