From 506bb988795fc3e72b3c176617f4cb6568b3672f Mon Sep 17 00:00:00 2001
From: Jan Grewe <jan.grewe@g-node.org>
Date: Fri, 13 Mar 2020 11:58:34 +0100
Subject: [PATCH] [buffer] add method to read last image

---
 imagebuffer.cpp | 18 +++++++++++++++++-
 imagebuffer.h   |  1 +
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/imagebuffer.cpp b/imagebuffer.cpp
index da01b3f..2af6bfb 100644
--- a/imagebuffer.cpp
+++ b/imagebuffer.cpp
@@ -46,7 +46,7 @@ void ImageBuffer::push(const MyImage &img) {
   if (load < static_cast<int64_t>(buffer.capacity()))
     load += 1;
   mutex.unlock();
-  std::cerr << "load: " << bufferLoad() << "\t pressure: " << bufferPreassure() << std::endl;
+  //std::cerr << "load: " << bufferLoad() << "\t pressure: " << bufferPreassure() << std::endl;
 }
 
 bool ImageBuffer::bufferNotEmpty() {
@@ -69,3 +69,19 @@ bool ImageBuffer::pop(MyImage &img) {
   return ret;
 }
 
+bool ImageBuffer::readLast(MyImage &img) {
+  bool ret = false;
+  mutex.lock();
+  if (load > 0) {
+    size_t idx;
+    if (current_write_index != 0) {
+      idx = static_cast<size_t>(current_write_index) - 1;
+    } else {
+      idx = buffer.capacity() -1;
+    }
+    img = buffer[idx];
+    ret = true;
+  }
+  mutex.unlock();
+  return ret;
+}
diff --git a/imagebuffer.h b/imagebuffer.h
index 1d5e708..8b272a3 100644
--- a/imagebuffer.h
+++ b/imagebuffer.h
@@ -17,6 +17,7 @@ public:
   void resize(size_t new_size);
   void push(const MyImage &img);
   bool pop(MyImage &img);
+  bool readLast(MyImage &img);
   size_t capacity();
   double bufferPreassure();
   int64_t bufferLoad();