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 <QList>
23 #include <QMutex>
24 #include <QWaitCondition>
25 #include <QMutexLocker>
26 
46 template <class T>
47 class DataQueue
48 {
49 public:
51  typedef enum {
56 
63  explicit DataQueue(int maxSize, OverflowMode mode);
64 
66  virtual ~DataQueue();
67 
74  void push(const T& item);
75 
82  T pop();
83 
85  int count() const;
86 
87 private:
88  QList<T> m_queue;
89  int m_maxSize;
90  OverflowMode m_mode;
91  mutable QMutex m_mutex;
92  QWaitCondition m_notEmptyCondition;
93  QWaitCondition m_notFullCondition;
94 };
95 
96 template <class T>
98  : m_queue()
99  , m_maxSize(maxSize)
100  , m_mode(mode)
101  , m_mutex(QMutex::NonRecursive)
102  , m_notEmptyCondition()
103  , m_notFullCondition()
104 {
105 }
106 
107 template <class T>
109 {
110 }
111 
112 template <class T>
113 void DataQueue<T>::push(const T& item)
114 {
115  m_mutex.lock();
116  if (m_queue.size() == m_maxSize) {
117  switch(m_mode) {
118  case OverflowModeDiscardOldest:
119  m_queue.removeFirst();
120  m_queue.append(item);
121  break;
122  case OverflowModeDiscardNewest:
123  // This item is the newest so discard it and exit
124  break;
125  case OverflowModeWait:
126  m_notFullCondition.wait(&m_mutex);
127  m_queue.append(item);
128  break;
129  }
130  } else {
131  m_queue.append(item);
132  if (m_queue.size() == 1) {
133  m_notEmptyCondition.wakeOne();
134  }
135  }
136  m_mutex.unlock();
137 }
138 
139 template <class T>
141 {
142  T retVal;
143  m_mutex.lock();
144  if (m_queue.size() == 0) {
145  m_notEmptyCondition.wait(&m_mutex);
146  }
147  retVal = m_queue.takeFirst();
148  if (m_mode == OverflowModeWait && m_queue.size() == m_maxSize - 1) {
149  m_notFullCondition.wakeOne();
150  }
151  m_mutex.unlock();
152  return retVal;
153 }
154 
155 template <class T>
157 {
158  QMutexLocker locker(&m_mutex);
159  return m_queue.size();
160 }
161 
162 #endif // DATAQUEUE_H
DataQueue::DataQueue
DataQueue(int maxSize, OverflowMode mode)
Definition: dataqueue.h:97
DataQueue::OverflowModeWait
@ OverflowModeWait
Wait for space to be free.
Definition: dataqueue.h:54
DataQueue::OverflowModeDiscardNewest
@ OverflowModeDiscardNewest
Discard newest items.
Definition: dataqueue.h:53
DataQueue::push
void push(const T &item)
Definition: dataqueue.h:113
DataQueue::pop
T pop()
Definition: dataqueue.h:140
DataQueue::count
int count() const
Returns the number of items in the queue.
Definition: dataqueue.h:156
DataQueue::~DataQueue
virtual ~DataQueue()
Destructs a DataQueue.
Definition: dataqueue.h:108
DataQueue::OverflowModeDiscardOldest
@ OverflowModeDiscardOldest
Discard oldest items.
Definition: dataqueue.h:52
DataQueue::OverflowMode
OverflowMode
Overflow behavior modes.
Definition: dataqueue.h:51
DataQueue
The DataQueue provides a thread safe container for passing data between objects.
Definition: dataqueue.h:48