summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2017-11-09 12:09:03 +0100
committerThomas Haller <thaller@redhat.com>2017-11-13 11:35:44 +0100
commitd5c9c95e96a376834e7582843cf019fbdba03a7c (patch)
treec5b2fd7fc296e1c6b1226575ef20ef5c0621aa78
parent557d83bf2ddf832828a9b85ffffcad0b7d88272a (diff)
downloadNetworkManager-d5c9c95e96a376834e7582843cf019fbdba03a7c.tar.gz
core: use NM_CONSTCAST() for NM_IP_CONFIG_CAST()
-rw-r--r--src/nm-ip4-config.h70
-rw-r--r--src/tests/test-ip4-config.c2
2 files changed, 45 insertions, 27 deletions
diff --git a/src/nm-ip4-config.h b/src/nm-ip4-config.h
index e7fb2703eb..12cbd6c17b 100644
--- a/src/nm-ip4-config.h
+++ b/src/nm-ip4-config.h
@@ -272,40 +272,56 @@ gboolean nm_ip4_config_equal (const NMIP4Config *a, const NMIP4Config *b);
#include "nm-ip6-config.h"
+static inline gboolean
+NM_IS_IP_CONFIG (gconstpointer config)
+{
+ return NM_IS_IP4_CONFIG (config) || NM_IS_IP6_CONFIG (config);
+}
+
#if _NM_CC_SUPPORT_GENERIC
-#define NM_IP_CONFIG_CAST(config) \
+/* _NM_IS_IP_CONFIG() is a bit unusual. If _Generic() is supported,
+ * it checks whether @config is either NM_IS_IP4_CONFIG() or NM_IS_IP6_CONFIG(),
+ * depending on the pointer type of @config.
+ *
+ * For example, with _Generic() support, the following assertions would fail:
+ * NMIP6Config *ptr = (NMIP6Config *) nm_ip4_config_new(...);
+ * g_assert (_NM_IS_IP_CONFIG (ptr, ptr));
+ * but the following would pass:
+ * NMIP4Config *ptr = nm_ip4_config_new(...);
+ * g_assert (_NM_IS_IP_CONFIG (ptr, ptr));
+ */
+#define _NM_IS_IP_CONFIG(typeexpr, config) \
({ \
const void *const _config = (config); \
- \
- nm_assert (_Generic ((config), \
- const void *: (NM_IS_IP4_CONFIG (_config) || NM_IS_IP6_CONFIG (_config)), \
- void *: (NM_IS_IP4_CONFIG (_config) || NM_IS_IP6_CONFIG (_config)), \
- const NMIPConfig *: (NM_IS_IP4_CONFIG (_config) || NM_IS_IP6_CONFIG (_config)), \
- NMIPConfig *: (NM_IS_IP4_CONFIG (_config) || NM_IS_IP6_CONFIG (_config)), \
- const NMIP4Config *: (NM_IS_IP4_CONFIG (_config)), \
- NMIP4Config *: (NM_IS_IP4_CONFIG (_config)), \
- const NMIP6Config *: (NM_IS_IP6_CONFIG (_config)), \
- NMIP6Config *: (NM_IS_IP6_CONFIG (_config)))); \
- \
- _Generic ((config), \
- const void *: ((const NMIPConfig *) _config), \
- void *: (( NMIPConfig *) _config), \
- const NMIPConfig *: ((const NMIPConfig *) _config), \
- NMIPConfig *: (( NMIPConfig *) _config), \
- const NMIP4Config *: ((const NMIPConfig *) _config), \
- NMIP4Config *: (( NMIPConfig *) _config), \
- const NMIP6Config *: ((const NMIPConfig *) _config), \
- NMIP6Config *: (( NMIPConfig *) _config)); \
+ _Generic ((typeexpr), \
+ const void *const: (NM_IS_IP4_CONFIG (_config) || NM_IS_IP6_CONFIG (_config)), \
+ const void * : (NM_IS_IP4_CONFIG (_config) || NM_IS_IP6_CONFIG (_config)), \
+ void *const: (NM_IS_IP4_CONFIG (_config) || NM_IS_IP6_CONFIG (_config)), \
+ void * : (NM_IS_IP4_CONFIG (_config) || NM_IS_IP6_CONFIG (_config)), \
+ const NMIPConfig *const: (NM_IS_IP4_CONFIG (_config) || NM_IS_IP6_CONFIG (_config)), \
+ const NMIPConfig * : (NM_IS_IP4_CONFIG (_config) || NM_IS_IP6_CONFIG (_config)), \
+ NMIPConfig *const: (NM_IS_IP4_CONFIG (_config) || NM_IS_IP6_CONFIG (_config)), \
+ NMIPConfig * : (NM_IS_IP4_CONFIG (_config) || NM_IS_IP6_CONFIG (_config)), \
+ const NMIP4Config *const: (NM_IS_IP4_CONFIG (_config)), \
+ const NMIP4Config * : (NM_IS_IP4_CONFIG (_config)), \
+ NMIP4Config *const: (NM_IS_IP4_CONFIG (_config)), \
+ NMIP4Config * : (NM_IS_IP4_CONFIG (_config)), \
+ const NMIP6Config *const: (NM_IS_IP6_CONFIG (_config)), \
+ const NMIP6Config * : (NM_IS_IP6_CONFIG (_config)), \
+ NMIP6Config *const: (NM_IS_IP6_CONFIG (_config)), \
+ NMIP6Config * : (NM_IS_IP6_CONFIG (_config))); \
})
#else
-#define NM_IP_CONFIG_CAST(config) ((NMIPConfig *) (config))
+#define _NM_IS_IP_CONFIG(typeexpr, config) NM_IS_IP_CONFIG(config)
#endif
-static inline gboolean
-NM_IS_IP_CONFIG (gconstpointer config)
-{
- return NM_IS_IP4_CONFIG (config) || NM_IS_IP6_CONFIG (config);
-}
+#define NM_IP_CONFIG_CAST(config) \
+ ({ \
+ const void *const _configx = (config); \
+ \
+ nm_assert (!_configx || _NM_IS_IP_CONFIG ((config), _configx)); \
+ NM_CONSTCAST_FULL (NMIPConfig, (config), _configx, NMIP4Config, NMIP6Config); \
+ })
static inline int
nm_ip_config_get_addr_family (const NMIPConfig *config)
diff --git a/src/tests/test-ip4-config.c b/src/tests/test-ip4-config.c
index 649b443f2a..4c3c344e46 100644
--- a/src/tests/test-ip4-config.c
+++ b/src/tests/test-ip4-config.c
@@ -38,6 +38,8 @@ build_test_config (void)
/* Build up the config to subtract */
config = nmtst_ip4_config_new (1);
+ nm_assert (NM_IP_CONFIG_CAST (config));
+
addr = *nmtst_platform_ip4_address ("192.168.1.10", "1.2.3.4", 24);
nm_ip4_config_add_address (config, &addr);