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); /*****************************************************************************/