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
swt::Message Class Reference

Message object for event-driven communication between threads and handlers. More...

#include <Message.h>

Inheritance diagram for swt::Message:
Inheritance graph
Collaboration diagram for swt::Message:
Collaboration graph

Public Member Functions

 Message ()
 Default constructor - creates empty message.
 
 Message (Message &other)
 Copy constructor - deep copy from another message.
 
virtual ~Message ()
 Virtual destructor.
 
bool sendToTarget ()
 Send this message to its target handler.
 
std::shared_ptr< Messagedup () const
 Create a deep copy of this message.
 
template<typename T >
void getObject (std::shared_ptr< T > &obj)
 Get attached object as shared pointer of type T.
 
void setTo (const Message &other)
 Set this message to another message's contents.
 

Static Public Member Functions

static std::shared_ptr< Messageobtain ()
 Obtain a new empty message.
 
static std::shared_ptr< Messageobtain (const Message *message)
 Obtain a copy of an existing message.
 
static std::shared_ptr< Messageobtain (const std::shared_ptr< Handler > &handler)
 Obtain a message associated with a handler.
 
static std::shared_ptr< Messageobtain (const std::shared_ptr< Handler > &handler, int32_t obtain_what)
 Obtain a message with handler and type code.
 
static std::shared_ptr< Messageobtain (const std::shared_ptr< Handler > &handler, int32_t obtain_what, int32_t obtain_arg1)
 Obtain a message with handler, type code, and argument.
 
static std::shared_ptr< Messageobtain (const std::shared_ptr< Handler > &handler, int32_t obtain_what, void *obtain_obj)
 Obtain a message with handler, type code, and object pointer.
 
static std::shared_ptr< Messageobtain (const std::shared_ptr< Handler > &handler, int32_t obtain_what, int32_t obtain_arg1, int32_t obtain_arg2)
 Obtain a message with handler, type code, and two arguments.
 
static std::shared_ptr< Messageobtain (const std::shared_ptr< Handler > &handler, int32_t obtain_what, int32_t obtain_arg1, int32_t obtain_arg2, void *obtain_obj)
 Obtain a message with handler, type code, two arguments, and object pointer.
 
static std::shared_ptr< Messageobtain (const std::shared_ptr< Handler > &handler, int32_t obtain_what, int32_t obtain_arg1, int32_t obtain_arg2, int32_t obtain_arg3)
 Obtain a message with handler, type code, and three arguments.
 
static std::shared_ptr< Messageobtain (const std::shared_ptr< Handler > &handler, int32_t obtain_what, std::shared_ptr< RefBase > obtain_spRef)
 Obtain a message with handler, type code, and smart pointer reference.
 

Public Attributes

std::shared_ptr< HandlermHandler
 Target handler.
 
std::shared_ptr< MessagemNextMessage
 Next message in queue.
 
int32_t what
 Message type code.
 
int32_t arg1
 First argument.
 
int32_t arg2
 Second argument.
 
int32_t arg3
 Third argument.
 
void * obj
 Raw object pointer.
 
ssize_t obj_size
 Size of object (if applicable)
 
std::shared_ptr< RefBasespRef
 Smart pointer reference.
 

Protected Member Functions

Messageoperator= (const Message &other)
 Assignment operator - deep copy from another message.
 
void clear ()
 Clear all fields in the message.
 

Friends

class Handler
 
class EventQueue
 
class SLLooper
 

Detailed Description

Message object for event-driven communication between threads and handlers.

Message encapsulates data and metadata for asynchronous communication in the event loop framework. It supports flexible construction, deep copy, and safe transfer between threads. Each Message can be associated with a Handler and posted to an event queue (EventQueue) for timed or immediate processing.

Key features:

  • Flexible construction: Multiple static obtain() overloads for various use cases
  • Deep copy: dup() and setTo() for safe message duplication
  • Object attachment: Support for raw pointers and smart pointers (spRef)
  • Argument fields: Four integer arguments for custom data
  • Safe handler association: mHandler for target handler
  • Chaining: mNextMessage for message queueing
auto msg = Message::obtain(handler, 1, 42, 0);
msg->obj = somePtr;
handler->sendMessage(msg);
static std::shared_ptr< Message > obtain()
Obtain a new empty message.
Definition Message.cpp:38
See also
Handler, EventQueue, SLLooper

