How to define and implement a new GObject

Boilerplate header code
Boilerplate code
Object Construction
Object Destruction
Object methods
Non-virtual public methods
Virtual public methods
Virtual private Methods
Chaining up

Clearly, this is one of the most common questions people ask: they just want to crank code and implement a subclass of a GObject. Sometimes because they want to create their own class hierarchy, sometimes because they want to subclass one of GTK+'s widget. This chapter will focus on the implementation of a subtype of GObject.

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 not only by GTK+'s code but also by most users of GObject. If you feel the need not to obey the rules stated below, think about it twice:

  • If your users are a bit accustomed to GTK+ code or any GLib code, they will be a bit surprised and getting used to the conventions you decided upon will take time (money) and will make them grumpy (not a good thing)

  • You must assess the fact that these conventions might have been designed by both smart and experienced people: maybe they were at least partly right. Try to put your ego aside.

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.

When you need some private (internal) declarations in several (sub)classes, you can define them in a private header file which is often named by appending the private keyword to the public header name. For example, one could use maman-bar-private.h, maman_bar_private.h or mamanbarprivate.h. Typically, such private header files are not installed.

The basic conventions for any header which exposes a GType are described in the section called “Conventions”. Most GObject-based code also obeys one of of the following conventions: pick one and stick to it.

  • If you want to declare a type named bar with prefix maman, name the type instance MamanBar and its class MamanBarClass (name is case-sensitive). It is customary to declare them with code similar to the following:

    /*
     * 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.
     */
    
    /*
     * Type macros.
     */
    #define MAMAN_TYPE_BAR                  (maman_bar_get_type ())
    #define MAMAN_BAR(obj)                  (G_TYPE_CHECK_INSTANCE_CAST ((obj), MAMAN_TYPE_BAR, MamanBar))
    #define MAMAN_IS_BAR(obj)               (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MAMAN_TYPE_BAR))
    #define MAMAN_BAR_CLASS(klass)          (G_TYPE_CHECK_CLASS_CAST ((klass), MAMAN_TYPE_BAR, MamanBarClass))
    #define MAMAN_IS_BAR_CLASS(klass)       (G_TYPE_CHECK_CLASS_TYPE ((klass), MAMAN_TYPE_BAR))
    #define MAMAN_BAR_GET_CLASS(obj)        (G_TYPE_INSTANCE_GET_CLASS ((obj), MAMAN_TYPE_BAR, MamanBarClass))
    
    typedef struct _MamanBar        MamanBar;
    typedef struct _MamanBarClass   MamanBarClass;
    
    struct _MamanBar
    {
      GObject parent_instance;
    
      /* instance members */
    };
    
    struct _MamanBarClass
    {
      GObjectClass parent_class;
    
      /* class members */
    };
    
    /* used by MAMAN_TYPE_BAR */
    GType maman_bar_get_type (void);
    
    /*
     * Method definitions.
     */
    
    #endif /* __MAMAN_BAR_H__ */
    

  • Most GTK+ types declare their private fields in the public header with a /* private */ comment, relying on their user's intelligence not to try to play with these fields. Fields not marked private are considered public by default. The /* protected */ comment (same semantics as those of C++) is also used, mainly in the GType library, in code written by Tim Janik.

    struct _MamanBar
    {
      GObject parent_instance;
    
      /*< private >*/
      int hsize;
    };
    

  • All of Nautilus code and a lot of GNOME libraries use private indirection members, as described by Herb Sutter in his Pimpl articles(see Compilation Firewalls and The Fast Pimpl Idiom: he summarizes the different issues better than I will).

    typedef struct _MamanBarPrivate MamanBarPrivate;
    
    struct _MamanBar
    {
      GObject parent_instance;
        
      /*< private >*/
      MamanBarPrivate *priv;
    };
    

    Note

    Do not call this private, as that is a registered c++ keyword.

    The private structure is then defined in the .c file, using the g_type_class_add_private() function to notify the presence of a private memory area for each instance and it can either be retrieved using G_TYPE_INSTANCE_GET_PRIVATE() each time is needed, or assigned to the priv member of the instance structure inside the object's init function.

    #define MAMAN_BAR_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), MAMAN_TYPE_BAR, MamanBarPrivate))
    
    struct _MamanBarPrivate
    {
      int hsize;
    }
    
    static void
    maman_bar_class_init (MamanBarClass *klass)
    {
      g_type_class_add_private (klass, sizeof (MamanBarPrivate));
    }
    
    static void
    maman_bar_init (MamanBar *self)
    {
      MamanBarPrivate *priv;
    
      self->priv = priv = MAMAN_BAR_GET_PRIVATE (self);
    
      priv->hsize = 42;
    }
    

  • You don't need to free or allocate the private structure, only the objects or pointers that it may contain. Another advantage of this to the previous version is that is lessens memory fragmentation, as the public and private parts of the instance memory are allocated at once.

Finally, there are different header include conventions. Again, pick one and stick to it. I personally use indifferently any of the two, depending on the codebase I work on: the rule, as always, is consistency.

  • Some people add at the top of their headers a number of #include directives to pull in all the headers needed to compile client code. This allows client code to simply #include "maman-bar.h".

  • Other do not #include anything and expect the client to #include themselves the headers they need before including your header. This speeds up compilation because it minimizes the amount of pre-processor work. This can be used in conjunction with the re-declaration of certain unused types in the client code to minimize compile-time dependencies and thus speed up compilation.