From e2b1f449971db46a752e6c963d4735660433be45 Mon Sep 17 00:00:00 2001 From: Jan Grewe Date: Thu, 12 Mar 2020 17:47:53 +0100 Subject: [PATCH] [buffer] working circular buffer --- imagebuffer.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++++-- imagebuffer.h | 26 ++++++++++++++++++- 2 files changed, 91 insertions(+), 3 deletions(-) diff --git a/imagebuffer.cpp b/imagebuffer.cpp index 9c792b5..6e2632f 100644 --- a/imagebuffer.cpp +++ b/imagebuffer.cpp @@ -1,6 +1,70 @@ #include "imagebuffer.h" -ImageBuffer::ImageBuffer(QObject *parent) : QObject(parent) -{ +ImageBuffer::ImageBuffer(size_t buffer_size, QObject *parent) : QObject(parent), buffer_size(buffer_size) { + buffer.resize(buffer_size); +} + +void ImageBuffer::clear() { + resize(buffer_size); +} + +size_t ImageBuffer::capacity() { + return buffer.capacity(); +} + +double ImageBuffer::bufferPreassure() { + double preassure; + mutex.lock(); + preassure = static_cast(load)/static_cast(buffer.capacity()); + mutex.unlock(); + return preassure * 100; +} + +int64_t ImageBuffer::bufferLoad() { + int64_t l; + mutex.lock(); + l = load; + mutex.unlock(); + return l; +} + +void ImageBuffer::resize(size_t new_size) { + mutex.lock(); + buffer_size = new_size; + buffer.clear(); + buffer.resize(new_size); + current_read_index = -1; + current_write_index = -1; + load = 0; + mutex.unlock(); +} + +void ImageBuffer::push(const MyImage &img) { + mutex.lock(); + static_cast(current_write_index) < (buffer.size() - 1) ? current_write_index += 1 : current_write_index = 0; + buffer[static_cast(current_write_index)] = img; + if (load < static_cast(buffer.capacity())) + load += 1; + mutex.unlock(); +} + +bool ImageBuffer::bufferNotEmpty() { + bool res; + mutex.lock(); + res = load > 0; + mutex.unlock(); + return res; +} +bool ImageBuffer::pop(MyImage &img) { + bool ret = false; + mutex.lock(); + if (load > 0) { + img = buffer[static_cast(current_read_index)]; + static_cast(current_read_index) < (buffer.capacity() - 1) ? current_read_index += 1 : current_read_index = 0; + load -= 1; + } + mutex.unlock(); + return ret; } + diff --git a/imagebuffer.h b/imagebuffer.h index bfc6056..1d5e708 100644 --- a/imagebuffer.h +++ b/imagebuffer.h @@ -1,14 +1,38 @@ #ifndef IMAGEBUFFER_H #define IMAGEBUFFER_H +#include #include +#include +#include class ImageBuffer : public QObject { Q_OBJECT public: - explicit ImageBuffer(QObject *parent = nullptr); + explicit ImageBuffer(size_t buffer_size=100, QObject *parent = nullptr); + + void clear(); + void resize(size_t new_size); + void push(const MyImage &img); + bool pop(MyImage &img); + size_t capacity(); + double bufferPreassure(); + int64_t bufferLoad(); + bool bufferNotEmpty(); + +private: + // QWaitCondition bufferNotEmpty; // signals that the consumer can get data + // QWaitCondition bufferNotFull; // signals that the producer can store data + QMutex mutex; + int numUsedBytes = 0; + + std::vector buffer; + int64_t current_write_index = -1; + int64_t current_read_index = 0; + int64_t load = 0; + size_t buffer_size; signals: