libnm: add direct_offset for uint32 properties

And as example, implement NMSettingVrf.table this way. This also
makes all properties of NMSettingVrf implemened as "direct" properties,
and we can drop the explicit getter/setters.
This commit is contained in:
Thomas Haller 2021-06-29 07:57:41 +02:00
parent 27621dde45
commit 3c801ec4f3
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
4 changed files with 122 additions and 46 deletions

View file

@ -300,6 +300,7 @@ extern const NMSettInfoPropertType nm_sett_info_propert_type_plain_i;
extern const NMSettInfoPropertType nm_sett_info_propert_type_plain_u;
extern const NMSettInfoPropertType nm_sett_info_propert_type_direct_boolean;
extern const NMSettInfoPropertType nm_sett_info_propert_type_direct_uint32;
extern const NMSettInfoPropertType nm_sett_info_propert_type_direct_string;
NMSettingVerifyResult
@ -496,6 +497,51 @@ _nm_properties_override(GArray *properties_override, const NMSettInfoProperty *p
/*****************************************************************************/
#define _nm_setting_property_define_direct_uint32(properties_override, \
obj_properties, \
prop_name, \
prop_id, \
min_value, \
max_value, \
default_value, \
param_flags, \
private_struct_type, \
private_struct_field, \
... /* extra NMSettInfoProperty fields */) \
G_STMT_START \
{ \
GParamSpec *_param_spec; \
\
G_STATIC_ASSERT( \
!NM_FLAGS_ANY((param_flags), \
~(NM_SETTING_PARAM_FUZZY_IGNORE | NM_SETTING_PARAM_INFERRABLE))); \
G_STATIC_ASSERT((min_value) <= (guint64) G_MAXUINT32); \
G_STATIC_ASSERT((max_value) <= (guint64) G_MAXUINT32); \
G_STATIC_ASSERT((default_value) <= (guint64) G_MAXUINT32); \
\
_param_spec = \
g_param_spec_uint("" prop_name "", \
"", \
"", \
(min_value), \
(max_value), \
(default_value), \
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | (param_flags)); \
\
(obj_properties)[(prop_id)] = _param_spec; \
\
_nm_properties_override_gobj( \
(properties_override), \
_param_spec, \
&nm_sett_info_propert_type_direct_uint32, \
.direct_offset = \
NM_STRUCT_OFFSET_ENSURE_TYPE(guint32, private_struct_type, private_struct_field), \
__VA_ARGS__); \
} \
G_STMT_END
/*****************************************************************************/
#define _nm_setting_property_define_direct_string_full(properties_override, \
obj_properties, \
prop_name, \

View file

