libnm-util: refactor NMSetting name and register_settings

- refactor register_settings to allow lookup by GType and
  add the settings name to SettingInfo.

- setting NM_SETTING_NAME is deprecated and should not be set anymore.
  Indeed it has always be a bug, to reset the name to a different value.
  The only valid place to set the name was in the _init() function of
  the derived class itself.
  This is now no longer needed/possible. Instead the name get's
  detected based on the registered setting types. This makes use of
  the registered metadata that is available anyway since every
  usable setting has to register itself.

Signed-off-by: Thomas Haller <thaller@redhat.com>
This commit is contained in:
Thomas Haller 2013-11-28 11:37:40 +01:00
parent 844b89d497
commit 9d319e6da0
29 changed files with 85 additions and 90 deletions

View file

@ -2639,7 +2639,6 @@ verify (NMSetting *setting, GSList *all_settings, GError **error)
static void
nm_setting_802_1x_init (NMSetting8021x *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_802_1X_SETTING_NAME, NULL);
}
static void

View file

@ -276,7 +276,6 @@ need_secrets (NMSetting *setting)
static void
nm_setting_adsl_init (NMSettingAdsl *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_ADSL_SETTING_NAME, NULL);
}
static void

View file

@ -201,7 +201,6 @@ verify (NMSetting *setting, GSList *all_settings, GError **error)
static void
nm_setting_bluetooth_init (NMSettingBluetooth *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_BLUETOOTH_SETTING_NAME, NULL);
}
static void

View file

@ -704,9 +704,6 @@ nm_setting_bond_init (NMSettingBond *setting)
{
NMSettingBondPrivate *priv = NM_SETTING_BOND_GET_PRIVATE (setting);
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_BOND_SETTING_NAME,
NULL);
priv->options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
/* Default values: */

View file

@ -195,7 +195,6 @@ nm_setting_bridge_port_new (void)
static void
nm_setting_bridge_port_init (NMSettingBridgePort *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_BRIDGE_PORT_SETTING_NAME, NULL);
}
static void

View file

@ -322,7 +322,6 @@ get_virtual_iface_name (NMSetting *setting)
static void
nm_setting_bridge_init (NMSettingBridge *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_BRIDGE_SETTING_NAME, NULL);
}
static void

View file

@ -215,7 +215,6 @@ need_secrets (NMSetting *setting)
static void
nm_setting_cdma_init (NMSettingCdma *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_CDMA_SETTING_NAME, NULL);
}
static void

View file

@ -840,7 +840,6 @@ compare_property (NMSetting *setting,
static void
nm_setting_connection_init (NMSettingConnection *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_CONNECTION_SETTING_NAME, NULL);
}
static void

View file

@ -802,7 +802,6 @@ verify (NMSetting *setting, GSList *all_settings, GError **error)
static void
nm_setting_dcb_init (NMSettingDcb *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_DCB_SETTING_NAME, NULL);
}
static inline void

View file

@ -91,7 +91,6 @@ nm_setting_generic_new (void)
static void
nm_setting_generic_init (NMSettingGeneric *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_GENERIC_SETTING_NAME, NULL);
}
static void

View file

@ -405,7 +405,6 @@ need_secrets (NMSetting *setting)
static void
nm_setting_gsm_init (NMSettingGsm *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_GSM_SETTING_NAME, NULL);
}
static void

View file

@ -255,7 +255,6 @@ verify (NMSetting *setting, GSList *all_settings, GError **error)
static void
nm_setting_infiniband_init (NMSettingInfiniband *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_INFINIBAND_SETTING_NAME, NULL);
}
static void

View file

@ -838,7 +838,6 @@ nm_setting_ip4_config_init (NMSettingIP4Config *setting)
{
NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_IP4_CONFIG_SETTING_NAME, NULL);
priv->dns = g_array_sized_new (FALSE, TRUE, sizeof (guint32), 3);
}

View file

@ -759,7 +759,6 @@ verify (NMSetting *setting, GSList *all_settings, GError **error)
static void
nm_setting_ip6_config_init (NMSettingIP6Config *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_IP6_CONFIG_SETTING_NAME, NULL);
}
static void