Definition at line 37 of file Message.h.

Constructor & Destructor Documentation

◆ Message() [1/2]

swt::Message::Message ( )

Default constructor - creates empty message.

Definition at line 6 of file Message.cpp.

References mHandler, mNextMessage, and spRef.

◆ Message() [2/2]

swt::Message::Message ( Message other)

Copy constructor - deep copy from another message.

Parameters
otherSource message to copy from

Definition at line 21 of file Message.cpp.

References setTo().

Here is the call graph for this function:

◆ ~Message()

swt::Message::~Message ( )
virtual

Virtual destructor.

Definition at line 34 of file Message.cpp.

Member Function Documentation

◆ clear()

void swt::Message::clear ( )
protected

Clear all fields in the message.

Definition at line 168 of file Message.cpp.

References arg1, arg2, arg3, mHandler, mNextMessage, obj, obj_size, spRef, and what.

◆ dup()

std::shared_ptr< Message > swt::Message::dup ( ) const

Create a deep copy of this message.

Returns
Shared pointer to duplicated Message

Definition at line 163 of file Message.cpp.

References obtain().

Here is the call graph for this function:

◆ getObject()

template<typename T >
void swt::Message::getObject ( std::shared_ptr< T > &  obj)
inline

Get attached object as shared pointer of type T.

Template Parameters
TTarget type
Parameters
objOutput shared pointer

Definition at line 177 of file Message.h.

References obj, and spRef.

◆ obtain() [1/10]

std::shared_ptr< Message > swt::Message::obtain ( )
static

◆ obtain() [2/10]

std::shared_ptr< Message > swt::Message::obtain ( const Message message)
static

Obtain a copy of an existing message.

Parameters
messagePointer to source message
Returns
Shared pointer to new Message

Definition at line 117 of file Message.cpp.

References arg1, arg2, arg3, mHandler, obj, obtain(), spRef, and what.

Here is the call graph for this function:

◆ obtain() [3/10]

std::shared_ptr< Message > swt::Message::obtain ( const std::shared_ptr< Handler > &  handler)
static

Obtain a message associated with a handler.

Parameters
handlerTarget handler
Returns
Shared pointer to new Message
See also
Handler

Definition at line 44 of file Message.cpp.

References obtain().

Here is the call graph for this function:

◆ obtain() [4/10]

std::shared_ptr< Message > swt::Message::obtain ( const std::shared_ptr< Handler > &  handler,
int32_t  obtain_what 
)
static

Obtain a message with handler and type code.

Parameters
handlerTarget handler
obtain_whatMessage type code
Returns
Shared pointer to new Message

Definition at line 51 of file Message.cpp.

References obtain().

Here is the call graph for this function:

◆ obtain() [5/10]

std::shared_ptr< Message > swt::Message::obtain ( const std::shared_ptr< Handler > &  handler,
int32_t  obtain_what,
int32_t  obtain_arg1 
)
static

Obtain a message with handler, type code, and argument.

Parameters
handlerTarget handler
obtain_whatMessage type code
obtain_arg1First argument
Returns
Shared pointer to new Message

Definition at line 59 of file Message.cpp.

References obtain().

Here is the call graph for this function:

◆ obtain() [6/10]

std::shared_ptr< Message > swt::Message::obtain ( const std::shared_ptr< Handler > &  handler,
int32_t  obtain_what,
int32_t  obtain_arg1,
int32_t  obtain_arg2 
)
static

Obtain a message with handler, type code, and two arguments.

Parameters
handlerTarget handler
obtain_whatMessage type code
obtain_arg1First argument
obtain_arg2Second argument
Returns
Shared pointer to new Message

Definition at line 80 of file Message.cpp.

References obtain().

Here is the call graph for this function:

◆ obtain() [7/10]

std::shared_ptr< Message > swt::Message::obtain ( const std::shared_ptr< Handler > &  handler,
int32_t  obtain_what,
int32_t  obtain_arg1,
int32_t  obtain_arg2,
int32_t  obtain_arg3 
)
static

Obtain a message with handler, type code, and three arguments.

Parameters
handlerTarget handler
obtain_whatMessage type code
obtain_arg1First argument
obtain_arg2Second argument
obtain_arg3Third argument
Returns
Shared pointer to new Message

Definition at line 105 of file Message.cpp.