@ -80,38 +80,6 @@ verify(NMSetting *setting, NMConnection *connection, GError **error)
/*****************************************************************************/
static void
get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
NMSettingVrf *self = NM_SETTING_VRF(object);
switch (prop_id) {
case PROP_TABLE:
g_value_set_uint(value, self->table);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
NMSettingVrf *self = NM_SETTING_VRF(object);
switch (prop_id) {
case PROP_TABLE:
self->table = g_value_get_uint(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
/*****************************************************************************/
static void
nm_setting_vrf_init(NMSettingVrf *setting)
{}
@ -134,13 +102,15 @@ nm_setting_vrf_new(void)
static void
nm_setting_vrf_class_init(NMSettingVrfClass *klass)
{
GObjectClass * object_class = G_OBJECT_CLASS(klass);
NMSettingClass *setting_class = NM_SETTING_CLASS(klass);
GObjectClass * object_class = G_OBJECT_CLASS(klass);
NMSettingClass *setting_class = NM_SETTING_CLASS(klass);
GArray * properties_override = _nm_sett_info_property_override_create_array();
object_class->get_property = get_property;
object_class->set_property = set_property;
object_class->get_property = _nm_setting_property_get_property_direct;
object_class->set_property = _nm_setting_property_set_property_direct;
setting_class->verify = verify;
setting_class->verify = verify;
setting_class->finalize_direct = TRUE;
/**
* NMSettingVrf:table:
@ -149,16 +119,18 @@ nm_setting_vrf_class_init(NMSettingVrfClass *klass)
*
* Since: 1.24
**/
obj_properties[PROP_TABLE] =
g_param_spec_uint(NM_SETTING_VRF_TABLE,
"",
"",
0,
G_MAXUINT32,
0,
G_PARAM_READWRITE | NM_SETTING_PARAM_INFERRABLE | G_PARAM_STATIC_STRINGS);
_nm_setting_property_define_direct_uint32(properties_override,
obj_properties,
NM_SETTING_VRF_TABLE,
PROP_TABLE,
0,
G_MAXUINT32,
0,
NM_SETTING_PARAM_INFERRABLE,
NMSettingVrf,
table);
g_object_class_install_properties(object_class, _PROPERTY_ENUMS_LAST, obj_properties);
_nm_setting_class_commit(setting_class, NM_META_SETTING_TYPE_VRF, NULL, NULL, 0);
_nm_setting_class_commit(setting_class, NM_META_SETTING_TYPE_VRF, NULL, properties_override, 0);
}

View file

@ -651,6 +651,14 @@ _nm_setting_property_get_property_direct(GObject * object,
g_value_set_boolean(value, *p_val);
return;
}
case NM_VALUE_TYPE_UINT32:
{
const guint32 *p_val =
_nm_setting_get_private(setting, sett_info, property_info->direct_offset);
g_value_set_uint(value, *p_val);
return;
}
case NM_VALUE_TYPE_STRING:
{
const char *const *p_val =
@ -703,6 +711,21 @@ _nm_setting_property_set_property_direct(GObject * object,
*p_val = v;
goto out_notify;
}
case NM_VALUE_TYPE_UINT32:
{
guint32 *p_val = _nm_setting_get_private(setting, sett_info, property_info->direct_offset);
guint v;
v = g_value_get_uint(value);
if (*p_val == v)
return;
*p_val = v;
/* truncation cannot happen, because the param_spec is supposed to have suitable
* minimum/maximum values so that we are in range for uint32. */
nm_assert(*p_val == v);
goto out_notify;
}
case NM_VALUE_TYPE_STRING:
{
char **p_val = _nm_setting_get_private(setting, sett_info, property_info->direct_offset);
@ -752,6 +775,7 @@ _finalize_direct(NMSetting *setting)
switch (property_info->property_type->direct_type) {
case NM_VALUE_TYPE_NONE:
case NM_VALUE_TYPE_BOOL:
case NM_VALUE_TYPE_UINT32:
break;
case NM_VALUE_TYPE_STRING:
{
@ -791,6 +815,17 @@ _nm_setting_property_to_dbus_fcn_direct(const NMSettInfoSetting *
return NULL;
return g_variant_ref(nm_g_variant_singleton_b(val));
}
case NM_VALUE_TYPE_UINT32:
{
guint32 val;
val = *(
(guint32 *) _nm_setting_get_private(setting, sett_info, property_info->direct_offset));
if (!property_info->to_dbus_data.including_default
&& val == NM_G_PARAM_SPEC_GET_DEFAULT_UINT(property_info->param_spec))
return NULL;
return g_variant_new_uint32(val);
}
case NM_VALUE_TYPE_STRING:
{
const char *val;
@ -2648,6 +2683,11 @@ const NMSettInfoPropertType nm_sett_info_propert_type_direct_boolean =
.direct_type = NM_VALUE_TYPE_BOOL,
.to_dbus_fcn = _nm_setting_property_to_dbus_fcn_direct);
const NMSettInfoPropertType nm_sett_info_propert_type_direct_uint32 =
NM_SETT_INFO_PROPERT_TYPE_DBUS_INIT(G_VARIANT_TYPE_UINT32,
.direct_type = NM_VALUE_TYPE_UINT32,
.to_dbus_fcn = _nm_setting_property_to_dbus_fcn_direct);
const NMSettInfoPropertType nm_sett_info_propert_type_direct_string =
NM_SETT_INFO_PROPERT_TYPE_DBUS_INIT(G_VARIANT_TYPE_STRING,
.direct_type = NM_VALUE_TYPE_STRING,

View file

@ -4450,6 +4450,24 @@ test_setting_metadata(void)
== _nm_setting_property_to_dbus_fcn_direct);
g_assert(sip->param_spec);
g_assert(sip->param_spec->value_type == G_TYPE_BOOLEAN);
can_set_including_default = TRUE;
} else if (sip->property_type->direct_type == NM_VALUE_TYPE_UINT32) {
const GParamSpecUInt *pspec;
g_assert(sip->property_type == &nm_sett_info_propert_type_direct_uint32);
g_assert(g_variant_type_equal(sip->property_type->dbus_type, "u"));
g_assert(sip->property_type->to_dbus_fcn
== _nm_setting_property_to_dbus_fcn_direct);
g_assert(sip->param_spec);
g_assert(sip->param_spec->value_type == G_TYPE_UINT);
pspec = NM_G_PARAM_SPEC_CAST_UINT(sip->param_spec);
g_assert_cmpint(pspec->minimum, <=, pspec->maximum);
g_assert_cmpint(pspec->default_value, >=, pspec->minimum);
g_assert_cmpint(pspec->default_value, <=, pspec->maximum);
g_assert_cmpint(pspec->maximum, <=, (guint64) G_MAXUINT32);
can_set_including_default = TRUE;
} else if (sip->property_type->direct_type == NM_VALUE_TYPE_STRING) {
g_assert(g_variant_type_equal(sip->property_type->dbus_type, "s"));