一个很有代表性的boost状态机实例

#include <boost/statechart/event.hpp>
#include <boost/statechart/state_machine.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/transition.hpp>
#include <iostream>

namespace sc = boost::statechart;

struct Event1 : sc::event<Event1>
{
};

struct Event2 : sc::event<Event2>
{
};

struct SimpleState1;

struct StateMachine1 : sc::state_machine<StateMachine1, SimpleState1>
{
	StateMachine1(void)
	{
		std::cout << "construct StateMachine1" << std::endl;
	}

	~StateMachine1(void)
	{
		std::cout << "distruct StateMachine1" << std::endl;
	}
};

struct SimpleState1_1;

struct SimpleState1 : sc::simple_state<SimpleState1, StateMachine1, SimpleState1_1>
{
	typedef sc::transition<Event1, SimpleState1> reactions;

	SimpleState1(void)
	{
		std::cout << "enter SimpleState1" << std::endl;
	}

	~SimpleState1(void)
	{
		std::cout << "leave SimpleState1" << std::endl;
	}
};

struct SimpleState1_2;

struct SimpleState1_1 : sc::simple_state<SimpleState1_1, SimpleState1>
{
	typedef sc::transition<Event2, SimpleState1_2> reactions;

	SimpleState1_1(void)
	{
		std::cout << "enter SimpleState1_1" << std::endl;
	}

	~SimpleState1_1(void)
	{
		std::cout << "leave SimpleState1_1" << std::endl;
	}
};

struct SimpleState1_2 : sc::simple_state<SimpleState1_2, SimpleState1>
{
	typedef sc::transition<Event2, SimpleState1_1> reactions;

	SimpleState1_2(void)
	{
		std::cout << "enter SimpleState1_2" << std::endl;
	}

	~SimpleState1_2(void)
	{
		std::cout << "leave SimpleState1_2" << std::endl;
	}
};

int main(int argc, char ** argv)
{
	StateMachine1 myMachine;
	myMachine.initiate();
	std::cout << "----- before process Event2" << std::endl;
	myMachine.process_event(Event2());
	std::cout << "----- after process Event2" << std::endl;
	std::cout << "----- before process Event1" << std::endl;
	myMachine.process_event(Event1());
	std::cout << "----- after process Event1" << std::endl;
	return 0;
}

运行结果

construct StateMachine1
enter SimpleState1
enter SimpleState1_1
----- before process Event2
leave SimpleState1_1
enter SimpleState1_2
----- after process Event2
----- before process Event1
leave SimpleState1_2
leave SimpleState1
enter SimpleState1
enter SimpleState1_1
----- after process Event1
distruct StateMachine1
leave SimpleState1_1
leave SimpleState1
请按任意键继续. . .



你可能感兴趣的:(struct,include)