Elma
An event loop manager for embedded systems
process.h
1 #ifndef _ELMA_PROCESS_H
2 #define _ELMA_PROCESS_H
3 
4 #include <string>
5 #include <chrono>
6 #include <functional>
7 
8 #include "elma.h"
9 
10 namespace elma {
11 
12  class Manager;
13  class Channel;
14 
15  using std::string;
16  using namespace std::chrono;
17 
19 
24  class Process {
25 
26  public:
27 
28  friend class Manager;
29 
32  typedef enum { UNINITIALIZED, STOPPED, RUNNING } status_type;
33 
35  Process(int n = 0) : _name("unnamed process"), _status(UNINITIALIZED), _manager_ptr(NULL), _priority(n) {}
36 
38 
41  Process(std::string name, int n = 0) : _name(name), _status(UNINITIALIZED), _manager_ptr(NULL), _priority(n) {}
42  virtual ~Process() = default;
43 
44  // Interface for derived classes
45 
50  virtual void init() = 0;
51 
55  virtual void start() = 0;
56 
61  virtual void update() = 0;
62 
66  virtual void stop() = 0;
67 
68  // Inline methods that just replace method calls with their bodies
69 
72  inline string name() { return _name; }
73 
76  inline void set_name(std::string str) { _name = str; }
77 
80  inline status_type status() { return _status; }
81 
84  inline high_resolution_clock::duration period() { return _period; }
85 
88  inline int num_updates() { return _num_updates; }
89 
92  inline time_point<high_resolution_clock> start_time() { return _start_time; }
93 
97  inline high_resolution_clock::duration last_update() { return _last_update; }
98 
102  inline high_resolution_clock::duration previous_update() { return _previous_update; }
103 
104  // documentation for these methods is in process.cc
105  Channel& channel(string name);
106  double milli_time();
107  double delta();
108 
109  void watch(string event_name, std::function<void(Event&)> handler);
110  void emit(const Event& event);
111 
112  void http_get(std::string url, std::function<void(json&)> handler);
113 
114  void halt();
115 
116  void set_manager(Manager * m_ptr);
117 
118  private:
119 
120  // Manager interface
121  void _init();
122  void _start(high_resolution_clock::duration elapsed);
123  void _update(high_resolution_clock::duration elapsed);
124  void _stop();
125 
126  // Instance variables
127  string _name;
128  status_type _status;
129  high_resolution_clock::duration _period, // request time between updates
130  _previous_update, // duration from start to update before last
131  _last_update; // duration from start to last update
132  time_point<high_resolution_clock> _start_time; // time of most recent start
133  int _num_updates, _priority; // number of times update() has been called
134 
135  protected:
136  Manager * _manager_ptr; // a pointer to the manager
137 
138  };
139 
140 }
141 
142 #endif
The Process Manager class.
Definition: manager.h:27
Process(std::string name, int n=0)
Constructor that takes a name for the process.
Definition: process.h:41
int num_updates()
Definition: process.h:88
high_resolution_clock::duration previous_update()
Definition: process.h:102
time_point< high_resolution_clock > start_time()
Definition: process.h:92
Process(int n=0)
Default constructor. Names process "no name".
Definition: process.h:35
Events that can be emitted, watched, and responded to with event handlers.
Definition: event.h:23
string name()
Definition: process.h:72
status_type status()
Definition: process.h:80
high_resolution_clock::duration last_update()
Definition: process.h:97
high_resolution_clock::duration period()
Definition: process.h:84
A channel for sending double values to and from Process objects.
Definition: channel.h:21
Definition: channel.cc:5
void set_name(std::string str)
Definition: process.h:76
An abstract base class for processes.
Definition: process.h:24