SW Task Event Loop Framework v1.0.0
High-performance C++ asynchronous event loop framework with timer management and promise-based programming
|
Android-style message handler for event-driven programming. More...
#include <Handler.h>
Public Member Functions | |
Handler () | |
Default constructor - not associated with any looper. | |
Handler (std::shared_ptr< SLLooper > &looper) | |
Constructor with looper association. | |
virtual | ~Handler () |
Virtual destructor. | |
std::shared_ptr< Message > | obtainMessage () |
Obtain a new empty message. | |
std::shared_ptr< Message > | obtainMessage (int32_t what) |
Obtain a message with type code. | |
std::shared_ptr< Message > | obtainMessage (int32_t what, int32_t arg1) |
Obtain a message with type code and argument. | |
std::shared_ptr< Message > | obtainMessage (int32_t what, void *obj) |
Obtain a message with type code and object pointer. | |
std::shared_ptr< Message > | obtainMessage (int32_t what, int32_t arg1, int32_t arg2) |
Obtain a message with type code and two arguments. | |
std::shared_ptr< Message > | obtainMessage (int32_t what, int32_t arg1, int32_t arg2, void *obj) |
Obtain a message with type code, two arguments, and object pointer. | |
std::shared_ptr< Message > | obtainMessage (int32_t what, int32_t arg1, int32_t arg2, int32_t arg3) |
Obtain a message with type code and three arguments. | |
std::shared_ptr< Message > | obtainMessage (int32_t what, std::shared_ptr< RefBase > spRef) |
Obtain a message with type code and shared pointer reference. | |
bool | sendMessage (const std::shared_ptr< Message > &message) |
Send message for immediate processing. | |
bool | sendMessageDelayed (const std::shared_ptr< Message > &message, int64_t delayMs) |
Send message with delay. | |
bool | sendMessageAtTime (const std::shared_ptr< Message > &message, int64_t whenUs) |
Send message at specific time. | |
bool | hasMessages (int32_t what) |
Check if message with type code exists in queue. | |
bool | removeMessages (int32_t what) |
Remove messages with type code from queue. | |
bool | removeMessages (int32_t what, void *obj) |
Remove messages with type code and object pointer from queue. | |
void | dispatchMessage (const std::shared_ptr< Message > &message) |
Dispatch message to handler (internal use) | |
int64_t | uptimeMicros () |
Get current system uptime in microseconds. | |
virtual void | handleMessage (const std::shared_ptr< Message > &msg)=0 |
Override to handle received messages. | |
Android-style message handler for event-driven programming.
Handler provides a flexible mechanism for processing messages and events in the context of an event loop (SLLooper). It enables asynchronous communication between threads and decouples message producers from consumers. Each Handler is associated with a specific event loop and message queue (EventQueue).
Key features:
swt::Handler::Handler | ( | ) |
Default constructor - not associated with any looper.
swt::Handler::Handler | ( | std::shared_ptr< SLLooper > & | looper | ) |
Constructor with looper association.
Constructor - associates handler with event loop.
looper | Shared pointer to SLLooper for event loop context |
looper | SLLooper instance for message queue access |
Initializes the handler with a reference to the event loop's message queue. The handler will post messages to this queue and receive callbacks when messages are processed in the event loop thread.
Key initialization:
Definition at line 42 of file Handler.cpp.
|
virtual |
Virtual destructor.
Destructor - cleanup handler resources.
Performs cleanup of handler resources. In a complete implementation, this would remove any pending messages associated with this handler from the event queue.
Definition at line 57 of file Handler.cpp.
void swt::Handler::dispatchMessage | ( | const std::shared_ptr< Message > & | message | ) |
Dispatch message to handler (internal use)
Dispatch message to handler for processing.
message | Message to dispatch |
message | Message to process |
Called by the event loop when a message targeted to this handler is ready for processing. Delegates to the virtual handleMessage() method for actual message handling.
This method provides an extension point for message preprocessing or logging before actual message handling occurs.
Definition at line 349 of file Handler.cpp.
References handleMessage().
|
pure virtual |
Override to handle received messages.
msg | Message to process |
Referenced by dispatchMessage().
bool swt::Handler::hasMessages | ( | int32_t | what | ) |
Check if message with type code exists in queue.
Check if handler has pending messages of specific type.
what | Message type code |
what | Message type identifier to check |
Queries the event queue for pending messages with the specified 'what' value that are targeted to this handler.
Definition at line 291 of file Handler.cpp.
std::shared_ptr< Message > swt::Handler::obtainMessage | ( | ) |
Obtain a new empty message.
Create a basic message with this handler as target.
Creates a message that will be delivered to this handler's handleMessage() when processed by the event loop. This is the most basic message creation.
Definition at line 80 of file Handler.cpp.
References swt::Message::obtain().
std::shared_ptr< Message > swt::Handler::obtainMessage | ( | int32_t | what | ) |
Obtain a message with type code.
Create message with 'what' identifier.
what | Message type code |
what | Message type identifier |
Creates a message with a specific type identifier. The 'what' field allows handlers to distinguish between different message types.
Definition at line 98 of file Handler.cpp.
References swt::Message::obtain().
std::shared_ptr< Message > swt::Handler::obtainMessage | ( | int32_t | what, |
int32_t | arg1 | ||
) |
Obtain a message with type code and argument.
Create message with 'what' and first argument.
what | Message type code |
arg1 | First argument |
what | Message type identifier |
arg1 | First integer argument |
Creates a message with type and one integer argument. Useful for simple parameter passing in message-based communication.
Definition at line 112 of file Handler.cpp.
References swt::Message::obtain().
std::shared_ptr< Message > swt::Handler::obtainMessage | ( | int32_t | what, |
int32_t | arg1, | ||
int32_t | arg2 | ||
) |
Obtain a message with type code and two arguments.
Create message with 'what' and two arguments.
what | Message type code |
arg1 | First argument |
arg2 | Second argument |
what | Message type identifier |
arg1 | First integer argument |
arg2 | Second integer argument |
Creates a message with type and two integer arguments. Common pattern for coordinate pairs, dimensions, or simple parameter combinations.
Definition at line 149 of file Handler.cpp.
References swt::Message::obtain().
std::shared_ptr< Message > swt::Handler::obtainMessage | ( | int32_t | what, |
int32_t | arg1, | ||
int32_t | arg2, | ||
int32_t | arg3 | ||
) |
Obtain a message with type code and three arguments.
Create message with 'what' and three arguments.
what | Message type code |
arg1 | First argument |
arg2 | Second argument |
arg3 | Third argument |
what | Message type identifier |
arg1 | First integer argument |
arg2 | Second integer argument |
arg3 | Third integer argument |
Extended message creation with three integer arguments for scenarios requiring additional parameter data.
Definition at line 181 of file Handler.cpp.
References swt::Message::obtain().
std::shared_ptr< Message > swt::Handler::obtainMessage | ( | int32_t | what, |
int32_t | arg1, | ||
int32_t | arg2, | ||
void * | obj | ||
) |
Obtain a message with type code, two arguments, and object pointer.
Create message with 'what', two arguments, and object.
what | Message type code |
arg1 | First argument |
arg2 | Second argument |
obj | Object pointer |
what | Message type identifier |
arg1 | First integer argument |
arg2 | Second integer argument |
obj | Pointer to arbitrary object data |
Creates a message with full parameter set: type, two integers, and object. Most comprehensive message creation method for complex scenarios.
Definition at line 165 of file Handler.cpp.
References swt::Message::obtain().
std::shared_ptr< Message > swt::Handler::obtainMessage | ( | int32_t | what, |
std::shared_ptr< RefBase > | spRef | ||
) |
Obtain a message with type code and shared pointer reference.
Create message with 'what' and shared reference object.
what | Message type code |
spRef | Shared pointer reference |
what | Message type identifier |
spRef | Shared pointer to reference-counted object |
Creates a message with a shared reference object. Preferred over raw pointers for automatic lifetime management and memory safety.
Definition at line 202 of file Handler.cpp.
References swt::Message::obtain().
std::shared_ptr< Message > swt::Handler::obtainMessage | ( | int32_t | what, |
void * | obj | ||
) |
Obtain a message with type code and object pointer.
Create message with 'what' and object pointer.
what | Message type code |
obj | Object pointer |
what | Message type identifier |
obj | Pointer to arbitrary object data |
Creates a message with type and object pointer. The object lifetime must be managed carefully to ensure it remains valid when message is processed.
Definition at line 129 of file Handler.cpp.
References swt::Message::obtain().
bool swt::Handler::removeMessages | ( | int32_t | what | ) |
Remove messages with type code from queue.
Remove pending messages of specific type.
what | Message type code |
what | Message type identifier to remove |
Removes all pending messages of the specified type that are targeted to this handler from the event queue.
Definition at line 307 of file Handler.cpp.
bool swt::Handler::removeMessages | ( | int32_t | what, |
void * | obj | ||
) |
Remove messages with type code and object pointer from queue.
Remove pending messages of specific type and object.
what | Message type code |
obj | Object pointer |
what | Message type identifier to remove |
obj | Object pointer to match for removal |
Removes pending messages that match both the 'what' type and object pointer, providing more selective message removal.
Definition at line 326 of file Handler.cpp.
bool swt::Handler::sendMessage | ( | const std::shared_ptr< Message > & | message | ) |
Send message for immediate processing.
Send message immediately to event queue.
message | Message to send |
message | Message to send |
Posts message to the event queue for immediate processing. The message will be processed as soon as the event loop reaches it in the queue.
Definition at line 227 of file Handler.cpp.
References sendMessageAtTime(), and uptimeMicros().
bool swt::Handler::sendMessageAtTime | ( | const std::shared_ptr< Message > & | message, |
int64_t | whenUs | ||
) |
Send message at specific time.
message | Message to send |
whenUs | Absolute time in microseconds |
message | Message to send |
whenUs | Absolute time in microseconds when to process message |
Posts message to be processed at a specific absolute time. Uses the internal microsecond timestamp format for precise timing control.
Definition at line 267 of file Handler.cpp.
Referenced by sendMessage(), and sendMessageDelayed().
bool swt::Handler::sendMessageDelayed | ( | const std::shared_ptr< Message > & | message, |
int64_t | delayMs | ||
) |
Send message with delay.
message | Message to send |
delayMs | Delay in milliseconds |
message | Message to send |
delayMs | Delay in milliseconds before processing |
Posts message to the event queue with a specified delay. The message will be processed after the delay expires.
Definition at line 250 of file Handler.cpp.
References ms2us, sendMessageAtTime(), and uptimeMicros().
int64_t swt::Handler::uptimeMicros | ( | ) |
Get current system uptime in microseconds.
Provides monotonic time measurement for message timing and scheduling. Uses steady_clock to avoid issues with system clock adjustments.
Key characteristics:
Used internally for:
Definition at line 382 of file Handler.cpp.
Referenced by sendMessage(), and sendMessageDelayed().