KSquare Utilities
MsgQueue.h
Go to the documentation of this file.
1 /* MsgQueue.h -- Implements KKStr message queue meant to work in a multi-threaded environment.
2  * Copyright (C) 2011-2014 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 #if !defined(_KKU_MSGQUEUE_)
6 #define _KKU_MSGQUEUE_
7 #include <queue>
8 
10 
11 namespace KKB
12 {
13  /**
14  *@class MsgQueue
15  *@brief Will manage a buffer that will allow multiple threads to add and remove messages to a queue.
16  *@details A 'GoalKeepeer' object 'gateKeeper' will be used to enforce integrity in the Multi-Threaded
17  * environment. It will guarantee that only one thread at a time can access the Queue.
18  */
19  class MsgQueue
20  {
21  public:
23 
24  MsgQueue (const KKStr& _name);
25 
26  ~MsgQueue ();
27 
28 
29  /**
30  *@brief Returns an estimate of the amount of memory consumed in bytes by this instance.
31  *@details This will help managed objects keep track of how much memory they are using in the
32  * unmanaged world.
33  */
35 
36  /**
37  *@brief Take ownership of 'msg' and add to end of the queue.
38  *@param[in] msg Pointer to message that is to be added to end of queue. The caller will
39  * pass ownership of this string to this instance of MsgQueue.
40  */
41  void AddMsg (KKStrPtr msg); /**< Taking ownership of 'msg' */
42 
43 
44  /*@brief A copy of the message 'msg' will be added to the end of the queue. */
45  void AddMsg (const KKStr& msg);
46 
47 
48  /**
49  *@brief Adds the contents of 'msgs' to the message queue and depending on 'takeOwnership' will
50  * either assume that its owns (taking ownership) the KKStr instances or create duplicates.
51  *@details If 'takeOwnership' is true will and the KKStr instances in 'msgs' to the queue
52  * otherwise it will make duplicates of the KKStr instances. It is up to the caller to make
53  * sure that they set the Owner flag on the 'msgs' instance; for example if 'takeOwnership' is
54  * 'true' then caller should set 'msgs.Owner (false);'.
55  */
56  void AddMsgs (const KKStrListPtr msgs,
57  bool takeOwnership
58  );
59 
60 
61  /**
62  *@brief Returns a duplicate of the last string added to the message queue.
63  *@details This will not effect the current copy of the message queue. The returned string
64  * will be owned by the caller who will be responsible for deleting it.
65  */
66  KKStrPtr GetCopyOfLastMsg ();
67 
68  /**
69  *@brief Returns all messages that are currently in the queue.
70  */
71  KKStrListPtr GetAllMsgs ();
72 
73  /**
74  *@brief Removes from the queue the oldest message added to the queue that has not been removed.
75  *@details The caller will get ownership of the string and be responsible for deleting it.
76  */
77  KKStrPtr GetNextMsg ();
78 
79 
80  private:
81  GoalKeeperSimplePtr gateKeeper; /**< Used to manage synchronization amongst different threads to this queue. */
82  kkint32 memoryConsumed;
83  KKStr name; /**< Name of msgQueue. */
84  std::queue<KKStrPtr> queue;
85  }; /* MsgQueue */
86 
87 
88  typedef MsgQueue::MsgQueuePtr MsgQueuePtr;
89 } /* KKB */
90 
91 #endif
void AddMsg(KKStrPtr msg)
Take ownership of &#39;msg&#39; and add to end of the queue.
Definition: MsgQueue.cpp:58
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 MemoryConsumedEstimated()
Returns an estimate of the amount of memory consumed in bytes by this instance.
Definition: MsgQueue.cpp:146
void AddMsg(const KKStr &msg)
Definition: MsgQueue.cpp:76
void AddMsgs(const KKStrListPtr msgs, bool takeOwnership)
Adds the contents of &#39;msgs&#39; to the message queue and depending on &#39;takeOwnership&#39; will either assume ...
Definition: MsgQueue.cpp:86
KKStrPtr GetNextMsg()
Removes from the queue the oldest message added to the queue that has not been removed.
Definition: MsgQueue.cpp:107
KKStrListPtr GetAllMsgs()
Returns all messages that are currently in the queue.
Definition: MsgQueue.cpp:125
KKTHread * KKTHreadPtr
Will manage a buffer that will allow multiple threads to add and remove messages to a queue...
Definition: MsgQueue.h:19
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
MsgQueue(const KKStr &_name)
Definition: MsgQueue.cpp:29
KKStrPtr GetCopyOfLastMsg()
Returns a duplicate of the last string added to the message queue.
Definition: MsgQueue.cpp:157
MsgQueue * MsgQueuePtr
Definition: MsgQueue.h:22