[gjs: 1/3] arg-inl: Add gjs_arg_steal to unset a value and return its value




commit d4356e0dc549c472e304ae5a525c59f30d588f9c
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Fri Sep 4 00:44:51 2020 +0200

    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

 gi/arg-inl.h |  9 +++++++++
 gi/arg.cpp   | 11 +++--------
 gi/boxed.cpp |  2 +-
 3 files changed, 13 insertions(+), 9 deletions(-)
---
diff --git a/gi/arg-inl.h b/gi/arg-inl.h
index 063308841..852bf904c 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>
diff --git a/gi/arg.cpp b/gi/arg.cpp
index 5caf59097..ded17c4fc 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -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 b1d66a94c..e4b687204 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");
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]