SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
messagequeue.cpp
Go to the documentation of this file.
1 // Copyright (C) 2015 F4EXB //
3 // written by Edouard Griffiths //
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 as 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 V3 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/>. //
18 
19 #include <QDebug>
20 #include <QMutexLocker>
21 #include "util/messagequeue.h"
22 #include "util/message.h"
23 
24 MessageQueue::MessageQueue(QObject* parent) :
25  QObject(parent),
26  m_lock(QMutex::Recursive),
27  m_queue()
28 {
29 }
30 
32 {
33  Message* message;
34 
35  while ((message = pop()) != 0)
36  {
37  qDebug() << "MessageQueue::~MessageQueue: message: " << message->getIdentifier() << " was still in queue";
38  delete message;
39  }
40 }
41 
42 void MessageQueue::push(Message* message, bool emitSignal)
43 {
44  if (message)
45  {
46  m_lock.lock();
47  m_queue.append(message);
48  m_lock.unlock();
49  }
50 
51  if (emitSignal)
52  {
53  emit messageEnqueued();
54  }
55 }
56 
58 {
59  QMutexLocker locker(&m_lock);
60 
61  if (m_queue.isEmpty())
62  {
63  return 0;
64  }
65  else
66  {
67  return m_queue.takeFirst();
68  }
69 }
70 
72 {
73  QMutexLocker locker(&m_lock);
74 
75  return m_queue.size();
76 }
77 
79 {
80  QMutexLocker locker(&m_lock);
81 
82  while (!m_queue.isEmpty()) {
83  delete m_queue.takeFirst();
84  }
85 }
Message * pop()
Pop message from queue.
void push(Message *message, bool emitSignal=true)
Push message onto queue.
int size()
Returns queue size.
void clear()
Empty queue.
QQueue< Message * > m_queue
Definition: messagequeue.h:47
MessageQueue(QObject *parent=NULL)
void messageEnqueued()
virtual const char * getIdentifier() const
Definition: message.cpp:35
QMutex m_lock
Definition: messagequeue.h:46