Boost :: msm Как инициализировать state_machine_def и msm :: front :: state с помощью конструктора не по умолчанию

У меня конечный автомат выглядит так:

class FsmDef : public boost::msm::front::state_machine_def<FsmDef> {
private:
Args args;
using State = boost::msm::front::state<>;
public:
FsmDef(Args args) : args{args}
{}struct InitState {};
struct State1 {
Args1 args1;
State1(Args1 args1) : args1(args1)
{}
};

struct transition_table : boost::mpl::vector<
boost::msm::front::Row<Init, boost::msm::front::none, State1>
> { };

using initial_state = InitState;
};

using Fsm = boost::msm::back::state_machine<FsmDef>;

Fsm fsm;

Как я могу построить fsm и инициализировать личные данные для FsmDef, То же самое с State1.

0

Решение

FsmDef может быть не конструктивным по умолчанию. Но State1 должен быть конструктивным по умолчанию.

Вот способ передать аргументы FsmDef,

#include <iostream>
#include <boost/msm/back/state_machine.hpp>

#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>

struct Args {
int val;
};

class FsmDef : public boost::msm::front::state_machine_def<FsmDef> {
private:
Args args_;
using State = boost::msm::front::state<>;
public:
FsmDef(Args args) : args_{args}
{
std::cout << args_.val << std::endl;
}struct InitState : boost::msm::front::state<> {};
struct State1 : boost::msm::front::state<> {
// states must be default constructible
//    Args1 args1;
//    State1(Args1 args1) : args1(args1)
//    {}
};

struct transition_table : boost::mpl::vector<
boost::msm::front::Row<InitState, boost::msm::front::none, State1>
> { };

using initial_state = InitState;
};

using Fsm = boost::msm::back::state_machine<FsmDef>;

int main() {
Args a {42};
Fsm fsm(a);
}

Запуск демо https://wandbox.org/permlink/ZhhblHFKYWd3ieDK

Fsm, boost::msm::back::state_machine<FsmDef> имеет конструктор, который имеет те же параметры, что и FsmDef, AFAIK, это явно не задокументировано.

Вот код, определяющий конструктор.

https://github.com/boostorg/msm/blob/boost-1.64.0/include/boost/msm/back/state_machine.hpp#L1625

2

Другие решения

Других решений пока нет …