Tizen 2.1 base
[platform/upstream/glib2.0.git] / docs / reference / gobject / html / ch06s03.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <title>Interface definition prerequisites</title>
6 <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
7 <link rel="home" href="index.html" title="GObject Reference Manual">
8 <link rel="up" href="howto-interface.html" title="How to define and implement interfaces">
9 <link rel="prev" href="howto-interface-implement.html" title="How To define implement an Interface?">
10 <link rel="next" href="howto-interface-properties.html" title="Interface Properties">
11 <meta name="generator" content="GTK-Doc V1.18 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
13 </head>
14 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
15 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
16 <td><a accesskey="p" href="howto-interface-implement.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
17 <td><a accesskey="u" href="howto-interface.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
18 <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
19 <th width="100%" align="center">GObject Reference Manual</th>
20 <td><a accesskey="n" href="howto-interface-properties.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
21 </tr></table>
22 <div class="sect1">
23 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
24 <a name="id674886"></a>Interface definition prerequisites</h2></div></div></div>
25 <p>
26       To specify that an interface requires the presence of other interfaces
27       when implemented, GObject introduces the concept of
28       <span class="emphasis"><em>prerequisites</em></span>: it is possible to associate
29       a list of prerequisite interfaces to an interface. For example, if
30       object A wishes to implement interface I1, and if interface I1 has a
31       prerequisite on interface I2, A has to implement both I1 and I2.
32     </p>
33 <p>
34       The mechanism described above is, in practice, very similar to
35       Java's interface I1 extends interface I2. The example below shows
36       the GObject equivalent:
37 </p>
38 <pre class="programlisting">
39   /* inside the GType function of the MamanIbar interface */
40   type = g_type_register_static (G_TYPE_INTERFACE, "MamanIbar", &amp;info, 0);
41
42   /* Make the MamanIbar interface require MamanIbaz interface. */
43   g_type_interface_add_prerequisite (type, MAMAN_TYPE_IBAZ);
44 </pre>
45 <p>
46       The code shown above adds the MamanIbaz interface to the list of
47       prerequisites of MamanIbar while the code below shows how an
48       implementation can implement both interfaces and register their
49       implementations:
50 </p>
51 <pre class="programlisting">
52 static void
53 maman_ibar_do_another_action (MamanIbar *ibar)
54 {
55   MamanBar *self = MAMAN_BAR (ibar);
56
57   g_print ("Bar implementation of IBar interface Another Action: 0x%x.\n",
58            self-&gt;instance_member);
59 }
60
61 static void
62 maman_ibar_interface_init (MamanIbarInterface *iface)
63 {
64   iface-&gt;do_another_action = maman_ibar_do_another_action;
65 }
66
67 static void
68 maman_ibaz_do_action (MamanIbaz *ibaz)
69 {
70   MamanBar *self = MAMAN_BAR (ibaz);
71
72   g_print ("Bar implementation of IBaz interface Action: 0x%x.\n",
73            self-&gt;instance_member);
74 }
75
76 static void
77 maman_ibaz_interface_init (MamanIbazInterface *iface)
78 {
79   iface-&gt;do_action = maman_ibaz_do_action;
80 }
81
82 static void
83 maman_bar_class_init (MamanBarClass *klass)
84 {
85
86 }
87
88 static void
89 maman_bar_init (MamanBar *self)
90 {
91   self-&gt;instance_member = 0x666;
92 }
93
94 G_DEFINE_TYPE_WITH_CODE (MamanBar, maman_bar, G_TYPE_OBJECT,
95                          G_IMPLEMENT_INTERFACE (MAMAN_TYPE_IBAZ,
96                                                 maman_ibaz_interface_init)
97                          G_IMPLEMENT_INTERFACE (MAMAN_TYPE_IBAR,
98                                                 maman_ibar_interface_init));
99 </pre>
100 <p>
101       It is very important to notice that the order in which interface
102       implementations are added to the main object is not random:
103       <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>,
104       which is called by <code class="function">G_IMPLEMENT_INTERFACE</code>, must be
105       invoked first on the interfaces which have no prerequisites and then on
106       the others.
107     </p>
108 </div>
109 <div class="footer">
110 <hr>
111           Generated by GTK-Doc V1.18</div>
112 </body>
113 </html>