From ad06497b4bb8fb7e88e66638951a93eb4838a47a Mon Sep 17 00:00:00 2001 From: Albert S Date: Sat, 6 Aug 2022 09:03:05 +0200 Subject: [PATCH] shared: Add LimitQueue which discards oldest entry once limit hit --- shared/limitqueue.h | 48 +++++++++++++++++++++++++++++++++++++++++++++ shared/shared.pro | 1 + 2 files changed, 49 insertions(+) create mode 100644 shared/limitqueue.h diff --git a/shared/limitqueue.h b/shared/limitqueue.h new file mode 100644 index 0000000..28ad7a9 --- /dev/null +++ b/shared/limitqueue.h @@ -0,0 +1,48 @@ +#ifndef LIMITQUEUE_H +#define LIMITQUEUE_H +#include + +template class LimitQueue +{ + protected: + QQueue queue; + unsigned int limit = 0; + + public: + LimitQueue(); + LimitQueue(unsigned int limit) + { + this->limit = limit; + } + + void enqueue(const T &t) + { + if(queue.size() == limit) + { + queue.dequeue(); + } + queue.enqueue(t); + } + + int size() + { + return queue.size(); + } + + T dequeue() + { + return queue.dequeue(); + } + + void setLimit(unsigned int limit) + { + this->limit = limit; + } + + void clear() + { + queue.clear(); + } +}; + +#endif // LIMITQUEUE_H diff --git a/shared/shared.pro b/shared/shared.pro index 3e887b6..2139c9f 100644 --- a/shared/shared.pro +++ b/shared/shared.pro @@ -77,6 +77,7 @@ HEADERS += sqlitesearch.h \ filescanworker.h \ indexer.h \ indexsyncer.h \ + limitqueue.h \ logger.h \ looqsgeneralexception.h \ looqsquery.h \