libnm: cleanup implementations for nm_meta_setting_infos_by_gtype()

The file "nm-meta-setting-base-impl.c" is present twice in our source
tree and compiled twice. Once with internal headers of libnm-core and
once with only public headers.

Consequently, there are two implementations for
nm_meta_setting_infos_by_gtype(), one that can benefit from internal
access to the data structure, and the one for libnmc, which cannot.

Refactor the implementations, in the hope to have it clearer.
This commit is contained in:
Thomas Haller 2022-09-06 11:29:20 +02:00
parent 28fa48aee4
commit 5f4eb5eed5
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 62 additions and 52 deletions

View file

@ -703,19 +703,21 @@ nm_meta_setting_infos_by_name(const char *name)
return idx >= 0 ? &nm_meta_setting_infos[idx] : NULL;
}
const NMMetaSettingInfo *
nm_meta_setting_infos_by_gtype(GType gtype)
{
/*****************************************************************************/
#if _NM_META_SETTING_BASE_IMPL_LIBNM
static const NMMetaSettingInfo *
_infos_by_gtype_from_class(GType gtype)
{
nm_auto_unref_gtypeclass GTypeClass *gtypeclass_unref = NULL;
GTypeClass *gtypeclass;
NMSettingClass *klass;
if (!g_type_is_a(gtype, NM_TYPE_SETTING))
goto out_none;
return NULL;
gtypeclass = g_type_class_peek(gtype);
if (!gtypeclass)
if (G_UNLIKELY(!gtypeclass))
gtypeclass = gtypeclass_unref = g_type_class_ref(gtype);
nm_assert(NM_IS_SETTING_CLASS(gtypeclass));
@ -723,38 +725,41 @@ nm_meta_setting_infos_by_gtype(GType gtype)
klass = (NMSettingClass *) gtypeclass;
if (!klass->setting_info)
goto out_none;
return NULL;
nm_assert(klass->setting_info->get_setting_gtype);
nm_assert(klass->setting_info->get_setting_gtype() == gtype);
return klass->setting_info;
}
#endif
out_none:
static const NMMetaSettingInfo *
_infos_by_gtype_search(GType gtype)
{
int i;
if (NM_MORE_ASSERTS > 10) {
int i;
/* this might hint to a bug, but it would be expected for NM_TYPE_SETTING
* and NM_TYPE_SETTING_IP_CONFIG.
*
* Assert that we didn't lookup for a gtype, which we would expect to find.
* An assertion failure here, hints to a bug in nm_setting_*_class_init().
*/
for (i = 0; i < _NM_META_SETTING_TYPE_NUM; i++)
nm_assert(nm_meta_setting_infos[i].get_setting_gtype() != gtype);
}
return NULL;
#else
guint i;
for (i = 0; i < _NM_META_SETTING_TYPE_NUM; i++) {
for (i = 0; i < (int) _NM_META_SETTING_TYPE_NUM; i++) {
if (nm_meta_setting_infos[i].get_setting_gtype() == gtype)
return &nm_meta_setting_infos[i];
}
return NULL;
}
const NMMetaSettingInfo *
nm_meta_setting_infos_by_gtype(GType gtype)
{
const NMMetaSettingInfo *setting_info;
#if _NM_META_SETTING_BASE_IMPL_LIBNM
setting_info = _infos_by_gtype_from_class(gtype);
if (NM_MORE_ASSERTS > 20)
nm_assert(setting_info == _infos_by_gtype_search(gtype));
#else
setting_info = _infos_by_gtype_search(gtype);
#endif
return setting_info;
}
/*****************************************************************************/

View file

@ -703,19 +703,21 @@ nm_meta_setting_infos_by_name(const char *name)
return idx >= 0 ? &nm_meta_setting_infos[idx] : NULL;
}
const NMMetaSettingInfo *
nm_meta_setting_infos_by_gtype(GType gtype)
{
/*****************************************************************************/
#if _NM_META_SETTING_BASE_IMPL_LIBNM
static const NMMetaSettingInfo *
_infos_by_gtype_from_class(GType gtype)
{
nm_auto_unref_gtypeclass GTypeClass *gtypeclass_unref = NULL;
GTypeClass *gtypeclass;
NMSettingClass *klass;
if (!g_type_is_a(gtype, NM_TYPE_SETTING))
goto out_none;
return NULL;
gtypeclass = g_type_class_peek(gtype);
if (!gtypeclass)
if (G_UNLIKELY(!gtypeclass))
gtypeclass = gtypeclass_unref = g_type_class_ref(gtype);
nm_assert(NM_IS_SETTING_CLASS(gtypeclass));
@ -723,38 +725,41 @@ nm_meta_setting_infos_by_gtype(GType gtype)
klass = (NMSettingClass *) gtypeclass;
if (!klass->setting_info)
goto out_none;
return NULL;
nm_assert(klass->setting_info->get_setting_gtype);
nm_assert(klass->setting_info->get_setting_gtype() == gtype);
return klass->setting_info;
}
#endif
out_none:
static const NMMetaSettingInfo *
_infos_by_gtype_search(GType gtype)
{
int i;
if (NM_MORE_ASSERTS > 10) {
int i;
/* this might hint to a bug, but it would be expected for NM_TYPE_SETTING
* and NM_TYPE_SETTING_IP_CONFIG.
*
* Assert that we didn't lookup for a gtype, which we would expect to find.
* An assertion failure here, hints to a bug in nm_setting_*_class_init().
*/
for (i = 0; i < _NM_META_SETTING_TYPE_NUM; i++)
nm_assert(nm_meta_setting_infos[i].get_setting_gtype() != gtype);
}
return NULL;
#else
guint i;
for (i = 0; i < _NM_META_SETTING_TYPE_NUM; i++) {
for (i = 0; i < (int) _NM_META_SETTING_TYPE_NUM; i++) {
if (nm_meta_setting_infos[i].get_setting_gtype() == gtype)
return &nm_meta_setting_infos[i];
}
return NULL;
}
const NMMetaSettingInfo *
nm_meta_setting_infos_by_gtype(GType gtype)
{
const NMMetaSettingInfo *setting_info;
#if _NM_META_SETTING_BASE_IMPL_LIBNM
setting_info = _infos_by_gtype_from_class(gtype);
if (NM_MORE_ASSERTS > 20)
nm_assert(setting_info == _infos_by_gtype_search(gtype));
#else
setting_info = _infos_by_gtype_search(gtype);
#endif
return setting_info;
}
/*****************************************************************************/