Initial commit
[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.75.2">
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.13 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
13 <link rel="preface" href="pr01.html" title="Introduction">
14 <link rel="part" href="pt01.html" title="Part I. Concepts">
15 <link rel="chapter" href="chapter-intro.html" title="Background">
16 <link rel="chapter" href="chapter-gtype.html" title="The GLib Dynamic Type System">
17 <link rel="chapter" href="chapter-gobject.html" title="The GObject base class">
18 <link rel="chapter" href="chapter-signal.html" title="The GObject messaging system">
19 <link rel="reference" href="rn01.html" title="API Reference">
20 <link rel="reference" href="rn02.html" title="Tools Reference">
21 <link rel="part" href="pt02.html" title="Part IV. Tutorial">
22 <link rel="chapter" href="howto-gobject.html" title="How to define and implement a new GObject">
23 <link rel="chapter" href="howto-interface.html" title="How to define and implement interfaces">
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">
26 <link rel="chapter" href="tools-vala.html" title="Vala">
27 <link rel="chapter" href="tools-gob.html" title="GObject builder">
28 <link rel="chapter" href="tools-ginspector.html" title="Graphical inspection of GObjects">
29 <link rel="chapter" href="tools-refdb.html" title="Debugging reference count problems">
30 <link rel="chapter" href="tools-gtkdoc.html" title="Writing API docs">
31 <link rel="index" href="api-index-full.html" title="Index">
32 <link rel="index" href="api-index-deprecated.html" title="Index of deprecated symbols">
33 <link rel="index" href="api-index-2-2.html" title="Index of new symbols in 2.2">
34 <link rel="index" href="api-index-2-4.html" title="Index of new symbols in 2.4">
35 <link rel="index" href="api-index-2-6.html" title="Index of new symbols in 2.6">
36 <link rel="index" href="api-index-2-8.html" title="Index of new symbols in 2.8">
37 <link rel="index" href="api-index-2-10.html" title="Index of new symbols in 2.10">
38 <link rel="index" href="api-index-2-12.html" title="Index of new symbols in 2.12">
39 <link rel="index" href="api-index-2-14.html" title="Index of new symbols in 2.14">
40 <link rel="index" href="api-index-2-18.html" title="Index of new symbols in 2.18">
41 <link rel="index" href="api-index-2-22.html" title="Index of new symbols in 2.22">
42 <link rel="index" href="api-index-2-24.html" title="Index of new symbols in 2.24">
43 </head>
44 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
45 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
46 <td><a accesskey="p" href="howto-interface-implement.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
47 <td><a accesskey="u" href="howto-interface.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
48 <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
49 <th width="100%" align="center">GObject Reference Manual</th>
50 <td><a accesskey="n" href="howto-interface-properties.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
51 </tr></table>
52 <div class="sect1" title="Interface definition prerequisites">
53 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
54 <a name="id650299"></a>Interface definition prerequisites</h2></div></div></div>
55 <p>
56       To specify that an interface requires the presence of other interfaces
57       when implemented, GObject introduces the concept of
58       <span class="emphasis"><em>prerequisites</em></span>: it is possible to associate
59       a list of prerequisite interfaces to an interface. For example, if
60       object A wishes to implement interface I1, and if interface I1 has a
61       prerequisite on interface I2, A has to implement both I1 and I2.
62     </p>
63 <p>
64       The mechanism described above is, in practice, very similar to
65       Java's interface I1 extends interface I2. The example below shows
66       the GObject equivalent:
67 </p>
68 <pre class="programlisting">
69   /* inside the GType function of the MamanIbar interface */
70   type = g_type_register_static (G_TYPE_INTERFACE, "MamanIbar", &amp;info, 0);
71
72   /* Make the MamanIbar interface require MamanIbaz interface. */
73   g_type_interface_add_prerequisite (type, MAMAN_TYPE_IBAZ);
74 </pre>
75 <p>
76       The code shown above adds the MamanIbaz interface to the list of
77       prerequisites of MamanIbar while the code below shows how an
78       implementation can implement both interfaces and register their
79       implementations:
80 </p>
81 <pre class="programlisting">
82 static void
83 maman_ibar_do_another_action (MamanIbar *ibar)
84 {
85   MamanBar *self = MAMAN_BAR (ibar);
86
87   g_print ("Bar implementation of IBar interface Another Action: 0x%x.\n",
88            self-&gt;instance_member);
89 }
90
91 static void
92 maman_ibar_interface_init (MamanIbarInterface *iface)
93 {
94   iface-&gt;do_another_action = maman_ibar_do_another_action;
95 }
96
97 static void
98 maman_ibaz_do_action (MamanIbaz *ibaz)
99 {
100   MamanBar *self = MAMAN_BAR (ibaz);
101
102   g_print ("Bar implementation of IBaz interface Action: 0x%x.\n",
103            self-&gt;instance_member);
104 }
105
106 static void
107 maman_ibaz_interface_init (MamanIbazInterface *iface)
108 {
109   iface-&gt;do_action = maman_ibaz_do_action;
110 }
111
112 static void
113 maman_bar_class_init (MamanBarClass *klass)
114 {
115
116 }
117
118 static void
119 maman_bar_init (MamanBar *self)
120 {
121   self-&gt;instance_member = 0x666;
122 }
123
124 G_DEFINE_TYPE_WITH_CODE (MamanBar, maman_bar, G_TYPE_OBJECT,
125                          G_IMPLEMENT_INTERFACE (MAMAN_TYPE_IBAZ,
126                                                 maman_ibaz_interface_init)
127                          G_IMPLEMENT_INTERFACE (MAMAN_TYPE_IBAR,
128                                                 maman_ibar_interface_init));
129 </pre>
130 <p>
131       It is very important to notice that the order in which interface
132       implementations are added to the main object is not random:
133       <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>,
134       which is called by <code class="function">G_IMPLEMENT_INTERFACE</code>, must be
135       invoked first on the interfaces which have no prerequisites and then on
136       the others.
137     </p>
138 </div>
139 <div class="footer">
140 <hr>
141           Generated by GTK-Doc V1.13</div>
142 </body>
143 </html>