From e6dedbcc230f6382875bbb5f78a918dc204e8baa Mon Sep 17 00:00:00 2001 From: wm4 Date: Mon, 26 Sep 2016 16:45:55 +0200 Subject: qthelper: introduce new convenience functions (Why the heck is the C++ helper not in a separate repository?) --- libmpv/qthelper.hpp | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) (limited to 'libmpv') diff --git a/libmpv/qthelper.hpp b/libmpv/qthelper.hpp index 70b3baffc4..8615d0f84b 100644 --- a/libmpv/qthelper.hpp +++ b/libmpv/qthelper.hpp @@ -29,6 +29,7 @@ #include #include #include +#include #include @@ -227,6 +228,8 @@ struct node_autofree { * Return the given property as mpv_node converted to QVariant, or QVariant() * on error. * + * @deprecated use get_property() instead + * * @param name the property name */ static inline QVariant get_property_variant(mpv_handle *ctx, const QString &name) @@ -240,6 +243,8 @@ static inline QVariant get_property_variant(mpv_handle *ctx, const QString &name /** * Set the given property as mpv_node converted from the QVariant argument. + + * @deprecated use set_property() instead */ static inline int set_property_variant(mpv_handle *ctx, const QString &name, const QVariant &v) @@ -250,6 +255,8 @@ static inline int set_property_variant(mpv_handle *ctx, const QString &name, /** * Set the given option as mpv_node converted from the QVariant argument. + * + * @deprecated use set_property() instead */ static inline int set_option_variant(mpv_handle *ctx, const QString &name, const QVariant &v) @@ -261,6 +268,8 @@ static inline int set_option_variant(mpv_handle *ctx, const QString &name, /** * mpv_command_node() equivalent. Returns QVariant() on error (and * unfortunately, the same on success). + * + * @deprecated use command() instead */ static inline QVariant command_variant(mpv_handle *ctx, const QVariant &args) { @@ -272,7 +281,94 @@ static inline QVariant command_variant(mpv_handle *ctx, const QVariant &args) return node_to_variant(&res); } +/** + * This is used to return error codes wrapped in QVariant for functions which + * return QVariant. + * + * You can use get_error() or is_error() to extract the error status from a + * QVariant value. + */ +struct ErrorReturn +{ + /** + * enum mpv_error value (or a value outside of it if ABI was extended) + */ + int error; + + ErrorReturn() : error(0) {} + explicit ErrorReturn(int err) : error(err) {} +}; + +/** + * Return the mpv error code packed into a QVariant, or 0 (success) if it's not + * an error value. + * + * @return error code (<0) or success (>=0) + */ +static inline int get_error(const QVariant &v) +{ + if (!v.canConvert()) + return 0; + return v.value().error; } + +/** + * Return whether the QVariant carries a mpv error code. + */ +static inline bool is_error(const QVariant &v) +{ + return get_error(v) < 0; } +/** + * Return the given property as mpv_node converted to QVariant, or QVariant() + * on error. + * + * @param name the property name + * @return the property value, or an ErrorReturn with the error code + */ +static inline QVariant get_property(mpv_handle *ctx, const QString &name) +{ + mpv_node node; + int err = mpv_get_property(ctx, name.toUtf8().data(), MPV_FORMAT_NODE, &node); + if (err < 0) + return QVariant::fromValue(ErrorReturn(err)); + node_autofree f(&node); + return node_to_variant(&node); +} + +/** + * Set the given property as mpv_node converted from the QVariant argument. + * + * @return mpv error code (<0 on error, >= 0 on success) + */ +static inline int set_property(mpv_handle *ctx, const QString &name, + const QVariant &v) +{ + node_builder node(v); + return mpv_set_property(ctx, name.toUtf8().data(), MPV_FORMAT_NODE, node.node()); +} + +/** + * mpv_command_node() equivalent. + * + * @param args command arguments, with args[0] being the command name as string + * @return the property value, or an ErrorReturn with the error code + */ +static inline QVariant command(mpv_handle *ctx, const QVariant &args) +{ + node_builder node(args); + mpv_node res; + int err = mpv_command_node(ctx, node.node(), &res); + if (err < 0) + return QVariant::fromValue(ErrorReturn(err)); + node_autofree f(&res); + return node_to_variant(&res); +} + +} +} + +Q_DECLARE_METATYPE(mpv::qt::ErrorReturn) + #endif -- cgit v1.2.3