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
27 #include <simgear/structure/SGReferenced.hxx>
28 #include <simgear/structure/SGSharedPtr.hxx>
38 class StateMachine : public SGReferenced
42 virtual ~StateMachine();
44 class State : public SGReferenced
49 std::string name() const;
51 void addUpdateBinding(SGBinding* aBinding);
52 void addEntryBinding(SGBinding* aBinding);
53 void addExitBinding(SGBinding* aBinding);
56 friend class StateMachine;
58 State(const std::string& name);
60 void fireExitBindings();
61 void fireEntryBindings();
66 std::auto_ptr<StatePrivate> d;
69 class Transition : public SGReferenced
72 virtual ~Transition();
74 std::string name() const;
77 * The state we end in, after this transition fires
79 State* target() const;
82 * Add a state in which this transition is eligible to fire
84 void addSourceState(State* aSource);
87 * Specify the transition trigger condition. Takes ownership
89 void setTriggerCondition(SGCondition* aCondition);
92 void addBinding(SGBinding* aBinding);
94 friend class StateMachine;
96 Transition(const std::string& aName, State* aTarget);
99 * predicate to determine if this transition can fire given a
102 bool applicableForState(State* aCurrent) const;
105 * test if the transition should fire, based on current state
107 bool evaluate() const;
111 class TransitionPrivate;
112 std::auto_ptr<TransitionPrivate> d;
115 typedef SGSharedPtr<State> State_ptr;
116 typedef SGSharedPtr<Transition> Transition_ptr;
119 * create a state machine from a property list description
121 static StateMachine* createFromPlist(SGPropertyNode* desc, SGPropertyNode* root);
123 SGPropertyNode* root();
128 void update(double dt);
130 State_ptr state() const;
133 * public API to force a change to a particular state.
134 * @param aOnlyIfDifferent - only make a transition if the new state is
135 * different from the current state. Otherwise, the existing state will
136 * be exited and re-entered.
138 void changeToState(State_ptr aState, bool aOnlyIfDifferent=true);
140 /// wrapper to change state by looking up a name
141 void changeToStateName(const std::string& aName, bool aOnlyIfDifferent=true);
143 State_ptr findStateByName(const std::string& aName) const;
145 State_ptr stateByIndex(unsigned int aIndex) const;
147 int indexOfState(State_ptr aState) const;
149 // programatic creation
150 State_ptr createState(const std::string& aName);
151 Transition_ptr createTransition(const std::string& aName, State_ptr aTarget);
153 void addState(State_ptr aState);
154 void addTransition(Transition_ptr aTrans);
156 void innerChangeState(State_ptr aState, Transition_ptr aTrans);
158 class StateMachinePrivate;
159 std::auto_ptr<StateMachinePrivate> d;
162 typedef SGSharedPtr<StateMachine> StateMachine_ptr;
164 } // of simgear namespace
166 #endif // of SIMGEAR_STATE_MACHINE_H