Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
staged_transition.hpp
Go to the documentation of this file.
1#pragma once
2
27
28#include <algorithm>
29#include <cstdint>
30
31namespace pulp::signal {
32
34public:
38 static constexpr int due_steps(int total, std::int64_t elapsed,
39 std::int64_t horizon) noexcept {
40 if (total <= 0) return 0;
41 if (horizon <= 0 || elapsed >= horizon) return total;
42 if (elapsed <= 0) return 0;
43 const std::int64_t num = static_cast<std::int64_t>(total) * elapsed;
44 return static_cast<int>((num + horizon - 1) / horizon);
45 }
46
49 void begin(int total_steps) noexcept {
50 total_ = std::max(total_steps, 0);
51 done_ = 0;
52 }
53
55 void cancel() noexcept {
56 total_ = 0;
57 done_ = 0;
58 }
59
62 template <typename Step>
63 int run_due(std::int64_t elapsed, std::int64_t horizon, Step&& step) {
64 return run_until(due_steps(total_, elapsed, horizon), step);
65 }
66
68 template <typename Step>
69 int run_next(int count, Step&& step) {
70 return run_until(done_ + std::max(count, 0), step);
71 }
72
75 template <typename Step>
76 int finish_now(Step&& step) {
77 return run_until(total_, step);
78 }
79
80 bool active() const noexcept { return done_ < total_; }
81 int total() const noexcept { return total_; }
82 int completed() const noexcept { return done_; }
83 int remaining() const noexcept { return total_ - done_; }
84
85private:
86 template <typename Step>
87 int run_until(int target, Step& step) {
88 target = std::min(target, total_);
89 const int start = done_;
90 while (done_ < target) {
91 const int index = done_;
92 ++done_; // advance first so a step may inspect remaining()
93 step(index);
94 }
95 return done_ - start;
96 }
97
98 int total_ = 0;
99 int done_ = 0;
100};
101
102} // namespace pulp::signal
Definition staged_transition.hpp:33
bool active() const noexcept
Definition staged_transition.hpp:80
int completed() const noexcept
Definition staged_transition.hpp:82
int run_due(std::int64_t elapsed, std::int64_t horizon, Step &&step)
Run every step due by elapsed of horizon (see due_steps()).
Definition staged_transition.hpp:63
void cancel() noexcept
Drop the schedule without running the remaining steps.
Definition staged_transition.hpp:55
void begin(int total_steps) noexcept
Schedule total_steps steps; runs nothing.
Definition staged_transition.hpp:49
int remaining() const noexcept
Definition staged_transition.hpp:83
static constexpr int due_steps(int total, std::int64_t elapsed, std::int64_t horizon) noexcept
Steps that must have completed once elapsed of horizon units have passed: ceil(total * elapsed / hori...
Definition staged_transition.hpp:38
int total() const noexcept
Definition staged_transition.hpp:81
int run_next(int count, Step &&step)
Run up to count further steps. Returns the number run.
Definition staged_transition.hpp:69
int finish_now(Step &&step)
Run every remaining step now (for a consumer that needs the finished state immediately).
Definition staged_transition.hpp:76
Definition additive_bank.hpp:218