Boost.Statechart - 选择点的记录方法问题

时间:2010-07-13 21:41:37

标签: c++ boost-statechart

根据documentation中的示例,我制作了以下无法编译的代码,因为custom_reaction<>似乎与预期作为第三个模板参数的概念不匹配状态<>。我如何真正做出选择点? (我也在提升清单上问过这个问题)

#include <boost/statechart/state_machine.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/state.hpp>
#include <boost/statechart/custom_reaction.hpp>
#include <boost/statechart/event.hpp>
#include <boost/statechart/transition.hpp>
#include <boost/mpl/list.hpp>

#include <iostream>

namespace sc = boost::statechart;

struct make_choice : sc::event< make_choice > {};

// universal choice point base class template
template< class MostDerived, class Context >
struct choice_point : sc::state< MostDerived, Context, 
  sc::custom_reaction< make_choice > >
{
  typedef sc::state< MostDerived, Context, 
    sc::custom_reaction< make_choice > > base_type;
  typedef typename base_type::my_context my_context;
  typedef choice_point my_base;

  choice_point( my_context ctx ) : base_type( ctx )
  {
    this->post_event( boost::intrusive_ptr< make_choice >(
      new make_choice() ) );
  }
};

// ...

struct MyChoicePoint;
struct Machine : sc::state_machine< Machine, MyChoicePoint > {};

struct Dest1 : sc::simple_state< Dest1, Machine > {};
struct Dest2 : sc::simple_state< Dest2, Machine > 
{
  Dest2() { std::cout << "Dest2\n"; }
};
struct Dest3 : sc::simple_state< Dest3, Machine > {};

struct MyChoicePoint : choice_point< MyChoicePoint, Machine >
{
  MyChoicePoint( my_context ctx ) : my_base( ctx ) {}

  sc::result react( const make_choice & )
  {
    if ( 0 )
    {
      return transit< Dest1 >();
    }
    else if ( 1 )
    {
      return transit< Dest2 >();
    }
    else
    {
      return transit< Dest3 >();
    }
  }
};

int main()
{
  Machine machine;
  machine.initiate();

  std::cin.get();
}

1 个答案:

答案 0 :(得分:0)

我想我可能找到了答案。我将等待来自提升列表的验证,但看起来文档是错误的,如果您按照相机示例中的说明进行自定义反应,它可以正常工作。即,choice_point模板需要更改为:

// universal choice point base class template
template< class MostDerived, class Context >
struct choice_point : sc::state< MostDerived, Context >
{
  typedef sc::state< MostDerived, Context > base_type;
  typedef typename base_type::my_context my_context;
  typedef choice_point my_base;

  typedef sc::custom_reaction<make_choice> reactions;

  choice_point( my_context ctx ) : base_type( ctx )
  {
    this->post_event( boost::intrusive_ptr< make_choice >(
      new make_choice() ) );
  }
};

这似乎有效,但我会等一下,以防专家告诉我这是错误的。

相关问题