From 61912bffb59140416e1a55461230db847d448740 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 29 Aug 2019 09:12:18 +0100 Subject: [PATCH 1/3] tutorial: Improve type safety of property usage in GObject tutorial MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This means that if you compile with `-Wswitch-enum`, the compiler will warn you about properties which you’ve forgotten to handle in `set_property()` or `get_property()`. Signed-off-by: Philip Withnall Helps: #1858 --- docs/reference/gobject/tut_gobject.xml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/reference/gobject/tut_gobject.xml b/docs/reference/gobject/tut_gobject.xml index 161150ebf..e6d7923f5 100644 --- a/docs/reference/gobject/tut_gobject.xml +++ b/docs/reference/gobject/tut_gobject.xml @@ -465,12 +465,12 @@ ViewerFile *file = g_object_new (VIEWER_TYPE_FILE, NULL); /* Implementation */ /************************************************/ -enum +typedef enum { PROP_FILENAME = 1, PROP_ZOOM_LEVEL, N_PROPERTIES -}; +} ViewerFileProperty; static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, }; @@ -482,7 +482,7 @@ viewer_file_set_property (GObject *object, { ViewerFile *self = VIEWER_FILE (object); - switch (property_id) + switch ((ViewerFileProperty) property_id) { case PROP_FILENAME: g_free (self->priv->filename); @@ -510,7 +510,7 @@ viewer_file_get_property (GObject *object, { ViewerFile *self = VIEWER_FILE (object); - switch (property_id) + switch ((ViewerFileProperty) property_id) { case PROP_FILENAME: g_value_set_string (value, self->priv->filename); From 22b59c91adcfd5a59205dffb5451dc536b7b6a7d Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 29 Aug 2019 09:19:03 +0100 Subject: [PATCH 2/3] tutorial: Stop referring to `->priv` for example class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The class has used `G_DECLARE_FINAL_TYPE` for a while now, so doesn’t have a `priv` struct. Private members are declared inline. Signed-off-by: Philip Withnall Fixes: #1858 --- docs/reference/gobject/tut_gobject.xml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/reference/gobject/tut_gobject.xml b/docs/reference/gobject/tut_gobject.xml index e6d7923f5..c42d5e6b3 100644 --- a/docs/reference/gobject/tut_gobject.xml +++ b/docs/reference/gobject/tut_gobject.xml @@ -485,14 +485,14 @@ viewer_file_set_property (GObject *object, switch ((ViewerFileProperty) property_id) { case PROP_FILENAME: - g_free (self->priv->filename); - self->priv->filename = g_value_dup_string (value); - g_print ("filename: %s\n", self->priv->filename); + g_free (self->filename); + self->filename = g_value_dup_string (value); + g_print ("filename: %s\n", self->filename); break; case PROP_ZOOM_LEVEL: - self->priv->zoom_level = g_value_get_uint (value); - g_print ("zoom level: %u\n", self->priv->zoom_level); + self->zoom_level = g_value_get_uint (value); + g_print ("zoom level: %u\n", self->zoom_level); break; default: @@ -513,11 +513,11 @@ viewer_file_get_property (GObject *object, switch ((ViewerFileProperty) property_id) { case PROP_FILENAME: - g_value_set_string (value, self->priv->filename); + g_value_set_string (value, self->filename); break; case PROP_ZOOM_LEVEL: - g_value_set_uint (value, self->priv->zoom_level); + g_value_set_uint (value, self->zoom_level); break; default: From ca60cd3314f649afd847bc502f4ed12f19a3b318 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 29 Aug 2019 09:20:01 +0100 Subject: [PATCH 3/3] tutorial: Add private members to example code in tutorial Add the private members referred to in the property setting/getting example, and a finalize function for them, to make the tutorial code more self-contained. Signed-off-by: Philip Withnall Helps: #1858 --- docs/reference/gobject/tut_gobject.xml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/reference/gobject/tut_gobject.xml b/docs/reference/gobject/tut_gobject.xml index c42d5e6b3..fc7d6fe16 100644 --- a/docs/reference/gobject/tut_gobject.xml +++ b/docs/reference/gobject/tut_gobject.xml @@ -65,6 +65,8 @@ struct _ViewerFile GObject parent_instance; /* instance members */ + gchar *filename; + guint zoom_level; }; /* will create viewer_file_get_type and set viewer_file_parent_class */ @@ -80,12 +82,25 @@ viewer_file_constructed (GObject *obj) G_OBJECT_CLASS (viewer_file_parent_class)->constructed (obj); } +static void +viewer_file_finalize (GObject *obj) +{ + ViewerFile *self = VIEWER_FILE (obj); + + g_free (self->filename); + + /* Always chain up to the parent finalize function to complete object + * destruction. */ + G_OBJECT_CLASS (viewer_file_parent_class)->finalize (obj); +} + static void viewer_file_class_init (ViewerFileClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); object_class->constructed = viewer_file_constructed; + object_class->finalize = viewer_file_finalize; } static void