CMessageQueue now uses #include <queue> container

pull/14/head
Tom Early 6 years ago
parent d4bfa810e5
commit 71fd26bfe6

@ -1,114 +1,86 @@
#include "IRCMessageQueue.h"
/*
CIRCDDB - ircDDB client library in C++
Based on code by:
Copyright (C) 2010 Michael Dirska, DL1BFF (dl1bff@mdx.de)
Completely rewritten by:
Copyright (c) 2017 by Thomas A. Early N7TAE
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "IRCMessageQueue.h"
IRCMessageQueue::IRCMessageQueue()
{
eof = false;
first = NULL;
last = NULL;
m_eof = false;
}
IRCMessageQueue::~IRCMessageQueue()
{
while (messageAvailable()) {
IRCMessage * m = getMessage();
delete m;
accessMutex.lock();
while (! m_queue.empty()) {
delete m_queue.front();
m_queue.pop();
}
accessMutex.unlock();
}
bool IRCMessageQueue::isEOF()
{
return eof;
return m_eof;
}
void IRCMessageQueue::signalEOF()
{
eof = true;
m_eof = true;
}
bool IRCMessageQueue::messageAvailable()
{
accessMutex.lock();
IRCMessageQueueItem *m = first;
accessMutex.unlock();
return (m != NULL);
accessMutex.lock();
bool retv = ! m_queue.empty();
accessMutex.unlock();
return retv;
}
IRCMessage * IRCMessageQueue::peekFirst()
IRCMessage *IRCMessageQueue::peekFirst()
{
accessMutex.lock();
IRCMessageQueueItem * k = first;
IRCMessage *msg = m_queue.empty() ? NULL : m_queue.front();
accessMutex.unlock();
if ( k == NULL ) {
return NULL;
}
return k->msg;
return msg;
}
IRCMessage * IRCMessageQueue::getMessage()
IRCMessage *IRCMessageQueue::getMessage()
{
accessMutex.lock();
IRCMessageQueueItem * k;
if (first == NULL) {
return NULL;
}
k = first;
first = k -> next;
if (k -> next == NULL) {
last = NULL;
} else {
k -> next -> prev = NULL;
}
IRCMessage * msg = k -> msg;
delete k;
IRCMessage *msg = m_queue.empty() ? NULL : m_queue.front();
if (msg)
m_queue.pop();
accessMutex.unlock();
return msg;
}
void IRCMessageQueue::putMessage( IRCMessage * m )
void IRCMessageQueue::putMessage(IRCMessage *m)
{
accessMutex.lock();
m_queue.push(m);
accessMutex.unlock();
}
//printf("IRCMessageQueue::putMessage\n");
IRCMessageQueueItem * k = new IRCMessageQueueItem(m);
k -> prev = last;
k -> next = NULL;
if (last == NULL) {
first = k;
} else {
last -> next = k;
}
last = k;
accessMutex.unlock();
}

@ -1,53 +1,49 @@
#pragma once
/*
CIRCDDB - ircDDB client library in C++
#include <mutex>
Based on original code by:
Copyright (C) 2010 Michael Dirska, DL1BFF (dl1bff@mdx.de)
#include "IRCMessage.h"
Completely rewritten by:
Copyright (c) 2017 by Thomas A. Early N7TAE
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
class IRCMessageQueueItem
{
public:
IRCMessageQueueItem( IRCMessage * m ) {
msg = m;
}
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
~IRCMessageQueueItem() {
}
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
IRCMessage * msg;
#pragma once
IRCMessageQueueItem * prev;
IRCMessageQueueItem * next;
};
#include <mutex>
#include <queue>
#include "IRCMessage.h"
class IRCMessageQueue
{
public:
IRCMessageQueue();
~IRCMessageQueue();
bool isEOF();
void signalEOF();
bool messageAvailable();
IRCMessage * getMessage();
IRCMessage * peekFirst();
void putMessage ( IRCMessage * m );
IRCMessage *getMessage();
IRCMessage *peekFirst();
void putMessage(IRCMessage *m);
private:
bool eof;
IRCMessageQueueItem * first;
IRCMessageQueueItem * last;
bool m_eof;
std::mutex accessMutex;
std::queue<IRCMessage *> m_queue;
};

Loading…
Cancel
Save

Powered by TurnKey Linux.