| EventStudio 2.5 Sequence diagram based system design and modeling | |
#include <Message_Queue.h> Message Queues are a very important design pattern in embedded and real-time systems. Here we implement the Message Queue class as a very thin wrapper over the STL queue container adaptor.
STL queue container adaptor supports the standard queue operations, viz. add to queue, remove from queue. STL allows you to specify the underlying container used for implementing the queue. By changing one line of code you could change from a linked list implementation to an array like double ended queue In this example we will be implementing the queue using the list. This organizes the message queue as a linked list of nodes containing pointers to the message.
Definition at line 28 of file Message_Queue.h.
Public Member Functions | |
| void | Add (Message *pMsg) |
| Add a message to the end of the queue. | |
| Message * | Remove () |
| Remove a message from the head of the queue. | |
| int | GetLength () const |
| Obtain the length of the queue. | |
Private Types | |
| typedef queue< Message *, list< Message * > > | MsgQueType |
| Declaring the type for a queue. | |
Private Attributes | |
| MsgQueType | m_msgQueue |
| Declaration of the message queue based on the type declaration MsgQueType. | |
|
Declaring the type for a queue. Here we are specifying a queue of message pointers organized as a linked list. Definition at line 32 of file Message_Queue.h. |
|
Add a message to the end of the queue. The push method supported by queue is used to add the message to the end of the queue. Note that all the memory management associated with the queue container management is taken care by STL.
Definition at line 44 of file Message_Queue.h. 00045 {
00046 // Insert the element at the end of the queue
00047 m_msgQueue.push(pMsg);
00048 }
|
|
Remove a message from the head of the queue. Returns NULL if no message is found. This method performs the following steps:
retval Message Pointer to the message removed from the queue. Returns NULL if no message is found in the queue. Definition at line 60 of file Message_Queue.h. 00061 {
00062 Message *pMsg = NULL;
00063
00064 // Check if the message queue is not empty
00065 if (!m_msgQueue.empty())
00066 {
00067 // Queue is not empty so get a pointer to the
00068 // first message in the queue
00069 pMsg = m_msgQueue.front();
00070
00071 // Now remove the pointer from the message queue
00072 m_msgQueue.pop();
00073 }
00074 return pMsg;
00075 }
|
|
Obtain the length of the queue. The size method is used to obtain the queue length.
Definition at line 80 of file Message_Queue.h. 00081 {
00082 return m_msgQueue.size();
00083 }
|
1.3.4 | |
| Copyright © 2000-2005 EventHelix.com Inc. All Rights Reserved. |