summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Lortie <desrt@desrt.ca>2015-02-06 11:38:26 (GMT)
committerRyan Lortie <desrt@desrt.ca>2015-02-06 14:14:57 (GMT)
commite668796c5a90a19bce0ff893794817af6aad4dc2 (patch)
tree8b83d9ba3c4b7bc0b2c819fee7b8100ed5a957b5
parentb5538416c065bafe760220e92754f891abd254b2 (diff)
downloadglib-e668796c5a90a19bce0ff893794817af6aad4dc2.zip
glib-e668796c5a90a19bce0ff893794817af6aad4dc2.tar.xz
Add new API g_steal_pointer()
This is particularly nice when used with g_autoptr(). See examples in the docs. This patch is based upon an idea (and original patch submission) from Will Manley <will@williammanley.net>. https://bugzilla.gnome.org/show_bug.cgi?id=742456
-rw-r--r--glib/gmem.h71
1 files changed, 71 insertions, 0 deletions
diff --git a/glib/gmem.h b/glib/gmem.h
index 5dcc3f8..82507b5 100644
--- a/glib/gmem.h
+++ b/glib/gmem.h
@@ -125,6 +125,77 @@ gpointer g_try_realloc_n (gpointer mem,
} \
} G_STMT_END
+/**
+ * g_steal_pointer:
+ * @pp: a pointer to a pointer
+ *
+ * Sets @pp to %NULL, returning the value that was there before.
+ *
+ * Conceptually, this transfers the ownership of the pointer from the
+ * referenced variable to the "caller" of the macro (ie: "steals" the
+ * reference).
+ *
+ * The return value will be properly typed, according to the type of
+ * @pp.
+ *
+ * This can be very useful when combined with g_autoptr() to prevent the
+ * return value of a function from being automatically freed. Consider
+ * the following example (which only works on GCC and clang):
+ *
+ * |[
+ * GObject *
+ * create_object (void)
+ * {
+ * g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
+ *
+ * if (early_error_case)
+ * return NULL;
+ *
+ * return g_steal_pointer (&obj);
+ * }
+ * ]|
+ *
+ * It can also be used in similar ways for 'out' parameters and is
+ * particularly useful for dealing with optional out parameters:
+ *
+ * |[
+ * gboolean
+ * get_object (GObject **obj_out)
+ * {
+ * g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
+ *
+ * if (early_error_case)
+ * return FALSE;
+ *
+ * if (obj_out)
+ * *obj_out = g_steal_pointer (&obj);
+ *
+ * return TRUE;
+ * }
+ * ]|
+ *
+ * In the above example, the object will be automatically freed in the
+ * early error case and also in the case that %NULL was given for
+ * @obj_out.
+ *
+ * Since: 2.44
+ */
+static inline gpointer
+(g_steal_pointer) (gpointer pp)
+{
+ gpointer *ptr = pp;
+ gpointer ref;
+
+ ref = *ptr;
+ *ptr = NULL;
+
+ return ref;
+}
+
+/* type safety */
+#define g_steal_pointer(pp) \
+ (0 ? (*(pp)) : (g_steal_pointer) (pp))
+
/* Optimise: avoid the call to the (slower) _n function if we can
* determine at compile-time that no overflow happens.
*/