Aria 2.0.0
C++23 MVVM framework (C++20 minimum) — reactive, coroutine-first, ABI-layered
Loading...
Searching...
No Matches
dispatcher.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "aria/abi/export.hpp"
4#include "aria/property_ops.hpp" // IDelayedScheduler
5#include <chrono>
6#include <functional>
7#include <memory>
8#include <cstddef>
9
10namespace aria::runtime {
11
27public:
28 ~IDispatcher() override = default;
29
31 virtual void post(std::function<void()> fn) = 0;
32
34 virtual void post_delayed(std::chrono::milliseconds delay, std::function<void()> fn) = 0;
35
37 [[nodiscard]] bool is_main_thread() const noexcept override = 0;
38
40 void post_after(std::chrono::milliseconds delay,
41 std::function<void()> fn) override {
42 post_delayed(delay, std::move(fn));
43 }
44
45 // ── IScheduler bridge ────────────────────────────────────────────
46 [[nodiscard]] ::aria::SchedulerCaps caps() const noexcept override {
47 return ::aria::SchedulerCaps::Post
51 }
52 void schedule(std::function<void()> fn) override {
53 post(std::move(fn));
54 }
55 void schedule_after(std::chrono::milliseconds delay,
56 std::function<void()> fn) override {
57 post_delayed(delay, std::move(fn));
58 }
59};
60
64ARIA_RUNTIME_API std::shared_ptr<IDispatcher> main_dispatcher();
65ARIA_RUNTIME_API void set_main_dispatcher(std::shared_ptr<IDispatcher> dispatcher);
66
70public:
73
78
79 void post(std::function<void()> fn) override;
80 void post_delayed(std::chrono::milliseconds delay, std::function<void()> fn) override;
81 [[nodiscard]] bool is_main_thread() const noexcept override;
82
85 std::size_t pump(std::chrono::milliseconds budget = std::chrono::milliseconds{50});
86
89 void run_one();
90
91private:
92 struct Impl;
93 // RAII pImpl; C4251 on the shared_ptr member is a false positive for an
94 // incomplete opaque pointee consumed only via non-template API.
95#ifdef _MSC_VER
96# pragma warning(push)
97# pragma warning(disable: 4251)
98#endif
99 std::shared_ptr<Impl> impl_;
100#ifdef _MSC_VER
101# pragma warning(pop)
102#endif
103};
104
105} // namespace aria::runtime
Tiny interface — anything that can post a function to run after a delay.
Definition property_ops.hpp:62
Abstract main-thread dispatcher.
Definition dispatcher.hpp:26
::aria::SchedulerCaps caps() const noexcept override
Capability bitmask.
Definition dispatcher.hpp:46
void schedule(std::function< void()> fn) override
Submit fn for execution "soon". Defines Caps::Post.
Definition dispatcher.hpp:52
void post_after(std::chrono::milliseconds delay, std::function< void()> fn) override
IDelayedScheduler adapter — routes to post_delayed.
Definition dispatcher.hpp:40
virtual void post(std::function< void()> fn)=0
Schedule the callable to run on the main thread (asynchronously).
virtual void post_delayed(std::chrono::milliseconds delay, std::function< void()> fn)=0
Like post(), but with a delay.
~IDispatcher() override=default
void schedule_after(std::chrono::milliseconds delay, std::function< void()> fn) override
Submit fn for execution after delay.
Definition dispatcher.hpp:55
bool is_main_thread() const noexcept override=0
Returns true if currently executing on the main thread.
SimpleDispatcher & operator=(SimpleDispatcher &&)=delete
SimpleDispatcher & operator=(const SimpleDispatcher &)=delete
void run_one()
Block on the creating thread until one callable is available, then run it.
void post_delayed(std::chrono::milliseconds delay, std::function< void()> fn) override
Like post(), but with a delay.
void post(std::function< void()> fn) override
Schedule the callable to run on the main thread (asynchronously).
bool is_main_thread() const noexcept override
Returns true if currently executing on the main thread.
SimpleDispatcher(const SimpleDispatcher &)=delete
SimpleDispatcher(SimpleDispatcher &&)=delete
std::size_t pump(std::chrono::milliseconds budget=std::chrono::milliseconds{50})
Pump pending callables on the creating thread.
#define ARIA_RUNTIME_API
Definition export.hpp:30
Definition container.hpp:11
void set_main_dispatcher(std::shared_ptr< IDispatcher > dispatcher)
std::shared_ptr< IDispatcher > main_dispatcher()
Obtain an owning snapshot of the main dispatcher.
SchedulerCaps
Definition scheduler.hpp:83
@ Pumpable
Posted work is held in a queue until a pump-style call drains it (e.g.
Definition scheduler.hpp:102
@ Delay
Can submit work after a wall-clock or virtual-time delay.
Definition scheduler.hpp:93
@ MainThread
Submitted work runs on a single, identifiable "main" thread that is consistent across calls.
Definition scheduler.hpp:97
Definition validation_key.hpp:110