Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 4cdb4e9d rédigé par Kubat's avatar Kubat
Parcourir les fichiers

LIB: Add a way to do the `if (ptr) free(ptr)` in a generic way

parent af777881
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!18Implement the VivyDocument specification
...@@ -37,24 +37,17 @@ public: ...@@ -37,24 +37,17 @@ public:
class Stream final { class Stream final {
VIVY_UNMOVABLE_OBJECT(Stream) VIVY_UNMOVABLE_OBJECT(Stream)
// All the needed deleters static inline Utils::DeleterFunctionType<double> dataDeleter =
static inline constexpr auto codecContexteleter = [](AVCodecContext *ptr) noexcept -> void { std::bind_front(Utils::freePtrIfNotNull<double>, av_free);
if (ptr)
avcodec_free_context(&ptr); static inline Utils::DeleterFunctionType<AVCodecContext> codecContexteleter =
}; std::bind_front(Utils::freePPtrIfNotNull<AVCodecContext>, avcodec_free_context);
static constexpr inline auto dataDeleter = [](double *ptr) noexcept -> void {
if (ptr) static inline Utils::DeleterFunctionType<AVFrame> avFrameDeleter =
av_free(ptr); std::bind_front(Utils::freePPtrIfNotNull<AVFrame>, av_frame_free);
};
static constexpr inline auto avFrameDeleter = [](AVFrame *ptr) noexcept -> void { static inline Utils::DeleterFunctionType<SwrContext> swrContenxtDeleter =
if (ptr) std::bind_front(Utils::freePPtrIfNotNull<SwrContext>, swr_free);
av_frame_free(&ptr);
};
static constexpr inline auto swrContenxtDeleter = [](SwrContext *swr) noexcept -> void {
if (swr)
swr_free(&swr);
};
// All the used types // All the used types
using AVCodecContextPtr = std::unique_ptr<AVCodecContext, decltype(codecContexteleter)>; using AVCodecContextPtr = std::unique_ptr<AVCodecContext, decltype(codecContexteleter)>;
...@@ -135,11 +128,9 @@ public: ...@@ -135,11 +128,9 @@ public:
private: private:
// Regarding the format // Regarding the format
static inline constexpr auto avFormatContextDeleter = static inline Utils::DeleterFunctionType<AVFormatContext> avFormatContextDeleter =
[](AVFormatContext *ptr) noexcept -> void { std::bind_front(Utils::freePtrIfNotNull<AVFormatContext>, avformat_free_context);
if (ptr)
avformat_free_context(ptr);
};
using AVFormatContextPtr = std::unique_ptr<AVFormatContext, decltype(avFormatContextDeleter)>; using AVFormatContextPtr = std::unique_ptr<AVFormatContext, decltype(avFormatContextDeleter)>;
AVFormatContextPtr format{ avformat_alloc_context(), avFormatContextDeleter }; AVFormatContextPtr format{ avformat_alloc_context(), avFormatContextDeleter };
......
...@@ -172,6 +172,22 @@ QString getBaseName(const QString &) noexcept; ...@@ -172,6 +172,22 @@ QString getBaseName(const QString &) noexcept;
void writeAssertLocation(const char *msg); 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 { struct OsSpecificAspects final {
private: private:
OsSpecificAspects() {} OsSpecificAspects() {}
......
  • Kubat @martin2018

    mentioned in commit 49bbb2d7

    ·

    mentioned in commit 49bbb2d7

    Afficher/masquer la liste des validations
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter