首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Boost.Statechart -选择点的文档化方法问题

Boost.Statechart -选择点的文档化方法问题
EN

Stack Overflow用户
提问于 2010-07-14 05:41:37
回答 1查看 371关注 0票数 1

根据documentation中的示例,我编写了以下无法编译的代码,因为custom_reaction<>似乎与作为state<>的第三个模板参数的概念不匹配。我如何才能真正做出选择呢?(我也在boost列表中提出了这个问题)

代码语言:javascript
复制
#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();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-07-14 05:57:33

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

代码语言:javascript
复制
// 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() ) );
  }
};

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3241781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档