gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:26:48 25 4
gpt4 key购买 nike

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

最佳答案

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

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

关于c++ - Boost.Statechart - 记录的选择点方法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3241781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com