View file

@ -89,7 +89,6 @@ NMSetting *nm_setting_olpc_mesh_new (void)
static void
nm_setting_olpc_mesh_init (NMSettingOlpcMesh *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_OLPC_MESH_SETTING_NAME, NULL);
}
const GByteArray *

View file

@ -413,7 +413,6 @@ verify (NMSetting *setting, GSList *all_settings, GError **error)
static void
nm_setting_ppp_init (NMSettingPPP *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_PPP_SETTING_NAME, NULL);
}
static void

View file

@ -206,7 +206,6 @@ need_secrets (NMSetting *setting)
static void
nm_setting_pppoe_init (NMSettingPPPOE *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_PPPOE_SETTING_NAME, NULL);
}
static void

View file

@ -33,6 +33,10 @@ void _nm_register_setting (const char *name,
const GType type,
const guint32 priority,
const GQuark error_quark);
/* Ensure, that name is a compile time constant string. Put the function name in parenthesis to suppress expansion. */
#define _nm_register_setting(name, type, priority, error_quark) _nm_register_setting ((name ""), type, priority, error_quark)
gboolean _nm_setting_is_base_type (NMSetting *setting);
GType _nm_setting_lookup_setting_type (const char *name);
GType _nm_setting_lookup_setting_type_by_quark (GQuark error_quark);

View file

@ -179,7 +179,6 @@ verify (NMSetting *setting, GSList *all_settings, GError **error)
static void
nm_setting_serial_init (NMSettingSerial *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_SERIAL_SETTING_NAME, NULL);
}
static void

View file

@ -110,7 +110,6 @@ verify (NMSetting *setting, GSList *all_settings, GError **error)
static void
nm_setting_team_port_init (NMSettingTeamPort *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_TEAM_PORT_SETTING_NAME, NULL);
}
static void

View file

@ -156,8 +156,6 @@ get_virtual_iface_name (NMSetting *setting)
static void
nm_setting_team_init (NMSettingTeam *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_TEAM_SETTING_NAME,
NULL);
}
static void

View file

@ -53,7 +53,6 @@ verify (NMSetting *setting, GSList *all_settings)
static void
nm_setting_template_init (NMSettingTemplate *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_TEMPLATE_SETTING_NAME, NULL);
}
static void

View file

@ -447,7 +447,6 @@ nm_setting_vlan_clear_priorities (NMSettingVlan *setting, NMVlanPriorityMap map)
static void
nm_setting_vlan_init (NMSettingVlan *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_VLAN_SETTING_NAME, NULL);
}
static gboolean

View file

@ -694,7 +694,6 @@ nm_setting_vpn_init (NMSettingVPN *setting)
{
NMSettingVPNPrivate *priv = NM_SETTING_VPN_GET_PRIVATE (setting);
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_VPN_SETTING_NAME, NULL);
priv->data = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
priv->secrets = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, destroy_one_secret);
}

View file

@ -163,7 +163,6 @@ verify (NMSetting *setting, GSList *all_settings, GError **error)
static void
nm_setting_wimax_init (NMSettingWimax *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_WIMAX_SETTING_NAME, NULL);
}
static void

View file

@ -650,7 +650,6 @@ nm_setting_wired_init (NMSettingWired *setting)
{
NMSettingWiredPrivate *priv = NM_SETTING_WIRED_GET_PRIVATE (setting);
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_WIRED_SETTING_NAME, NULL);
priv->s390_options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
}

View file

@ -1055,7 +1055,6 @@ set_secret_flags (NMSetting *setting,
static void
nm_setting_wireless_security_init (NMSettingWirelessSecurity *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME, NULL);
}
static void

View file

@ -811,7 +811,6 @@ verify (NMSetting *setting, GSList *all_settings, GError **error)
static void
nm_setting_wireless_init (NMSettingWireless *setting)
{
g_object_set (setting, NM_SETTING_NAME, NM_SETTING_WIRELESS_SETTING_NAME, NULL);
}
static void

