NetworkManager/src/libnm-core-impl/nm-simple-connection.c
Thomas Haller bd8d49495b
libnm: embed private structure in NMSetting and avoid g_type_class_add_private()
Historically, the NMSetting types were in public headers. Theoretically,
that allowed users to subtype our classes. However in practice that was
impossible because they lacked required access to internal functions to
fully create an NMSetting class outside of libnm. And it also was not
useful, because you simply cannot extend libnm by subtyping a libnm
class. And supporting such a use case would be hard and limit what we can
do in libnm.

Having GObject structs in public headers also require that we don't
change it's layout. The ABI of those structs must not change, if anybody
out there was actually subclassing our GObjects.

In libnm 1.34 (commit e46d484fae ('libnm: hide NMSetting types from
public headers')) we moved the structs from headers to internal.
This would have caused a compiler error if anybody was using those
struct definitions. However, we still didn't change the ABI/layout so
that we didn't break users who relied on it (for whatever reason).

It doesn't seem there were any affected user. We waited long enough.
Change internal ABI.

No longer use g_type_class_add_private(). Instead, embed the private
structs directly (_NM_GET_PRIVATE()) or indirectly
(_NM_GET_PRIVATE_PTR()) in the object.

The main benefit is for debugging in the debugger, where we can now
easily find the private data. Previously that was so cumbersome to be
effectively impossible.

It's also the fastest possible way, since NM_SETTING_*_GET_PRIVATE()
literally resolves to "&self->_priv" (plus static asserts and
nm_assert() for type checking).

_NM_GET_PRIVATE() also propagates constness and requires that the
argument is a compatible pointer type (at compile time).

Note that g_type_class_add_private() is also deprecated in glib 2.58 and
replaced by G_ADD_PRIVATE(). For one, we still don't rely on 2.58. Also,
G_ADD_PRIVATE() is a worse solution as it supports a usecase that we
don't care for (public structs in headers). _NM_GET_PRIVATE() is still
faster, works with older glib and most importantly: is better for
debugging as you can find the private data from an object pointer.

For NMSettingIPConfig this is rather awkward, because all direct
properties require a common "klass->private_offset". This was however
the case before this change. Nothing new here. And if you ever touch
this and do something wrong, many unit tests will fail. It's almost
impossible to get wrong, albeit it can be confusing to understand.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1773
2023-10-31 11:29:37 +01:00

176 lines
5.2 KiB
C

/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2007 - 2008 Novell, Inc.
* Copyright (C) 2007 - 2014 Red Hat, Inc.
*/
#include "libnm-core-impl/nm-default-libnm-core.h"
#include "nm-simple-connection.h"
#include "nm-setting-private.h"
/**
* SECTION:nm-simple-connection
* @short_description: An unmanaged connection
*
* An #NMSimpleConnection does not directly represent a D-Bus-exported connection,
* but might be used in the process of creating a new one.
**/
/*****************************************************************************/
GTypeClass *_nm_simple_connection_class_instance = NULL;
int _nm_simple_connection_private_offset;
/*****************************************************************************/
/**
* NMSimpleConnection:
*/
struct _NMSimpleConnection {
GObject parent;
};
struct _NMSimpleConnectionClass {
GObjectClass parent;
};
static void nm_simple_connection_interface_init(NMConnectionInterface *iface);
G_DEFINE_TYPE_WITH_CODE(NMSimpleConnection,
nm_simple_connection,
G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE(NM_TYPE_CONNECTION,
nm_simple_connection_interface_init);)
/*****************************************************************************/
static void
nm_simple_connection_init(NMSimpleConnection *self)
{
NMConnectionPrivate *priv;
priv = _NM_SIMPLE_CONNECTION_GET_CONNECTION_PRIVATE(self);
priv->self = (NMConnection *) self;
}
/**
* nm_simple_connection_new:
*
* Creates a new #NMSimpleConnection object with no #NMSetting objects.
*
* Returns: (transfer full): the new empty #NMConnection object
**/
NMConnection *
nm_simple_connection_new(void)
{
return g_object_new(NM_TYPE_SIMPLE_CONNECTION, NULL);
}
/**
* _nm_simple_connection_new_from_dbus:
* @dict: a #GVariant of type %NM_VARIANT_TYPE_CONNECTION describing the connection
* @error: on unsuccessful return, an error
*
* Creates a new #NMSimpleConnection from a hash table describing the
* connection. See nm_connection_to_dbus() for a description of the expected
* hash table.
*
* Returns: (transfer full): the new #NMSimpleConnection object, populated with
* settings created from the values in the hash table, or %NULL if there was
* an error.
**/
NMConnection *
_nm_simple_connection_new_from_dbus(GVariant *dict, NMSettingParseFlags parse_flags, GError **error)
{
NMConnection *connection;
g_return_val_if_fail(dict != NULL, NULL);
g_return_val_if_fail(g_variant_is_of_type(dict, NM_VARIANT_TYPE_CONNECTION), NULL);
g_return_val_if_fail(!NM_FLAGS_ANY(parse_flags, ~NM_SETTING_PARSE_FLAGS_ALL), NULL);
g_return_val_if_fail(
!NM_FLAGS_ALL(parse_flags,
NM_SETTING_PARSE_FLAGS_STRICT | NM_SETTING_PARSE_FLAGS_BEST_EFFORT),
NULL);
connection = nm_simple_connection_new();
if (!_nm_connection_replace_settings(connection, dict, parse_flags, error))
g_clear_object(&connection);
return connection;
}
/**
* nm_simple_connection_new_from_dbus:
* @dict: a #GVariant of type %NM_VARIANT_TYPE_CONNECTION describing the connection
* @error: on unsuccessful return, an error
*
* Creates a new #NMSimpleConnection from a hash table describing the
* connection and normalize the connection. See nm_connection_to_dbus() for a
* description of the expected hash table.
*
* Returns: (transfer full): the new #NMSimpleConnection object, populated with
* settings created from the values in the hash table, or %NULL if the
* connection failed to normalize.
**/
NMConnection *
nm_simple_connection_new_from_dbus(GVariant *dict, GError **error)
{
return _nm_simple_connection_new_from_dbus(dict, NM_SETTING_PARSE_FLAGS_NORMALIZE, error);
}
/**
* nm_simple_connection_new_clone:
* @connection: the #NMConnection to clone
*
* Clones an #NMConnection as an #NMSimpleConnection.
*
* Returns: (transfer full): a new #NMConnection containing the same settings
* and properties as the source #NMConnection
**/
NMConnection *
nm_simple_connection_new_clone(NMConnection *connection)
{
NMConnection *clone;
g_return_val_if_fail(NM_IS_CONNECTION(connection), NULL);
clone = nm_simple_connection_new();
_nm_connection_set_path_rstr(clone, _nm_connection_get_path_rstr(connection));
nm_connection_replace_settings_from_connection(clone, connection);
return clone;
}
static void
dispose(GObject *object)
{
#if NM_MORE_ASSERTS
g_signal_handlers_disconnect_by_data(object,
(gpointer) &_nm_assert_connection_unchanging_user_data);
#endif
_nm_connection_private_clear(_NM_SIMPLE_CONNECTION_GET_CONNECTION_PRIVATE(object));
G_OBJECT_CLASS(nm_simple_connection_parent_class)->dispose(object);
}
static void
nm_simple_connection_class_init(NMSimpleConnectionClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
g_type_class_add_private(klass, sizeof(NMConnectionPrivate));
object_class->dispose = dispose;
_nm_simple_connection_private_offset = g_type_class_get_instance_private_offset(klass);
_nm_simple_connection_class_instance = (GTypeClass *) klass;
}
static void
nm_simple_connection_interface_init(NMConnectionInterface *iface)
{}