summaryrefslogtreecommitdiffstats
path: root/libmpv/qthelper.hpp
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-12-30 22:24:57 +0100
committerwm4 <wm4@nowhere>2014-12-30 22:24:57 +0100
commit50c40b6dedcaf35bd2af68a3535b023c871d1295 (patch)
tree851e86a604f3d00dc100a44cd93911658c32613f /libmpv/qthelper.hpp
parent63a414c708ea2986386639124fdfa9cdbd58e520 (diff)
downloadmpv-50c40b6dedcaf35bd2af68a3535b023c871d1295.tar.bz2
mpv-50c40b6dedcaf35bd2af68a3535b023c871d1295.tar.xz
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).
Diffstat (limited to 'libmpv/qthelper.hpp')
-rw-r--r--libmpv/qthelper.hpp30
1 files changed, 30 insertions, 0 deletions
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 <QString>
#include <QList>
#include <QHash>
+#include <QSharedPointer>
#include <mpv/client.h>
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<container> 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<container>(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) {