CuteLogger
Fast and simple logging solution for Qt based applications
dataqueue.h
1 /*
2  * Copyright (c) 2015 Meltytech, LLC
3  * Author: Brian Matherly <code@brianmatherly.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef DATAQUEUE_H
20 #define DATAQUEUE_H
21 
22 #include <QMutex>
23 #include <QWaitCondition>
24 #include <QMutexLocker>
25 
26 #include <deque>
27 
47 template <class T>
48 class DataQueue
49 {
50 public:
52  typedef enum {
57 
64  explicit DataQueue(int maxSize, OverflowMode mode);
65 
67  virtual ~DataQueue();
68 
75  void push(const T& item);
76 
83  T pop();
84 
86  int count() const;
87 
88 private:
89  std::deque<T> m_queue;
90  int m_maxSize;
91  OverflowMode m_mode;
92  mutable QMutex m_mutex;
93  QWaitCondition m_notEmptyCondition;
94  QWaitCondition m_notFullCondition;
95 };
96 
97 template <class T>
99  : m_queue()
100  , m_maxSize(maxSize)
101  , m_mode(mode)
102  , m_mutex(QMutex::NonRecursive)
103  , m_notEmptyCondition()
104  , m_notFullCondition()
105 {
106 }
107 
108 template <class T>
110 {
111 }
112 
113 template <class T>
114 void DataQueue<T>::push(const T& item)
115 {
116  m_mutex.lock();
117  if (m_queue.size() == m_maxSize) {
118  switch(m_mode) {
119  case OverflowModeDiscardOldest:
120  m_queue.pop_front();
121  m_queue.push_back(item);
122  break;
123  case OverflowModeDiscardNewest:
124  // This item is the newest so discard it and exit
125  break;
126  case OverflowModeWait:
127  m_notFullCondition.wait(&m_mutex);
128  m_queue.push_back(item);
129  break;
130  }
131  } else {
132  m_queue.push_back(item);
133  if (m_queue.size() == 1) {
134  m_notEmptyCondition.wakeOne();
135  }
136  }
137  m_mutex.unlock();
138 }
139 
140 template <class T>
142 {
143  T retVal;
144  m_mutex.lock();
145  if (m_queue.size() == 0) {
146  m_notEmptyCondition.wait(&m_mutex);
147  }
148  retVal = m_queue.front();
149  m_queue.pop_front();
150  if (m_mode == OverflowModeWait && m_queue.size() == m_maxSize - 1) {
151  m_notFullCondition.wakeOne();
152  }
153  m_mutex.unlock();
154  return retVal;
155 }
156 
157 template <class T>
159 {
160  QMutexLocker locker(&m_mutex);
161  return m_queue.size();
162 }
163 
164 #endif // DATAQUEUE_H
DataQueue::DataQueue
DataQueue(int maxSize, OverflowMode mode)
Definition: dataqueue.h:98
DataQueue::OverflowModeWait
@ OverflowModeWait
Wait for space to be free.
Definition: dataqueue.h:55
DataQueue::OverflowModeDiscardNewest
@ OverflowModeDiscardNewest
Discard newest items.
Definition: dataqueue.h:54
DataQueue::push
void push(const T &item)
Definition: dataqueue.h:114
DataQueue::pop
T pop()
Definition: dataqueue.h:141
DataQueue::count
int count() const
Returns the number of items in the queue.
Definition: dataqueue.h:158
DataQueue::~DataQueue
virtual ~DataQueue()
Destructs a DataQueue.
Definition: dataqueue.h:109
DataQueue::OverflowModeDiscardOldest
@ OverflowModeDiscardOldest
Discard oldest items.
Definition: dataqueue.h:53
DataQueue::OverflowMode
OverflowMode
Overflow behavior modes.
Definition: dataqueue.h:52
DataQueue
The DataQueue provides a thread safe container for passing data between objects.
Definition: dataqueue.h:49