View file

@ -66,7 +66,14 @@ G_DEFINE_ABSTRACT_TYPE (NMSetting, nm_setting, G_TYPE_OBJECT)
#define NM_SETTING_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING, NMSettingPrivate))
typedef struct {
char *name;
const char *name;
GType type;
guint32 priority;
GQuark error_quark;
} SettingInfo;
typedef struct {
const SettingInfo *info;
} NMSettingPrivate;
enum {
@ -79,21 +86,38 @@ enum {
/*************************************************************/
static GHashTable *registered_settings = NULL;
static GHashTable *registered_settings_by_type = NULL;
static gboolean
_nm_gtype_equal (gconstpointer v1, gconstpointer v2)
{
return *((const GType *) v1) == *((const GType *) v2);
}
static guint
_nm_gtype_hash (gconstpointer v)
{
return *((const GType *) v);
}
static void __attribute__((constructor))
_ensure_registered (void)
{
g_type_init ();
_nm_value_transforms_register ();
if (G_UNLIKELY (registered_settings == NULL))
if (G_UNLIKELY (registered_settings == NULL)) {
g_type_init ();
_nm_value_transforms_register ();
registered_settings = g_hash_table_new (g_str_hash, g_str_equal);
registered_settings_by_type = g_hash_table_new (_nm_gtype_hash, _nm_gtype_equal);
}
}
typedef struct {
GType type;
guint32 priority;
GQuark error_quark;
} SettingInfo;
#define _ensure_setting_info(self, priv) \
G_STMT_START { \
NMSettingPrivate *_priv_esi = (priv); \
if (G_UNLIKELY (!_priv_esi->info)) { \
_priv_esi->info = _nm_setting_lookup_setting_by_type (G_OBJECT_TYPE (self)); \
g_assert (_priv_esi->info); \
} \
} G_STMT_END
/*************************************************************/
@ -131,14 +155,14 @@ typedef struct {
* 4: IP-level stuff
*/
void
_nm_register_setting (const char *name,
const GType type,
const guint32 priority,
const GQuark error_quark)
(_nm_register_setting) (const char *name,
const GType type,
const guint32 priority,
const GQuark error_quark)
{
SettingInfo *info;
g_return_if_fail (name != NULL);
g_return_if_fail (name != NULL && *name);
g_return_if_fail (type != G_TYPE_INVALID);
g_return_if_fail (type != G_TYPE_NONE);
g_return_if_fail (error_quark != 0);
@ -146,8 +170,14 @@ _nm_register_setting (const char *name,
_ensure_registered ();
if (G_LIKELY (g_hash_table_lookup (registered_settings, name)))
if (G_LIKELY ((info = g_hash_table_lookup (registered_settings, name)))) {
g_return_if_fail (info->type == type);
g_return_if_fail (info->error_quark == error_quark);
g_return_if_fail (info->priority == priority);
g_return_if_fail (g_strcmp0 (info->name, name) == 0);
return;
}
g_return_if_fail (g_hash_table_lookup (registered_settings_by_type, &type) == NULL);
if (priority == 0)
g_assert_cmpstr (name, ==, NM_SETTING_CONNECTION_SETTING_NAME);
@ -156,23 +186,27 @@ _nm_register_setting (const char *name,
info->type = type;
info->priority = priority;
info->error_quark = error_quark;
g_hash_table_insert (registered_settings, (gpointer) name, info);
info->name = name;
g_hash_table_insert (registered_settings, (void *) info->name, info);
g_hash_table_insert (registered_settings_by_type, &info->type, info);
}
static const SettingInfo *
_nm_setting_lookup_setting_by_type (GType type)
{
_ensure_registered ();
return g_hash_table_lookup (registered_settings_by_type, &type);
}
static guint32
_get_setting_priority (NMSetting *setting)
{
GHashTableIter iter;
SettingInfo *info;
NMSettingPrivate *priv;
_ensure_registered ();
g_hash_table_iter_init (&iter, registered_settings);
while (g_hash_table_iter_next (&iter, NULL, (gpointer) &info)) {
if (G_OBJECT_TYPE (setting) == info->type)
return info->priority;
}
return G_MAXUINT32;
g_return_val_if_fail (NM_IS_SETTING (setting), G_MAXUINT32);
priv = NM_SETTING_GET_PRIVATE (setting);
_ensure_setting_info (setting, priv);
return priv->info->priority;
}
gboolean
@ -387,7 +421,7 @@ duplicate_setting (NMSetting *setting,
GParamFlags flags,
gpointer user_data)
{
if (flags & G_PARAM_WRITABLE)
if ((flags & (G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)) == G_PARAM_WRITABLE)
g_object_set_property (G_OBJECT (user_data), name, value);
}
@ -450,9 +484,12 @@ nm_setting_find_in_list (GSList *settings_list,
const char *
nm_setting_get_name (NMSetting *setting)
{
g_return_val_if_fail (NM_IS_SETTING (setting), NULL);
NMSettingPrivate *priv;
return NM_SETTING_GET_PRIVATE (setting)->name;
g_return_val_if_fail (NM_IS_SETTING (setting), NULL);
priv = NM_SETTING_GET_PRIVATE (setting);
_ensure_setting_info (setting, priv);
return priv->info->name;
}
/**
@ -473,8 +510,7 @@ gboolean
nm_setting_verify (NMSetting *setting, GSList *all_settings, GError **error)
{
g_return_val_if_fail (NM_IS_SETTING (setting), FALSE);
if (error)
g_return_val_if_fail (*error == NULL, FALSE);
g_return_val_if_fail (!error || *error == NULL, FALSE);
if (NM_SETTING_GET_CLASS (setting)->verify)
return NM_SETTING_GET_CLASS (setting)->verify (setting, all_settings, error);
@ -1167,48 +1203,34 @@ nm_setting_init (NMSetting *setting)
static GObject*
constructor (GType type,
guint n_construct_params,
GObjectConstructParam *construct_params)
guint n_construct_params,
GObjectConstructParam *construct_params)
{
GObject *object;
NMSettingPrivate *priv;
object = G_OBJECT_CLASS (nm_setting_parent_class)->constructor (type,
n_construct_params,
construct_params);
if (!object)
return NULL;
priv = NM_SETTING_GET_PRIVATE (object);
if (!priv->name) {
g_warning ("Setting name is not set.");
g_object_unref (object);
object = NULL;
}
n_construct_params,
construct_params);
_ensure_setting_info (object, NM_SETTING_GET_PRIVATE (object));
return object;
}
static void
finalize (GObject *object)
{
NMSettingPrivate *priv = NM_SETTING_GET_PRIVATE (object);
g_free (priv->name);
G_OBJECT_CLASS (nm_setting_parent_class)->finalize (object);
}
static void
set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
const GValue *value, GParamSpec *pspec)
{
NMSettingPrivate *priv = NM_SETTING_GET_PRIVATE (object);
switch (prop_id) {
case PROP_NAME:
g_free (priv->name);
priv->name = g_value_dup_string (value);
/* The setter for NAME is deprecated and should not be used anymore.
* Keep the setter for NAME to remain backward compatible.
* Only assert that the caller does not try to set the name to a different value
* then the registered name, which would be extra wrong.
**/
_ensure_setting_info (object, priv);
g_return_if_fail (!g_strcmp0 (priv->info->name, g_value_get_string (value)));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@ -1218,7 +1240,7 @@ set_property (GObject *object, guint prop_id,
static void
get_property (GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec)
GValue *value, GParamSpec *pspec)
{
NMSetting *setting = NM_SETTING (object);
@ -1243,7 +1265,6 @@ nm_setting_class_init (NMSettingClass *setting_class)
object_class->constructor = constructor;
object_class->set_property = set_property;
object_class->get_property = get_property;
object_class->finalize = finalize;
setting_class->update_one_secret = update_one_secret;
setting_class->get_secret_flags = get_secret_flags;