From 1d9d7ca15c934ceb366cd1b19ed7f223974ba6e5 Mon Sep 17 00:00:00 2001
From: Andrews Sobral <andrewssobral@users.noreply.github.com>
Date: Tue, 6 Aug 2019 18:10:51 +0200
Subject: [PATCH] use smart pointers (sprint 1) (#157)

---
 Demo2.cpp                |  4 ++--
 Main.cpp                 |  5 ++---
 VideoAnalysis.cpp        | 12 +++---------
 VideoAnalysis.h          |  4 ++--
 VideoCapture.cpp         |  2 +-
 VideoCapture.h           |  5 +++--
 package_bgs/IBGS.h       |  2 --
 package_bgs/bgslibrary.h |  2 +-
 8 files changed, 14 insertions(+), 22 deletions(-)

diff --git a/Demo2.cpp b/Demo2.cpp
index ad95e73..5e3b6c0 100644
--- a/Demo2.cpp
+++ b/Demo2.cpp
@@ -41,10 +41,10 @@ int main(int argc, char **argv)
       frame_counter++;
       std::stringstream ss;
       ss << frame_counter;
-      std::string fileName = "dataset/frames/" + ss.str() + ".png";
+      auto fileName = "dataset/frames/" + ss.str() + ".png";
       std::cout << "reading " << fileName << std::endl;
 
-      cv::Mat img_input = cv::imread(fileName, CV_LOAD_IMAGE_COLOR);
+      auto img_input = cv::imread(fileName, CV_LOAD_IMAGE_COLOR);
 
       if (img_input.empty())
         break;
diff --git a/Main.cpp b/Main.cpp
index e28eb04..94bf8dd 100644
--- a/Main.cpp
+++ b/Main.cpp
@@ -38,11 +38,11 @@ namespace bgslibrary
 
       try
       {
-        int key = KEY_ESC;
+        auto key = KEY_ESC;
 
         do
         {
-          VideoAnalysis* videoAnalysis = new VideoAnalysis;
+          auto videoAnalysis = std::make_unique<VideoAnalysis>();
 
           if (videoAnalysis->setup(argc, argv))
           {
@@ -56,7 +56,6 @@ namespace bgslibrary
           }
 
           cv::destroyAllWindows();
-          delete videoAnalysis;
 
         } while (key == KEY_REPEAT);
       }
diff --git a/VideoAnalysis.cpp b/VideoAnalysis.cpp
index 03e47b3..9ec0754 100644
--- a/VideoAnalysis.cpp
+++ b/VideoAnalysis.cpp
@@ -132,8 +132,8 @@ namespace bgslibrary
 
     do
     {
-      videoCapture = new VideoCapture;
-      frameProcessor = new FrameProcessor;
+      videoCapture = std::make_unique<VideoCapture>();
+      frameProcessor = std::make_shared<FrameProcessor>();
 
       frameProcessor->init();
       frameProcessor->frameToStop = frameToStop;
@@ -154,16 +154,10 @@ namespace bgslibrary
 
       frameProcessor->finish();
 
-      int key = cvWaitKey(500);
+      auto key = cv::waitKey(500);
       if (key == KEY_ESC)
         break;
 
-      delete frameProcessor;
-      delete videoCapture;
-
     } while (1);
-
-    delete frameProcessor;
-    delete videoCapture;
   }
 }
diff --git a/VideoAnalysis.h b/VideoAnalysis.h
index f548a7b..b41d9ba 100644
--- a/VideoAnalysis.h
+++ b/VideoAnalysis.h
@@ -27,8 +27,8 @@ namespace bgslibrary
   class VideoAnalysis
   {
   private:
-    VideoCapture* videoCapture;
-    FrameProcessor* frameProcessor;
+    std::unique_ptr<VideoCapture> videoCapture;
+    std::shared_ptr<FrameProcessor> frameProcessor;
     bool use_file;
     std::string filename;
     bool use_camera;
diff --git a/VideoCapture.cpp b/VideoCapture.cpp
index ed8964d..6c3a8f2 100644
--- a/VideoCapture.cpp
+++ b/VideoCapture.cpp
@@ -90,7 +90,7 @@ namespace bgslibrary
     std::cout << "~VideoCapture()" << std::endl;
   }
 
-  void VideoCapture::setFrameProcessor(IFrameProcessor* frameProcessorPtr)
+  void VideoCapture::setFrameProcessor(std::shared_ptr<IFrameProcessor> frameProcessorPtr)
   {
     frameProcessor = frameProcessorPtr;
   }
diff --git a/VideoCapture.h b/VideoCapture.h
index 8b7cd63..a4872c3 100644
--- a/VideoCapture.h
+++ b/VideoCapture.h
@@ -18,6 +18,7 @@ along with BGSLibrary.  If not, see <http://www.gnu.org/licenses/>.
 
 #include <iostream>
 #include <fstream>
+#include <memory>
 //#include <chrono>
 //#include <thread>
 #include <opencv2/opencv.hpp>
@@ -32,7 +33,7 @@ namespace bgslibrary
   class VideoCapture
   {
   private:
-    IFrameProcessor* frameProcessor;
+    std::shared_ptr<IFrameProcessor> frameProcessor;
     cv::VideoCapture capture;
     cv::Mat frame;
     int key;
@@ -65,7 +66,7 @@ namespace bgslibrary
     VideoCapture();
     ~VideoCapture();
 
-    void setFrameProcessor(IFrameProcessor* frameProcessorPtr);
+    void setFrameProcessor(std::shared_ptr<IFrameProcessor> frameProcessorPtr);
     void setCamera(int cameraIndex);
     void setVideo(std::string filename);
     void start();
diff --git a/package_bgs/IBGS.h b/package_bgs/IBGS.h
index 67a0568..f08e4ee 100644
--- a/package_bgs/IBGS.h
+++ b/package_bgs/IBGS.h
@@ -56,8 +56,6 @@ namespace bgslibrary
       }
       virtual void process(const cv::Mat &img_input, cv::Mat &img_foreground, cv::Mat &img_background) = 0;
       virtual ~IBGS() {}
-      //static IBGS* create(const std::string alg_name);
-      //static std::list<std::string> get_algs_name();
     protected:
       bool firstTime = true;
       bool showOutput = true;
diff --git a/package_bgs/bgslibrary.h b/package_bgs/bgslibrary.h
index be45236..f97f162 100644
--- a/package_bgs/bgslibrary.h
+++ b/package_bgs/bgslibrary.h
@@ -22,7 +22,7 @@ along with BGSLibrary.  If not, see <http://www.gnu.org/licenses/>.
 #include "StaticFrameDifference.h"
 #include "WeightedMovingMean.h"
 #include "WeightedMovingVariance.h"
-#include "MixtureOfGaussianV1.h" // Only for OpenCV >= 2
+#include "MixtureOfGaussianV1.h" // Only for OpenCV == 2
 #include "MixtureOfGaussianV2.h"
 #include "AdaptiveBackgroundLearning.h"
 #include "AdaptiveSelectiveBackgroundLearning.h"
-- 
GitLab