Elma
An event loop manager for embedded systems
Public Member Functions | List of all members
elma::Manager Class Reference

The Process Manager class. More...

#include <manager.h>

Public Member Functions

 Manager ()
 Default constructor.
 
Managerschedule (Process &process, high_resolution_clock::duration period)
 
Managerremove (Process &process)
 
Managerall (std::function< void(Process &)> f)
 
Managerset_priority (Process &process, int priority)
 
Managersort_processes ()
 
Managerinit ()
 
Managerstart ()
 
Managerupdate ()
 
Managerstop ()
 
Manageruse_simulated_time ()
 
Manageruse_real_time ()
 
Managerrun (high_resolution_clock::duration runtime)
 
Managerrun ()
 
Managerrun (std::function< bool()> condition)
 
high_resolution_clock::time_point start_time ()
 
high_resolution_clock::duration elapsed ()
 
std::mutex & get_update_mutex ()
 
Managerset_niceness (high_resolution_clock::duration n)
 
Manageradd_channel (Channel &)
 
Channelchannel (string)
 
Managerwatch (string event_name, std::function< void(Event &)> handler)
 
Manageremit (const Event &event)
 
Clientclient ()
 

Detailed Description

The Process Manager class.

Example usage:

#include <iostream>
#include <chrono>
#include "elma.h"
using namespace std::chrono;
using namespace elma;
namespace basic_example {
class BasicProcess : public Process {
public:
BasicProcess(std::string name) : Process(name) {}
void init() {}
void start() {}
void update() {
std::cout << name() << ": "
<< milli_time()
<< "ms\n";
}
void stop() {}
};
}
int main() {
Manager m; // Make a manager
basic_example:: BasicProcess p("A"), q("B"); // Make two processes
m.schedule(p, 1_ms) // Schedule the first to run every millisecond
.schedule(q, 5_ms) // Schedule the second to run every 5 milliseconds
.init() // Initialize the manager (which calls init for the processes)
.run(11_ms); // Run for 11 milliseconds
}

Definition at line 27 of file manager.h.

Member Function Documentation

◆ add_channel()

Manager & elma::Manager::add_channel ( Channel channel)

Add a channel to the manager

Parameters
Thechannel to be added
Returns
A reference to the manager, for chaining

Definition at line 43 of file manager.cc.

◆ all()

Manager & elma::Manager::all ( std::function< void(Process &)>  f)

Apply a function to all processes.

Parameters
fThe function to apply. It should take a reference to a process and return void.
Returns
A reference to the manager, for chaining

Definition at line 100 of file manager.cc.

◆ channel()

Channel & elma::Manager::channel ( string  name)

Retrieve a reference to an existing channel. Throws an error if no such channel exists.

Returns
The channel requested.

Definition at line 50 of file manager.cc.

◆ elapsed()

high_resolution_clock::duration elma::Manager::elapsed ( )
inline

Getter

Returns
The duration of time since the manager was most recently started

Definition at line 59 of file manager.h.

◆ emit()

Manager & elma::Manager::emit ( const Event event)

Emit an event associated with a name. Typically, a process would emit events in its update() method using something like the following code"

emit(Event("name", value));

where value is any jsonable value. For example, you can write

emit(Event("velocity", 3.41));
Parameters
eventThe Event to be emitted
Returns
A reference to the manager for chaining.

Definition at line 85 of file manager.cc.

◆ get_update_mutex()

std::mutex& elma::Manager::get_update_mutex ( )
inline

Getter

Returns
The mutex used for the update method. Useful if some other non-elma process needs to act on shared data.

Definition at line 64 of file manager.h.

◆ init()

Manager & elma::Manager::init ( )

Initialize all processes. Usually called before run()

Returns
A reference to the manager, for chaining

Definition at line 109 of file manager.cc.

◆ remove()

Manager & elma::Manager::remove ( Process process)

Remove a Process from the manager so it will no longer be updated. Calls its stop() method.

