Your IP : 216.73.216.48
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Interface definition prerequisites: GObject Reference Manual</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-implement.html" title="Implementing interfaces">
<link rel="next" href="howto-interface-properties.html" title="Interface properties">
<meta name="generator" content="GTK-Doc V1.24 (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="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts"></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="howto-interface.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="howto-interface-implement.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="howto-interface-properties.html"><img src="right.png" width="16" height="16" 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-prerequisite"></a>Interface definition prerequisites</h2></div></div></div>
<p>
To specify that an interface requires the presence of other interfaces
when implemented, GObject introduces the concept of
<span class="emphasis"><em>prerequisites</em></span>: it is possible to associate
a list of prerequisite types to an interface. For example, if
object A wishes to implement interface I1, and if interface I1 has a
prerequisite on interface I2, A has to implement both I1 and I2.
</p>
<p>
The mechanism described above is, in practice, very similar to
Java's interface I1 extends interface I2. The example below shows
the GObject equivalent:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="comment">/* Make the MamanIbar interface require MamanIbaz interface. */</span>
<span class="function"><a href="gobject-Type-Information.html#G-DEFINE-INTERFACE:CAPS">G_DEFINE_INTERFACE</a></span> <span class="gtkdoc opt">(</span>MamanIbar<span class="gtkdoc opt">,</span> maman_ibar<span class="gtkdoc opt">,</span> MAMAN_TYPE_IBAZ<span class="gtkdoc opt">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
In the <code class="function"><a class="link" href="gobject-Type-Information.html#G-DEFINE-INTERFACE:CAPS" title="G_DEFINE_INTERFACE()">G_DEFINE_INTERFACE</a></code>
call above, the third parameter defines the prerequisite type. This
is the GType of either an interface or a class. In this case
the <span class="type">MamanIbaz</span> interface is a prerequisite of
<span class="type">MamanIbar</span>. The code
below shows how an implementation can implement both interfaces and
register their implementations:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>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</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="gtkdoc kwb">static void</span>
<span class="function">maman_ibar_do_another_action</span> <span class="gtkdoc opt">(</span>MamanIbar <span class="gtkdoc opt">*</span>ibar<span class="gtkdoc opt">)</span>
<span class="gtkdoc opt">{</span>
MamanBar <span class="gtkdoc opt">*</span>self <span class="gtkdoc opt">=</span> <span class="function">MAMAN_BAR</span> <span class="gtkdoc opt">(</span>ibar<span class="gtkdoc opt">);</span>
<span class="function"><a href="/usr/share/gtk-doc/html/glib/glib-Warnings-and-Assertions.html#g-print">g_print</a></span> <span class="gtkdoc opt">(</span><span class="string">"Bar implementation of IBar interface Another Action: 0x%x.</span><span class="gtkdoc esc">\n</span><span class="string">"</span><span class="gtkdoc opt">,</span>
self<span class="gtkdoc opt">-></span>instance_member<span class="gtkdoc opt">);</span>
<span class="gtkdoc opt">}</span>
<span class="gtkdoc kwb">static void</span>
<span class="function">maman_ibar_interface_init</span> <span class="gtkdoc opt">(</span>MamanIbarInterface <span class="gtkdoc opt">*</span>iface<span class="gtkdoc opt">)</span>
<span class="gtkdoc opt">{</span>
iface<span class="gtkdoc opt">-></span>do_another_action <span class="gtkdoc opt">=</span> maman_ibar_do_another_action<span class="gtkdoc opt">;</span>
<span class="gtkdoc opt">}</span>
<span class="gtkdoc kwb">static void</span>
<span class="function">maman_ibaz_do_action</span> <span class="gtkdoc opt">(</span>MamanIbaz <span class="gtkdoc opt">*</span>ibaz<span class="gtkdoc opt">)</span>
<span class="gtkdoc opt">{</span>
MamanBar <span class="gtkdoc opt">*</span>self <span class="gtkdoc opt">=</span> <span class="function">MAMAN_BAR</span> <span class="gtkdoc opt">(</span>ibaz<span class="gtkdoc opt">);</span>
<span class="function"><a href="/usr/share/gtk-doc/html/glib/glib-Warnings-and-Assertions.html#g-print">g_print</a></span> <span class="gtkdoc opt">(</span><span class="string">"Bar implementation of Ibaz interface Action: 0x%x.</span><span class="gtkdoc esc">\n</span><span class="string">"</span><span class="gtkdoc opt">,</span>
self<span class="gtkdoc opt">-></span>instance_member<span class="gtkdoc opt">);</span>
<span class="gtkdoc opt">}</span>
<span class="gtkdoc kwb">static void</span>
<span class="function">maman_ibaz_do_something</span> <span class="gtkdoc opt">(</span>MamanIbaz <span class="gtkdoc opt">*</span>ibaz<span class="gtkdoc opt">)</span>
<span class="gtkdoc opt">{</span>
MamanBar <span class="gtkdoc opt">*</span>self <span class="gtkdoc opt">=</span> <span class="function">MAMAN_BAR</span> <span class="gtkdoc opt">(</span>ibaz<span class="gtkdoc opt">);</span>
<span class="function"><a href="/usr/share/gtk-doc/html/glib/glib-Warnings-and-Assertions.html#g-print">g_print</a></span> <span class="gtkdoc opt">(</span><span class="string">"Bar implementation of Ibaz interface Something: 0x%x.</span><span class="gtkdoc esc">\n</span><span class="string">"</span><span class="gtkdoc opt">,</span>
self<span class="gtkdoc opt">-></span>instance_member<span class="gtkdoc opt">);</span>
<span class="gtkdoc opt">}</span>
<span class="gtkdoc kwb">static void</span>
<span class="function">maman_ibaz_interface_init</span> <span class="gtkdoc opt">(</span>MamanIbazInterface <span class="gtkdoc opt">*</span>iface<span class="gtkdoc opt">)</span>
<span class="gtkdoc opt">{</span>
iface<span class="gtkdoc opt">-></span>do_action <span class="gtkdoc opt">=</span> maman_ibaz_do_action<span class="gtkdoc opt">;</span>
iface<span class="gtkdoc opt">-></span>do_something <span class="gtkdoc opt">=</span> maman_ibaz_do_something<span class="gtkdoc opt">;</span>
<span class="gtkdoc opt">}</span>
<span class="gtkdoc kwb">static void</span>
<span class="function">maman_bar_class_init</span> <span class="gtkdoc opt">(</span>MamanBarClass <span class="gtkdoc opt">*</span>klass<span class="gtkdoc opt">)</span>
<span class="gtkdoc opt">{</span>
<span class="comment">/* Nothing here. */</span>
<span class="gtkdoc opt">}</span>
<span class="gtkdoc kwb">static void</span>
<span class="function">maman_bar_init</span> <span class="gtkdoc opt">(</span>MamanBar <span class="gtkdoc opt">*</span>self<span class="gtkdoc opt">)</span>
<span class="gtkdoc opt">{</span>
self<span class="gtkdoc opt">-></span>instance_member <span class="gtkdoc opt">=</span> <span class="number">0x666</span><span class="gtkdoc opt">;</span>
<span class="gtkdoc opt">}</span>
<span class="function"><a href="gobject-Type-Information.html#G-DEFINE-TYPE-WITH-CODE:CAPS">G_DEFINE_TYPE_WITH_CODE</a></span> <span class="gtkdoc opt">(</span>MamanBar<span class="gtkdoc opt">,</span> maman_bar<span class="gtkdoc opt">,</span> G_TYPE_OBJECT<span class="gtkdoc opt">,</span>
<span class="function"><a href="gobject-Type-Information.html#G-IMPLEMENT-INTERFACE:CAPS">G_IMPLEMENT_INTERFACE</a></span> <span class="gtkdoc opt">(</span>MAMAN_TYPE_IBAZ<span class="gtkdoc opt">,</span>
maman_ibaz_interface_init<span class="gtkdoc opt">)</span>
<span class="function"><a href="gobject-Type-Information.html#G-IMPLEMENT-INTERFACE:CAPS">G_IMPLEMENT_INTERFACE</a></span> <span class="gtkdoc opt">(</span>MAMAN_TYPE_IBAR<span class="gtkdoc opt">,</span>
maman_ibar_interface_init<span class="gtkdoc opt">))</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
It is very important to notice that the order in which interface
implementations are added to the main object is not random:
<code class="function"><a class="link" href="gobject-Type-Information.html#g-type-add-interface-static" title="g_type_add_interface_static ()">g_type_add_interface_static</a></code>,
which is called by
<code class="function"><a class="link" href="gobject-Type-Information.html#G-DEFINE-INTERFACE:CAPS" title="G_DEFINE_INTERFACE()">G_IMPLEMENT_INTERFACE</a></code>,
must be invoked first on the interfaces which have no prerequisites and then on
the others.
</p>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.24</div>
</body>
</html>