【问题标题】:Boost.Statechart - issue with documented method for choice pointsBoost.Statechart - 选择点的记录方法问题
【发布时间】:2010-07-13 21:41:37
【问题描述】:

根据documentation 中的示例,我制作了以下无法编译的代码,因为 custom_reaction 似乎与 state 的第三个模板参数所期望的概念不匹配。我如何真正做出选择点? (我也在boost list上问过这个)

#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();
}

【问题讨论】:

    标签: c++ boost-statechart


    【解决方案1】:

    我想我可能已经找到了答案。我将等待来自 boost 列表的验证,但看起来文档是错误的,如果您按照相机示例中的说明进行自定义反应,它可以正常工作。即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() ) );
      }
    };
    

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

    【讨论】:

    • 好的,在 boost.user 中没有回复,其他人也没有答案。到目前为止,这似乎有效,所以我接受我自己的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多