Per-thread event loop providing event dispatch, timers, and posted callables. More...
#include <eventloop.h>
Public Types | |
| enum | ProcessEventsFlag : uint32_t { ExcludeTimers = 0x01 , ExcludePosted = 0x02 , WaitForMore = 0x04 } |
| Flags controlling processEvents() behavior. More... | |
Public Member Functions | |
| EventLoop () | |
| Constructs an EventLoop and registers it as current for this thread. | |
| ~EventLoop () | |
| Destroys the EventLoop. | |
| EventLoop (const EventLoop &)=delete | |
| EventLoop & | operator= (const EventLoop &)=delete |
| int | exec () |
| Blocks and processes events until quit() is called. | |
| void | processEvents (uint32_t flags=0, unsigned int timeoutMs=0) |
| Processes pending events and returns. | |
| void | quit (int returnCode=0) |
| Signals the event loop to exit with the given return code. | |
| void | postCallable (std::function< void()> func) |
| Posts a callable to be executed on this EventLoop's thread. | |
| void | postEvent (ObjectBase *receiver, Event *event) |
| Posts an Event to this EventLoop for delivery to a receiver. | |
| int | startTimer (ObjectBase *receiver, unsigned int intervalMs, bool singleShot=false) |
| Starts a timer that delivers TimerEvent to an ObjectBase. | |
| int | startTimer (unsigned int intervalMs, std::function< void()> func, bool singleShot=false) |
| Starts a standalone callable timer. | |
| void | stopTimer (int timerId) |
| Stops and removes a timer. | |
| bool | isRunning () const |
| Returns whether the event loop is currently running. | |
| int | exitCode () const |
| Returns the exit code set by the most recent quit(). | |
Static Public Member Functions | |
| static EventLoop * | current () |
| Returns the EventLoop running on the current thread. | |
Per-thread event loop providing event dispatch, timers, and posted callables.
EventLoop is not an ObjectBase — it is infrastructure that ObjectBase instances attach to. Each thread may have at most one active EventLoop, tracked by a thread_local pointer accessible via current().
Two execution models are supported:
exec() is simply a loop calling processEvents(WaitForMore).
Flags controlling processEvents() behavior.
| Enumerator | |
|---|---|
| ExcludeTimers | Skip timer processing. |
| ExcludePosted | Skip posted callables and events. |
| WaitForMore | Block until at least one event is available. |
| EventLoop::EventLoop | ( | ) |
| EventLoop::~EventLoop | ( | ) |
| int EventLoop::exec | ( | ) |
|
inline |
|
inline |
Returns whether the event loop is currently running.
true if exec() is active. Posts a callable to be executed on this EventLoop's thread.
Thread-safe. The callable will be invoked during the next processEvents() call.
| func | The callable to execute. |
| void EventLoop::postEvent | ( | ObjectBase * | receiver, |
| Event * | event | ||
| ) |
Posts an Event to this EventLoop for delivery to a receiver.
Takes ownership of event. Thread-safe. The event will be delivered during the next processEvents() call via the receiver's event() virtual method.
| receiver | The ObjectBase to deliver the event to. |
| event | The event to deliver. Ownership is transferred. |
Processes pending events and returns.
| flags | Combination of ProcessEventsFlag values. |
| timeoutMs | When WaitForMore is set, the maximum time to wait for events in milliseconds. Zero with WaitForMore waits indefinitely. Zero without WaitForMore drains pending events and returns immediately. |
| void EventLoop::quit | ( | int | returnCode = 0 | ) |
Signals the event loop to exit with the given return code.
Thread-safe. The loop will exit after the current processEvents() iteration completes.
| returnCode | The exit code returned by exec(). |
| int EventLoop::startTimer | ( | ObjectBase * | receiver, |
| unsigned int | intervalMs, | ||
| bool | singleShot = false |
||
| ) |
Starts a timer that delivers TimerEvent to an ObjectBase.
The receiver's timerEvent() virtual method will be called each time the timer fires. Must be called from the EventLoop's thread.
| receiver | The ObjectBase to receive TimerEvents. |
| intervalMs | The timer interval in milliseconds. |
| singleShot | If true, the timer fires once and is removed. |
| int EventLoop::startTimer | ( | unsigned int | intervalMs, |
| std::function< void()> | func, | ||
| bool | singleShot = false |
||
| ) |
Starts a standalone callable timer.
No ObjectBase is needed — the callable runs directly on the EventLoop's thread each time the timer fires.
| intervalMs | The timer interval in milliseconds. |
| func | The callable to invoke when the timer fires. |
| singleShot | If true, the timer fires once and is removed. |
| void EventLoop::stopTimer | ( | int | timerId | ) |
Stops and removes a timer.
| timerId | The ID returned by startTimer(). |