Deprecate g_type_class_add_private()

It's been 4 years and 8 development cycles since we introduced
G_ADD_PRIVATE and offset-based private data access. It is now
time to finally deprecate the old mechanism.

Closes: #699
This commit is contained in:
Emmanuele Bassi 2018-05-25 10:06:05 +01:00
parent d8c003dea6
commit 7e5db31d36
4 changed files with 33 additions and 15 deletions

View File

@ -76,7 +76,7 @@
* *
* Type instance and class structs are limited to a total of 64 KiB, * Type instance and class structs are limited to a total of 64 KiB,
* including all parent types. Similarly, type instances' private data * including all parent types. Similarly, type instances' private data
* (as created by g_type_class_add_private()) are limited to a total of * (as created by G_ADD_PRIVATE()) are limited to a total of
* 64 KiB. If a type instance needs a large static buffer, allocate it * 64 KiB. If a type instance needs a large static buffer, allocate it
* separately (typically by using #GArray or #GPtrArray) and put a pointer * separately (typically by using #GArray or #GPtrArray) and put a pointer
* to the buffer in the structure. * to the buffer in the structure.
@ -4562,6 +4562,8 @@ gobject_init_ctor (void)
* ]| * ]|
* *
* Since: 2.4 * Since: 2.4
* Deprecated: 2.58: Use the G_ADD_PRIVATE() macro with the `G_DEFINE_*`
* family of macros to add instance private data to a type
*/ */
void void
g_type_class_add_private (gpointer g_class, g_type_class_add_private (gpointer g_class,

View File

@ -634,6 +634,8 @@ struct _GTypeQuery
* This macro should only be used in type implementations. * This macro should only be used in type implementations.
* *
* Since: 2.4 * Since: 2.4
* Deprecated: 2.58: Use %G_ADD_PRIVATE and the generated
* `your_type_get_instance_private()` function instead
* Returns: (not nullable): a pointer to the private data structure * Returns: (not nullable): a pointer to the private data structure
*/ */
#define G_TYPE_INSTANCE_GET_PRIVATE(instance, g_type, c_type) ((c_type*) g_type_instance_get_private ((GTypeInstance*) (instance), (g_type))) #define G_TYPE_INSTANCE_GET_PRIVATE(instance, g_type, c_type) ((c_type*) g_type_instance_get_private ((GTypeInstance*) (instance), (g_type)))
@ -1297,7 +1299,7 @@ void g_type_interface_add_prerequisite (GType interface_type,
GLIB_AVAILABLE_IN_ALL GLIB_AVAILABLE_IN_ALL
GType*g_type_interface_prerequisites (GType interface_type, GType*g_type_interface_prerequisites (GType interface_type,
guint *n_prerequisites); guint *n_prerequisites);
GLIB_AVAILABLE_IN_ALL GLIB_DEPRECATED_IN_2_58
void g_type_class_add_private (gpointer g_class, void g_type_class_add_private (gpointer g_class,
gsize private_size); gsize private_size);
GLIB_AVAILABLE_IN_2_38 GLIB_AVAILABLE_IN_2_38

View File

@ -128,7 +128,9 @@ G_DEFINE_TYPE (TestMixed, test_mixed, test_object_get_type ())
static void static void
test_mixed_class_init (TestMixedClass *klass) test_mixed_class_init (TestMixedClass *klass)
{ {
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
g_type_class_add_private (klass, sizeof (TestMixedPrivate)); g_type_class_add_private (klass, sizeof (TestMixedPrivate));
G_GNUC_END_IGNORE_DEPRECATIONS
} }
static void static void

View File

@ -154,6 +154,13 @@ static gboolean test_signal_accumulator (GSignalInvocationHint *ihint,
static gchar* test_object_test_signal (TestObject *tobject, static gchar* test_object_test_signal (TestObject *tobject,
TestIface *iface_object, TestIface *iface_object,
gpointer tdata); gpointer tdata);
static gint TestObject_private_offset;
static inline gpointer
test_object_get_instance_private (TestObject *self)
{
return (G_STRUCT_MEMBER_P (self, TestObject_private_offset));
}
static GType static GType
test_object_get_type (void) test_object_get_type (void)
{ {
@ -177,6 +184,9 @@ test_object_get_type (void)
test_object_type = g_type_register_static (G_TYPE_OBJECT, "TestObject", &test_object_info, 0); test_object_type = g_type_register_static (G_TYPE_OBJECT, "TestObject", &test_object_info, 0);
g_type_add_interface_static (test_object_type, TEST_TYPE_IFACE, &iface_info); g_type_add_interface_static (test_object_type, TEST_TYPE_IFACE, &iface_info);
TestObject_private_offset =
g_type_add_instance_private (test_object_type, sizeof (TestObjectPrivate));
} }
return test_object_type; return test_object_type;
@ -185,6 +195,7 @@ static void
test_object_class_init (TestObjectClass *class) test_object_class_init (TestObjectClass *class)
{ {
/* GObjectClass *gobject_class = G_OBJECT_CLASS (class); */ /* GObjectClass *gobject_class = G_OBJECT_CLASS (class); */
g_type_class_adjust_private_offset (class, &TestObject_private_offset);
class->test_signal = test_object_test_signal; class->test_signal = test_object_test_signal;
@ -195,15 +206,11 @@ test_object_class_init (TestObjectClass *class)
test_signal_accumulator, NULL, test_signal_accumulator, NULL,
g_cclosure_marshal_STRING__OBJECT_POINTER, g_cclosure_marshal_STRING__OBJECT_POINTER,
G_TYPE_STRING, 2, TEST_TYPE_IFACE, G_TYPE_POINTER); G_TYPE_STRING, 2, TEST_TYPE_IFACE, G_TYPE_POINTER);
g_type_class_add_private (class, sizeof (TestObjectPrivate));
} }
static void static void
test_object_init (TestObject *tobject) test_object_init (TestObject *tobject)
{ {
TestObjectPrivate *priv; TestObjectPrivate *priv = test_object_get_instance_private (tobject);
priv = TEST_OBJECT_GET_PRIVATE (tobject);
g_assert (priv); g_assert (priv);
@ -215,9 +222,7 @@ test_object_init (TestObject *tobject)
static void static void
test_object_check_private_init (TestObject *tobject) test_object_check_private_init (TestObject *tobject)
{ {
TestObjectPrivate *priv; TestObjectPrivate *priv = test_object_get_instance_private (tobject);
priv = TEST_OBJECT_GET_PRIVATE (tobject);
g_print ("private data during initialization: %u == %u\n", priv->dummy1, 54321); g_print ("private data during initialization: %u == %u\n", priv->dummy1, 54321);
g_assert (priv->dummy1 == 54321); g_assert (priv->dummy1 == 54321);
@ -317,6 +322,12 @@ struct _DerivedObjectPrivate
}; };
static void derived_object_class_init (DerivedObjectClass *class); static void derived_object_class_init (DerivedObjectClass *class);
static void derived_object_init (DerivedObject *dobject); static void derived_object_init (DerivedObject *dobject);
static gint DerivedObject_private_offset;
static inline gpointer
derived_object_get_instance_private (DerivedObject *self)
{
return (G_STRUCT_MEMBER_P (self, DerivedObject_private_offset));
}
static GType static GType
derived_object_get_type (void) derived_object_get_type (void)
{ {
@ -340,6 +351,8 @@ derived_object_get_type (void)
derived_object_type = g_type_register_static (TEST_TYPE_OBJECT, "DerivedObject", &derived_object_info, 0); derived_object_type = g_type_register_static (TEST_TYPE_OBJECT, "DerivedObject", &derived_object_info, 0);
g_type_add_interface_static (derived_object_type, TEST_TYPE_IFACE, &iface_info); g_type_add_interface_static (derived_object_type, TEST_TYPE_IFACE, &iface_info);
DerivedObject_private_offset =
g_type_add_instance_private (derived_object_type, sizeof (DerivedObjectPrivate));
} }
return derived_object_type; return derived_object_type;
@ -347,7 +360,7 @@ derived_object_get_type (void)
static void static void
derived_object_class_init (DerivedObjectClass *class) derived_object_class_init (DerivedObjectClass *class)
{ {
g_type_class_add_private (class, sizeof (DerivedObjectPrivate)); g_type_class_adjust_private_offset (class, &DerivedObject_private_offset);
} }
static void static void
derived_object_init (DerivedObject *dobject) derived_object_init (DerivedObject *dobject)
@ -355,14 +368,13 @@ derived_object_init (DerivedObject *dobject)
TestObjectPrivate *test_priv; TestObjectPrivate *test_priv;
DerivedObjectPrivate *derived_priv; DerivedObjectPrivate *derived_priv;
derived_priv = DERIVED_OBJECT_GET_PRIVATE (dobject); derived_priv = derived_object_get_instance_private (dobject);
g_assert (derived_priv); g_assert (derived_priv);
test_priv = TEST_OBJECT_GET_PRIVATE (dobject); test_priv = test_object_get_instance_private (TEST_OBJECT (dobject));
g_assert (test_priv); g_assert (test_priv);
} }
/* --- main --- */ /* --- main --- */
@ -411,7 +423,7 @@ main (int argc,
iface_print_string (TEST_IFACE (sigarg), "iface-string-from-test-type"); iface_print_string (TEST_IFACE (sigarg), "iface-string-from-test-type");
iface_print_string (TEST_IFACE (dobject), "iface-string-from-derived-type"); iface_print_string (TEST_IFACE (dobject), "iface-string-from-derived-type");
priv = TEST_OBJECT_GET_PRIVATE (dobject); priv = test_object_get_instance_private (TEST_OBJECT (dobject));
g_print ("private data after initialization: %u == %u\n", priv->dummy1, 54321); g_print ("private data after initialization: %u == %u\n", priv->dummy1, 54321);
g_assert (priv->dummy1 == 54321); g_assert (priv->dummy1 == 54321);