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() IRCMessageQueue::IRCMessageQueue()
{ {
eof = false; m_eof = false;
first = NULL;
last = NULL;
} }
IRCMessageQueue::~IRCMessageQueue() IRCMessageQueue::~IRCMessageQueue()
{ {
while (messageAvailable()) { accessMutex.lock();
IRCMessage * m = getMessage(); while (! m_queue.empty()) {
delete m_queue.front();
delete m; m_queue.pop();
} }
accessMutex.unlock();
} }
bool IRCMessageQueue::isEOF() bool IRCMessageQueue::isEOF()
{ {
return eof; return m_eof;
} }
void IRCMessageQueue::signalEOF() void IRCMessageQueue::signalEOF()
{ {
eof = true; m_eof = true;
} }
bool IRCMessageQueue::messageAvailable() bool IRCMessageQueue::messageAvailable()
{ {
accessMutex.lock(); accessMutex.lock();
bool retv = ! m_queue.empty();
IRCMessageQueueItem *m = first; accessMutex.unlock();
return retv;
accessMutex.unlock();
return (m != NULL);
} }
IRCMessage *IRCMessageQueue::peekFirst()
IRCMessage * IRCMessageQueue::peekFirst()
{ {
accessMutex.lock(); accessMutex.lock();
IRCMessage *msg = m_queue.empty() ? NULL : m_queue.front();
IRCMessageQueueItem * k = first;
accessMutex.unlock(); accessMutex.unlock();
return msg;
if ( k == NULL ) {
return NULL;
}
return k->msg;
} }
IRCMessage *IRCMessageQueue::getMessage()
IRCMessage * IRCMessageQueue::getMessage()
{ {
accessMutex.lock(); accessMutex.lock();
IRCMessage *msg = m_queue.empty() ? NULL : m_queue.front();
IRCMessageQueueItem * k; if (msg)
m_queue.pop();
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;
accessMutex.unlock(); accessMutex.unlock();
return msg; return msg;
} }
void IRCMessageQueue::putMessage(IRCMessage *m)
void IRCMessageQueue::putMessage( IRCMessage * m )
{ {
accessMutex.lock(); 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 This program is distributed in the hope that it will be useful,
{ but WITHOUT ANY WARRANTY; without even the implied warranty of
public: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IRCMessageQueueItem( IRCMessage * m ) { GNU General Public License for more details.
msg = m;
}
~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; #include <mutex>
IRCMessageQueueItem * next; #include <queue>
};
#include "IRCMessage.h"
class IRCMessageQueue class IRCMessageQueue
{ {
public: public:
IRCMessageQueue(); IRCMessageQueue();
~IRCMessageQueue(); ~IRCMessageQueue();
bool isEOF(); bool isEOF();
void signalEOF(); void signalEOF();
bool messageAvailable(); bool messageAvailable();
IRCMessage *getMessage();
IRCMessage * getMessage(); IRCMessage *peekFirst();
void putMessage(IRCMessage *m);
IRCMessage * peekFirst();
void putMessage ( IRCMessage * m );
private: private:
bool m_eof;
bool eof;
IRCMessageQueueItem * first;
IRCMessageQueueItem * last;
std::mutex accessMutex; std::mutex accessMutex;
std::queue<IRCMessage *> m_queue;
}; };

Loading…
Cancel
Save

Powered by TurnKey Linux.