manpagez: man pages & more
html files: gobject
Home | html | info | man

Boilerplate code

In your code, the first step is to #include the needed headers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 * Copyright information
 */

#include "viewer-file.h"

/* Private structure definition. */
typedef struct {
  gchar *filename;
  /* stuff */
} ViewerFilePrivate;

/* 
 * 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:

1
2
3
4
5
6
struct _ViewerFile
{
  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 viewer_file_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 the section called “Boilerplate header code”), private data should be placed in the instance structure, ViewerFile, 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.

1
G_DEFINE_TYPE (ViewerFile, viewer_file, G_TYPE_OBJECT)

or

1
G_DEFINE_TYPE_WITH_PRIVATE (ViewerFile, viewer_file, 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.

© manpagez.com 2000-2024
Individual documents may contain additional copyright information.