2017-07-19 113 views
0

我有一個狀態機是這樣的:的boost ::男男性接觸者如何初始化state_machine_def和MSM ::前::狀態與非默認構造函數

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也是一樣。

回答

1

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

Fsmboost::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