diff options
author | Thomas Haller <thaller@redhat.com> | 2018-11-29 09:35:15 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2018-12-03 12:28:45 +0100 |
commit | c54a2ed82f5efc7cfe1e91d1580980d634cfcd10 (patch) | |
tree | cabb3528e84a8a0975893122c7ea05e69be9569a | |
parent | 8b87cd9ddf47c81dc1210247b44668f30748bc98 (diff) |
shared: add nm_strv_ptrarray_*() helpers
These are simple macros/functions that append a heap-allocated string to
a GPtrArray. It is intended for a GPtrArray which takes ownership of the
strings (meaning, it has a g_free() GDestroyNotify).
-rw-r--r-- | shared/nm-utils/nm-shared-utils.h | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h index 940a193f7..e82b53f7c 100644 --- a/shared/nm-utils/nm-shared-utils.h +++ b/shared/nm-utils/nm-shared-utils.h @@ -296,6 +296,37 @@ nm_memdup (gconstpointer data, gsize size) /*****************************************************************************/ +/* generic macro to convert an int to a (heap allocated) string. + * + * Usually, an inline function nm_strdup_int64() would be enough. However, + * that cannot be used for guint64. So, we would also need nm_strdup_uint64(). + * This causes suble error potential, because the caller needs to ensure to + * use the right one (and compiler isn't going to help as it silently casts). + * + * Instead, this generic macro is supposed to handle all integers correctly. */ +#if _NM_CC_SUPPORT_GENERIC +#define nm_strdup_int(val) \ + _Generic ((val), \ + gint8: g_strdup_printf ("%d", (int) (val)), \ + gint16: g_strdup_printf ("%d", (int) (val)), \ + gint32: g_strdup_printf ("%d", (int) (val)), \ + gint64: g_strdup_printf ("%"G_GINT64_FORMAT, (gint64) (val)), \ + \ + guint8: g_strdup_printf ("%u", (guint) (val)), \ + guint16: g_strdup_printf ("%u", (guint) (val)), \ + guint32: g_strdup_printf ("%u", (guint) (val)), \ + guint64: g_strdup_printf ("%"G_GUINT64_FORMAT, (guint64) (val)) \ + ) +#else +#define nm_strdup_int(val) \ + ( ( sizeof (val) == sizeof (guint64) \ + && ((typeof (val)) -1) > 0) \ + ? g_strdup_printf ("%"G_GUINT64_FORMAT, (guint64) (val)) \ + : g_strdup_printf ("%"G_GINT64_FORMAT, (gint64) (val))) +#endif + +/*****************************************************************************/ + extern const void *const _NM_PTRARRAY_EMPTY[1]; #define NM_PTRARRAY_EMPTY(type) ((type const*) _NM_PTRARRAY_EMPTY) @@ -959,4 +990,44 @@ void nm_utils_invoke_on_idle (NMUtilsInvokeOnIdleCallback callback, gpointer callback_user_data, GCancellable *cancellable); +/*****************************************************************************/ + +static inline void +nm_strv_ptrarray_add_string_take (GPtrArray *cmd, + char *str) +{ + nm_assert (cmd); + nm_assert (str); + + g_ptr_array_add (cmd, str); +} + +static inline void +nm_strv_ptrarray_add_string_dup (GPtrArray *cmd, + const char *str) +{ + nm_strv_ptrarray_add_string_take (cmd, + g_strdup (str)); +} + +#define nm_strv_ptrarray_add_string_concat(cmd, ...) \ + nm_strv_ptrarray_add_string_take ((cmd), g_strconcat (__VA_ARGS__, NULL)) + +#define nm_strv_ptrarray_add_string_printf(cmd, ...) \ + nm_strv_ptrarray_add_string_take ((cmd), g_strdup_printf (__VA_ARGS__)) + +#define nm_strv_ptrarray_add_int(cmd, val) \ + nm_strv_ptrarray_add_string_take ((cmd), nm_strdup_int (val)) + +static inline void +nm_strv_ptrarray_take_gstring (GPtrArray *cmd, + GString **gstr) +{ + nm_assert (gstr && *gstr); + + nm_strv_ptrarray_add_string_take (cmd, + g_string_free (g_steal_pointer (gstr), + FALSE)); +} + #endif /* __NM_SHARED_UTILS_H__ */ |