References obtain().

Here is the call graph for this function:

◆ obtain() [8/10]

std::shared_ptr< Message > swt::Message::obtain ( const std::shared_ptr< Handler > &  handler,
int32_t  obtain_what,
int32_t  obtain_arg1,
int32_t  obtain_arg2,
void *  obtain_obj 
)
static

Obtain a message with handler, type code, two arguments, and object pointer.

Parameters
handlerTarget handler
obtain_whatMessage type code
obtain_arg1First argument
obtain_arg2Second argument
obtain_objObject pointer
Returns
Shared pointer to new Message

Definition at line 92 of file Message.cpp.

References obtain().

Here is the call graph for this function:

◆ obtain() [9/10]

std::shared_ptr< Message > swt::Message::obtain ( const std::shared_ptr< Handler > &  handler,
int32_t  obtain_what,
std::shared_ptr< RefBase obtain_spRef 
)
static

Obtain a message with handler, type code, and smart pointer reference.

Parameters
handlerTarget handler
obtain_whatMessage type code
obtain_spRefShared pointer reference
Returns
Shared pointer to new Message

Definition at line 133 of file Message.cpp.

References obtain().

Here is the call graph for this function:

◆ obtain() [10/10]

std::shared_ptr< Message > swt::Message::obtain ( const std::shared_ptr< Handler > &  handler,
int32_t  obtain_what,
void *  obtain_obj 
)
static

Obtain a message with handler, type code, and object pointer.

Parameters
handlerTarget handler
obtain_whatMessage type code
obtain_objObject pointer
Returns
Shared pointer to new Message

Definition at line 69 of file Message.cpp.

References obtain().

Here is the call graph for this function:

◆ operator=()

Message & swt::Message::operator= ( const Message other)
protected

Assignment operator - deep copy from another message.

Parameters
otherSource message
Returns
Reference to this message

Definition at line 184 of file Message.cpp.

References setTo().

Here is the call graph for this function:

◆ sendToTarget()

bool swt::Message::sendToTarget ( )

Send this message to its target handler.

Returns
true if message was successfully sent
See also
Handler

Definition at line 146 of file Message.cpp.

References mHandler.

◆ setTo()

void swt::Message::setTo ( const Message other)

Set this message to another message's contents.

Parameters
otherSource message

Definition at line 190 of file Message.cpp.

References arg1, arg2, arg3, obj, obj_size, spRef, and what.

Referenced by Message(), and operator=().

Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ EventQueue

friend class EventQueue
friend

Definition at line 218 of file Message.h.

◆ Handler

friend class Handler
friend

Definition at line 217 of file Message.h.

◆ SLLooper

friend class SLLooper
friend

Definition at line 219 of file Message.h.

Member Data Documentation

◆ arg1

int32_t swt::Message::arg1

First argument.

Definition at line 206 of file Message.h.

Referenced by clear(), obtain(), and setTo().

◆ arg2

int32_t swt::Message::arg2

Second argument.

Definition at line 207 of file Message.h.

Referenced by clear(), obtain(), and setTo().

◆ arg3

int32_t swt::Message::arg3

Third argument.

Definition at line 208 of file Message.h.

Referenced by clear(), obtain(), and setTo().

◆ mHandler

std::shared_ptr<Handler> swt::Message::mHandler

Target handler.

Definition at line 202 of file Message.h.

Referenced by clear(), Message(), obtain(), and sendToTarget().

◆ mNextMessage

std::shared_ptr<Message> swt::Message::mNextMessage

Next message in queue.

Definition at line 203 of file Message.h.

Referenced by clear(), and Message().

◆ obj

void* swt::Message::obj

Raw object pointer.

Definition at line 210 of file Message.h.

Referenced by clear(), getObject(), obtain(), and setTo().

◆ obj_size

ssize_t swt::Message::obj_size

Size of object (if applicable)

Definition at line 211 of file Message.h.

Referenced by clear(), and setTo().

◆ spRef

std::shared_ptr<RefBase> swt::Message::spRef

Smart pointer reference.

Definition at line 212 of file Message.h.

Referenced by clear(), getObject(), Message(), obtain(), and setTo().

◆ what

int32_t swt::Message::what

Message type code.

Definition at line 205 of file Message.h.

Referenced by clear(), obtain(), and setTo().


The documentation for this class was generated from the following files: