SW Task Event Loop Framework v1.0.0
High-performance C++ asynchronous event loop framework with timer management and promise-based programming
Loading...
Searching...
No Matches
Handler.cpp
Go to the documentation of this file.
1
9 #include <assert.h>
10 #include <chrono>
11 #include "Handler.h"
12 #include <iostream>
13 #include <string>
14
23 #define ms2us(x) (x*1000LL)
24
25namespace swt {
42 Handler::Handler(std::shared_ptr<SLLooper>& looper)
43 {
44 mEventQueue = looper->getEventQueue(); // ✅ Updated method name
45 mLooper = looper;
46 }
47
58 {
59 // Future: Remove pending messages from queue
60 // mEventQueue->removeMessagesForHandler(shared_from_this());
61 }
62
63 // ========== MESSAGE CREATION API ==========
64
80 std::shared_ptr<Message> Handler::obtainMessage()
81 {
82 return Message::obtain(shared_from_this()); // ✅ SAFE!
83 }
84
98 std::shared_ptr<Message> Handler::obtainMessage(int32_t what)
99 {
100 return Message::obtain(shared_from_this(), what); // ✅ SAFE!
101 }
102
112 std::shared_ptr<Message> Handler::obtainMessage(int32_t what, int32_t arg1)
113 {
114 return Message::obtain(shared_from_this(), what, arg1); // ✅ SAFE!
115 }
116
129 std::shared_ptr<Message> Handler::obtainMessage(int32_t what, void* obj)
130 {
131 return Message::obtain(shared_from_this(), what, obj); // ✅ SAFE!
132 }
133
149 std::shared_ptr<Message> Handler::obtainMessage(int32_t what, int32_t arg1, int32_t arg2)
150 {
151 return Message::obtain(shared_from_this(), what, arg1, arg2); // ✅ SAFE!
152 }
153
165 std::shared_ptr<Message> Handler::obtainMessage(int32_t what, int32_t arg1, int32_t arg2, void* obj)
166 {
167 return Message::obtain(shared_from_this(), what, arg1, arg2, obj); // ✅ SAFE!
168 }
169
181 std::shared_ptr<Message> Handler::obtainMessage(int32_t what, int32_t arg1, int32_t arg2, int32_t arg3)
182 {
183 return Message::obtain(shared_from_this(), what, arg1, arg2, arg3); // ✅ SAFE!
184 }
185
202 std::shared_ptr<Message> Handler::obtainMessage(int32_t what, std::shared_ptr<RefBase> spRef)
203 {
204 return Message::obtain(shared_from_this(), what, spRef);
205 }
206
207 // ========== MESSAGE SENDING API ==========
208
227 bool Handler::sendMessage(const std::shared_ptr<Message>& message)
228 {
229 return sendMessageAtTime(message, uptimeMicros());
230 }
231
250 bool Handler::sendMessageDelayed(const std::shared_ptr<Message>& message, int64_t delayMs)
251 {
252 return sendMessageAtTime(message, (uptimeMicros() + ms2us(delayMs)));
253 }
254
267 bool Handler::sendMessageAtTime(const std::shared_ptr<Message>& message, int64_t whenUs)
268 {
269 return mEventQueue->enqueueMessage(message, whenUs);
270 }
271
272 // ========== MESSAGE QUERY API ==========
273
291 bool Handler::hasMessages(int32_t what)
292 {
293 return mEventQueue->hasMessage(shared_from_this(), what, NULL); // ✅ SAFE!
294 }
295
307 bool Handler::removeMessages(int32_t what)
308 {
309 // TODO: Implement message removal
310 // return mEventQueue->removeMessages(shared_from_this(), what);
311 return false;
312 }
313
326 bool Handler::removeMessages(int32_t what, void* obj)
327 {
328 // TODO: Implement selective message removal
329 // return mEventQueue->removeMessages(shared_from_this(), what, obj);
330 return false;
331 }
332
333 // ========== MESSAGE PROCESSING ==========
334
349 void Handler::dispatchMessage(const std::shared_ptr<Message>& message)
350 {
351 // Future: Add message preprocessing here
352 // logMessage(message);
353 // validateMessage(message);
354
355 handleMessage(message);
356 }
357
358 // ========== UTILITY METHODS ==========
359
383 {
384 auto now = std::chrono::steady_clock::now();
385 auto dur = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
386 return dur.count();
387 }
388
389} // namespace swt
#define ms2us(x)
Convert milliseconds to microseconds.
Definition Handler.cpp:23
bool sendMessage(const std::shared_ptr< Message > &message)
Send message for immediate processing.
Definition Handler.cpp:227
int64_t uptimeMicros()
Get current system uptime in microseconds.
Definition Handler.cpp:382
bool sendMessageDelayed(const std::shared_ptr< Message > &message, int64_t delayMs)
Send message with delay.
Definition Handler.cpp:250
bool sendMessageAtTime(const std::shared_ptr< Message > &message, int64_t whenUs)
Send message at specific time.
Definition Handler.cpp:267
virtual ~Handler()
Virtual destructor.
Definition Handler.cpp:57
virtual void handleMessage(const std::shared_ptr< Message > &msg)=0
Override to handle received messages.
std::shared_ptr< Message > obtainMessage()
Obtain a new empty message.
Definition Handler.cpp:80
bool hasMessages(int32_t what)
Check if message with type code exists in queue.
Definition Handler.cpp:291
Handler()
Default constructor - not associated with any looper.
bool removeMessages(int32_t what)
Remove messages with type code from queue.
Definition Handler.cpp:307
void dispatchMessage(const std::shared_ptr< Message > &message)
Dispatch message to handler (internal use)
Definition Handler.cpp:349
static std::shared_ptr< Message > obtain()
Obtain a new empty message.
Definition Message.cpp:38
Software Timer namespace containing all timer-related classes.
Definition Awaitable.h:21