File: smach.cpp

package info (click to toggle)
synfigstudio 1.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 162,048 kB
  • sloc: cpp: 208,474; javascript: 25,487; ansic: 13,216; python: 7,509; sh: 6,391; makefile: 2,999; objc: 1,400; csh: 486; perl: 238; ruby: 73; xml: 11
file content (206 lines) | stat: -rw-r--r-- 4,800 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*! ========================================================================
** Extended Template and Library Test Suite
** Angle Class Test
** $Id$
**
** Copyright (c) 2002 Robert B. Quattlebaum Jr.
**
** This package is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License as
** published by the Free Software Foundation; either version 2 of
** the License, or (at your option) any later version.
**
** This package is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
** General Public License for more details.
**
** === N O T E S ===========================================================
**
** ========================================================================= */

/* === H E A D E R S ======================================================= */

#include <ETL/smach>
#include <cstdio>

/* === M A C R O S ========================================================= */

using namespace etl;

/* === C L A S S E S ======================================================= */

enum EventKey
{
	EVENT_1,
	EVENT_2,
	EVENT_3,
	EVENT_4
};



struct MachineContext
{
	smach<MachineContext,EventKey> machine;

	MachineContext():machine(this)
	{
	}
};

typedef smach<MachineContext,EventKey> Smach;

class Event1 : public Smach::event
{
public:
	Event1():Smach::event(EVENT_1) { }
};


class DefaultStateContext
{
	MachineContext *context;
public:
	DefaultStateContext(MachineContext *context):context(context) { printf("Entered Default State\n"); }
	~DefaultStateContext() { printf("Left Default State\n"); }

	Smach::event_result event1_handler(const Smach::event& x)
	{
		printf("DEFAULT STATE: Received Event 1\n");
		return Smach::RESULT_ACCEPT;
	}
};

class DefaultState : public Smach::state<DefaultStateContext>
{
public:
	DefaultState():Smach::state<DefaultStateContext>("DefaultState")
	{
		insert(event_def(EVENT_1,&DefaultStateContext::event1_handler));
	}

} default_state;







class State1Context
{
	MachineContext *context;
public:
	State1Context(MachineContext *context):context(context) { printf("Entered State 1\n"); }
	~State1Context() { printf("Left State 1\n"); }

	Smach::event_result event1_handler(const Smach::event& x)
	{
		printf("STATE1: Received Event 1\n");
		return Smach::RESULT_OK;
	}

	Smach::event_result event3_handler(const Smach::event& x);
};

class State1 : public Smach::state<State1Context>
{
public:
	State1():Smach::state<State1Context>("State1")
	{
		insert(event_def(EVENT_1,&State1Context::event1_handler));
		insert(event_def(EVENT_3,&State1Context::event3_handler));
	}

} state_1;


class State2Context
{
	MachineContext *context;
public:
	State2Context(MachineContext *context):context(context) { printf("Entered State 2\n"); }
	~State2Context() { printf("Left State 2\n"); }

	Smach::event_result event1_handler(const Smach::event& /*x*/)
	{
		printf("STATE2: Received Event 1\n");
		return Smach::RESULT_OK;
	}

	Smach::event_result event2_handler(const Smach::event& /*x*/)
	{
		printf("STATE2: Received Event 2\n");
		return Smach::RESULT_OK;
	}

	Smach::event_result event3_handler(const Smach::event& /*x*/)
	{
		printf("STATE2: Received Event 3\n");
		return Smach::RESULT_OK;
	}
};

class State2 : public Smach::state<State2Context>
{
public:
	State2():Smach::state<State2Context>("State2")
	{
		insert(event_def(EVENT_1,&State2Context::event1_handler));
		insert(event_def(EVENT_2,&State2Context::event2_handler));
		insert(event_def(EVENT_3,&State2Context::event3_handler));
	}

} state_2;

Smach::event_result
State1Context::event3_handler(const Smach::event& x)
{
	printf("STATE1: Received Event 3, throwing state to change to...\n");

	throw &state_2;
//	context->machine.enter(&state_2);
//	return Smach::RESULT_ACCEPT;
}

/* === G L O B A L S ======================================================= */

/* === E N T R Y P O I N T ================================================= */

int main()
{
	int error=0;

	MachineContext context;
	try
	{
		Smach& state_machine(context.machine);

		state_machine.set_default_state(&default_state);

		state_machine.enter(&state_1);

		state_machine.process_event(Event1());
		state_machine.process_event(EVENT_1);
		state_machine.process_event(EVENT_2);
		state_machine.process_event(EVENT_3);

		state_machine.process_event(Event1());
		state_machine.process_event(EVENT_1);
		state_machine.process_event(EVENT_2);
		state_machine.process_event(EVENT_3);

		state_machine.process_event(Event1());
		state_machine.process_event(EVENT_1);
		state_machine.process_event(EVENT_2);
		state_machine.process_event(EVENT_3);
	}
	catch(...)
	{
		printf("Uncaught exception\n");
		error++;
	}

	return error;
}