From 50c40b6dedcaf35bd2af68a3535b023c871d1295 Mon Sep 17 00:00:00 2001 From: wm4 Date: Tue, 30 Dec 2014 22:24:57 +0100 Subject: client API: qthelper: add a refcounting wrapper around mpv_handle This is useful to deal with crazy Qt object lifetime issues (the following commit needs it). --- DOCS/client-api-changes.rst | 1 + libmpv/client.h | 2 +- libmpv/qthelper.hpp | 30 ++++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/DOCS/client-api-changes.rst b/DOCS/client-api-changes.rst index 69458d0d49..04af0c9438 100644 --- a/DOCS/client-api-changes.rst +++ b/DOCS/client-api-changes.rst @@ -25,6 +25,7 @@ API changes :: + 1.12 - add class Handle to qthelper.hpp 1.11 - add OpenGL rendering interop API - allows an application to combine its own and mpv's OpenGL rendering Warning: this API is not stable yet - anything in opengl_cb.h might diff --git a/libmpv/client.h b/libmpv/client.h index 344bfdd9d5..88009b79ca 100644 --- a/libmpv/client.h +++ b/libmpv/client.h @@ -167,7 +167,7 @@ extern "C" { * relational operators (<, >, <=, >=). */ #define MPV_MAKE_VERSION(major, minor) (((major) << 16) | (minor) | 0UL) -#define MPV_CLIENT_API_VERSION MPV_MAKE_VERSION(1, 11) +#define MPV_CLIENT_API_VERSION MPV_MAKE_VERSION(1, 12) /** * Return the MPV_CLIENT_API_VERSION the mpv source has been compiled with. diff --git a/libmpv/qthelper.hpp b/libmpv/qthelper.hpp index 1b0ff47a0b..c36a7d7924 100644 --- a/libmpv/qthelper.hpp +++ b/libmpv/qthelper.hpp @@ -25,12 +25,42 @@ #include #include #include +#include #include namespace mpv { namespace qt { +// Wrapper around mpv_handle. Does refcounting under the hood. +class Handle +{ + struct container { + container(mpv_handle *h) : mpv(h) {} + ~container() { mpv_terminate_destroy(mpv); } + mpv_handle *mpv; + }; + QSharedPointer sptr; +public: + // Construct a new Handle from a raw mpv_handle with refcount 1. If the + // last Handle goes out of scope, the mpv_handle will be destroyed with + // mpv_terminate_destroy(). + // Never destroy the mpv_handle manually when using this wrapper. You + // will create dangling pointers. Just let the wrapper take care of + // destroying the mpv_handle. + // Never create multiple wrappers from the same raw mpv_handle; copy the + // wrapper instead (that's what it's for). + static Handle FromRawHandle(mpv_handle *handle) { + Handle h; + h.sptr = QSharedPointer(new container(handle)); + return h; + } + + // Return the raw handle; for use with the libmpv C API. + operator mpv_handle*() { return (*sptr).mpv; } + operator mpv_handle*() const { return (*sptr).mpv; } +}; + static inline QVariant node_to_variant(const mpv_node *node) { switch (node->format) { -- cgit v1.2.3