summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2018-04-11 14:23:04 +0200
committerThomas Haller <thaller@redhat.com>2018-04-18 07:55:15 +0200
commit458b422468b30b4738eb245c35c150925c8399e9 (patch)
tree9d39a1932d82f50d45dd1ff1dc47a67514d6e883
parenta99d51cb501d4a4022273aeb0622e1a8eaac2657 (diff)
downloadNetworkManager-458b422468b30b4738eb245c35c150925c8399e9.tar.gz
shared: add NM_CAST_STRV_*() helper macros
-rw-r--r--shared/nm-utils/nm-macros-internal.h35
1 files changed, 35 insertions, 0 deletions
diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h
index 2e3940fd76..63d0128739 100644
--- a/shared/nm-utils/nm-macros-internal.h
+++ b/shared/nm-utils/nm-macros-internal.h
@@ -441,6 +441,41 @@ NM_G_ERROR_MSG (GError *error)
#endif
#if _NM_CC_SUPPORT_GENERIC
+/* these macros cast (value) to
+ * - "const char **" (for "MC", mutable-const)
+ * - "const char *const*" (for "CC", const-const)
+ * The point is to do this cast, but only accepting pointers
+ * that are compatible already.
+ *
+ * The problem is, if you add a function like g_strdupv(), the input
+ * argument is not modified (CC), but you want to make it work also
+ * for "char **". C doesn't allow this form of casting (for good reasons),
+ * so the function makes a choice like g_strdupv(char**). That means,
+ * every time you want to call ith with a const argument, you need to
+ * explicitly cast it.
+ *
+ * These macros do the cast, but they only accept a compatible input
+ * type, otherwise they will fail compilation.
+ */
+#define NM_CAST_STRV_MC(value) \
+ (_Generic ((value), \
+ const char * *: (const char * *) (value), \
+ char * *: (const char * *) (value), \
+ void *: (const char * *) (value)))
+#define NM_CAST_STRV_CC(value) \
+ (_Generic ((value), \
+ const char *const*: (const char *const*) (value), \
+ const char * *: (const char *const*) (value), \
+ char *const*: (const char *const*) (value), \
+ char * *: (const char *const*) (value), \
+ const void *: (const char *const*) (value), \
+ void *: (const char *const*) (value)))
+#else
+#define NM_CAST_STRV_MC(value) ((const char * *) (value))
+#define NM_CAST_STRV_CC(value) ((const char *const*) (value))
+#endif
+
+#if _NM_CC_SUPPORT_GENERIC
#define NM_PROPAGATE_CONST(test_expr, ptr) \
(_Generic ((test_expr), \
const typeof (*(test_expr)) *: ((const typeof (*(ptr)) *) (ptr)), \