Boost logo

Boost Users :

From: Andreas Huber (ahd6974-spamgroupstrap_at_[hidden])
Date: 2006-08-11 13:29:28


Hi Johan

> I need a way to get the current state from an asynchronous state
> machine for
> testing purposes - something analogous to state_cast.
>
> I'll only be using this in my unit tests, so I don't mind if it's a
> bit
> cumbersome to use. Thread-safety also isn't an issue. Is there any way
> of
> getting a reference to the actual Processor instance?

No, there isn't. This is because any call to a member function of
asynchronous_state_machine or simple_state from a different thread than
the worker thread will *very* likely lead to a race condition (or a dead
lock, if you try to use a mutex besides the one in fifo_scheduler).
Here's one way but it is not pretty:
1. Derive a new event, e.g. EvQueryState. As members, add a member
stateType of type const std::type_info & and a boost::function object,
accepting a bool as the only parameter.
2. Add a global handler for unconsumed events to your FSM
<http://boost-consulting.com/boost/libs/statechart/doc/reference.html#unconsumed_event>
3. In the handler check whether the unconsumed event is EvQueryState. If
so, call the embedded function object with
typeid(*(this->state_begin())) == stateType. This of course assumes that
the machine has exactly one innermost state at all times. If you also
need to detect if the machine is terminated or has multiple innermost
states you'd need to pass a collection of const std::type_info *.
4. Build a utility function that accepts a fifo_scheduler, a
processor_handle object and a const type_info &. The function queues a
EvQueryState, waits for the callback and then returns the bool that came
with the callback.

Please let me know whether that works for you.

HTH,

-- 
Andreas Huber
When replying by private email, please remove the words spam and trap
from the address shown in the header.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net