Conventions: GObject Reference Manual

## Conventions

There are a number of conventions users are expected to follow when creating new types which are to be exported in a header file:

- Type names (including object names) must be at least three characters long and start with ‘a–z’, ‘A–Z’ or ‘_’.

- Use the `object_method` pattern for function names: to invoke the method named foo on an instance of object type bar, call `bar_foo`.

- Use prefixing to avoid namespace conflicts with other projects. If your library (or application) is named Maman, [2] prefix all your function names with maman_. For example: `maman_object_method`.

- Create a macro named `PREFIX_TYPE_OBJECT` which always returns the GType for the associated object type. For an object of type Bar in a library prefixed by maman, use: `MAMAN_TYPE_BAR`. This macro is implemented using a function named `prefix_object_get_type`.

- Use `G_DECLARE_FINAL_TYPE` or `G_DECLARE_DERIVABLE_TYPE` to define various other conventional macros for your object:

  - `PREFIX_OBJECT (obj)`, which returns a pointer of type PrefixObject. This macro is used to enforce static type safety by doing explicit casts wherever needed. It also enforces dynamic type safety by doing runtime checks. It is possible to disable the dynamic type checks in production builds (see building GLib). For example, we would create `MAMAN_BAR (obj)` to keep the previous example.

  - `PREFIX_OBJECT_CLASS (klass)`, which is strictly equivalent to the previous casting macro: it does static casting with dynamic type checking of class structures. It is expected to return a pointer to a class structure of type PrefixObjectClass. An example is: `MAMAN_BAR_CLASS`.

  - `PREFIX_IS_BAR (obj)`, which returns a gboolean which indicates whether the input object instance pointer is non-NULL and of type BAR.

  - `PREFIX_IS_OBJECT_CLASS (klass)`, which returns a boolean if the input class pointer is a pointer to a class of type OBJECT.

  - `PREFIX_OBJECT_GET_CLASS (obj)`, which returns the class pointer associated to an instance of a given type. This macro is used for static and dynamic type safety purposes (just like the previous casting macros).

The implementation of these macros is pretty straightforward: a number of simple-to-use macros are provided in `gtype.h`. For the example we used above, we would write the following trivial code to declare the macros:

    1
    2
    #define MAMAN_TYPE_BAR maman_bar_get_type ()
    G_DECLARE_FINAL_TYPE (MamanBar, maman_bar, MAMAN, BAR, GObject)

Unless your code has special requirements, you can use the `G_DEFINE_TYPE` macro to define a class:

    1
    G_DEFINE_TYPE (MamanBar, maman_bar, G_TYPE_OBJECT)

Otherwise, the `maman_bar_get_type` function must be implemented manually:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    GType maman_bar_get_type (void)
    {
      static GType type = 0;
      if (type == 0) {
        const GTypeInfo info = {
          /* You fill this structure. */
        };
        type = g_type_register_static (G_TYPE_OBJECT,
                                       "MamanBarType",
                                       &info, 0);
      }
      return type;
    }

[2] Maman is the French word for mum or mother — nothing more and nothing less.