Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6070963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:59:35+00:00 2026-05-23T09:59:35+00:00

I’ve developed some concept code for a project that I will be working on

  • 0

I’ve developed some concept code for a project that I will be working on shortly. The project lends itself to a state machine design and I think boost::statechart will do a good job. I hit a roadblock when I tried to use context() however. Here’s a sample (I’m happy to put more code up, but I think this is the relevant part):

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                                                                                                                                                
    }
  }

};

The state Active has methods called “[over|under]flow_condition” which just return true at this point. Problems with my design aside, I am getting the following assertion failure when I instantiate thusly:

int main(void) {

  Throttler my_throttler;

  my_throttler.initiate();

  return 0;

}

and here’s the assertion:

assertion “get_pointer( stt.pContext_
) != 0” failed

I looked this assertion up in file “/usr/include/boost/statechart/simple_state.hpp”, line 689 (boost 1.45) and the comments say that it is there to prevent simple_state from using contexts. This puzzled me when I revisited the stopwatch example and saw that the example was doing the very thing I was trying to do. So I compiled it and this assertion is not violated by the stopwatch code unsurprisingly. Am I missing something? Maybe there’s something elsewhere in the code that I missed? Here’s the entire header (please remember it’s concept code… I’m not releasing this into the wild until it’s been thoroughly genericized):

    #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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:59:35+00:00Added an answer on May 23, 2026 at 9:59 am

    As you’ve noted in your comment, it’s related to attempting to access the outer context from within the constructor, which is not allowed for a simple_state.

    From 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 );
    

    So you should be able to access the outer context from the constructor if you base your states on the state class (rather than a simple_state).

    That said, I’m not sure what impacts or implications this may have for your states. If this question gets an answer it may be helpful to you as well (:

    From what I understand, you’ll need to change Wait to derive from state:

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

    and then change the Wait() constructor to something like

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

    The my_context type is defined (as protected) within state<>, and needs to be passed in from the derived class’s constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.