Elma
An event loop manager for embedded systems
 All Classes Files Functions Enumerations
state_machine.cc
1 #include <iostream>
2 #include "elma.h"
3 
4 namespace elma {
5 
6  int State::_id_counter = 0;
7 
9  _initial = &s;
10  return *this;
11  }
12 
13  StateMachine& StateMachine::add_transition(string event_name, State& from, State& to) {
14  _transitions.push_back(Transition(event_name, from, to));
15  to._state_machine_ptr = this;
16  from._state_machine_ptr = this;
17  return *this;
18  }
19 
21  for (auto transition : _transitions ) {
22  watch(transition.event_name(), [this, transition](Event& e) {
23  if ( _current->id() == transition.from().id() ) {
24  _current->exit(e);
25  _current = &transition.to();
26  _current->entry(e);
27  if ( !_propagate ) {
28  e.stop_propagation();
29  }
30  }
31  });
32  }
33  }
34 
36  if ( _initial == NULL ) {
37  throw(Exception("State machine started without an initial state (call set_initial(...) first)"));
38  }
39  _current = _initial;
40  _current->entry(Event("start"));
41  }
42 
44  _current->during();
45  }
46 
48 
49 };
void start()
Do not override init() for a state machine.
void update()
Do not override init() for a state machine.
StateMachine & add_transition(std::string event_name, State &from, State &to)
int id()
Definition: state.h:34
virtual void entry(const Event &e)=0
void init()
Do not override init() for a state machine.
StateMachine & set_initial(State &s)
Definition: state_machine.cc:8
Events that can be emitted, watched, and responded to with event handlers.
Definition: event.h:23
States for finite state machines (FSM)
Definition: transition.h:12
States for the StateMachine class.
Definition: state.h:14
virtual void exit(const Event &e)=0
void stop()
Do not override init() for a state machine.
An exception class for Elma.
Definition: exceptions.h:13
virtual void during()=0
A finite state machine class.
Definition: state_machine.h:13
Definition: manager.h:11