[Statechart] How to decide between post_event and process_event atrun time ?
Hi, I have a method in which I want to post/process an event. The thing is, this method can be called from outside the state machine (so I need to call process_event), or indirectly by a reaction of the state machine (in this case I must call post_event since process_event is not reentrant). I don't want to write 2 distinct methods to do this (one that calls post, and the other process), I would much rather prefer adding a 'if' in my method that tells me if I have to call process_event or post_event (if I'm being called indirectly by the state machine or not). Is there a way to do this with statechart ? regards, Philippe
Hi Philippe
I have a method in which I want to post/process an event. The thing is, this method can be called from outside the state machine (so I need to call process_event), or indirectly by a reaction of the state machine (in this case I must call post_event since process_event is not reentrant).
I don't want to write 2 distinct methods to do this (one that calls post, and the other process), I would much rather prefer adding a 'if' in my method that tells me if I have to call process_event or post_event (if I'm being called indirectly by the state machine or not). Is there a way to do this with statechart ?
No, not currently. How about implementing two functions and having them delegate to the same implementation: class MyMachine : ... { public: void DoX() { DoXImpl(false); } void DoXPost() { DoXImpl(true); } private: void DoXImpl(bool post) { ... if (post) { ... } else { ... } ... } }; Yes, it is slightly more work than if there was a state_machine::is_processing() function. I'm just not sure whether your use case is sufficiently common to justify adding such a function. Maybe you could provide more details so that I understand it better? Thanks & Regards, -- Andreas Huber When replying by private email, please remove the words spam and trap from the address shown in the header.
Hi Andreas, I'm having the same issue as Philippe, and your proposed solution doesn't address how one might decide whether to call DoX or DoXPost. Given that there's no state_machine::is_processing() function, is there some other way to tell if one is "inside" or "outside" the state machine (short of examining the stack for a 'react' method call)? Richard -- View this message in context: http://boost.2283326.n4.nabble.com/Statechart-How-to-decide-between-post-eve... Sent from the Boost - Users mailing list archive at Nabble.com.
One approach that works for my application, that uses a single subclass of state_machine, is to override (i.e. hide) the base state_Machine::process_event with a version that simply sets an 'is_processing' flag before calling the base process_event, and clears it afterwards. Then other methods can call post_event if is_processing is true, and process_event otherwise. -- View this message in context: http://boost.2283326.n4.nabble.com/Statechart-How-to-decide-between-post-eve... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (3)
-
Andreas Huber
-
Philippe DAVID
-
rgreene62