Parameters
processThe process to be removed.

Definition at line 31 of file manager.cc.

◆ run() [1/3]

Manager & elma::Manager::run ( high_resolution_clock::duration  runtime)

Run the manager for the specified amount of time.

Parameters
Thedesired amount of time to run
Returns
A reference to the manager, for chaining

Definition at line 190 of file manager.cc.

◆ run() [2/3]

Manager & elma::Manager::run ( )

Run the manager indefinitely or until a process calls halt().

Returns
A reference to the manager, for chaining

Definition at line 198 of file manager.cc.

◆ run() [3/3]

Manager & elma::Manager::run ( std::function< bool()>  condition)

Run the manager until the provided contion is true or until a process calls halt().

Parameters
Thecondition, a function returning a boolean and taking no arguments.
Returns
A reference to the manager, for chaining

Definition at line 207 of file manager.cc.

◆ schedule()

Manager & elma::Manager::schedule ( Process process,
high_resolution_clock::duration  period 
)

Add a Process to the manager, to be run at a certain frequency.

Parameters
processThe process to be scheduled, usually derived from the Process abstract base class
periodThe desired duration of time between updates
Returns
A reference to the manager, for chaining

Definition at line 13 of file manager.cc.

◆ set_niceness()

Manager& elma::Manager::set_niceness ( high_resolution_clock::duration  n)
inline

Setter

Returns
A reference to the manager, for chaining

Definition at line 68 of file manager.h.

◆ set_priority()

Manager & elma::Manager::set_priority ( Process process,
int  priority 
)

Set Process Priority and sort _Processes to ensure higher priority are updated first. Priority may be set -5 (low priority) to 15 (high priority) This should allow priority adjustment while running.

Parameters
processThe process you want to adjust priority level.
priority,ainteger between -5 and 15
Returns
A reference to the manager, for chaining

Definition at line 176 of file manager.cc.

◆ sort_processes()

Manager & elma::Manager::sort_processes ( )

sort _Processes based on _priority to ensure higher priority process are updated first.

Returns
A reference to the manager, for chaining

Definition at line 144 of file manager.cc.

◆ start()

Manager & elma::Manager::start ( )

Start all processes. Usually not called directly.

Returns
A reference to the manager, for chaining

Definition at line 116 of file manager.cc.

◆ start_time()

high_resolution_clock::time_point elma::Manager::start_time ( )
inline

Getter

Returns
The time the Manager was most recently started

Definition at line 55 of file manager.h.

◆ stop()

Manager & elma::Manager::stop ( )

Stop all processes. Usually not called directly.

Returns
A reference to the manager, for chaining

Definition at line 123 of file manager.cc.

◆ update()

Manager & elma::Manager::update ( )

Update all processes if enough time has passed. Usually not called directly.

Returns
A reference to the manager, for chaining

Definition at line 130 of file manager.cc.

◆ use_real_time()

Manager & elma::Manager::use_real_time ( )

Sets if the manager will run in real time. In real time, the manager will continually advance time until the next process needs to update.

Parameters
Ifthe manager runs in simulated time.
Returns
A reference to the manager, for chaining

Definition at line 165 of file manager.cc.

◆ use_simulated_time()

Manager & elma::Manager::use_simulated_time ( )

Sets if the manager will run in simulated time. In simulated time, the next schefuled process will immediately run at the completion of the last.

Returns
A reference to the manager, for chaining

Definition at line 156 of file manager.cc.

◆ watch()

Manager & elma::Manager::watch ( string  event_name,
std::function< void(Event &)>  handler 
)

Watch for an event associated with the given name. For watching events, you would typically register event handlers in your process' init() method. For example,

watch("velocity", [this](Event& e) {
std::cout << "got velocity " << e.value() << std::endl;
})
Parameters
event_nameThe name of the event A function or lambda that takes an event and returns nothing.

Definition at line 68 of file manager.cc.


The documentation for this class was generated from the following files: