]> git.sesse.net Git - vlc/blob - modules/gui/skins2/utils/fsm.hpp
Uniformize source files encoding
[vlc] / modules / gui / skins2 / utils / fsm.hpp
1 /*****************************************************************************
2  * fsm.hpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifndef FSM_HPP
26 #define FSM_HPP
27
28 #include "../src/skin_common.hpp"
29 #include <string>
30 #include <map>
31 #include <set>
32
33 class EvtGeneric;
34 class CmdGeneric;
35
36
37 /// This class implements a Finite State Machine (FSM)
38 class FSM: public SkinObject
39 {
40     public:
41         FSM( intf_thread_t *pIntf ): SkinObject( pIntf ) {}
42         virtual ~FSM() {}
43
44         /// Add a state to the machine
45         void addState( const string &state );
46
47         /// Add a transition to the machine
48         void addTransition( const string &state1, const string &event,
49                             const string &state2,
50                             CmdGeneric *pCmd = NULL );
51
52         /// Retrieve the current state
53         const string &getState() const { return m_currentState; }
54
55         /// Set the current state, without bothering about transitions
56         void setState( const string &state );
57
58         /// Find a transition from the current state with the input event,
59         /// change the state, and call the associated callback (if any).
60         void handleTransition( const string &event );
61
62     private:
63         /// A Key_t contains the initial state of a transition, and a string
64         /// characterizing an event (for example: "mouse:left:down:ctrl")
65         typedef pair<string, string> Key_t;
66
67         /// A Data_t contains the final state of a transition, and a callback
68         /// to execute when the transition is applied
69         typedef pair<string, CmdGeneric*> Data_t;
70
71         /// Current state of the machine
72         string m_currentState;
73
74         /// Set containing the different states
75         set<string> m_states;
76
77         /// Map containing the different transitions between defined types
78         /// It associates a final state (and potentially a callback)
79         /// with a couple of the form: (currentState, triggerEvent)
80         map<Key_t, Data_t> m_transitions;
81 };
82
83
84 #endif