diff options
author | Marco Trevisan (Treviño) <mail@3v1n0.net> | 2020-09-04 00:44:51 +0200 |
---|---|---|
committer | Marco Trevisan (Treviño) <mail@3v1n0.net> | 2020-09-04 00:52:29 +0200 |
commit | d4356e0dc549c472e304ae5a525c59f30d588f9c (patch) | |
tree | 2d9fad7169427a1012e254d0d6a61673ec5eec56 | |
parent | cf023383ae6a38e26b03fdda8c1bd728d15f2fff (diff) | |
download | gjs-d4356e0dc549c472e304ae5a525c59f30d588f9c.tar.gz |
arg-inl: Add gjs_arg_steal to unset a value and return its value
It can be useful to be used as replacement of g_steal_pointer with
appropriate casting for type
-rw-r--r-- | gi/arg-inl.h | 9 | ||||
-rw-r--r-- | gi/arg.cpp | 11 | ||||
-rw-r--r-- | gi/boxed.cpp | 2 |
3 files changed, 13 insertions, 9 deletions
diff --git a/gi/arg-inl.h b/gi/arg-inl.h index 06330884..852bf904 100644 --- a/gi/arg-inl.h +++ b/gi/arg-inl.h @@ -35,6 +35,8 @@ // gjs_arg_set(GIArgument*, T) - sets the appropriate union member for type T. // gjs_arg_unset<T>(GIArgument*) - sets the appropriate zero value in the // appropriate union member for type T. +// gjs_arg_steal<T>(GIArgument*) - sets the appropriate zero value in the +// appropriate union member for type T and returns the replaced value. template <typename T> [[nodiscard]] inline decltype(auto) gjs_arg_member(GIArgument* arg, @@ -163,6 +165,13 @@ inline void gjs_arg_unset(GIArgument* arg) { gjs_arg_set<T, TAG>(arg, static_cast<T>(0)); } +template <typename T, GITypeTag TAG = GI_TYPE_TAG_VOID> +[[nodiscard]] inline T gjs_arg_steal(GIArgument* arg) { + auto val = gjs_arg_get<T, TAG>(arg); + gjs_arg_unset<T, TAG>(arg); + return val; +} + // Implementation to store rounded (u)int64_t numbers into double template <typename BigT> @@ -3395,17 +3395,13 @@ gjs_g_arg_release_internal(JSContext *context, } else if (g_type_is_a(gtype, G_TYPE_VALUE)) { /* G_TYPE_VALUE is-a G_TYPE_BOXED, but we special case it */ if (g_type_info_is_pointer (type_info)) - g_boxed_free( - gtype, - g_steal_pointer(&gjs_arg_member<void*>(arg))); + g_boxed_free(gtype, gjs_arg_steal<void*>(arg)); else g_clear_pointer(&gjs_arg_member<GValue*>(arg), g_value_unset); } else if (g_type_is_a(gtype, G_TYPE_BOXED)) { if (transfer != TRANSFER_IN_NOTHING) - g_boxed_free( - gtype, - g_steal_pointer(&gjs_arg_member<void*>(arg))); + g_boxed_free(gtype, gjs_arg_steal<void*>(arg)); } else if (g_type_is_a(gtype, G_TYPE_VARIANT)) { if (transfer != TRANSFER_IN_NOTHING) g_clear_pointer(&gjs_arg_member<GVariant*>(arg), @@ -3419,8 +3415,7 @@ gjs_g_arg_release_internal(JSContext *context, if (transfer != TRANSFER_IN_NOTHING) { auto* priv = FundamentalPrototype::for_gtype(context, gtype); - priv->call_unref_function( - g_steal_pointer(&gjs_arg_member<void*>(arg))); + priv->call_unref_function(gjs_arg_steal<void*>(arg)); } } else { gjs_throw(context, "Unhandled GType %s releasing GArgument", diff --git a/gi/boxed.cpp b/gi/boxed.cpp index b1d66a94..e4b68720 100644 --- a/gi/boxed.cpp +++ b/gi/boxed.cpp @@ -376,7 +376,7 @@ bool BoxedInstance::constructor_impl(JSContext* context, JS::HandleObject obj, return false; } - own_ptr(g_steal_pointer(&gjs_arg_member<void*>(&rval_arg))); + own_ptr(gjs_arg_steal<void*>(&rval_arg)); debug_lifecycle("Boxed pointer created from zero-args constructor"); |