Blame ETL/test/smach.cpp

darco b3016b
/*! ========================================================================
darco b3016b
** Extended Template and Library Test Suite
darco b3016b
** Angle Class Test
dooglus 36d01e
** $Id$
darco b3016b
**
darco b3016b
** Copyright (c) 2002 Robert B. Quattlebaum Jr.
darco b3016b
**
darco b3016b
** This package is free software; you can redistribute it and/or
darco b3016b
** modify it under the terms of the GNU General Public License as
darco b3016b
** published by the Free Software Foundation; either version 2 of
darco b3016b
** the License, or (at your option) any later version.
darco b3016b
**
darco b3016b
** This package is distributed in the hope that it will be useful,
darco b3016b
** but WITHOUT ANY WARRANTY; without even the implied warranty of
darco b3016b
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
darco b3016b
** General Public License for more details.
darco b3016b
**
darco b3016b
** === N O T E S ===========================================================
darco b3016b
**
darco b3016b
** ========================================================================= */
darco b3016b
darco b3016b
/* === H E A D E R S ======================================================= */
darco b3016b
darco b3016b
#include <etl smach=""></etl>
darco b3016b
#include <cstdio></cstdio>
darco b3016b
darco b3016b
/* === M A C R O S ========================================================= */
darco b3016b
darco b3016b
using namespace std;
darco b3016b
using namespace etl;
darco b3016b
darco b3016b
/* === C L A S S E S ======================================================= */
darco b3016b
darco b3016b
enum EventKey
darco b3016b
{
darco b3016b
	EVENT_1,
darco b3016b
	EVENT_2,
darco b3016b
	EVENT_3,
darco b3016b
	EVENT_4
darco b3016b
};
darco b3016b
darco b3016b
darco b3016b
darco b3016b
struct MachineContext
darco b3016b
{
darco b3016b
	smach<machinecontext,eventkey> machine;</machinecontext,eventkey>
dooglus cee594
darco b3016b
	MachineContext():machine(this)
darco b3016b
	{
darco b3016b
	}
darco b3016b
};
darco b3016b
darco b3016b
typedef smach<machinecontext,eventkey> Smach;</machinecontext,eventkey>
darco b3016b
darco b3016b
class Event1 : public Smach::event
darco b3016b
{
darco b3016b
public:
darco b3016b
	Event1():Smach::event(EVENT_1) { }
darco b3016b
};
darco b3016b
darco b3016b
darco b3016b
class DefaultStateContext
darco b3016b
{
darco b3016b
	MachineContext *context;
darco b3016b
public:
dooglus a66acb
	DefaultStateContext(MachineContext *context):context(context) { printf("Entered Default State\n"); }
darco b3016b
	~DefaultStateContext() { printf("Left Default State\n"); }
dooglus cee594
dooglus c06ce2
	Smach::event_result event1_handler(const Smach::event& x __attribute__ ((unused)))
darco b3016b
	{
darco b3016b
		printf("DEFAULT STATE: Received Event 1\n");
darco b3016b
		return Smach::RESULT_ACCEPT;
darco b3016b
	}
darco b3016b
};
darco b3016b
darco b3016b
class DefaultState : public Smach::state<defaultstatecontext></defaultstatecontext>
darco b3016b
{
darco b3016b
public:
darco b3016b
	DefaultState():Smach::state<defaultstatecontext>("DefaultState")</defaultstatecontext>
darco b3016b
	{
darco b3016b
		insert(event_def(EVENT_1,&DefaultStateContext::event1_handler));
darco b3016b
	}
dooglus cee594
darco b3016b
} default_state;
darco b3016b
darco b3016b
darco b3016b
darco b3016b
darco b3016b
darco b3016b
darco b3016b
darco b3016b
class State1Context
darco b3016b
{
darco b3016b
	MachineContext *context;
darco b3016b
public:
dooglus a66acb
	State1Context(MachineContext *context):context(context) { printf("Entered State 1\n"); }
darco b3016b
	~State1Context() { printf("Left State 1\n"); }
dooglus cee594
dooglus c06ce2
	Smach::event_result event1_handler(const Smach::event& x __attribute__ ((unused)))
darco b3016b
	{
darco b3016b
		printf("STATE1: Received Event 1\n");
darco b3016b
		return Smach::RESULT_OK;
darco b3016b
	}
darco b3016b
darco b3016b
	Smach::event_result event3_handler(const Smach::event& x);
darco b3016b
};
darco b3016b
darco b3016b
class State1 : public Smach::state<state1context></state1context>
darco b3016b
{
darco b3016b
public:
darco b3016b
	State1():Smach::state<state1context>("State1")</state1context>
darco b3016b
	{
darco b3016b
		insert(event_def(EVENT_1,&State1Context::event1_handler));
darco b3016b
		insert(event_def(EVENT_3,&State1Context::event3_handler));
darco b3016b
	}
dooglus cee594
darco b3016b
} state_1;
darco b3016b
darco b3016b
darco b3016b
class State2Context
darco b3016b
{
darco b3016b
	MachineContext *context;
darco b3016b
public:
dooglus a66acb
	State2Context(MachineContext *context):context(context) { printf("Entered State 2\n"); }
darco b3016b
	~State2Context() { printf("Left State 2\n"); }
dooglus cee594
dooglus c06ce2
	Smach::event_result event1_handler(const Smach::event& x __attribute__ ((unused)))
darco b3016b
	{
darco b3016b
		printf("STATE2: Received Event 1\n");
darco b3016b
		return Smach::RESULT_OK;
darco b3016b
	}
darco b3016b
dooglus c06ce2
	Smach::event_result event2_handler(const Smach::event& x __attribute__ ((unused)))
darco b3016b
	{
darco b3016b
		printf("STATE2: Received Event 2\n");
darco b3016b
		return Smach::RESULT_OK;
darco b3016b
	}
darco b3016b
dooglus c06ce2
	Smach::event_result event3_handler(const Smach::event& x __attribute__ ((unused)))
darco b3016b
	{
darco b3016b
		printf("STATE2: Received Event 3\n");
darco b3016b
		return Smach::RESULT_OK;
darco b3016b
	}
darco b3016b
};
darco b3016b
darco b3016b
class State2 : public Smach::state<state2context></state2context>
darco b3016b
{
darco b3016b
public:
darco b3016b
	State2():Smach::state<state2context>("State2")</state2context>
darco b3016b
	{
darco b3016b
		insert(event_def(EVENT_1,&State2Context::event1_handler));
darco b3016b
		insert(event_def(EVENT_2,&State2Context::event2_handler));
darco b3016b
		insert(event_def(EVENT_3,&State2Context::event3_handler));
darco b3016b
	}
dooglus cee594
darco b3016b
} state_2;
darco b3016b
darco b3016b
Smach::event_result
dooglus c06ce2
State1Context::event3_handler(const Smach::event& x __attribute__ ((unused)))
darco b3016b
{
darco b3016b
	printf("STATE1: Received Event 3, throwing state to change to...\n");
dooglus cee594
darco b3016b
	throw &state_2;
darco b3016b
//	context->machine.enter(&state_2);
darco b3016b
//	return Smach::RESULT_ACCEPT;
darco b3016b
}
darco b3016b
darco b3016b
/* === G L O B A L S ======================================================= */
darco b3016b
darco b3016b
/* === E N T R Y P O I N T ================================================= */
darco b3016b
darco b3016b
int main()
darco b3016b
{
darco b3016b
	int error=0;
darco b3016b
dooglus cee594
	MachineContext context;
darco b3016b
	try
darco b3016b
	{
darco b3016b
		Smach& state_machine(context.machine);
darco b3016b
darco b3016b
		state_machine.set_default_state(&default_state);
darco b3016b
darco b3016b
		state_machine.enter(&state_1);
dooglus cee594
darco b3016b
		state_machine.process_event(Event1());
darco b3016b
		state_machine.process_event(EVENT_1);
darco b3016b
		state_machine.process_event(EVENT_2);
darco b3016b
		state_machine.process_event(EVENT_3);
darco b3016b
darco b3016b
		state_machine.process_event(Event1());
darco b3016b
		state_machine.process_event(EVENT_1);
darco b3016b
		state_machine.process_event(EVENT_2);
darco b3016b
		state_machine.process_event(EVENT_3);
darco b3016b
darco b3016b
		state_machine.process_event(Event1());
darco b3016b
		state_machine.process_event(EVENT_1);
darco b3016b
		state_machine.process_event(EVENT_2);
darco b3016b
		state_machine.process_event(EVENT_3);
darco b3016b
	}
darco b3016b
	catch(...)
darco b3016b
	{
darco b3016b
		printf("Uncaught exception\n");
darco b3016b
		error++;
darco b3016b
	}
dooglus cee594
darco b3016b
	return error;
darco b3016b
}