From 03fcf0cc187810ea65e6e49b2b96ef182fc7b018 Mon Sep 17 00:00:00 2001 From: 30hours Date: Tue, 10 Sep 2024 13:12:59 +0000 Subject: [PATCH] Use smart pointers for detector/tracker --- src/blah2.cpp | 18 ++++++++---------- src/process/detection/Centroid.cpp | 6 ++---- src/process/detection/Centroid.h | 3 ++- src/process/detection/CfarDetector1D.cpp | 6 ++---- src/process/detection/CfarDetector1D.h | 3 ++- src/process/detection/Interpolate.cpp | 6 ++---- src/process/detection/Interpolate.h | 4 +++- src/process/tracker/Tracker.cpp | 4 ++-- src/process/tracker/Tracker.h | 4 +++- 9 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/blah2.cpp b/src/blah2.cpp index e984f76..75cd7e5 100644 --- a/src/blah2.cpp +++ b/src/blah2.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include Capture *CAPTURE_POINTER = NULL; @@ -102,10 +103,10 @@ int main(int argc, char **argv) IqData *x = new IqData(nSamples); IqData *y = new IqData(nSamples); Map> *map; - Detection *detection; - Detection *detection1; - Detection *detection2; - Track *track; + std::unique_ptr detection; + std::unique_ptr detection1; + std::unique_ptr detection2; + std::unique_ptr track; // setup fftw multithread if (fftw_init_threads() == 0) @@ -265,15 +266,15 @@ int main(int argc, char **argv) if (isDetection) { detection1 = cfarDetector1D->process(map); - detection2 = centroid->process(detection1); - detection = interpolate->process(detection2, map); + detection2 = centroid->process(detection1.get()); + detection = interpolate->process(detection2.get(), map); timing_helper(timing_name, timing_time, time, "detector"); } // tracker process if (isTracker) { - track = tracker->process(detection, time[0]/1000); + track = tracker->process(detection.get(), time[0]/1000); timing_helper(timing_name, timing_time, time, "tracker"); } @@ -296,9 +297,6 @@ int main(int argc, char **argv) detectionJson = detection->to_json(time[0]/1000); detectionJson = detection->delay_bin_to_km(detectionJson, fs); socket_detection.sendData(detectionJson); - delete detection; - delete detection1; - delete detection2; } // output tracker data diff --git a/src/process/detection/Centroid.cpp b/src/process/detection/Centroid.cpp index e780ca3..865c48e 100644 --- a/src/process/detection/Centroid.cpp +++ b/src/process/detection/Centroid.cpp @@ -16,7 +16,7 @@ Centroid::~Centroid() { } -Detection *Centroid::process(Detection *x) +std::unique_ptr Centroid::process(Detection *x) { // store detections temporarily std::vector delay, doppler, snr; @@ -69,7 +69,5 @@ Detection *Centroid::process(Detection *x) } // create detection - Detection *detection = new Detection(delay2, doppler2, snr2); - - return detection; + return std::make_unique(delay2, doppler2, snr2); } diff --git a/src/process/detection/Centroid.h b/src/process/detection/Centroid.h index fa9b7c7..aacb530 100644 --- a/src/process/detection/Centroid.h +++ b/src/process/detection/Centroid.h @@ -9,6 +9,7 @@ #include "data/Detection.h" #include +#include class Centroid { @@ -40,7 +41,7 @@ public: /// @brief Implement the 1D CFAR detector. /// @param x Detections from the 1D CFAR detector. /// @return Centroided detections. - Detection *process(Detection *x); + std::unique_ptr process(Detection *x); }; #endif diff --git a/src/process/detection/CfarDetector1D.cpp b/src/process/detection/CfarDetector1D.cpp index 7758e8e..d46910c 100644 --- a/src/process/detection/CfarDetector1D.cpp +++ b/src/process/detection/CfarDetector1D.cpp @@ -20,7 +20,7 @@ CfarDetector1D::~CfarDetector1D() { } -Detection *CfarDetector1D::process(Map> *x) +std::unique_ptr CfarDetector1D::process(Map> *x) { int32_t nDelayBins = x->get_nCols(); int32_t nDopplerBins = x->get_nRows(); @@ -96,7 +96,5 @@ Detection *CfarDetector1D::process(Map> *x) } // create detection - Detection *detection = new Detection(delay, doppler, snr); - - return detection; + return std::make_unique(delay, doppler, snr); } diff --git a/src/process/detection/CfarDetector1D.h b/src/process/detection/CfarDetector1D.h index 9d39693..01df49d 100644 --- a/src/process/detection/CfarDetector1D.h +++ b/src/process/detection/CfarDetector1D.h @@ -12,6 +12,7 @@ #include "data/Detection.h" #include #include +#include class CfarDetector1D { @@ -51,7 +52,7 @@ public: /// @brief Implement the 1D CFAR detector. /// @param x Ambiguity map data of IQ samples. /// @return Detections from the 1D CFAR detector. - Detection *process(Map> *x); + std::unique_ptr process(Map> *x); }; #endif diff --git a/src/process/detection/Interpolate.cpp b/src/process/detection/Interpolate.cpp index b807089..23918e9 100644 --- a/src/process/detection/Interpolate.cpp +++ b/src/process/detection/Interpolate.cpp @@ -17,7 +17,7 @@ Interpolate::~Interpolate() { } -Detection *Interpolate::process(Detection *x, Map> *y) +std::unique_ptr Interpolate::process(Detection *x, Map> *y) { // store detections temporarily std::vector delay, doppler, snr; @@ -87,7 +87,5 @@ Detection *Interpolate::process(Detection *x, Map> *y) } // create detection - Detection *detection = new Detection(delay2, doppler2, snr2); - - return detection; + return std::make_unique(delay2, doppler2, snr2); } diff --git a/src/process/detection/Interpolate.h b/src/process/detection/Interpolate.h index c1758a2..0072ad5 100644 --- a/src/process/detection/Interpolate.h +++ b/src/process/detection/Interpolate.h @@ -14,6 +14,8 @@ #include "data/Map.h" #include "data/Detection.h" +#include + class Interpolate { private: @@ -40,7 +42,7 @@ public: /// @brief Implement the 1D CFAR detector. /// @param x Detections from the 1D CFAR detector. /// @return Interpolated detections. - Detection *process(Detection *x, Map> *y); + std::unique_ptr process(Detection *x, Map> *y); }; #endif diff --git a/src/process/tracker/Tracker.cpp b/src/process/tracker/Tracker.cpp index daedd20..992df71 100644 --- a/src/process/tracker/Tracker.cpp +++ b/src/process/tracker/Tracker.cpp @@ -28,7 +28,7 @@ Tracker::~Tracker() { } -Track *Tracker::process(Detection *detection, uint64_t currentTime) +std::unique_ptr Tracker::process(Detection *detection, uint64_t currentTime) { doNotInitiate.clear(); for (size_t i = 0; i < detection->get_nDetections(); i++) @@ -46,7 +46,7 @@ Track *Tracker::process(Detection *detection, uint64_t currentTime) } initiate(detection); - return &track; + return std::make_unique(track); } void Tracker::update(Detection *detection, uint64_t current) diff --git a/src/process/tracker/Tracker.h b/src/process/tracker/Tracker.h index 0bc8961..bfd6663 100644 --- a/src/process/tracker/Tracker.h +++ b/src/process/tracker/Tracker.h @@ -13,7 +13,9 @@ #include "data/Detection.h" #include "data/Track.h" + #include +#include class Tracker { @@ -72,7 +74,7 @@ public: /// @param detection Detection data for last CPI. /// @param timestamp POSIX timestamp (ms). /// @return Pointer to track data. - Track *process(Detection *detection, uint64_t timestamp); + std::unique_ptr process(Detection *detection, uint64_t timestamp); /// @brief Update tracks by associating detections. /// @param detection Detection data for last CPI.