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
Message.h
Go to the documentation of this file.
1#ifndef MESSAGE_H
2#define MESSAGE_H
3
4#include <memory>
5#include <cstdint>
6#include "Refbase.h"
7
8namespace swt {
9
10class Handler;
11
37class Message : public std::enable_shared_from_this<Message>
38{
39public:
43 Message();
44
49 Message(Message& other);
50
54 virtual ~Message();
55
56 // ========== STATIC FACTORY METHODS ==========
57
62 static std::shared_ptr<Message> obtain();
63
69 static std::shared_ptr<Message> obtain(const Message* message);
70
77 static std::shared_ptr<Message> obtain(const std::shared_ptr<Handler>& handler);
78
85 static std::shared_ptr<Message> obtain(const std::shared_ptr<Handler>& handler, int32_t obtain_what);
86
94 static std::shared_ptr<Message> obtain(const std::shared_ptr<Handler>& handler, int32_t obtain_what,
95 int32_t obtain_arg1);
96
104 static std::shared_ptr<Message> obtain(const std::shared_ptr<Handler>& handler, int32_t obtain_what,
105 void* obtain_obj);
106
115 static std::shared_ptr<Message> obtain(const std::shared_ptr<Handler>& handler, int32_t obtain_what,
116 int32_t obtain_arg1, int32_t obtain_arg2);
117
127 static std::shared_ptr<Message> obtain(const std::shared_ptr<Handler>& handler, int32_t obtain_what,
128 int32_t obtain_arg1, int32_t obtain_arg2, void* obtain_obj);
129
139 static std::shared_ptr<Message> obtain(const std::shared_ptr<Handler>& handler, int32_t obtain_what,
140 int32_t obtain_arg1, int32_t obtain_arg2, int32_t obtain_arg3);
141
149 static std::shared_ptr<Message> obtain(const std::shared_ptr<Handler>& handler, int32_t obtain_what,
150 std::shared_ptr<RefBase> obtain_spRef);
151
152 /*
153 template<typename T>
154 static std::shared_ptr<Message> obtain(const std::shared_ptr<Handler>& handler, int32_t obtain_what,
155 std::shared_ptr<T> obtain_spRef);
156 */
157
163 bool sendToTarget();
164
169 std::shared_ptr<Message> dup() const;
170
176 template<typename T>
177 void getObject(std::shared_ptr<T>& obj)
178 {
179 obj = static_cast<T*>( spRef.get() );
180 }
181
182protected:
188 Message& operator=(const Message& other);
189
193 void clear();
194
195public:
200 void setTo(const Message& other);
201
202 std::shared_ptr<Handler> mHandler;
203 std::shared_ptr<Message> mNextMessage;
205 int32_t what;
206 int32_t arg1;
207 int32_t arg2;
208 int32_t arg3;
210 void* obj;
211 ssize_t obj_size;
212 std::shared_ptr<RefBase> spRef;
214private:
215 int64_t whenUs;
217 friend class Handler;
218 friend class EventQueue;
219 friend class SLLooper;
220};
221
222} // namespace swt
223
224#endif // MESSAGE_H
Thread-safe unified queue for messages and function tasks with timed execution.
Definition EventQueue.h:86
Android-style message handler for event-driven programming.
Definition Handler.h:46
Message object for event-driven communication between threads and handlers.
Definition Message.h:38
std::shared_ptr< Message > dup() const
Create a deep copy of this message.
Definition Message.cpp:163
void getObject(std::shared_ptr< T > &obj)
Get attached object as shared pointer of type T.
Definition Message.h:177
void setTo(const Message &other)
Set this message to another message's contents.
Definition Message.cpp:190
std::shared_ptr< RefBase > spRef
Smart pointer reference.
Definition Message.h:212
virtual ~Message()
Virtual destructor.
Definition Message.cpp:34
Message()
Default constructor - creates empty message.
Definition Message.cpp:6
bool sendToTarget()
Send this message to its target handler.
Definition Message.cpp:146
ssize_t obj_size
Size of object (if applicable)
Definition Message.h:211
Message & operator=(const Message &other)
Assignment operator - deep copy from another message.
Definition Message.cpp:184
std::shared_ptr< Message > mNextMessage
Next message in queue.
Definition Message.h:203
void clear()
Clear all fields in the message.
Definition Message.cpp:168
std::shared_ptr< Handler > mHandler
Target handler.
Definition Message.h:202
int32_t arg3
Third argument.
Definition Message.h:208
int32_t what
Message type code.
Definition Message.h:205
int32_t arg2
Second argument.
Definition Message.h:207
void * obj
Raw object pointer.
Definition Message.h:210
int32_t arg1
First argument.
Definition Message.h:206
static std::shared_ptr< Message > obtain()
Obtain a new empty message.
Definition Message.cpp:38
Central event loop coordinator providing asynchronous task execution and timer management.
Definition SLLooper.h:83
Software Timer namespace containing all timer-related classes.
Definition Awaitable.h:21