Elma
An event loop manager for embedded systems
state_machine.cc
1 #include "elma.h"
2 
3 namespace elma {
4 
5  int State::_id_counter = 0;
6 
8  _initial = &s;
9  return *this;
10  }
11 
12  StateMachine& StateMachine::add_transition(string event_name, State& from, State& to) {
13  _transitions.push_back(Transition(event_name, from, to));
14  to._state_machine_ptr = this;
15  from._state_machine_ptr = this;
16  return *this;
17  }
18 
20  for (auto transition : _transitions ) {
21  watch(transition.event_name(), [this, transition](Event& e) {
22  if ( _current->id() == transition.from().id() ) {
23  _current->exit(e);
24  _current = &transition.to();
25  _current->entry(e);
26  if ( !_propagate ) {
27  e.stop_propagation();
28  }
29  }
30  });
31  }
32  }
33 
35  if ( _initial == NULL ) {
36  throw(Exception("State machine started without an initial state (call set_initial(...) first)"));
37  }
38  _current = _initial;
39  _current->entry(Event("start"));
40  }
41 
43  if ( _current == NULL ) {
44  throw(Exception("State machine updated without a valid current state (call set_initial(...) first)"));
45  }
46  _current->during();
47  }
48 
50 
51 };
StateMachine & add_transition(std::string event_name, State &from, State &to)
int id()
Definition: state.h:34
virtual void entry(const Event &e)=0
StateMachine & set_initial(State &s)
Definition: state_machine.cc:7
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
An exception class for Elma.
Definition: exceptions.h:13
virtual void during()=0
A finite state machine class.
Definition: state_machine.h:13
Definition: channel.cc:5