[StateChart] Accessing machine and state members from within state constructor
Hello,
I'd like to outsource some common functionality into a base state which
needs to access some member variables from the state machine or from its
direct context. I've managed to do exactly this in the constructor of
the inherited class but not inside the constructor of the base class. I
believe I am missing some bits here.
Thanks in advance
-Andre
Code:
#include
StateBase( my_context ctx ) : base_type( ctx ) { std::cout << "StateBase cst" << std::endl; // critical section outermost_context().fsmValue++; // access member of FSM (outermost context) ctx().stateValue++; // access member in direct context
The above line sould be: context<Active>().stateValue++;
On 07/11/2012 01:19 PM, Igor R wrote:
StateBase( my_context ctx ) : base_type( ctx ) { std::cout << "StateBase cst" << std::endl; // critical section outermost_context().fsmValue++; // access member of FSM (outermost context) ctx().stateValue++; // access member in direct context
The above line sould be:
context<Active>().stateValue++;
Hi Igor, what you suggested works from within the constructor of State1 but Active is not known to the constructor of the base class. And that's exactly the problem with outermost_context() too. Thanks again, -Andre
Andre Puschmann
StateBase( my_context ctx ) : base_type( ctx ) { std::cout << "StateBase cst" << std::endl; // critical section outermost_context().fsmValue++; // access member of FSM (outermost context) ctx().stateValue++; // access member in direct context }
Please try ... StateBase( my_context ctx ) : base_type( ctx ) { std::cout << "StateBase cst" << std::endl; this->outermost_context().fsmValue++; this->template context<Active>().stateValue++; } ... and let me know whether that works for you. Regards, -- Andreas Huber When replying by private email, please remove the words spam and trap from the address shown in the header.
On 07/11/2012 10:13 PM, Andreas Huber wrote:
Please try ...
StateBase( my_context ctx ) : base_type( ctx ) { std::cout << "StateBase cst" << std::endl; this->outermost_context().fsmValue++; this->template context<Active>().stateValue++; }
... and let me know whether that works for you.
Works like a charm, excellent. Thanks. And this->template context<Context>().stateValue++; lets me access the templated state. Thanks Andreas and Igor BR -Andre
participants (3)
-
Andre Puschmann
-
Andreas Huber
-
Igor R