如何访问(boost meta)状态机中的所有状态?

时间:2011-11-27 20:18:54

标签: c++ boost boost-msm

有没有办法访问boost msm中的所有状态(不仅是活动状态)? 例如。放置在状态中的所有UI控件都应该在resize事件上调整大小,无论它们的状态是否处于活动状态。

更新: 让我澄清一点,我需要通过我的状态机创建的所有对象状态的某种迭代器。

更新#2: 以下是一个例子。我需要调用所有状态的resize方法。

struct EventOne {};
struct EventTwo {};

struct StateOne : public state<> {
    void resize() { }
};

struct StateTwo : public state<> {
    void resize() { }
};

struct MyFsm : public state_machine_def<MyFsm> {
    typedef int no_exception_thrown;
    typedef StateOne initial_state;

    struct transition_table : boost::mpl::vector<
        //      Start,          Event,          Next,           Action,         Guard
        Row<    StateOne,       EventOne,       StateTwo,       none,           none            >,
        Row<    StateTwo,       EventTwo,       StateOne,       none,           none            >
    > {
    };
};

typedef boost::msm::back::state_machine<MyFsm> Fsm;

2 个答案:

答案 0 :(得分:4)

你有几种方法。一种方法是使用状态的基类,但是,它是一个基类,所以这是MPL方式:

template <class stt>
struct Visit
{
    Visit(Fsm* fsm): fsm_(fsm){}
    template <class StateType>
    void operator()(boost::msm::wrap<StateType> const&)
    {
        StateType& s = fsm_->template get_state<StateType&>();
        s.resize();
    }
    Fsm* fsm_;
};

Fsm f;
typedef Fsm::stt Stt;
// a set of all state types
typedef boost::msm::back::generate_state_set<Stt>::type all_states; //states
// visit them all using MPL
boost::mpl::for_each<all_states,boost::msm::wrap<boost::mpl::placeholders::_1> > (Visit<Stt>(&f));

这是一个有趣的问题,我会将其添加到文档中。

干杯, 克里斯托夫

PS:这个对SO来说太先进了。它在提升列表上会更快,因为我只是偶尔的SO访问者。

答案 1 :(得分:0)

样本包含此样本

(以及其他?)显示了如何使用州访问者(docs)访问州

相关问题