1 /*! ========================================================================
2 ** Extended Template and Library Test Suite
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
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.
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.
18 ** === N O T E S ===========================================================
20 ** ========================================================================= */
22 /* === H E A D E R S ======================================================= */
27 /* === M A C R O S ========================================================= */
32 /* === C L A S S E S ======================================================= */
46 smach<MachineContext,EventKey> machine;
48 MachineContext():machine(this)
53 typedef smach<MachineContext,EventKey> Smach;
55 class Event1 : public Smach::event
58 Event1():Smach::event(EVENT_1) { }
62 class DefaultStateContext
64 MachineContext *context;
66 DefaultStateContext(MachineContext *context):context(context) { printf("Entered Default State\n"); }
67 ~DefaultStateContext() { printf("Left Default State\n"); }
69 Smach::event_result event1_handler(const Smach::event& x __attribute__ ((unused)))
71 printf("DEFAULT STATE: Received Event 1\n");
72 return Smach::RESULT_ACCEPT;
76 class DefaultState : public Smach::state<DefaultStateContext>
79 DefaultState():Smach::state<DefaultStateContext>("DefaultState")
81 insert(event_def(EVENT_1,&DefaultStateContext::event1_handler));
94 MachineContext *context;
96 State1Context(MachineContext *context):context(context) { printf("Entered State 1\n"); }
97 ~State1Context() { printf("Left State 1\n"); }
99 Smach::event_result event1_handler(const Smach::event& x __attribute__ ((unused)))
101 printf("STATE1: Received Event 1\n");
102 return Smach::RESULT_OK;
105 Smach::event_result event3_handler(const Smach::event& x);
108 class State1 : public Smach::state<State1Context>
111 State1():Smach::state<State1Context>("State1")
113 insert(event_def(EVENT_1,&State1Context::event1_handler));
114 insert(event_def(EVENT_3,&State1Context::event3_handler));
122 MachineContext *context;
124 State2Context(MachineContext *context):context(context) { printf("Entered State 2\n"); }
125 ~State2Context() { printf("Left State 2\n"); }
127 Smach::event_result event1_handler(const Smach::event& x __attribute__ ((unused)))
129 printf("STATE2: Received Event 1\n");
130 return Smach::RESULT_OK;
133 Smach::event_result event2_handler(const Smach::event& x __attribute__ ((unused)))
135 printf("STATE2: Received Event 2\n");
136 return Smach::RESULT_OK;
139 Smach::event_result event3_handler(const Smach::event& x __attribute__ ((unused)))
141 printf("STATE2: Received Event 3\n");
142 return Smach::RESULT_OK;
146 class State2 : public Smach::state<State2Context>
149 State2():Smach::state<State2Context>("State2")
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));
159 State1Context::event3_handler(const Smach::event& x __attribute__ ((unused)))
161 printf("STATE1: Received Event 3, throwing state to change to...\n");
164 // context->machine.enter(&state_2);
165 // return Smach::RESULT_ACCEPT;
168 /* === G L O B A L S ======================================================= */
170 /* === E N T R Y P O I N T ================================================= */
176 MachineContext context;
179 Smach& state_machine(context.machine);
181 state_machine.set_default_state(&default_state);
183 state_machine.enter(&state_1);
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);
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);
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);
202 printf("Uncaught exception\n");