summaryrefstree
path: root/usr/share/gtk-doc/html/gobject/howto-interface-implement.html
blob: cdc7c10e7e23ea51d5b9f2f9aac7d0569caf55ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Implementing interfaces</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
<link rel="home" href="index.html" title="GObject Reference Manual">
<link rel="up" href="howto-interface.html" title="How to define and implement interfaces">
<link rel="prev" href="howto-interface.html" title="How to define and implement interfaces">
<link rel="next" href="howto-interface-prerequisite.html" title="Interface definition prerequisites">
<meta name="generator" content="GTK-Doc V1.19 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
<td><a accesskey="p" href="howto-interface.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
<td><a accesskey="u" href="howto-interface.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
<th width="100%" align="center">GObject Reference Manual</th>
<td><a accesskey="n" href="howto-interface-prerequisite.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
</tr></table>
<div class="sect1">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="howto-interface-implement"></a>Implementing interfaces</h2></div></div></div>
<p>
      Once the interface is defined, implementing it is rather trivial.
    </p>
<p>
      The first step is to define a normal GObject class, like:
</p>
<pre class="programlisting">
#ifndef __MAMAN_BAZ_H__
#define __MAMAN_BAZ_H__

#include &lt;glib-object.h&gt;

#define MAMAN_TYPE_BAZ             (maman_baz_get_type ())
#define MAMAN_BAZ(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), MAMAN_TYPE_BAZ, Mamanbaz))
#define MAMAN_IS_BAZ(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MAMAN_TYPE_BAZ))
#define MAMAN_BAZ_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), MAMAN_TYPE_BAZ, MamanbazClass))
#define MAMAN_IS_BAZ_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), MAMAN_TYPE_BAZ))
#define MAMAN_BAZ_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), MAMAN_TYPE_BAZ, MamanbazClass))


typedef struct _MamanBaz        MamanBaz;
typedef struct _MamanBazClass   MamanBazClass;

struct _MamanBaz
{
  GObject parent_instance;

  gint instance_member;
};

struct _MamanBazClass
{
  GObjectClass parent_class;
};

GType maman_baz_get_type (void);

#endif /* __MAMAN_BAZ_H__ */
</pre>
<p>
      
      There is clearly nothing specifically weird or scary about this header:
      it does not define any weird API or derive from a weird type.
    </p>
<p>
      The second step is to implement <span class="type">MamanBaz</span> by defining
      its GType. Instead of using
      <code class="function"><a class="link" href="gobject-Type-Information.html#G-DEFINE-TYPE:CAPS" title="G_DEFINE_TYPE()">G_DEFINE_TYPE</a></code>
      we use
      <code class="function"><a class="link" href="gobject-Type-Information.html#G-DEFINE-TYPE-WITH-CODE:CAPS" title="G_DEFINE_TYPE_WITH_CODE()">G_DEFINE_TYPE_WITH_CODE</a></code>
      and the
      <code class="function"><a class="link" href="gobject-Type-Information.html#G-IMPLEMENT-INTERFACE:CAPS" title="G_IMPLEMENT_INTERFACE()">G_IMPLEMENT_INTERFACE</a></code>
      macros.
</p>
<pre class="programlisting">
static void maman_ibaz_interface_init (MamanIbazInterface *iface);

G_DEFINE_TYPE_WITH_CODE (MamanBar, maman_bar, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (MAMAN_TYPE_IBAZ,
                                                maman_ibaz_interface_init))
</pre>
<p>
      This definition is very much like all the similar functions we looked
      at previously. The only interface-specific code present here is the call to
      <code class="function"><a class="link" href="gobject-Type-Information.html#G-IMPLEMENT-INTERFACE:CAPS" title="G_IMPLEMENT_INTERFACE()">G_IMPLEMENT_INTERFACE</a></code>.
    </p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>Classes can implement multiple interfaces by using multiple calls to
    <code class="function"><a class="link" href="gobject-Type-Information.html#G-IMPLEMENT-INTERFACE:CAPS" title="G_IMPLEMENT_INTERFACE()">G_IMPLEMENT_INTERFACE</a></code>
    inside the call to
    <code class="function"><a class="link" href="gobject-Type-Information.html#G-DEFINE-TYPE-WITH-CODE:CAPS" title="G_DEFINE_TYPE_WITH_CODE()">G_DEFINE_TYPE_WITH_CODE</a></code>
    </p>
</div>
<p>
      <code class="function">maman_baz_interface_init</code>, the interface
      initialization function: inside it every virtual method of the interface
      must be assigned to its implementation:
</p>
<pre class="programlisting">
static void
maman_baz_do_action (MamanBaz *self)
{
  g_print ("Baz implementation of Ibaz interface Action: 0x%x.\n",
           self-&gt;instance_member);
}

static void
maman_baz_do_something (MamanBaz *self)
{
  g_print ("Baz implementation of Ibaz interface Something: 0x%x.\n",
           self-&gt;instance_member);
}

static void
maman_ibaz_interface_init (MamanIbazInterface *iface)
{
  iface-&gt;do_action = maman_baz_do_action;
  iface-&gt;do_something = maman_baz_do_something;
}

static void
maman_baz_init (MamanBaz *self)
{
  MamanBaz *self = MAMAN_BAZ (instance);
  self-&gt;instance_member = 0xdeadbeaf;
}
</pre>
<p>
    </p>
</div>
<div class="footer">
<hr>
          Generated by GTK-Doc V1.19</div>
</body>
</html>