4

我已经为我将很快开展的项目开发了一些概念代码。该项目适用于状态机设计,我认为 boost::statechart 会做得很好。但是,当我尝试使用 context() 时遇到了障碍。这是一个示例(我很高兴添加更多代码,但我认为这是相关部分):

struct Wait : fsm::simple_state< Wait, Active > {

  typedef mpl::list<fsm::transition< UnderflowEvent, Exec> > reactions;

 public:
  Wait()
    : m_wait_op() {
    std::cout << "entering wait state." << std::endl;
    wait();
  }
  ~Wait() { std::cout << "exiting wait state." << std::endl; }

 private:
  default_wait m_wait_op;
  fsm::result wait() {
    if(context<Active>().underflow_condition()) {
      m_wait_op();
      return transit<Wait>();
    }
    else if(context<Active>().overflow_condition()) {
      return transit<Exec>();
    }
    else {
      // undefined - keep waiting                                                                                                                                                
    }
  }

};

状态 Active 具有称为“[over|under]flow_condition”的方法,此时仅返回 true。除了我的设计问题之外,当我实例化时,我遇到了以下断言失败:

int main(void) {

  Throttler my_throttler;

  my_throttler.initiate();

  return 0;

}

这是断言:

断言“get_pointer(stt.pContext_)!= 0”失败

我在文件“/usr/include/boost/statechart/simple_state.hpp”第 689 行(boost 1.45)中查看了这个断言,评论说它是为了防止 simple_state 使用上下文。当我重新访问秒表示例并看到该示例正在做我想做的事情时,这让我感到困惑。所以我编译了它,不出所料,秒表代码没有违反这个断言。我错过了什么吗?也许我错过了代码中的其他地方?这是整个标题(请记住它是概念代码......在完全通用化之前,我不会将其发布到野外):

    #ifndef _THROTTLER_H_
#define _THROTTLER_H_

#include<queue>
#include<vector>
#include<ctime>

#include<boost/statechart/event.hpp>
#include<boost/statechart/transition.hpp>
#include<boost/statechart/state_machine.hpp>
#include<boost/statechart/simple_state.hpp>

#include<boost/mpl/list.hpp>

#include<iostream>


namespace mpl = boost::mpl;
namespace fsm = boost::statechart;

namespace {

  unsigned int DEFAULT_WAIT_TIME(1000);

}

struct noop {
public:

  noop() { m_priority = (1 << sizeof(int)); }
  noop(unsigned int priority) { m_priority = priority; }
  virtual ~noop() {}

  bool operator()(void) {
    return true;
  }
  friend bool operator<(noop a, noop b);

private:
  unsigned int m_priority;
};



bool operator<(noop a, noop b) {
  return a.m_priority < b.m_priority;
}

struct compare_noops {
  bool operator()(noop a, noop b) {
  }
};


struct default_wait {
  void operator()(unsigned long msecs = DEFAULT_WAIT_TIME) {
    std::clock_t endtime =
      std::clock() + (msecs*1000*CLOCKS_PER_SEC);
    while(clock() < endtime);
  }
};



struct OverflowEvent : fsm::event< OverflowEvent > {};
struct UnderflowEvent : fsm::event< UnderflowEvent > {};
struct ResetEvent : fsm::event< ResetEvent > {};

struct Active;
struct Throttler : fsm::state_machine< Throttler, Active > {};

struct Wait;
struct Active : fsm::simple_state< Active, Throttler, Wait > {

 public:

  typedef mpl::list<fsm::transition< ResetEvent, Active> > reactions;

  bool overflow_condition(void) { return true; }
  bool underflow_condition(void) { return true; }

  void queue_operation(noop op) {
    m_operation_queue.push(op);
  }
  void perform_operation(void) {
    noop op(m_operation_queue.top());
    if(op())
      m_operation_queue.pop();
    else
      throw;
  }

 private:

  std::priority_queue<noop, std::vector<noop>, compare_noops > m_operation_queue;
 private:

  std::priority_queue<noop, std::vector<noop>, compare_noops > m_operation_queue;

};

struct Exec : fsm::simple_state< Exec, Active > {

  typedef mpl::list<fsm::transition< OverflowEvent, Wait> > reactions;

  Exec() { std::cout << "entering exec state." << std::endl; }
  ~Exec() { std::cout << "exiting exec state." << std::endl; }

};

struct Wait : fsm::simple_state< Wait, Active > {

  typedef mpl::list<fsm::transition< UnderflowEvent, Exec> > reactions;

 public:
  Wait()
    : m_wait_op() {
    std::cout << "entering wait state." << std::endl;
    wait();
  }
  ~Wait() { std::cout << "exiting wait state." << std::endl; }

 private:
  default_wait m_wait_op;
  fsm::result wait() {
    if(context<Active>().underflow_condition()) {
      m_wait_op();
      return transit<Wait>();
    }
    else if(context<Active>().overflow_condition()) {
      return transit<Exec>();
    }
    else {
      // undefined - keep waiting                                                                                                                                                
    }
  }

};


#endif
4

1 回答 1

4

正如您在评论中指出的那样,它与尝试从构造函数中访问外部上下文有关,而simple_state.

来自simple_state.hpp

  // This assert fails when an attempt is made to access the state machine
  // from a constructor of a state that is *not* a subtype of state<>.
  // To correct this, derive from state<> instead of simple_state<>.
  BOOST_ASSERT( get_pointer( pContext_ ) != 0 );

state因此,如果您将状态基于类(而不是 a simple_state),您应该能够从构造函数访问外部上下文。

也就是说,我不确定这可能对您所在的州产生什么影响或影响。如果这个问题得到答案,它也可能对您有所帮助(:

据我了解,您需要更改Wait以派生自state

 struct Wait : fsm::state< Wait, Active > {

然后将Wait()构造函数更改为类似

typedef fsm::state< Wait, Active > my_base;
Wait( my_context ctx ) : my_base( ctx )
// and any other pre-constructor initialisation...

my_context类型在 中定义(作为受保护的)state<>,并且需要从派生类的构造函数传入。

于 2011-06-30T05:48:56.950 回答