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