[buffer] working circular buffer
This commit is contained in:
parent
0c78fd96a0
commit
e2b1f44997
@ -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<double>(load)/static_cast<double>(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<size_t>(current_write_index) < (buffer.size() - 1) ? current_write_index += 1 : current_write_index = 0;
|
||||
buffer[static_cast<size_t>(current_write_index)] = img;
|
||||
if (load < static_cast<int64_t>(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<size_t>(current_read_index)];
|
||||
static_cast<size_t>(current_read_index) < (buffer.capacity() - 1) ? current_read_index += 1 : current_read_index = 0;
|
||||
load -= 1;
|
||||
}
|
||||
mutex.unlock();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,38 @@
|
||||
#ifndef IMAGEBUFFER_H
|
||||
#define IMAGEBUFFER_H
|
||||
|
||||
#include <QMutex>
|
||||
#include <QObject>
|
||||
#include <QWaitCondition>
|
||||
#include <myimage.h>
|
||||
|
||||
|
||||
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<MyImage> buffer;
|
||||
int64_t current_write_index = -1;
|
||||
int64_t current_read_index = 0;
|
||||
int64_t load = 0;
|
||||
size_t buffer_size;
|
||||
|
||||
signals:
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user