Boost logo

Boost :

From: Jeff Garland (jeff_at_[hidden])
Date: 2005-02-26 19:28:31

I'm reviewing the FSM library and I'm wondering if there is any way for a
transition to get a reference to the enclosing state machine. I couldn't see
a way to do this in the docs.

The reason I'm think I would like to get a reference to the machine itself is
that I can see an alternative design that might be 'simplier'. Consider the

using boost::posix_time;

class StopWatch : fsm::state_machine< StopWatch, Active >
  StopWatch() : m_elapsed(0,0,0), m_last_started(not_a_date_time) {};
  time_duration elapsed() const { return m_elapsed; }
  //some sort of friend access that allows states to call these methods
  friend StopwatchState;
  void reset() { m_elapsed = time_duration(0,0,0) };
  void start() { m_last_started = second_clock::local_time(); }
  void stop() { m_elapsed += second_clock::local_time() - m_last_started; }

  time_duration m_elapsed;
  ptime m_last_started;

The idea would be that the various transition constructors would simply
dispatch back to the stop watch machine. The structure of the machine would
naturally prevent the calling of stop() before start() which would result in
incorrect results (not_a_date_time - some_valid_time). And it would also
prevent other erroneous sequences such as start(); start(); step();

Of course this will break the mapping to UML states and might be a bad idea,
but I'm just trying to explore exactly how the library can be used. And maybe
it would actually remove the need for the Active state. Anyway, thoughts?

One small note, it would be nice if there was a second version of the UML
diagram for the example that shows the transition details (that is, running
exit updates active.elapsed) so that the code mapping was more obvious.


Boost list run by bdawes at, gregod at, cpdaniel at, john at