2021-05-27 21:10:11 +08:00
|
|
|
#ifndef DATAQUEUE_H
|
|
|
|
#define DATAQUEUE_H
|
|
|
|
#include <QMutex>
|
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QList>
|
|
|
|
#include "libsearch_global.h"
|
2021-12-14 14:43:35 +08:00
|
|
|
namespace UkuiSearch {
|
2021-05-27 21:10:11 +08:00
|
|
|
// TODO I want a unlocked queue
|
|
|
|
template <typename T>
|
2021-12-14 14:58:46 +08:00
|
|
|
class LIBSEARCH_EXPORT DataQueue : protected QList<T>
|
2021-05-27 21:10:11 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
inline void enqueue(const T &t) {
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
QList<T>::append(t);
|
|
|
|
}
|
|
|
|
inline T dequeue() {
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
return QList<T>::takeFirst();
|
|
|
|
}
|
2021-08-06 10:54:48 +08:00
|
|
|
inline void clear() {
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
QList<T>::clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
inline bool isEmpty() {
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
return QList<T>::isEmpty();
|
|
|
|
}
|
2023-01-21 11:37:39 +08:00
|
|
|
inline T tryDequeue() {
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
if(QList<T>::isEmpty()) {
|
|
|
|
return T();
|
|
|
|
} else {
|
|
|
|
return QList<T>::takeFirst();
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 21:10:11 +08:00
|
|
|
private:
|
|
|
|
QMutex m_mutex;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // DATAQUEUE_H
|