2018-08-25 00:04:26 +00:00
|
|
|
//
|
2019-07-29 01:49:54 +00:00
|
|
|
// DeferredQueue.hpp
|
2018-08-25 00:04:26 +00:00
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 23/08/2018.
|
|
|
|
// Copyright © 2018 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2024-01-17 04:34:46 +00:00
|
|
|
#pragma once
|
2018-08-25 00:04:26 +00:00
|
|
|
|
2018-11-04 01:54:25 +00:00
|
|
|
#include <functional>
|
2018-08-25 00:04:26 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
/*!
|
2020-01-30 03:18:41 +00:00
|
|
|
Provides the logic to insert into and traverse a list of future scheduled items.
|
2018-08-25 00:04:26 +00:00
|
|
|
*/
|
2019-07-29 01:49:54 +00:00
|
|
|
template <typename TimeUnit> class DeferredQueue {
|
2018-08-25 00:04:26 +00:00
|
|
|
public:
|
|
|
|
/*!
|
|
|
|
Schedules @c action to occur in @c delay units of time.
|
|
|
|
*/
|
|
|
|
void defer(TimeUnit delay, const std::function<void(void)> &action) {
|
2020-01-30 02:49:52 +00:00
|
|
|
// Apply immediately if there's no delay (or a negative delay).
|
2020-01-30 03:45:10 +00:00
|
|
|
if(delay <= TimeUnit(0)) {
|
2020-01-30 02:49:52 +00:00
|
|
|
action();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!pending_actions_.empty()) {
|
|
|
|
// Otherwise enqueue, having subtracted the delay for any preceding events,
|
|
|
|
// and subtracting from the subsequent, if any.
|
|
|
|
auto insertion_point = pending_actions_.begin();
|
|
|
|
while(insertion_point != pending_actions_.end() && insertion_point->delay < delay) {
|
|
|
|
delay -= insertion_point->delay;
|
|
|
|
++insertion_point;
|
|
|
|
}
|
|
|
|
if(insertion_point != pending_actions_.end()) {
|
|
|
|
insertion_point->delay -= delay;
|
|
|
|
}
|
|
|
|
|
|
|
|
pending_actions_.emplace(insertion_point, delay, action);
|
|
|
|
} else {
|
|
|
|
pending_actions_.emplace_back(delay, action);
|
|
|
|
}
|
2018-08-25 00:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2020-01-30 03:18:41 +00:00
|
|
|
@returns The amount of time until the next enqueued action will occur,
|
|
|
|
or TimeUnit(-1) if the queue is empty.
|
2018-08-25 00:04:26 +00:00
|
|
|
*/
|
2020-05-21 03:34:26 +00:00
|
|
|
TimeUnit time_until_next_action() const {
|
2020-01-30 03:18:41 +00:00
|
|
|
if(pending_actions_.empty()) return TimeUnit(-1);
|
|
|
|
return pending_actions_.front().delay;
|
|
|
|
}
|
2018-08-25 00:04:26 +00:00
|
|
|
|
2020-01-30 03:18:41 +00:00
|
|
|
/*!
|
|
|
|
Advances the queue the specified amount of time, performing any actions it reaches.
|
|
|
|
*/
|
|
|
|
void advance(TimeUnit time) {
|
2020-01-30 02:49:52 +00:00
|
|
|
auto erase_iterator = pending_actions_.begin();
|
|
|
|
while(erase_iterator != pending_actions_.end()) {
|
2020-01-30 03:18:41 +00:00
|
|
|
erase_iterator->delay -= time;
|
2020-01-30 02:49:52 +00:00
|
|
|
if(erase_iterator->delay <= TimeUnit(0)) {
|
2020-01-30 03:18:41 +00:00
|
|
|
time = -erase_iterator->delay;
|
2020-01-30 02:49:52 +00:00
|
|
|
erase_iterator->action();
|
|
|
|
++erase_iterator;
|
|
|
|
} else {
|
|
|
|
break;
|
2018-08-25 00:04:26 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-30 02:49:52 +00:00
|
|
|
if(erase_iterator != pending_actions_.begin()) {
|
|
|
|
pending_actions_.erase(pending_actions_.begin(), erase_iterator);
|
|
|
|
}
|
2020-01-30 03:18:41 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 00:04:26 +00:00
|
|
|
private:
|
|
|
|
// The list of deferred actions.
|
|
|
|
struct DeferredAction {
|
|
|
|
TimeUnit delay;
|
|
|
|
std::function<void(void)> action;
|
|
|
|
|
|
|
|
DeferredAction(TimeUnit delay, const std::function<void(void)> &action) : delay(delay), action(std::move(action)) {}
|
|
|
|
};
|
|
|
|
std::vector<DeferredAction> pending_actions_;
|
|
|
|
};
|
|
|
|
|
2020-01-30 03:18:41 +00:00
|
|
|
/*!
|
|
|
|
A DeferredQueue maintains a list of ordered actions and the times at which
|
|
|
|
they should happen, and divides a total execution period up into the portions
|
|
|
|
that occur between those actions, triggering each action when it is reached.
|
|
|
|
|
|
|
|
This list is efficient only for short queues.
|
|
|
|
*/
|
|
|
|
template <typename TimeUnit> class DeferredQueuePerformer: public DeferredQueue<TimeUnit> {
|
|
|
|
public:
|
|
|
|
/// Constructs a DeferredQueue that will call target(period) in between deferred actions.
|
2020-05-21 03:34:26 +00:00
|
|
|
constexpr DeferredQueuePerformer(std::function<void(TimeUnit)> &&target) : target_(std::move(target)) {}
|
2020-01-30 03:18:41 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Runs for @c length units of time.
|
|
|
|
|
|
|
|
The constructor-supplied target will be called with one or more periods that add up to @c length;
|
|
|
|
any scheduled actions will be called between periods.
|
|
|
|
*/
|
|
|
|
void run_for(TimeUnit length) {
|
|
|
|
auto time_to_next = DeferredQueue<TimeUnit>::time_until_next_action();
|
|
|
|
while(time_to_next != TimeUnit(-1) && time_to_next <= length) {
|
|
|
|
target_(time_to_next);
|
|
|
|
length -= time_to_next;
|
2020-01-30 03:45:10 +00:00
|
|
|
DeferredQueue<TimeUnit>::advance(time_to_next);
|
2020-01-30 03:18:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 03:45:10 +00:00
|
|
|
DeferredQueue<TimeUnit>::advance(length);
|
2020-01-30 03:18:41 +00:00
|
|
|
target_(length);
|
2020-01-30 03:45:10 +00:00
|
|
|
|
|
|
|
// TODO: optimise this to avoid the multiple std::vector deletes. Find a neat way to expose that solution, maybe?
|
2020-01-30 03:18:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::function<void(TimeUnit)> target_;
|
|
|
|
};
|