From a76242b35ab6809f7582fd06b8b30c05f82b3934 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Fri, 20 Feb 2015 13:14:08 +0000 Subject: [PATCH] docs: Add vfunc NULL checks to GObject how-to examples Not setting a pure vfunc is a programmer error, so can be handled with a g_return_if_fail() rather than needing a g_warning(). https://bugzilla.gnome.org/show_bug.cgi?id=744060 --- docs/reference/gobject/tut_howto.xml | 42 ++++++++++++++++++---------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/docs/reference/gobject/tut_howto.xml b/docs/reference/gobject/tut_howto.xml index 19c6fdaa8..15cdba26c 100644 --- a/docs/reference/gobject/tut_howto.xml +++ b/docs/reference/gobject/tut_howto.xml @@ -571,9 +571,14 @@ void maman_bar_do_action (MamanBar *self, /* parameters */); void maman_bar_do_action (MamanBar *self, /* parameters */) { + MamanBarClass *klass; + g_return_if_fail (MAMAN_IS_BAR (self)); - MAMAN_BAR_GET_CLASS (self)->do_action (self, /* parameters */); + klass = MAMAN_BAR_GET_CLASS (self); + g_return_if_fail (klass->do_action != NULL); + + klass->do_action (self, /* parameters */); } The code above simply redirects the do_action call @@ -596,7 +601,7 @@ maman_bar_real_do_action_two (MamanBar *self, /* parameters */) } static void -maman_bar_class_init (BarClass *klass) +maman_bar_class_init (MamanBarClass *klass) { /* this is not necessary, except for demonstration purposes. * @@ -611,27 +616,31 @@ maman_bar_class_init (BarClass *klass) void maman_bar_do_action_one (MamanBar *self, /* parameters */) { + MamanBarClass *klass; + g_return_if_fail (MAMAN_IS_BAR (self)); + klass = MAMAN_BAR_GET_CLASS (self); + /* if the method is purely virtual, then it is a good idea to * check that it has been overridden before calling it, and, * depending on the intent of the class, either ignore it silently * or warn the user. - / - if (MAMAN_BAR_GET_CLASS (self)->do_action_one != NULL) - MAMAN_BAR_GET_CLASS (self)->do_action_one (self, /* parameters */); - else - g_warning ("Class '%s' does not override the mandatory " - "MamanBarClass.do_action_one() virtual function.", - G_OBJECT_TYPE_NAME (self)); + */ + g_return_if_fail (klass->do_action != NULL); + klass->do_action_one (self, /* parameters */); } void maman_bar_do_action_two (MamanBar *self, /* parameters */) { + MamanBarClass *klass; + g_return_if_fail (MAMAN_IS_BAR (self)); - MAMAN_BAR_GET_CLASS (self)->do_action_two (self, /* parameters */); + klass = MAMAN_BAR_GET_CLASS (self); + if (klass->do_action_two != NULL) + klass->do_action_two (self, /* parameters */); } @@ -653,6 +662,10 @@ struct _MamanBarClass /* stuff */ void (* helper_do_specific_action) (MamanBar *self, /* parameters */); + + /* Padding to allow adding up to 12 new virtual functions without + * breaking ABI. */ + gpointer padding[12]; }; void maman_bar_do_any_action (MamanBar *self, /* parameters */); @@ -670,6 +683,8 @@ maman_bar_do_specific_action (MamanBar *self, /* parameters */) void maman_bar_do_any_action (MamanBar *self, /* parameters */) { + g_return_if_fail (MAMAN_IS_BAR (self)); + /* random code here */ /* @@ -965,8 +980,7 @@ maman_ibaz_interface_init (MamanIbazInterface *iface) static void maman_baz_init (MamanBaz *self) { - MamanBaz *self = MAMAN_BAZ (instance); - self->instance_member = 0xdeadbeaf; + self->instance_member = 0xdeadbeef; } @@ -1043,7 +1057,7 @@ maman_ibaz_interface_init (MamanIbazInterface *iface) static void maman_bar_class_init (MamanBarClass *klass) { - + /* Nothing here. */ } static void @@ -1118,7 +1132,6 @@ maman_ibaz_default_init (MamanIbazInterface *iface) The following code snippet shows the modifications needed in the MamanBaz declaration and implementation above: - struct _MamanBaz { GObject parent_instance; @@ -1210,6 +1223,7 @@ static void maman_derived_ibaz_do_action (MamanIbaz *ibaz) { MamanDerivedBaz *self = MAMAN_DERIVED_BAZ (ibaz); + g_print ("DerivedBaz implementation of Ibaz interface Action\n"); }