Skip to content

Commit

Permalink
various small improvements
Browse files Browse the repository at this point in the history
* Cannot override process methods in state machine
* can run indefinitely until a process calls halt
* check that a state machine _current != NULL
* get the statemachine pointer in a state
  • Loading branch information
klavins committed Mar 12, 2019
1 parent 83b53cb commit 8f077b6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 6 deletions.
4 changes: 3 additions & 1 deletion include/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace elma {
public:

//! Default constructor
Manager() {}
Manager() : _running(false) {}

Manager& schedule(Process& process, high_resolution_clock::duration period);
Manager& all(std::function<void(Process&)> f);
Expand All @@ -42,6 +42,7 @@ namespace elma {
Manager& stop();

Manager& run(high_resolution_clock::duration);
Manager& run();

//! Getter
//! \return The time the Manager was most recently started
Expand All @@ -68,6 +69,7 @@ namespace elma {
high_resolution_clock::time_point _start_time;
high_resolution_clock::duration _elapsed;
Client _client;
bool _running;

};

Expand Down
2 changes: 2 additions & 0 deletions include/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ namespace elma {

void http_get(std::string url, std::function<void(json&)> handler);

void halt();

private:

// Manager interface
Expand Down
3 changes: 3 additions & 0 deletions include/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ namespace elma {
//! \param e The event to emit
void emit(const Event& e);

//! \return Get a reference to the parent state machine.
StateMachine& state_machine() { return *_state_machine_ptr; }

private:
std::string _name;
int _id;
Expand Down
6 changes: 3 additions & 3 deletions include/state_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ namespace elma {
State& current() { return *_current; }

//! Do not override init() for a state machine.
void init();
void init() final;

//! Do not override init() for a state machine.
void start();
void start() final;

//! Do not override init() for a state machine.
void update();
void update() final;

//! Do not override init() for a state machine.
void stop();
Expand Down
22 changes: 21 additions & 1 deletion src/manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,14 @@ namespace elma {
//! Start all processes. Usually not called directly.
//! \return A reference to the manager, for chaining
Manager& Manager::start() {
_running = true;
return all([this](Process& p) { p._start(_elapsed) ;});
}

//! Stop all processes. Usually not called directly.
//! \return A reference to the manager, for chaining
Manager& Manager::stop() {
_running = false;
return all([](Process& p) { p._stop(); });
}

Expand Down Expand Up @@ -143,7 +145,7 @@ namespace elma {
if (Priority_min <= priority && priority <= Priority_max ){
process._priority = priority;
sort_processes();
}else{
} else {
throw Exception("Priority must be between -5(low priority) and 15(high priority)");
}
return *this;
Expand All @@ -169,4 +171,22 @@ namespace elma {

}

//! Run the manager indefinitely or until a process calls halt().
//! \return A reference to the manager, for chaining
Manager& Manager::run() {

_start_time = high_resolution_clock::now();
_elapsed = high_resolution_clock::duration::zero();
start();

while ( _running ) {
update();
_elapsed = high_resolution_clock::now() - _start_time;
}

return *this;

}


}
2 changes: 2 additions & 0 deletions src/process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,6 @@ namespace elma {
stop();
}

void Process::halt() { _manager_ptr->stop(); }

}
4 changes: 3 additions & 1 deletion src/state_machine.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <iostream>
#include "elma.h"

namespace elma {
Expand Down Expand Up @@ -41,6 +40,9 @@ namespace elma {
}

void StateMachine::update() {
if ( _current == NULL ) {
throw(Exception("State machine updated without a valid current state (call set_initial(...) first)"));
}
_current->during();
}

Expand Down

0 comments on commit 8f077b6

Please sign in to comment.