2010-07-13 22 views
1

根據documentation中的示例,我製作了以下無法編譯的代碼,因爲custom_reaction <>與預期的第三個模板參數< >。我如何真正做出選擇點? (我也在升壓列表上詢問了這個問題)Boost.Statechart - 針對選擇點的記錄方法

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

回答

0

我想我可能已經找到了答案。我會等待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())); 
    } 
}; 

這似乎工作,但我會稍微等一下,以防專家告訴我這是錯的。

+0

好吧,在boost.user中沒有回覆,沒有其他人有答案。這似乎工作到目前爲止,所以我接受我自己的答案。 – 2010-07-15 20:27:15