3 * Copyright (C) 2013 James Turner
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #ifndef SIMGEAR_STATE_MACHINE_H
23 #define SIMGEAR_STATE_MACHINE_H
28 #include <simgear/structure/SGReferenced.hxx>
29 #include <simgear/structure/SGSharedPtr.hxx>
39 class StateMachine : public SGReferenced
43 virtual ~StateMachine();
45 class State : public SGReferenced
50 std::string name() const;
52 void addUpdateBinding(SGBinding* aBinding);
53 void addEntryBinding(SGBinding* aBinding);
54 void addExitBinding(SGBinding* aBinding);
57 friend class StateMachine;
59 State(const std::string& name);
61 void fireExitBindings();
62 void fireEntryBindings();
67 std::auto_ptr<StatePrivate> d;
70 class Transition : public SGReferenced
73 virtual ~Transition();
75 std::string name() const;
78 * The state we end in, after this transition fires
80 State* target() const;
83 * Add a state in which this transition is eligible to fire
85 void addSourceState(State* aSource);
88 * Specify the transition trigger condition. Takes ownership
90 void setTriggerCondition(SGCondition* aCondition);
93 void addBinding(SGBinding* aBinding);
95 friend class StateMachine;
97 Transition(const std::string& aName, State* aTarget);
100 * predicate to determine if this transition can fire given a
103 bool applicableForState(State* aCurrent) const;
106 * test if the transition should fire, based on current state
108 bool evaluate() const;
112 class TransitionPrivate;
113 std::auto_ptr<TransitionPrivate> d;
116 typedef SGSharedPtr<State> State_ptr;
117 typedef SGSharedPtr<Transition> Transition_ptr;
120 * create a state machine from a property list description
122 static StateMachine* createFromPlist(SGPropertyNode* desc, SGPropertyNode* root);
124 SGPropertyNode* root();
129 void update(double dt);
131 State_ptr state() const;
134 * public API to force a change to a particular state.
135 * @param aOnlyIfDifferent - only make a transition if the new state is
136 * different from the current state. Otherwise, the existing state will
137 * be exited and re-entered.
139 void changeToState(State_ptr aState, bool aOnlyIfDifferent=true);
141 /// wrapper to change state by looking up a name
142 void changeToStateName(const std::string& aName, bool aOnlyIfDifferent=true);
144 State_ptr findStateByName(const std::string& aName) const;
146 State_ptr stateByIndex(unsigned int aIndex) const;
148 int indexOfState(State_ptr aState) const;
150 // programatic creation
151 State_ptr createState(const std::string& aName);
152 Transition_ptr createTransition(const std::string& aName, State_ptr aTarget);
154 void addState(State_ptr aState);
155 void addTransition(Transition_ptr aTrans);
157 void innerChangeState(State_ptr aState, Transition_ptr aTrans);
159 class StateMachinePrivate;
160 std::auto_ptr<StateMachinePrivate> d;
163 typedef SGSharedPtr<StateMachine> StateMachine_ptr;
165 } // of simgear namespace
167 #endif // of SIMGEAR_STATE_MACHINE_H