1
1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5
5
<title>How To define implement an Interface?</title>
6
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2">
7
<link rel="start" href="index.html" title="GObject Reference Manual">
6
<meta name="generator" content="DocBook XSL Stylesheets V1.74.0">
7
<link rel="home" href="index.html" title="GObject Reference Manual">
8
8
<link rel="up" href="howto-interface.html" title="How to define and implement interfaces">
9
9
<link rel="prev" href="howto-interface.html" title="How to define and implement interfaces">
10
10
<link rel="next" href="ch06s03.html" title="Interface definition prerequisites">
11
<meta name="generator" content="GTK-Doc V1.9 (XML mode)">
11
<meta name="generator" content="GTK-Doc V1.11 (XML mode)">
12
12
<link rel="stylesheet" href="style.css" type="text/css">
13
13
<link rel="preface" href="pr01.html" title="Introduction">
14
<link rel="part" href="pt01.html" title="Part I. Concepts">
14
<link rel="part" href="pt01.html" title="Part I. Concepts">
15
15
<link rel="chapter" href="chapter-intro.html" title="Background">
16
16
<link rel="chapter" href="chapter-gtype.html" title="The GLib Dynamic Type System">
17
17
<link rel="chapter" href="chapter-gobject.html" title="The GObject base class">
18
18
<link rel="chapter" href="chapter-signal.html" title="The GObject messaging system">
19
19
<link rel="reference" href="rn01.html" title="API Reference">
20
20
<link rel="reference" href="rn02.html" title="Tools Reference">
21
<link rel="part" href="pt02.html" title="Part IV. Tutorial">
21
<link rel="part" href="pt02.html" title="Part IV. Tutorial">
22
22
<link rel="chapter" href="howto-gobject.html" title="How to define and implement a new GObject">
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
<link rel="part" href="pt03.html" title="Part V. Related Tools">
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">
49
51
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
50
52
<a name="howto-interface-implement"></a>How To define implement an Interface?</h2></div></div></div>
52
Once the interface is defined, implementing it is rather trivial. Source code showing how to do this
53
for the <span class="type">IBaz</span> interface defined in the previous section is located in
54
<code class="filename">sample/interface/maman-baz.{h|c}</code>.
54
Once the interface is defined, implementing it is rather trivial.
57
The first step is to define a normal GType. Here, we have decided to use a GType which derives from
58
GObject. Its name is <span class="type">MamanBaz</span>:
57
The first step is to define a normal GObject class, like:
60
59
<pre class="programlisting">
60
#ifndef __MAMAN_BAZ_H__
61
#define __MAMAN_BAZ_H__
64
63
#include <glib-object.h>
66
65
#define MAMAN_TYPE_BAZ (maman_baz_get_type ())
67
66
#define MAMAN_BAZ(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MAMAN_TYPE_BAZ, Mamanbaz))
68
#define MAMAN_BAZ_CLASS(vtable) (G_TYPE_CHECK_CLASS_CAST ((vtable), MAMAN_TYPE_BAZ, MamanbazClass))
69
67
#define MAMAN_IS_BAZ(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MAMAN_TYPE_BAZ))
70
#define MAMAN_IS_BAZ_CLASS(vtable) (G_TYPE_CHECK_CLASS_TYPE ((vtable), MAMAN_TYPE_BAZ))
71
#define MAMAN_BAZ_GET_CLASS(inst) (G_TYPE_INSTANCE_GET_CLASS ((inst), MAMAN_TYPE_BAZ, MamanbazClass))
74
typedef struct _MamanBaz MamanBaz;
75
typedef struct _MamanBazClass MamanBazClass;
68
#define MAMAN_BAZ_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MAMAN_TYPE_BAZ, MamanbazClass))
69
#define MAMAN_IS_BAZ_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MAMAN_TYPE_BAZ))
70
#define MAMAN_BAZ_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MAMAN_TYPE_BAZ, MamanbazClass))
73
typedef struct _MamanBaz MamanBaz;
74
typedef struct _MamanBazClass MamanBazClass;
78
GObject parent_instance;
79
80
int instance_member;
82
struct _MamanBazClass {
85
GObjectClass parent_class;
86
88
GType maman_baz_get_type (void);
92
There is clearly nothing specifically weird or scary about this header: it does not define any weird API
93
or derives from a weird type.
96
The second step is to implement <code class="function">maman_baz_get_type</code>:
98
<pre class="programlisting">
100
maman_baz_get_type (void)
102
static GType type = 0;
104
static const GTypeInfo info = {
105
sizeof (MamanBazClass),
106
NULL, /* base_init */
107
NULL, /* base_finalize */
108
NULL, /* class_init */
109
NULL, /* class_finalize */
110
NULL, /* class_data */
113
baz_instance_init /* instance_init */
115
static const GInterfaceInfo ibaz_info = {
116
(GInterfaceInitFunc) baz_interface_init, /* interface_init */
117
NULL, /* interface_finalize */
118
NULL /* interface_data */
120
type = g_type_register_static (G_TYPE_OBJECT,
123
g_type_add_interface_static (type,
131
This function is very much like all the similar functions we looked at previously. The only interface-specific
132
code present here is the call to <code class="function"><a class="link" href="gobject-Type-Information.html#g-type-add-interface-static">g_type_add_interface_static</a></code> which is used to inform
133
the type system that this just-registered <span class="type"><a class="link" href="gobject-Type-Information.html#GType">GType</a></span> also implements the interface
134
<code class="function">MAMAN_TYPE_IBAZ</code>.
137
<code class="function">baz_interface_init</code>, the interface initialization function, is also pretty simple:
139
<pre class="programlisting">
140
static void baz_do_action (MamanBaz *self)
142
g_print ("Baz implementation of IBaz interface Action: 0x%x.\n", self->instance_member);
145
baz_interface_init (gpointer g_iface,
148
MamanIbazInteface *iface = (MamanIbazInteface *)g_iface;
149
iface->do_action = (void (*) (MamanIbaz *self))baz_do_action;
152
baz_instance_init (GTypeInstance *instance,
155
MamanBaz *self = MAMAN_BAZ(instance);
90
#endif /* __MAMAN_BAZ_H__ */
93
There is clearly nothing specifically weird or scary about this header:
94
it does not define any weird API or derives from a weird type.
97
The second step is to implement <span class="type">MamanBaz</span> by defining
98
its GType. Instead of using <code class="function">G_DEFINE_TYPE</code> we
99
use <code class="function">G_DEFINE_TYPE_WITH_CODE</code> and the
100
<code class="function">G_IMPLEMENT_INTERFACE</code> macros.
102
<pre class="programlisting">
103
static void maman_ibaz_interface_init (MamanIbazInterface *iface);
105
G_DEFINE_TYPE_WITH_CODE (MamanBar, maman_bar, G_TYPE_OBJECT,
106
G_IMPLEMENT_INTERFACE (MAMAN_TYPE_IBAZ,
107
maman_ibaz_interface_init));
110
This definition is very much like all the similar functions we looked
111
at previously. The only interface-specific code present here is the call
112
to <code class="function">G_IMPLEMENT_INTERFACE</code>.
114
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
115
<h3 class="title">Note</h3>
116
<p>Classes can implement multiple interfaces by using multiple
117
calls to <code class="function">G_IMPLEMENT_INTERFACE</code> inside the call
118
to <code class="function">G_DEFINE_TYPE_WITH_CODE</code>.</p>
121
<code class="function">maman_baz_interface_init</code>, the interface
122
initialization function: inside it every virtual method of the interface
123
must be assigned to its implementation:
125
<pre class="programlisting">
127
maman_baz_do_action (MamanBaz *self)
129
g_print ("Baz implementation of IBaz interface Action: 0x%x.\n",
130
self->instance_member);
134
maman_ibaz_interface_init (MamanIbazInterface *iface)
136
iface->do_action = baz_do_action;
140
maman_baz_init (MamanBaz *self)
142
MamanBaz *self = MAMAN_BAZ (instance);
156
143
self->instance_member = 0xdeadbeaf;
160
<code class="function">baz_interface_init</code> merely initializes the interface methods to the implementations
161
defined by <span class="type">MamanBaz</span>: <code class="function">maman_baz_do_action</code> does nothing very useful
151
Generated by GTK-Doc V1.11</div>