Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
coalesced_updater.hpp
Go to the documentation of this file.
1#pragma once
2
3// CoalescedUpdater — coalesce rapid updates to a single callback on the event thread.
4// Thread-safe: can be triggered from any thread.
5
6#include <algorithm>
7#include <atomic>
8#include <functional>
9#include <string>
10#include <string_view>
11#include <vector>
12
13namespace pulp::events {
14
18public:
19 CoalescedUpdater() = default;
20 virtual ~CoalescedUpdater() = default;
21
25 if (!pending_.exchange(true, std::memory_order_acq_rel)) {
26 // First trigger since last handle — post to event thread
27 post_to_event_thread();
28 }
29 }
30
33 pending_.store(false, std::memory_order_release);
34 }
35
37 bool update_pending() const {
38 return pending_.load(std::memory_order_acquire);
39 }
40
42 virtual void on_update() = 0;
43
46 if (pending_.exchange(false, std::memory_order_acq_rel)) {
47 on_update();
48 }
49 }
50
51private:
52 std::atomic<bool> pending_{false};
53
54 // Consumers should poll process_pending() from their event loop.
55 void post_to_event_thread() {}
56};
57
60public:
61 explicit LambdaCoalescedUpdater(std::function<void()> callback)
62 : callback_(std::move(callback)) {}
63
64 void on_update() override {
65 if (callback_) callback_();
66 }
67
68private:
69 std::function<void()> callback_;
70};
71
74public:
75 MultiTimer() = default;
76 virtual ~MultiTimer() = default;
77
79 void start_timer(int timer_id, int interval_ms);
80
82 void stop_timer(int timer_id);
83
85 bool is_timer_running(int timer_id) const;
86
88 virtual void timer_callback(int timer_id) = 0;
89
92
93private:
94 struct TimerEntry {
95 int id;
96 int interval_ms;
97 bool running = false;
98 };
99 std::vector<TimerEntry> timers_;
100};
101
104public:
105 using ActionCallback = std::function<void(std::string_view action)>;
106
108 void send_action(std::string_view action) {
109 std::vector<std::pair<int, ActionCallback>> callbacks;
110 callbacks.reserve(listeners_.size());
111 for (const auto& [id, fn] : listeners_) {
112 callbacks.push_back({id, fn});
113 }
114 for (auto& [id, fn] : callbacks) {
115 if (fn && has_listener(id)) fn(action);
116 }
117 }
118
121 int id = next_id_++;
122 listeners_.push_back({id, std::move(fn)});
123 return id;
124 }
125
127 void remove_listener(int id) {
128 listeners_.erase(
129 std::remove_if(listeners_.begin(), listeners_.end(),
130 [id](auto& p) { return p.first == id; }),
131 listeners_.end());
132 }
133
134private:
135 std::vector<std::pair<int, ActionCallback>> listeners_;
136 int next_id_ = 0;
137
138 bool has_listener(int id) const {
139 return std::any_of(listeners_.begin(), listeners_.end(),
140 [id](const auto& p) { return p.first == id; });
141 }
142};
143
157
158
159
160} // namespace pulp::events
ActionBroadcaster — string-based action dispatch (for menu commands, etc.).
Definition coalesced_updater.hpp:103
int add_listener(ActionCallback fn)
Add a listener. Returns an ID for removal.
Definition coalesced_updater.hpp:120
std::function< void(std::string_view action)> ActionCallback
Definition coalesced_updater.hpp:105
void remove_listener(int id)
Remove a listener by ID.
Definition coalesced_updater.hpp:127
void send_action(std::string_view action)
Send an action to all listeners.
Definition coalesced_updater.hpp:108
void cancel_update()
Cancel a pending update.
Definition coalesced_updater.hpp:32
virtual ~CoalescedUpdater()=default
virtual void on_update()=0
Override this to handle the async update (runs on event thread).
void request_update()
Trigger an async update.
Definition coalesced_updater.hpp:24
void process_pending()
Called by the event loop to process pending updates.
Definition coalesced_updater.hpp:45
bool update_pending() const
Whether an update is pending.
Definition coalesced_updater.hpp:37
void on_update() override
Override this to handle the async update (runs on event thread).
Definition coalesced_updater.hpp:64
LambdaCoalescedUpdater(std::function< void()> callback)
Definition coalesced_updater.hpp:61
void start_timer(int timer_id, int interval_ms)
Start a timer with the given ID and interval in milliseconds.
bool is_timer_running(int timer_id) const
Whether a timer is running.
virtual ~MultiTimer()=default
virtual void timer_callback(int timer_id)=0
Override to handle timer callbacks.
void stop_all_timers()
Stop all timers.
void stop_timer(int timer_id)
Stop a specific timer.
ScopedLowPowerModeDisabler(const ScopedLowPowerModeDisabler &)=delete
ScopedLowPowerModeDisabler & operator=(const ScopedLowPowerModeDisabler &)=delete
Definition child_process_manager.hpp:18
STL namespace.