23
23
<link rel="chapter" href="howto-interface.html" title="How to define and implement interfaces">
24
24
<link rel="chapter" href="howto-signals.html" title="How to create and use signals">
25
25
<link rel="part" href="pt03.html" title="Part V. Related Tools">
26
<link rel="chapter" href="tools-vala.html" title="Vala">
26
27
<link rel="chapter" href="tools-gob.html" title="GObject builder">
27
28
<link rel="chapter" href="tools-ginspector.html" title="Graphical inspection of GObjects">
28
29
<link rel="chapter" href="tools-refdb.html" title="Debugging reference count problems">
59
60
<a name="howto-interface-define"></a>How to define interfaces</h2></div></div></div>
61
62
The bulk of interface definition has already been shown in <a class="xref" href="gtype-non-instantiable-classed.html" title="Non-instantiable classed types: interfaces">the section called “Non-instantiable classed types: interfaces”</a>
62
but I feel it is needed to show exactly how to create an interface. The sample source code
63
associated to this section can be found in the documentation's source tarball, in the
64
<code class="filename">sample/interface/maman-ibaz.{h|c}</code> file.
63
but I feel it is needed to show exactly how to create an interface.
67
66
As above, the first step is to get the header right:
69
68
<pre class="programlisting">
69
#ifndef __MAMAN_IBAZ_H__
70
#define __MAMAN_IBAZ_H__
73
72
#include <glib-object.h>
75
#define MAMAN_TYPE_IBAZ (maman_ibaz_get_type ())
76
#define MAMAN_IBAZ(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MAMAN_TYPE_IBAZ, MamanIbaz))
77
#define MAMAN_IS_IBAZ(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MAMAN_TYPE_IBAZ))
78
#define MAMAN_IBAZ_GET_INTERFACE(inst) (G_TYPE_INSTANCE_GET_INTERFACE ((inst), MAMAN_TYPE_IBAZ, MamanIbazInterface))
81
typedef struct _MamanIbaz MamanIbaz; /* dummy object */
82
typedef struct _MamanIbazInterface MamanIbazInterface;
84
struct _MamanIbazInterface {
85
GTypeInterface parent;
74
#define MAMAN_TYPE_IBAZ (maman_ibaz_get_type ())
75
#define MAMAN_IBAZ(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MAMAN_TYPE_IBAZ, MamanIbaz))
76
#define MAMAN_IS_IBAZ(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MAMAN_TYPE_IBAZ))
77
#define MAMAN_IBAZ_GET_INTERFACE(inst) (G_TYPE_INSTANCE_GET_INTERFACE ((inst), MAMAN_TYPE_IBAZ, MamanIbazInterface))
80
typedef struct _MamanIbaz MamanIbaz; /* dummy object */
81
typedef struct _MamanIbazInterface MamanIbazInterface;
83
struct _MamanIbazInterface
85
GTypeInterface parent_iface;
87
87
void (*do_action) (MamanIbaz *self);
104
104
but with <code class="function">G_TYPE_INSTANCE_GET_INTERFACE</code>.
107
The instance type, <span class="type">MamanIbaz</span> is not fully defined: it is used merely as an abstract
108
type which represents an instance of whatever object which implements the interface.
107
The instance type, <span class="type">MamanIbaz</span> is not fully defined: it is
108
used merely as an abstract type which represents an instance of
109
whatever object which implements the interface.
112
The parent of the <span class="type">MamanIbazInterface</span> is not
113
<span class="type">GObjectClass</span> but <span class="type">GTypeInterface</span>.
124
129
<a class="xref" href="gtype-non-instantiable-classed.html#gtype-non-instantiable-classed-init" title="Interface Initialization">the section called “Interface Initialization”</a>,
125
130
<code class="function">base_init</code> is run once for each interface implementation
126
131
instantiation)</p></li>
127
<li><p><code class="function">maman_ibaz_do_action</code> dereferences the class
128
structure to access its associated class function and calls it.
132
<li><p><code class="function">maman_ibaz_do_action</code> dereferences
133
the class structure to access its associated class function and calls it.
135
140
maman_ibaz_base_init (gpointer g_class)
137
static gboolean initialized = FALSE;
140
/* create interface signals here. */
142
static gboolean is_initialized = FALSE;
146
/* add properties and signals to the interface here */
148
is_initialized = TRUE;
146
153
maman_ibaz_get_type (void)
148
static GType type = 0;
150
static const GTypeInfo info = {
151
sizeof (MamanIbazInterface),
152
maman_ibaz_base_init, /* base_init */
153
NULL, /* base_finalize */
154
NULL, /* class_init */
155
NULL, /* class_finalize */
156
NULL, /* class_data */
159
NULL /* instance_init */
161
type = g_type_register_static (G_TYPE_INTERFACE, "MamanIbaz", &info, 0);
155
static GType iface_type = 0;
158
static const GTypeInfo info = {
159
sizeof (MamanIbazInterface),
160
maman_ibaz_base_init, /* base_init */
161
NULL, /* base_finalize */
164
iface_type = g_type_register_static (G_TYPE_INTERFACE, "MamanIbaz",
166
void maman_ibaz_do_action (MamanIbaz *self)
172
maman_ibaz_do_action (MamanIbaz *self)
174
g_return_if_fail (MAMAN_IS_IBAZ (self));
168
176
MAMAN_IBAZ_GET_INTERFACE (self)->do_action (self);