Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
core_lib::threads::MessageQueueThread< MessageId, MessageType > Class Template Referencefinal

Message Queue Thread. More...

#include <MessageQueueThread.h>

Inheritance diagram for core_lib::threads::MessageQueueThread< MessageId, MessageType >:
core_lib::threads::ThreadBase

Public Types

using msg_handler_t = std::function< bool(MessageType &)>
 Typedef defining message handler functor. More...
 

Public Member Functions

 MessageQueueThread (const msg_id_decoder_t &messageIdDecoder, eOnDestroyOptions destroyOptions=eOnDestroyOptions::ignoreRemainingItems, const msg_deleter_t &messageDeleter=msg_deleter_t())
 Default constructor. More...
 
 MessageQueueThread (const MessageQueueThread &)=delete
 Copy constructor deleted.
 
MessageQueueThreadoperator= (const MessageQueueThread &)=delete
 Copy assignment operator deleted.
 
 MessageQueueThread (MessageQueueThread &&)=delete
 Move constructor deleted.
 
MessageQueueThreadoperator= (MessageQueueThread &&)=delete
 Move assignment operator deleted.
 
 ~MessageQueueThread () override
 Destructor.
 
void RegisterMessageHandler (MessageId const &messageID, msg_handler_t const &messageHandler)
 Register a function to handle a particular message. More...
 
void Push (MessageType &&msg)
 Push a message as an array of items onto this thread's queue. More...
 
- Public Member Functions inherited from core_lib::threads::ThreadBase
 ThreadBase (const ThreadBase &)=delete
 Copy constructor deleted.
 
ThreadBaseoperator= (const ThreadBase &)=delete
 Copy assignment operator deleted.
 
 ThreadBase (ThreadBase &&)=default
 Move constructor deleted.
 
ThreadBaseoperator= (ThreadBase &&)=default
 Move assignment operator deleted.
 
virtual ~ThreadBase ()=default
 Destructor.
 
bool IsStarted () const
 Is thread started. More...
 
bool Start ()
 Start the thread. More...
 
bool Stop ()
 Safely stop the thread. More...
 
std::thread::id ThreadID () const
 Get this thread's thread ID. More...
 
std::thread::native_handle_type NativeHandle () const
 Get the underlying std::thread's native handle. More...
 

Private Types

using msg_id_decoder_t = std::function< MessageId(const MessageType &)>
 Typedef defining message ID decoder function. More...
 
using msg_deleter_t = std::function< void(MessageType &)>
 Typedef defining message deleter function. More...
 
using msg_map_t = std::map< MessageId, msg_handler_t >
 Typedef for message map type.
 

Private Member Functions

void ThreadIteration () NO_EXCEPT_ override
 Execute a single iteration of the thread.
 
void ProcessTerminationConditions () NO_EXCEPT_ override
 Perform any special termination actions.
 
void ProcessNextMessage ()
 Process next message.
 
void DeleteNextMessage ()
 Delete next message.
 

Private Attributes

std::mutex m_mutex
 
msg_id_decoder_t m_msgIdDecoder {}
 Message ID decoder function object.
 
eOnDestroyOptions m_destroyOptions {}
 Control the handling of the queue items in destructor.
 
msg_deleter_t m_messageDeleter {}
 Optional message item deleter function object.
 
msg_map_t m_msgHandlerMap {}
 Message handler function Map.
 
ConcurrentQueue< MessageType > m_messageQueue {}
 Message queue.
 

Additional Inherited Members

- Protected Member Functions inherited from core_lib::threads::ThreadBase
 ThreadBase ()=default
 Default constructor.
 
void SetTerminating (bool terminating=true)
 Set terminating flag. More...
 
bool IsTerminating () const
 Is thread terminating. More...
 
void SleepForTime (unsigned int milliSecs) const
 Make this thread sleep for a period of time. More...
 

Detailed Description

template<typename MessageId, typename MessageType>
class core_lib::threads::MessageQueueThread< MessageId, MessageType >

Message Queue Thread.

This class is designed for processing messages specified by the template argument. The class itself contains no knowledge of how to process these messages, this is handled by the user registering message handler functors per registered command. Instead this class provides a safe thread based mechanism for processing messages added to this class's message queue.

Template args as follows: MessageId - define the type used for message ID. MessageType - define the type used for the message.

Member Typedef Documentation

◆ msg_deleter_t

template<typename MessageId , typename MessageType >
using core_lib::threads::MessageQueueThread< MessageId, MessageType >::msg_deleter_t = std::function<void(MessageType&)>
private

Typedef defining message deleter function.

Parameters
[in]message- Reference to message.

The deleter function should not throw any exceptions.

◆ msg_handler_t

template<typename MessageId , typename MessageType >
using core_lib::threads::MessageQueueThread< MessageId, MessageType >::msg_handler_t = std::function<bool(MessageType&)>

Typedef defining message handler functor.

Parameters
[in]message- Reference to message.
Returns
True if message is finished with, false if message ownership has taken by another object.

The decoder function should not throw any exceptions.

◆ msg_id_decoder_t

template<typename MessageId , typename MessageType >
using core_lib::threads::MessageQueueThread< MessageId, MessageType >::msg_id_decoder_t = std::function<MessageId(const MessageType&)>
private

Typedef defining message ID decoder function.

Parameters
[in]message- Const reference to message.
Returns
Unique ID of the message to be processed.

The decoder function should not throw any exceptions.

Constructor & Destructor Documentation

◆ MessageQueueThread()

template<typename MessageId , typename MessageType >
core_lib::threads::MessageQueueThread< MessageId, MessageType >::MessageQueueThread ( const msg_id_decoder_t messageIdDecoder,
eOnDestroyOptions  destroyOptions = eOnDestroyOptions::ignoreRemainingItems,
const msg_deleter_t messageDeleter = msg_deleter_t() 
)
inlineexplicit

Default constructor.

Parameters
[in]messageIdDecoder- Function object that returns the message ID for a message.
[in]destroyOptions- (Optional) Set the Message threads destroy option.
[in]messageDeleter- (Optional) Message deletion helper.

Throws a std::runtime_error exception if thread fails to start.

Member Function Documentation

◆ Push()

template<typename MessageId , typename MessageType >
void core_lib::threads::MessageQueueThread< MessageId, MessageType >::Push ( MessageType &&  msg)
inline

Push a message as an array of items onto this thread's queue.

Parameters
[in]msg- message, perfectly forwarded.

◆ RegisterMessageHandler()

template<typename MessageId , typename MessageType >
void core_lib::threads::MessageQueueThread< MessageId, MessageType >::RegisterMessageHandler ( MessageId const &  messageID,
msg_handler_t const &  messageHandler 
)
inline

Register a function to handle a particular message.

Parameters
[in]messageID- Message ID.
[in]messageHandler- Function object to handle messages with specified message ID.

Throws a std::runtime_error exception if handler for message ID is already defined.

Member Data Documentation

◆ m_mutex

template<typename MessageId , typename MessageType >
std::mutex core_lib::threads::MessageQueueThread< MessageId, MessageType >::m_mutex
mutableprivate

Mutex to lock access to message handler map.


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