Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / ETL / tags / ETL_0_04_09 / test / smach.cpp
1 /*! ========================================================================
2 ** Extended Template and Library Test Suite
3 ** Angle Class Test
4 ** $Id$
5 **
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 **
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
12 **
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 ** General Public License for more details.
17 **
18 ** === N O T E S ===========================================================
19 **
20 ** ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #include <ETL/smach>
25 #include <cstdio>
26
27 /* === M A C R O S ========================================================= */
28
29 using namespace std;
30 using namespace etl;
31
32 /* === C L A S S E S ======================================================= */
33
34 enum EventKey
35 {
36         EVENT_1,
37         EVENT_2,
38         EVENT_3,
39         EVENT_4
40 };
41
42
43
44 struct MachineContext
45 {
46         smach<MachineContext,EventKey> machine;
47
48         MachineContext():machine(this)
49         {
50         }
51 };
52
53 typedef smach<MachineContext,EventKey> Smach;
54
55 class Event1 : public Smach::event
56 {
57 public:
58         Event1():Smach::event(EVENT_1) { }
59 };
60
61
62 class DefaultStateContext
63 {
64         MachineContext *context;
65 public:
66         DefaultStateContext(MachineContext *context):context(context) { printf("Enterted Default State\n"); }
67         ~DefaultStateContext() { printf("Left Default State\n"); }
68
69         Smach::event_result event1_handler(const Smach::event& x)
70         {
71                 printf("DEFAULT STATE: Received Event 1\n");
72                 return Smach::RESULT_ACCEPT;
73         }
74 };
75
76 class DefaultState : public Smach::state<DefaultStateContext>
77 {
78 public:
79         DefaultState():Smach::state<DefaultStateContext>("DefaultState")
80         {
81                 insert(event_def(EVENT_1,&DefaultStateContext::event1_handler));
82         }
83
84 } default_state;
85
86
87
88
89
90
91
92 class State1Context
93 {
94         MachineContext *context;
95 public:
96         State1Context(MachineContext *context):context(context) { printf("Enterted State 1\n"); }
97         ~State1Context() { printf("Left State 1\n"); }
98
99         Smach::event_result event1_handler(const Smach::event& x)
100         {
101                 printf("STATE1: Received Event 1\n");
102                 return Smach::RESULT_OK;
103         }
104
105         Smach::event_result event3_handler(const Smach::event& x);
106 };
107
108 class State1 : public Smach::state<State1Context>
109 {
110 public:
111         State1():Smach::state<State1Context>("State1")
112         {
113                 insert(event_def(EVENT_1,&State1Context::event1_handler));
114                 insert(event_def(EVENT_3,&State1Context::event3_handler));
115         }
116
117 } state_1;
118
119
120 class State2Context
121 {
122         MachineContext *context;
123 public:
124         State2Context(MachineContext *context):context(context) { printf("Enterted State 2\n"); }
125         ~State2Context() { printf("Left State 2\n"); }
126
127         Smach::event_result event1_handler(const Smach::event& x)
128         {
129                 printf("STATE2: Received Event 1\n");
130                 return Smach::RESULT_OK;
131         }
132
133         Smach::event_result event2_handler(const Smach::event& x)
134         {
135                 printf("STATE2: Received Event 2\n");
136                 return Smach::RESULT_OK;
137         }
138
139         Smach::event_result event3_handler(const Smach::event& x)
140         {
141                 printf("STATE2: Received Event 3\n");
142                 return Smach::RESULT_OK;
143         }
144 };
145
146 class State2 : public Smach::state<State2Context>
147 {
148 public:
149         State2():Smach::state<State2Context>("State2")
150         {
151                 insert(event_def(EVENT_1,&State2Context::event1_handler));
152                 insert(event_def(EVENT_2,&State2Context::event2_handler));
153                 insert(event_def(EVENT_3,&State2Context::event3_handler));
154         }
155
156 } state_2;
157
158 Smach::event_result
159 State1Context::event3_handler(const Smach::event& x)
160 {
161         printf("STATE1: Received Event 3, throwing state to change to...\n");
162
163         throw &state_2;
164 //      context->machine.enter(&state_2);
165 //      return Smach::RESULT_ACCEPT;
166 }
167
168 /* === G L O B A L S ======================================================= */
169
170 /* === E N T R Y P O I N T ================================================= */
171
172 int main()
173 {
174         int error=0;
175
176         MachineContext context;
177         try
178         {
179                 Smach& state_machine(context.machine);
180
181                 state_machine.set_default_state(&default_state);
182
183                 state_machine.enter(&state_1);
184
185                 state_machine.process_event(Event1());
186                 state_machine.process_event(EVENT_1);
187                 state_machine.process_event(EVENT_2);
188                 state_machine.process_event(EVENT_3);
189
190                 state_machine.process_event(Event1());
191                 state_machine.process_event(EVENT_1);
192                 state_machine.process_event(EVENT_2);
193                 state_machine.process_event(EVENT_3);
194
195                 state_machine.process_event(Event1());
196                 state_machine.process_event(EVENT_1);
197                 state_machine.process_event(EVENT_2);
198                 state_machine.process_event(EVENT_3);
199         }
200         catch(...)
201         {
202                 printf("Uncaught exception\n");
203                 error++;
204         }
205
206         return error;
207 }