voxelman/containers/voxelman_unbounded_queue.cpp

93 lines
1.6 KiB
C++
Raw Normal View History

#include "voxelman_unbounded_queue.h"
/// VMQUeue
2020-01-09 04:29:05 +01:00
template <typename T>
void VMUBQueue<T>::enqueue(T obj) {
2020-01-09 04:29:05 +01:00
_enqueue_lock->write_lock();
2020-01-09 04:29:05 +01:00
UBQueueNode<T> *n = memnew(UBQueueNode<T>(obj));
2020-01-09 04:29:05 +01:00
_tail->next = n;
_tail = n;
2020-01-09 04:29:05 +01:00
_enqueue_lock->write_unlock();
}
2020-01-09 04:29:05 +01:00
template <typename T>
T VMUBQueue<T>::dequeue() {
2020-01-09 04:29:05 +01:00
T result;
2020-01-09 04:29:05 +01:00
_dequeue_lock->write_lock();
2020-01-09 04:29:05 +01:00
if (_head->next == NULL) {
return result;
}
2020-01-09 04:29:05 +01:00
result = _head->next->data;
2020-01-09 04:29:05 +01:00
UBQueueNode<T> *h = _head;
2020-01-09 04:29:05 +01:00
_head = _head->next;
2020-01-09 04:29:05 +01:00
memdelete(h);
2020-01-09 04:29:05 +01:00
_dequeue_lock->write_unlock();
return result;
}
2020-01-09 04:29:05 +01:00
template <typename T>
T VMUBQueue<T>::peek() {
2020-01-09 04:29:05 +01:00
return _head->data;
}
2020-01-09 04:29:05 +01:00
template <typename T>
VMUBQueue<T>::VMUBQueue() {
2020-01-09 04:29:05 +01:00
_head = memnew(UBQueueNode<T>);
_tail = _head;
2020-01-09 04:29:05 +01:00
_enqueue_lock = RWLock::create();
_dequeue_lock = RWLock::create();
}
2020-01-09 04:29:05 +01:00
template <typename T>
VMUBQueue<T>::~VMUBQueue() {
2020-01-09 04:29:05 +01:00
memdelete(_enqueue_lock);
memdelete(_dequeue_lock);
2020-01-09 04:29:05 +01:00
UBQueueNode<T> *h = _head;
2020-01-09 04:29:05 +01:00
while (h) {
UBQueueNode<T> *n = h->next;
2020-01-09 04:29:05 +01:00
memdelete(h);
2020-01-09 04:29:05 +01:00
h = n;
}
}
///VoxelmanUnboundedQueue
void VoxelmanUnboundedQueue::enqueue(Variant obj) {
2020-01-09 04:29:05 +01:00
_queue.enqueue(obj);
}
Variant VoxelmanUnboundedQueue::dequeue() {
2020-01-09 04:29:05 +01:00
return _queue.dequeue();
}
Variant VoxelmanUnboundedQueue::peek() {
2020-01-09 04:29:05 +01:00
return _queue.peek();
}
VoxelmanUnboundedQueue::VoxelmanUnboundedQueue() {
}
VoxelmanUnboundedQueue::~VoxelmanUnboundedQueue() {
}
void VoxelmanUnboundedQueue::_bind_methods() {
2020-01-09 04:29:05 +01:00
ClassDB::bind_method(D_METHOD("enqueue", "obj"), &VoxelmanUnboundedQueue::enqueue);
ClassDB::bind_method(D_METHOD("dequeue"), &VoxelmanUnboundedQueue::dequeue);
ClassDB::bind_method(D_METHOD("peek"), &VoxelmanUnboundedQueue::peek);
}