Barst  2.0
A server that controls lab hardware.
cpl queue.h
1 
3 #ifndef _CPL_QUEUE_H_
4 #define _CPL_QUEUE_H_
5 
6 #include <Windows.h>
7 #include <queue>
8 #include <type_traits>
9 
10 
16 template <class T>
17 class CQueue
18 {
19 public:
22  CQueue(HANDLE hEvent) {m_hEvent= hEvent; InitializeCriticalSection(&m_rLock);}
23  virtual ~CQueue() {DeleteCriticalSection(&m_rLock);}
24 
26  void Push(T pHead){
27  EnterCriticalSection(&m_rLock);
28  if (m_hEvent && !m_cPacketQueue.size())
29  SetEvent(m_hEvent);
30  m_cPacketQueue.push(pHead);
31  LeaveCriticalSection(&m_rLock);
32  }
33 
38  T Front(bool bPop, bool &bValid){
39  T pHead= T();
40  bValid= false;
41  EnterCriticalSection(&m_rLock);
42  if (m_cPacketQueue.size() != 0){
43  pHead= m_cPacketQueue.front();
44  if (bPop)
45  m_cPacketQueue.pop();
46  bValid= true;
47  }
48  LeaveCriticalSection(&m_rLock);
49  return pHead;
50  }
52  int GetSize(){
53  EnterCriticalSection(&m_rLock);
54  int nSize= (int)m_cPacketQueue.size();
55  LeaveCriticalSection(&m_rLock);
56  return nSize;
57  }
59  void ResetIfEmpty(){
60  EnterCriticalSection(&m_rLock);
61  if (!m_cPacketQueue.size())
62  ResetEvent(m_hEvent);
63  LeaveCriticalSection(&m_rLock);
64  }
65 private:
66  std::queue<T> m_cPacketQueue; // Queue holding the objects
67  CRITICAL_SECTION m_rLock; // Protects reading/writing to queue
68  HANDLE m_hEvent; // Signals when added to queue
69 };
70 
71 #endif
void ResetIfEmpty()
Definition: cpl queue.h:59
CQueue(HANDLE hEvent)
Definition: cpl queue.h:22
int GetSize()
Definition: cpl queue.h:52
void Push(T pHead)
Definition: cpl queue.h:26
T Front(bool bPop, bool &bValid)
Definition: cpl queue.h:38