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)

(cherry picked from commit db5946ac2f)
(cherry picked from commit 50b6c2d622)
This commit is contained in:
Thomas Haller 2023-05-03 12:01:14 +02:00 committed by Fernando Fernandez Mancera
parent 58fd65c37e
commit 1cd4f675c8
2 changed files with 19 additions and 2 deletions

View file

@ -311,8 +311,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);

View file

@ -479,6 +479,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);
/*****************************************************************************/