From 49bbb2d7735cc74c76db58ac9ecfd54346481d23 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Fri, 27 Aug 2021 15:11:33 +0200
Subject: [PATCH] LIB: Revert commit 4cdb4e9daaba4bfdaf28552344f3d6aff49884d3

Revert the commit because the std::function was not appy and a bad_call
was thrown when attempting to delete the unique_ptr...
---
 src/Lib/AbstractMediaContext.hh | 32 ++++++++++++++++++++------------
 src/Lib/Audio.hh                |  3 ---
 src/Lib/Utils.hh                | 16 ----------------
 src/Lib/Video.hh                |  5 -----
 4 files changed, 20 insertions(+), 36 deletions(-)

diff --git a/src/Lib/AbstractMediaContext.hh b/src/Lib/AbstractMediaContext.hh
index 35ada2ed..1e113c45 100644
--- a/src/Lib/AbstractMediaContext.hh
+++ b/src/Lib/AbstractMediaContext.hh
@@ -17,20 +17,28 @@ extern "C" {
 
 namespace Vivy
 {
-static inline Utils::DeleterFunctionType<double> dataDeleter =
-    std::bind_front(Utils::freePtrIfNotNull<double>, av_free);
-
-static inline Utils::DeleterFunctionType<AVCodecContext> codecContexteleter =
-    std::bind_front(Utils::freePPtrIfNotNull<AVCodecContext>, avcodec_free_context);
-
-static inline Utils::DeleterFunctionType<AVFrame> avFrameDeleter =
-    std::bind_front(Utils::freePPtrIfNotNull<AVFrame>, av_frame_free);
+static inline constexpr auto codecContexteleter = [](AVCodecContext *ptr) noexcept -> void {
+    if (ptr)
+        avcodec_free_context(&ptr);
+};
+static constexpr inline auto dataDeleter = [](double *ptr) noexcept -> void {
+    if (ptr)
+        av_free(ptr);
+};
+static constexpr inline auto avFrameDeleter = [](AVFrame *ptr) noexcept -> void {
+    if (ptr)
+        av_frame_free(&ptr);
+};
 
-static inline Utils::DeleterFunctionType<SwrContext> swrContenxtDeleter =
-    std::bind_front(Utils::freePPtrIfNotNull<SwrContext>, swr_free);
+static constexpr inline auto swrContenxtDeleter = [](SwrContext *swr) noexcept -> void {
+    if (swr)
+        swr_free(&swr);
+};
 
-static inline Utils::DeleterFunctionType<AVFormatContext> avFormatContextDeleter =
-    std::bind_front(Utils::freePtrIfNotNull<AVFormatContext>, avformat_free_context);
+static inline constexpr auto avFormatContextDeleter = [](AVFormatContext *ptr) noexcept -> void {
+    if (ptr)
+        avformat_free_context(ptr);
+};
 
 using AVFormatContextPtr = std::unique_ptr<AVFormatContext, decltype(avFormatContextDeleter)>;
 using AVCodecContextPtr  = std::unique_ptr<AVCodecContext, decltype(codecContexteleter)>;
diff --git a/src/Lib/Audio.hh b/src/Lib/Audio.hh
index a87e3dc2..36cc5519 100644
--- a/src/Lib/Audio.hh
+++ b/src/Lib/Audio.hh
@@ -68,9 +68,6 @@ public:
 
 private:
     // Regarding the format
-    static inline Utils::DeleterFunctionType<AVFormatContext> avFormatContextDeleter =
-        std::bind_front(Utils::freePtrIfNotNull<AVFormatContext>, avformat_free_context);
-
     using AVFormatContextPtr = std::unique_ptr<AVFormatContext, decltype(avFormatContextDeleter)>;
     AVFormatContextPtr format{ avformat_alloc_context(), avFormatContextDeleter };
 };
diff --git a/src/Lib/Utils.hh b/src/Lib/Utils.hh
index 97b0771f..f121cdc4 100644
--- a/src/Lib/Utils.hh
+++ b/src/Lib/Utils.hh
@@ -162,22 +162,6 @@ QString getBaseName(const QString &) noexcept;
 
 void writeAssertLocation(const char *msg);
 
-template <typename T> using DeleterFunctionType = const std::function<void(T *)>;
-
-template <typename Type> static inline void
-freePtrIfNotNull(DeleterFunctionType<Type> callback, Type *ptr) noexcept
-{
-    if (ptr != nullptr)
-        callback(ptr);
-}
-
-template <typename Type> static inline void
-freePPtrIfNotNull(DeleterFunctionType<Type *> callback, Type *ptr) noexcept
-{
-    if (ptr != nullptr)
-        callback(&ptr);
-}
-
 struct OsSpecificAspects final {
 private:
     OsSpecificAspects() {}
diff --git a/src/Lib/Video.hh b/src/Lib/Video.hh
index 2d3c6054..400feb30 100644
--- a/src/Lib/Video.hh
+++ b/src/Lib/Video.hh
@@ -23,11 +23,6 @@ public:
     double getFramesPerSecond() const noexcept;
 
     QJsonObject getProperties() const noexcept override;
-
-    static inline Utils::DeleterFunctionType<AVCodecContext> codecContexteleter =
-        std::bind_front(Utils::freePPtrIfNotNull<AVCodecContext>, avcodec_free_context);
-
-private:
 };
 
 // Like an audio context, but for videos.
-- 
GitLab