Skip to content

Instantly share code, notes, and snippets.

@pmf
Created June 28, 2024 11:58
Show Gist options
  • Save pmf/520607f3952d56c9482e2550296b1ab4 to your computer and use it in GitHub Desktop.
Save pmf/520607f3952d56c9482e2550296b1ab4 to your computer and use it in GitHub Desktop.
#include "sml/sml.hpp"
#include <deque>
#include <queue>
#include <iostream>
struct HandlerSml
{
struct ev_tick {};
struct ev_success {};
struct A {};
struct ActionDispatch
{
void operator()(boost::sml::back::process<ev_success> processEvent)
{
// I do it unconditionally here, but the real example hav condition checks here, making it impossible to
// use process() in the transition table
std::cout << "tick" << std::endl;
processEvent(ev_success{});
}
};
auto operator()() const {
using namespace boost::sml;
// clang-format off
return make_transition_table(
* state<A> + event<ev_tick> / ActionDispatch{}
);
// clang-format on
}
};
struct HandlerSmlParent
{
struct ActionDoneWithSuccess
{
void operator()()
{
std::cout << "success (parent)" << std::endl;
}
};
auto operator()() const
{
using namespace boost::sml;
// clang-format off
return make_transition_table(
*state<HandlerSml> + event<HandlerSml::ev_success> / ActionDoneWithSuccess{}
);
// clang-format on
}
};
int main(int argc, char *argv[])
{
boost::sml::sm<HandlerSmlParent, boost::sml::defer_queue<std::deque>, boost::sml::process_queue<std::queue>> sm{};
sm.process_event(HandlerSml::ev_tick{});
return 0;
}
#include "sml/sml.hpp"
#include <deque>
#include <queue>
#include <iostream>
struct HandlerSml
{
struct ev_tick {};
struct ev_success {};
struct ev_success_internal {};
struct A {};
struct DummyA {};
struct ActionDispatch
{
void operator()(boost::sml::back::process<ev_success_internal> processEvent)
{
std::cout << "tick" << std::endl;
processEvent(ev_success_internal{});
}
};
auto operator()() const {
using namespace boost::sml;
// clang-format off
return make_transition_table(
* state<A> + event<ev_tick> / ActionDispatch{},
state<A> + event<ev_success_internal> = state<DummyA>,
// dispatching to parent via process() here works
state<DummyA> + on_entry<_> / process(ev_success{})
);
// clang-format on
}
};
struct HandlerSmlParent
{
struct ActionDoneWithSuccess
{
void operator()()
{
std::cout << "success (parent)" << std::endl;
}
};
auto operator()() const
{
using namespace boost::sml;
// clang-format off
return make_transition_table(
*state<HandlerSml> + event<HandlerSml::ev_success> / ActionDoneWithSuccess{}
);
// clang-format on
}
};
int main(int argc, char *argv[])
{
boost::sml::sm<HandlerSmlParent, boost::sml::defer_queue<std::deque>, boost::sml::process_queue<std::queue>> sm{};
sm.process_event(HandlerSml::ev_tick{});
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment