In the documentation for boost::msm there is an example of a state machine without a default constructor. I can get it to work where I jump directly from a super-SM into a sub-SM. But when I jump from one sub-SM into another sub-SM the target SM is default constructed and my data is gone. Below is an example. I can't get it to compile without providing default constructors for the sub states. Do I need to provide some extra info in my sub-SMs for this to work?
When I run the example I get:
Jumping directly to Sub1
Sub1.data_ = 0x7fff5fbfd778
Jumping directly to Sub2
Sub2.data_ = 0x7fff5fbfd778
Jumping from Sub1 to Sub2
Sub1.data_ = 0x7fff5fbfd778
Sub2.data_ = 0xdeadbeef
I expected the 0xdeadbeef to be the same address as the others.
#include <iostream>
#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>
namespace {
using namespace boost::msm;
using namespace boost::msm::front;
namespace mpl = boost::mpl;
struct MyData {
int data;
};
struct EvGotoSub1 {};
struct EvGotoSub2 {};
struct MainSM_;
using Main = back::state_machine<MainSM_>;
struct Sub1SM_;
using Sub1 = back::state_machine<Sub1SM_>;
struct Sub2SM_;
using Sub2 = back::state_machine<Sub2SM_>;
struct Sub1SM_ : state_machine_def<Sub1SM_> {
struct Started : state<> {
template <class Event,class Fsm>
void on_entry(Event const& e, Fsm& fsm) {
std::cout << "Sub1.data_ = " << (void*) fsm.data_ << std::endl;
}
};
using initial_state = mpl::vector<Started>;
struct transition_table:mpl::vector<
Row<Started, EvGotoSub2, Sub2, none, none>
> {};
MyData* data_;
Sub1SM_() : data_((MyData*) 0xdeadbeef) {};
Sub1SM_(MyData* data) : data_(data) {};
};
struct Sub2SM_ : state_machine_def<Sub2SM_> {
struct Started : state<> {
template <class Event,class Fsm>
void on_entry(Event const& e, Fsm& fsm) {
std::cout << "Sub2.data_ = " << (void*) fsm.data_ << std::endl;
}
};
using initial_state = mpl::vector<Started>;
struct transition_table:mpl::vector<
//Row<Started, EvGotoSub1, Sub1, none, none>
> {};
MyData* data_;
Sub2SM_() : data_((MyData*) 0xdeadbeef) {};
Sub2SM_(MyData* data) : data_(data) {};
};
struct MainSM_ : state_machine_def<MainSM_> {
struct Started : state<> {};
using initial_state = mpl::vector<Started>;
struct transition_table:mpl::vector<
Row<Started, EvGotoSub1, Sub1, none, none>,
Row<Started, EvGotoSub2, Sub2, none, none>
> {};
MyData* data_;
MainSM_(MyData* data) : data_(data) {};
};
}
int main() {
MyData data { 123 };
auto CreateMain = [&data] {
auto ret = Main(back::states_ << Sub1(&data) << Sub2(&data), &data);
ret.start();
return ret;
};
std::cout << "Jumping directly to Sub1" << std::endl;
Main main = CreateMain();
main.process_event(EvGotoSub1());
std::cout << "Jumping directly to Sub2" << std::endl;
main = CreateMain();
main.process_event(EvGotoSub2());
std::cout << "Jumping from Sub1 to Sub2" << std::endl;
main = CreateMain();
main.process_event(EvGotoSub1());
main.process_event(EvGotoSub2());
}
The default constructor must be defined.
For Sub1
you must specify as well which constructor of Sub2
you want to use:
auto ret = Main(back::states_ << Sub1(back::states_ << Sub2(&data), &data) << Sub2(&data), &data);
This outputs:
Jumping directly to Sub1
Sub1.data_ = 0xbfe82bf0
Jumping directly to Sub2
Sub2.data_ = 0xbfe82bf0
Jumping from Sub1 to Sub2
Sub1.data_ = 0xbfe82bf0
Sub2.data_ = 0xbfe82bf0
live example: https://ideone.com/XMtDye