summaryrefslogtreecommitdiff
path: root/glib/garray.h
diff options
context:
space:
mode:
authorMarco Trevisan (Treviño) <mail@3v1n0.net>2022-12-14 02:42:55 +0100
committerMarco Trevisan (Treviño) <mail@3v1n0.net>2022-12-16 18:13:28 +0100
commitab621e15b52a57c8d95b3f4d93493c82f0f3216e (patch)
tree530765b34e22912347c35a8efc9c13bd1f48d424 /glib/garray.h
parent34618aea70d5582bf7be6a9de710e6438caa7308 (diff)
downloadglib-ab621e15b52a57c8d95b3f4d93493c82f0f3216e.tar.gz
garray: Add support adding literal values
GArray's g_array_append_val(), g_array_prened_val() and g_array_insert_val() macros required an user to use literals to add a new value. This could be inconvenient at times, but it's possible to avoid this with recent compilers, in fact in case glib_typeof is defined we can take advantage of it, to initialize a temporary variable to store the literal value and pass its address to the actual function.
Diffstat (limited to 'glib/garray.h')
-rw-r--r--glib/garray.h33
1 files changed, 30 insertions, 3 deletions
diff --git a/glib/garray.h b/glib/garray.h
index 2300e5f58..5b9c6752a 100644
--- a/glib/garray.h
+++ b/glib/garray.h
@@ -32,6 +32,7 @@
#endif
#include <glib/gtypes.h>
+#include <glib/glib-typeof.h>
G_BEGIN_DECLS
@@ -63,9 +64,35 @@ struct _GPtrArray
* order by moving the last element to the position of the removed.
*/
-#define g_array_append_val(a,v) g_array_append_vals (a, &(v), 1)
-#define g_array_prepend_val(a,v) g_array_prepend_vals (a, &(v), 1)
-#define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &(v), 1)
+#if defined (glib_typeof) && GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_76
+ #define g_array_append_val(a, v) \
+ (G_GNUC_EXTENSION ({ \
+ glib_typeof ((v)) gaa_val_ = (v); \
+ g_array_append_vals ((a), &gaa_val_, 1); \
+ }))
+
+ #define g_array_prepend_val(a, v) \
+ (G_GNUC_EXTENSION ({ \
+ glib_typeof ((v)) gap_val_ = (v); \
+ g_array_prepend_vals ((a), &gap_val_, 1); \
+ }))
+
+ #define g_array_insert_val(a, i, v) \
+ (G_GNUC_EXTENSION ({ \
+ glib_typeof ((v)) gai_val_ = (v); \
+ g_array_insert_vals ((a), (i), &gai_val_, 1); \
+ }))
+#else /* !defined (glib_typeof) || GLIB_VERSION_MIN_REQUIRED < GLIB_VERSION_2_76 */
+ #define g_array_append_val(a, v) \
+ g_array_append_vals ((a), &((v)), 1)
+
+ #define g_array_prepend_val(a, v) \
+ g_array_prepend_vals ((a), &((v)), 1)
+
+ #define g_array_insert_val(a, i, v) \
+ g_array_insert_vals ((a), (i), &((v)), 1)
+#endif
+
#define g_array_index(a,t,i) (((t*) (void *) (a)->data) [(i)])
GLIB_AVAILABLE_IN_ALL