#include <iostream>
#define ASYNC_STATE_MACHINE_DEBUG
struct Context
{
int a;
};
struct StateTop;
struct StateDisconnected;
struct StateConnected;
struct StateConnectedA;
struct StateConnectedB;
{
static constexpr auto NAME = "Top";
void init(void)
{
std::cout << "### StateTop::init" << std::endl;
}
void entry(void)
{
std::cout << "### StateTop::entry" << std::endl;
}
void exit(void)
{
std::cout << "### StateTop::exit" << std::endl;
}
virtual void eventA(void) {}
virtual void eventB(void) {}
};
{
static constexpr auto NAME = "Disconnected";
{
std::cout << "### StateDisconnected::init" << std::endl;
}
{
std::cout << "### StateDisconnected::entry" << std::endl;
}
{
std::cout << "### StateDisconnected::exit" << std::endl;
}
virtual void eventA(void) override
{
std::cout << "### StateDisconnected::eventA: ctx.a="
<< ctx().a << std::endl;
ctx().a = 24;
setState<StateConnected>();
}
};
{
static constexpr auto NAME = "Connected";
{
std::cout << "### StateConnected::init" << std::endl;
setState<StateConnectedA>();
}
{
std::cout << "### StateConnected::entry" << std::endl;
}
{
std::cout << "### StateConnected::exit" << std::endl;
}
virtual void eventB(void) override
{
std::cout << "### StateConnected::eventB: ctx.a="
<< ctx().a << std::endl;
setState<StateConnectedB>();
}
};
{
static constexpr auto NAME = "ConnectedA";
{
std::cout << "### StateConnectedA::init" << std::endl;
}
{
std::cout << "### StateConnectedA::entry" << std::endl;
}
{
std::cout << "### StateConnectedA::exit" << std::endl;
}
};
{
static constexpr auto NAME = "ConnectedB";
{
std::cout << "### StateConnectedB::entry" << std::endl;
}
{
std::cout << "### StateConnectedB::exit" << std::endl;
}
};
int main()
{
Context ctx;
ctx.a = 42;
sm.start();
sm.state().eventA();
sm.state().eventB();
sm.setState<StateDisconnected>();
return 0;
}
A_brief_description_for_this_file.
Implements a hierarchial state machine.
void entry(void)
Called when a state is entered.
void init(void)
Called before a transition from one state to another.
Implements a hierarchial state machine.
The base class for the top state of a state machine.
void setState(void)
Transition to the given state.