From db5946ac2fc349269835b18c37f1df35ac326cda Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 3 May 2023 12:01:14 +0200 Subject: [PATCH] libnm: expose _nm_connection_get_setting_by_metatype() in internal header We have several variants to get the NMSetting from an NMConnection. Some of them are public API (nm_connection_get_setting(), nm_connection_get_setting_by_name()). The most efficient way is lookup by NMMetaSettingType. Expose that as internal API, so it can be used. The NMMetaSettingType is internal, but it exists because it's a very useful enum. Allow others to make use of it. Also, add a static assert which prevents various wrong uses at compile time, for example _nm_connection_get_setting_by_metatype(connection, NM_TYPE_SETTING_CONNECTION) --- src/libnm-core-impl/nm-connection.c | 7 +++++-- src/libnm-core-intern/nm-core-internal.h | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/libnm-core-impl/nm-connection.c b/src/libnm-core-impl/nm-connection.c index d1bc3183f1..c3a51b6e0f 100644 --- a/src/libnm-core-impl/nm-connection.c +++ b/src/libnm-core-impl/nm-connection.c @@ -285,8 +285,11 @@ _get_setting_by_metatype(NMConnectionPrivate *priv, NMMetaSettingType meta_type) return priv->settings[meta_type]; } -static gpointer -_nm_connection_get_setting_by_metatype(NMConnection *connection, NMMetaSettingType meta_type) +/* The "unsafe" part here is that _nm_connection_get_setting_by_metatype() has a compile + * time check that meta_type is valid. With the unsafe variant, the caller must ensure that, + * and we only get an nm_assert() check -- which is basically nothing. */ +gpointer +_nm_connection_get_setting_by_metatype_unsafe(NMConnection *connection, NMMetaSettingType meta_type) { g_return_val_if_fail(NM_IS_CONNECTION(connection), NULL); diff --git a/src/libnm-core-intern/nm-core-internal.h b/src/libnm-core-intern/nm-core-internal.h index 5e1cd3582a..dbb5a7fa59 100644 --- a/src/libnm-core-intern/nm-core-internal.h +++ b/src/libnm-core-intern/nm-core-internal.h @@ -455,6 +455,20 @@ _nm_connection_get_setting(NMConnection *connection, GType type) return (gpointer) nm_connection_get_setting(connection, type); } +gpointer _nm_connection_get_setting_by_metatype_unsafe(NMConnection *connection, + NMMetaSettingType meta_type); + +/* This variant is the most efficient one, because it does not require resolving a + * name/GType first. The NMMetaSettingType enum allows for a direct lookup. */ +#define _nm_connection_get_setting_by_metatype(connection, meta_type) \ + ({ \ + /* Static assert that meta_type is in the valid range. If you don't want that, + * because the argument is no a compile time constant, use _nm_connection_get_setting_by_metatype_unsafe(). */ \ + G_STATIC_ASSERT((meta_type) < _NM_META_SETTING_TYPE_NUM && ((int) meta_type) >= 0); \ + \ + _nm_connection_get_setting_by_metatype_unsafe((connection), (meta_type)); \ + }) + NMSettingIPConfig *nm_connection_get_setting_ip_config(NMConnection *connection, int addr_family); /*****************************************************************************/