Tutorial This chapter tries to answer the real-life questions of users and presents the most common use cases in order from most likely to least likely. How to define and implement a new GObject This chapter focuses on the implementation of a subtype of GObject, for example to create a custom class hierarchy, or to subclass a GTK+ widget. Boilerplate header code The first step before writing the code for your GObject is to write the type's header which contains the needed type, function and macro definitions. Each of these elements is nothing but a convention which is followed by almost all users of GObject, and has been refined over multiple years of experience developing GObject-based code. If you are writing a library, it is particularly important for you to adhere closely to these conventions; users of your library will assume that you have. Even if not writing a library, it will help other people who want to work on your project. Pick a name convention for your headers and source code and stick to it: use a dash to separate the prefix from the typename: maman-bar.h and maman-bar.c (this is the convention used by Nautilus and most GNOME libraries). use an underscore to separate the prefix from the typename: maman_bar.h and maman_bar.c. Do not separate the prefix from the typename: mamanbar.h and mamanbar.c. (this is the convention used by GTK+) Some people like the first two solutions better: it makes reading file names easier for those with poor eyesight. The basic conventions for any header which exposes a GType are described in . If you want to declare a type named ‘bar’ in namespace ‘maman’, name the type instance MamanBar and its class MamanBarClass (names are case sensitive). The recommended method of declaring a type differs based on whether the type is final or derivable. Final types cannot be subclassed further, and should be the default choice for new types — changing a final type to be derivable is always a change that will be compatible with existing uses of the code, but the converse will often cause problems. Final types are declared using G_DECLARE_FINAL_TYPE, and require a structure to hold the instance data to be declared in the source code (not the header file). /* * Copyright/Licensing information. */ /* inclusion guard */ #ifndef __MAMAN_BAR_H__ #define __MAMAN_BAR_H__ #include <glib-object.h> /* * Potentially, include other headers on which this header depends. */ G_BEGIN_DECLS /* * Type declaration. */ #define MAMAN_TYPE_BAR maman_bar_get_type () G_DECLARE_FINAL_TYPE (MamanBar, maman_bar, MAMAN, BAR, GObject) /* * Method definitions. */ MamanBar *maman_bar_new (void); G_END_DECLS #endif /* __MAMAN_BAR_H__ */ Derivable types can be subclassed further, and their class and instance structures form part of the public API which must not be changed if API stability is cared about. They are declared using G_DECLARE_DERIVABLE_TYPE: /* * Copyright/Licensing information. */ /* inclusion guard */ #ifndef __MAMAN_BAR_H__ #define __MAMAN_BAR_H__ #include <glib-object.h> /* * Potentially, include other headers on which this header depends. */ G_BEGIN_DECLS /* * Type declaration. */ #define MAMAN_TYPE_BAR maman_bar_get_type () G_DECLARE_DERIVABLE_TYPE (MamanBar, maman_bar, MAMAN, BAR, GObject) struct _MamanBarClass { GObjectClass parent_class; /* Class virtual function fields. */ void (* handle_frob) (MamanBar *bar, guint n_frobs); /* Padding to allow adding up to 12 new virtual functions without * breaking ABI. */ gpointer padding[12]; }; /* * Method definitions. */ MamanBar *maman_bar_new (void); G_END_DECLS #endif /* __MAMAN_BAR_H__ */ The convention for header includes is to add the minimum number of #include directives to the top of your headers needed to compile that header. This allows client code to simply #include "maman-bar.h", without needing to know the prerequisites for maman-bar.h. Boilerplate code In your code, the first step is to #include the needed headers: /* * Copyright information */ #include "maman-bar.h" /* Private structure definition. */ typedef struct { gint member1; /* stuff */ } MamanBarPrivate; /* * forward definitions */ If the class is being declared as final using G_DECLARE_FINAL_TYPE, its instance structure should be defined in the C file: struct _MamanBar { GObject parent_instance; /* Other members, including private data. */ } Call the G_DEFINE_TYPE macro (or G_DEFINE_TYPE_WITH_PRIVATE if your class needs private data — final types do not need private data) using the name of the type, the prefix of the functions and the parent GType to reduce the amount of boilerplate needed. This macro will: implement the maman_bar_get_type function define a parent class pointer accessible from the whole .c file add private instance data to the type (if using G_DEFINE_TYPE_WITH_PRIVATE) If the class has been declared as final using G_DECLARE_FINAL_TYPE (see ), private data should be placed in the instance structure, MamanBar, and G_DEFINE_TYPE should be used instead of G_DEFINE_TYPE_WITH_PRIVATE. The instance structure for a final class is not exposed publicly, and is not embedded in the instance structures of any derived classes (because the class is final); so its size can vary without causing incompatibilities for code which uses the class. Conversely, private data for derivable classes must be included in a private structure, and G_DEFINE_TYPE_WITH_PRIVATE must be used. G_DEFINE_TYPE (MamanBar, maman_bar, G_TYPE_OBJECT) or G_DEFINE_TYPE_WITH_PRIVATE (MamanBar, maman_bar, G_TYPE_OBJECT) It is also possible to use the G_DEFINE_TYPE_WITH_CODE macro to control the get_type function implementation — for instance, to add a call to the G_IMPLEMENT_INTERFACE macro to implement an interface. Object construction People often get confused when trying to construct their GObjects because of the sheer number of different ways to hook into the objects's construction process: it is difficult to figure which is the correct, recommended way. shows what user-provided functions are invoked during object instantiation and in which order they are invoked. A user looking for the equivalent of the simple C++ constructor function should use the instance_init method. It will be invoked after all the parents’ instance_init functions have been invoked. It cannot take arbitrary construction parameters (as in C++) but if your object needs arbitrary parameters to complete initialization, you can use construction properties. Construction properties will be set only after all instance_init functions have run. No object reference will be returned to the client of g_object_new until all the construction properties have been set. It is important to note that object construction cannot ever fail. If you require a fallible GObject construction, you can use the GInitable and GAsyncInitable interfaces provided by the GIO library. You should write the following code first: G_DEFINE_TYPE_WITH_PRIVATE (MamanBar, maman_bar, G_TYPE_OBJECT) static void maman_bar_class_init (MamanBarClass *klass) { } static void maman_bar_init (MamanBar *self) { MamanBarPrivate *priv = maman_bar_get_instance_private (self); /* initialize all public and private members to reasonable default values. * They are all automatically initialized to 0 to begin with. */ } If you need special construction properties (with G_PARAM_CONSTRUCT_ONLY set), install the properties in the class_init() function, override the set_property() and get_property() methods of the GObject class, and implement them as described by . Property IDs must start from 1, as 0 is reserved for internal use by GObject. enum { PROP_MAMAN = 1, N_PROPERTIES }; static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, }; static void bar_class_init (MamanBarClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); object_class->set_property = bar_set_property; object_class->get_property = bar_get_property; obj_properties[PROP_MAMAN] = g_param_spec_string ("maman", "Maman construct prop", "Set maman's name", "no-name-set" /* default value */, G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); g_object_class_install_properties (object_class, N_PROPERTIES, obj_properties); } If you need this, make sure you can build and run code similar to the code shown above. Also, make sure your construct properties can be set without side effects during construction. Some people sometimes need to complete the initialization of a instance of a type only after the properties passed to the constructors have been set. This is possible through the use of the constructor() class method as described in or, more simply, using the constructed() class method. Note that the constructed() virtual function will only be invoked after the properties marked as G_PARAM_CONSTRUCT_ONLY or G_PARAM_CONSTRUCT have been consumed, but before the regular properties passed to g_object_new() have been set. Object destruction Again, it is often difficult to figure out which mechanism to use to hook into the object's destruction process: when the last g_object_unref function call is made, a lot of things happen as described in . The destruction process of your object is in two phases: dispose and finalize. This split is necessary to handle potential cycles due to the nature of the reference counting mechanism used by GObject, as well as dealing with temporary revival of instances in case of signal emission during the destruction sequence. See for more information. struct _MamanBarPrivate { GObject *an_object; gchar *a_string; }; G_DEFINE_TYPE_WITH_PRIVATE (MamanBar, maman_bar, G_TYPE_OBJECT) static void maman_bar_dispose (GObject *gobject) { MamanBarPrivate *priv = maman_bar_get_instance_private (MAMAN_BAR (gobject)); /* In dispose(), you are supposed to free all types referenced from this * object which might themselves hold a reference to self. Generally, * the most simple solution is to unref all members on which you own a * reference. */ /* dispose() might be called multiple times, so we must guard against * calling g_object_unref() on an invalid GObject by setting the member * NULL; g_clear_object() does this for us. */ g_clear_object (&priv->an_object); /* Always chain up to the parent class; there is no need to check if * the parent class implements the dispose() virtual function: it is * always guaranteed to do so */ G_OBJECT_CLASS (maman_bar_parent_class)->dispose (gobject); } static void maman_bar_finalize (GObject *gobject) { MamanBarPrivate *priv = maman_bar_get_instance_private (MAMAN_BAR (gobject)); g_free (priv->a_string); /* Always chain up to the parent class; as with dispose(), finalize() * is guaranteed to exist on the parent's class virtual function table */ G_OBJECT_CLASS (maman_bar_parent_class)->finalize (gobject); } static void maman_bar_class_init (MamanBarClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); object_class->dispose = maman_bar_dispose; object_class->finalize = maman_bar_finalize; } static void maman_bar_init (MamanBar *self); { MamanBarPrivate *priv = maman_bar_get_instance_private (self); priv->an_object = g_object_new (MAMAN_TYPE_BAZ, NULL); priv->a_string = g_strdup ("Maman"); } It is possible that object methods might be invoked after dispose is run and before finalize runs. GObject does not consider this to be a program error: you must gracefully detect this and neither crash nor warn the user, by having a disposed instance revert to an inert state. Object methods Just as with C++, there are many different ways to define object methods and extend them: the following list and sections draw on C++ vocabulary. (Readers are expected to know basic C++ concepts. Those who have not had to write C++ code recently can refer to e.g. to refresh their memories.) non-virtual public methods, virtual public methods and virtual private methods Non-virtual public methods These are the simplest, providing a simple method which acts on the object. Provide a function prototype in the header and an implementation of that prototype in the source file. /* declaration in the header. */ void maman_bar_do_action (MamanBar *self, /* parameters */); /* implementation in the source file */ void maman_bar_do_action (MamanBar *self, /* parameters */) { g_return_if_fail (MAMAN_IS_BAR (self)); /* do stuff here. */ } Virtual public methods This is the preferred way to create GObjects with overridable methods: Define the common method and its virtual function in the class structure in the public header Define the common method in the header file and implement it in the source file Implement a base version of the virtual function in the source file and initialize the virtual function pointer to this implementation in the object’s class_init function; or leave it as NULL for a ‘pure virtual’ method which must be overridden by derived classes Re-implement the virtual function in each derived class which needs to override it Note that virtual functions can only be defined if the class is derivable, declared using G_DECLARE_DERIVABLE_TYPE so the class structure can be defined. /* declaration in maman-bar.h. */ #define MAMAN_TYPE_BAR maman_bar_get_type () G_DECLARE_DERIVABLE_TYPE (MamanBar, maman_bar, MAMAN, BAR, GObject) struct _MamanBarClass { GObjectClass parent_class; /* stuff */ void (*do_action) (MamanBar *self, /* parameters */); /* Padding to allow adding up to 12 new virtual functions without * breaking ABI. */ gpointer padding[12]; }; void maman_bar_do_action (MamanBar *self, /* parameters */); /* implementation in maman-bar.c */ void maman_bar_do_action (MamanBar *self, /* parameters */) { MamanBarClass *klass; g_return_if_fail (MAMAN_IS_BAR (self)); 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 to the relevant virtual function. It is possible to provide a default implementation for this class method in the object's class_init function: initialize the klass->do_action field to a pointer to the actual implementation. By default, class methods that are not inherited are initialized to NULL, and thus are to be considered "pure virtual". static void maman_bar_real_do_action_two (MamanBar *self, /* parameters */) { /* Default implementation for the virtual method. */ } static void maman_bar_class_init (MamanBarClass *klass) { /* this is not necessary, except for demonstration purposes. * * pure virtual method: mandates implementation in children. */ klass->do_action_one = NULL; /* merely virtual method. */ klass->do_action_two = maman_bar_real_do_action_two; } 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. */ 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)); klass = MAMAN_BAR_GET_CLASS (self); if (klass->do_action_two != NULL) klass->do_action_two (self, /* parameters */); } Virtual private Methods These are very similar to virtual public methods. They just don't have a public function to call directly. The header file contains only a declaration of the virtual function: /* declaration in maman-bar.h. */ struct _MamanBarClass { GObjectClass parent; /* 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 */); These virtual functions are often used to delegate part of the job to child classes: /* this accessor function is static: it is not exported outside of this file. */ static void maman_bar_do_specific_action (MamanBar *self, /* parameters */) { MAMAN_BAR_GET_CLASS (self)->do_specific_action (self, /* parameters */); } void maman_bar_do_any_action (MamanBar *self, /* parameters */) { g_return_if_fail (MAMAN_IS_BAR (self)); /* random code here */ /* * Try to execute the requested action. Maybe the requested action * cannot be implemented here. So, we delegate its implementation * to the child class: */ maman_bar_do_specific_action (self, /* parameters */); /* other random code here */ } Again, it is possible to provide a default implementation for this private virtual function: static void maman_bar_class_init (MamanBarClass *klass) { /* pure virtual method: mandates implementation in children. */ klass->do_specific_action_one = NULL; /* merely virtual method. */ klass->do_specific_action_two = maman_bar_real_do_specific_action_two; } Children can then implement the subclass with code such as: static void maman_bar_subtype_class_init (MamanBarSubTypeClass *klass) { MamanBarClass *bar_class = MAMAN_BAR_CLASS (klass); /* implement pure virtual function. */ bar_class->do_specific_action_one = maman_bar_subtype_do_specific_action_one; } Chaining up Chaining up is often loosely defined by the following set of conditions: Parent class A defines a public virtual method named foo and provides a default implementation. Child class B re-implements method foo. B’s implementation of foo calls (‘chains up to’) its parent class A’s implementation of foo. There are various uses of this idiom: You need to extend the behaviour of a class without modifying its code. You create a subclass to inherit its implementation, re-implement a public virtual method to modify the behaviour and chain up to ensure that the previous behaviour is not really modified, just extended. You need to implement the Chain Of Responsibility pattern: each object of the inheritance tree chains up to its parent (typically, at the beginning or the end of the method) to ensure that each handler is run in turn. To explicitly chain up to the implementation of the virtual method in the parent class, you first need a handle to the original parent class structure. This pointer can then be used to access the original virtual function pointer and invoke it directly. The original adjective used in this sentence is not innocuous. To fully understand its meaning, recall how class structures are initialized: for each object type, the class structure associated with this object is created by first copying the class structure of its parent type (a simple memcpy) and then by invoking the class_init callback on the resulting class structure. Since the class_init callback is responsible for overwriting the class structure with the user re-implementations of the class methods, the modified copy of the parent class structure stored in the derived instance cannot be used. A copy of the class structure of an instance of the parent class is needed. Use the parent_class pointer created and initialized by the G_DEFINE_TYPE family of macros, for instance: static void b_method_to_call (B *obj, int a) { /* do stuff before chain up */ /* call the method_to_call() virtual function on the * parent of BClass, AClass. * * remember the explicit cast to AClass* */ A_CLASS (b_parent_class)->method_to_call (obj, a); /* do stuff after chain up */ } How to define and implement interfaces Defining interfaces The theory behind how GObject interfaces work is given in ; this section covers how to define and implement an interface. The first step is to get the header right. This interface defines two methods: /* * Copyright/Licensing information. */ #ifndef __MAMAN_IBAZ_H__ #define __MAMAN_IBAZ_H__ #include <glib-object.h> G_BEGIN_DECLS #define MAMAN_TYPE_IBAZ maman_ibaz_get_type () G_DECLARE_INTERFACE (MamanIbaz, maman_ibaz, MAMAN, IBAZ, GObject) struct _MamanIbazInterface { GTypeInterface parent_iface; void (*do_action) (MamanIbaz *self); void (*do_something) (MamanIbaz *self); }; void maman_ibaz_do_action (MamanIbaz *self); void maman_ibaz_do_something (MamanIbaz *self); G_END_DECLS #endif /* __MAMAN_IBAZ_H__ */ This code is the same as the code for a normal GType which derives from a GObject except for a few details: The _GET_CLASS function is called _GET_IFACE (and is defined by G_DECLARE_INTERFACE). The instance type, MamanIbaz is not fully defined: it is used merely as an abstract type which represents an instance of whatever object which implements the interface. The parent of the MamanIbazInterface is GTypeInterface, not GObjectClass. The implementation of the MamanIbaz type itself is trivial: G_DEFINE_INTERFACE creates a maman_ibaz_get_type function which registers the type in the type system. The third argument is used to define a prerequisite interface (which we'll talk about more later). Just pass 0 for this argument when an interface has no prerequisite. maman_ibaz_default_init is expected to register the interface's signals if there are any (we will see a bit later how to use them). The interface methods maman_ibaz_do_action and maman_ibaz_do_something dereference the interface structure to access its associated interface function and call it. G_DEFINE_INTERFACE (MamanIbaz, maman_ibaz, G_TYPE_OBJECT); static void maman_ibaz_default_init (MamanIbazInterface *iface) { /* add properties and signals to the interface here */ } void maman_ibaz_do_action (MamanIbaz *self) { g_return_if_fail (MAMAN_IS_IBAZ (self)); MAMAN_IBAZ_GET_IFACE (self)->do_action (self); } void maman_ibaz_do_something (MamanIbaz *self) { g_return_if_fail (MAMAN_IS_IBAZ (self)); MAMAN_IBAZ_GET_IFACE (self)->do_something (self); } Implementing interfaces Once the interface is defined, implementing it is rather trivial. The first step is to define a normal final GObject class exactly as in . The second step is to implement MamanBaz by defining it using G_DEFINE_TYPE_WITH_CODE and G_IMPLEMENT_INTERFACE instead of G_DEFINE_TYPE: static void maman_ibaz_interface_init (MamanIbazInterface *iface); G_DEFINE_TYPE_WITH_CODE (MamanBar, maman_bar, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (MAMAN_TYPE_IBAZ, maman_ibaz_interface_init)) This definition is very much like all the similar functions seen previously. The only interface-specific code present here is the use of G_IMPLEMENT_INTERFACE. Classes can implement multiple interfaces by using multiple calls to G_IMPLEMENT_INTERFACE inside the call to G_DEFINE_TYPE_WITH_CODE maman_baz_interface_init, the interface initialization function: inside it every virtual method of the interface must be assigned to its implementation: static void maman_baz_do_action (MamanBaz *self) { g_print ("Baz implementation of Ibaz interface Action: 0x%x.\n", self->instance_member); } static void maman_baz_do_something (MamanBaz *self) { g_print ("Baz implementation of Ibaz interface Something: 0x%x.\n", self->instance_member); } static void maman_ibaz_interface_init (MamanIbazInterface *iface) { iface->do_action = maman_baz_do_action; iface->do_something = maman_baz_do_something; } static void maman_baz_init (MamanBaz *self) { self->instance_member = 0xdeadbeef; } Interface definition prerequisites To specify that an interface requires the presence of other interfaces when implemented, GObject introduces the concept of prerequisites: it is possible to associate a list of prerequisite types to an interface. For example, if object A wishes to implement interface I1, and if interface I1 has a prerequisite on interface I2, A has to implement both I1 and I2. The mechanism described above is, in practice, very similar to Java's interface I1 extends interface I2. The example below shows the GObject equivalent: /* Make the MamanIbar interface require MamanIbaz interface. */ G_DEFINE_INTERFACE (MamanIbar, maman_ibar, MAMAN_TYPE_IBAZ); In the G_DEFINE_INTERFACE call above, the third parameter defines the prerequisite type. This is the GType of either an interface or a class. In this case the MamanIbaz interface is a prerequisite of MamanIbar. The code below shows how an implementation can implement both interfaces and register their implementations: static void maman_ibar_do_another_action (MamanIbar *ibar) { MamanBar *self = MAMAN_BAR (ibar); g_print ("Bar implementation of IBar interface Another Action: 0x%x.\n", self->instance_member); } static void maman_ibar_interface_init (MamanIbarInterface *iface) { iface->do_another_action = maman_ibar_do_another_action; } static void maman_ibaz_do_action (MamanIbaz *ibaz) { MamanBar *self = MAMAN_BAR (ibaz); g_print ("Bar implementation of Ibaz interface Action: 0x%x.\n", self->instance_member); } static void maman_ibaz_do_something (MamanIbaz *ibaz) { MamanBar *self = MAMAN_BAR (ibaz); g_print ("Bar implementation of Ibaz interface Something: 0x%x.\n", self->instance_member); } static void maman_ibaz_interface_init (MamanIbazInterface *iface) { iface->do_action = maman_ibaz_do_action; iface->do_something = maman_ibaz_do_something; } static void maman_bar_class_init (MamanBarClass *klass) { /* Nothing here. */ } static void maman_bar_init (MamanBar *self) { self->instance_member = 0x666; } G_DEFINE_TYPE_WITH_CODE (MamanBar, maman_bar, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (MAMAN_TYPE_IBAZ, maman_ibaz_interface_init) G_IMPLEMENT_INTERFACE (MAMAN_TYPE_IBAR, maman_ibar_interface_init)) It is very important to notice that the order in which interface implementations are added to the main object is not random: g_type_add_interface_static, which is called by G_IMPLEMENT_INTERFACE, must be invoked first on the interfaces which have no prerequisites and then on the others. Interface properties GObject interfaces can also have properties. Declaration of the interface properties is similar to declaring the properties of ordinary GObject types as explained in , except that g_object_interface_install_property is used to declare the properties instead of g_object_class_install_property. To include a property named 'name' of type string in the MamanIbaz interface example code above, we only need to add one call in maman_ibaz_default_init as shown below: static void maman_ibaz_default_init (MamanIbazInterface *iface) { g_object_interface_install_property (iface, g_param_spec_string ("name", "Name", "Name of the MamanIbaz", "maman", G_PARAM_READWRITE)); } One point worth noting is that the declared property wasn't assigned an integer ID. The reason being that integer IDs of properties are used only inside the get_property and set_property virtual methods. Since interfaces declare but do not implement properties, there is no need to assign integer IDs to them. An implementation declares and defines its properties in the usual way as explained in , except for one small change: it can declare the properties of the interface it implements using g_object_class_override_property instead of g_object_class_install_property. The following code snippet shows the modifications needed in the MamanBaz declaration and implementation above: struct _MamanBaz { GObject parent_instance; gint instance_member; gchar *name; }; enum { PROP_NAME = 1, N_PROPERTIES }; static void maman_baz_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) { MamanBaz *baz = MAMAN_BAZ (object); switch (prop_id) { case PROP_NAME: g_free (baz->name); baz->name = g_value_dup_string (value); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } } static void maman_baz_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) { MamanBaz *baz = MAMAN_BAZ (object); switch (prop_id) { case PROP_NAME: g_value_set_string (value, baz->name); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } } static void maman_baz_class_init (MamanBazClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); object_class->set_property = maman_baz_set_property; object_class->get_property = maman_baz_get_property; g_object_class_override_property (object_class, PROP_NAME, "name"); } Overriding interface methods If a base class already implements an interface and a derived class needs to implement the same interface but needs to override certain methods, you must reimplement the interface and set only the interface methods which need overriding. In this example, MamanDerivedBaz is derived from MamanBaz. Both implement the MamanIbaz interface. MamanDerivedBaz only implements one method of the MamanIbaz interface and uses the base class implementation of the other. static void maman_derived_ibaz_do_action (MamanIbaz *ibaz) { MamanDerivedBaz *self = MAMAN_DERIVED_BAZ (ibaz); g_print ("DerivedBaz implementation of Ibaz interface Action\n"); } static void maman_derived_ibaz_interface_init (MamanIbazInterface *iface) { /* Override the implementation of do_action */ iface->do_action = maman_derived_ibaz_do_action; /* * Leave iface->do_something alone, it is already set to the * base class implementation. */ } G_DEFINE_TYPE_WITH_CODE (MamanDerivedBaz, maman_derived_baz, MAMAN_TYPE_BAZ, G_IMPLEMENT_INTERFACE (MAMAN_TYPE_IBAZ, maman_derived_ibaz_interface_init)) static void maman_derived_baz_class_init (MamanDerivedBazClass *klass) { /* Nothing here. */ } static void maman_derived_baz_init (MamanDerivedBaz *self) { /* Nothing here. */ } To access the base class interface implementation use g_type_interface_peek_parent from within an interface's default_init function. To call the base class implementation of an interface method from an derived class where than interface method has been overridden, stash away the pointer returned from g_type_interface_peek_parent in a global variable. In this example MamanDerivedBaz overrides the do_action interface method. In its overridden method it calls the base class implementation of the same interface method. static MamanIbazInterface *maman_ibaz_parent_interface = NULL; static void maman_derived_ibaz_do_action (MamanIbaz *ibaz) { MamanDerivedBaz *self = MAMAN_DERIVED_BAZ (ibaz); g_print ("DerivedBaz implementation of Ibaz interface Action\n"); /* Now call the base implementation */ maman_ibaz_parent_interface->do_action (ibaz); } static void maman_derived_ibaz_interface_init (MamanIbazInterface *iface) { maman_ibaz_parent_interface = g_type_interface_peek_parent (iface); iface->do_action = maman_derived_ibaz_do_action; } G_DEFINE_TYPE_WITH_CODE (MamanDerivedBaz, maman_derived_baz, MAMAN_TYPE_BAZ, G_IMPLEMENT_INTERFACE (MAMAN_TYPE_IBAZ, maman_derived_ibaz_interface_init)) static void maman_derived_baz_class_init (MamanDerivedBazClass *klass) { /* Nothing here. */ } static void maman_derived_baz_init (MamanDerivedBaz *self) { /* Nothing here. */ } How to create and use signals The signal system in GType is pretty complex and flexible: it is possible for its users to connect at runtime any number of callbacks (implemented in any language for which a binding exists) A Python callback can be connected to any signal on any C-based GObject, and vice versa, assuming that the Python object inherits from GObject. to any signal and to stop the emission of any signal at any state of the signal emission process. This flexibility makes it possible to use GSignal for much more than just emitting signals to multiple clients. Simple use of signals The most basic use of signals is to implement event notification. For example, given a MamanFile object with a write method, a signal could be emitted whenever the file is changed using that method. The code below shows how the user can connect a callback to the "changed" signal. file = g_object_new (MAMAN_FILE_TYPE, NULL); g_signal_connect (file, "changed", (GCallback) changed_event, NULL); maman_file_write (file, buffer, strlen (buffer)); The MamanFile signal is registered in the class_init function: file_signals[CHANGED] = g_signal_newv ("changed", G_TYPE_FROM_CLASS (gobject_class), G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, NULL /* closure */, NULL /* accumulator */, NULL /* accumulator data */, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE /* return_type */, 0 /* n_params */, NULL /* param_types */); and the signal is emitted in maman_file_write: void maman_file_write (MamanFile *self, const guchar *buffer, gssize size) { g_return_if_fail (MAMAN_IS_FILE (self)); g_return_if_fail (buffer != NULL || size == 0); /* First write data. */ /* Then, notify user of data written. */ g_signal_emit (self, file_signals[CHANGED], 0 /* details */); } As shown above, the details parameter can safely be set to zero if no detail needs to be conveyed. For a discussion of what it can be used for, see The C signal marshaller should always be g_cclosure_marshal_generic, which implements generic conversion of arrays of parameters to C callback invocations. GLib used to use type-specific generated marshallers, but that has been deprecated in favour of the generic marshaller.