Your IP : 216.73.216.48


Current Path : /usr/X11R6/share/gtk-doc/html/gobject/
Upload File :
Current File : //usr/X11R6/share/gtk-doc/html/gobject/howto-interface-implement.html

<!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: 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.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.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.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="howto-interface-prerequisite.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-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 final GObject class exactly as in
      <a class="xref" href="howto-gobject.html#howto-gobject-header" title="Boilerplate header code">the section called “Boilerplate header code”</a>.
    </p>
<p>
      The second step is to implement <span class="type">MamanBaz</span> by defining
      it using
      <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
      <code class="function"><a class="link" href="gobject-Type-Information.html#G-IMPLEMENT-INTERFACE:CAPS" title="G_IMPLEMENT_INTERFACE()">G_IMPLEMENT_INTERFACE</a></code>
      instead of
      <code class="function"><a class="link" href="gobject-Type-Information.html#G-DEFINE-TYPE:CAPS" title="G_DEFINE_TYPE()">G_DEFINE_TYPE</a></code>:
</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</pre></td>
        <td class="listing_code"><pre class="programlisting"><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="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></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
      This definition is very much like all the similar functions seen
      previously. The only interface-specific code present here is the use of
      <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"><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>
<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</pre></td>
        <td class="listing_code"><pre class="programlisting"><span class="gtkdoc kwb">static void</span>
<span class="function">maman_baz_do_action</span> <span class="gtkdoc opt">(</span>MamanBaz <span class="gtkdoc opt">*</span>self<span class="gtkdoc opt">)</span>
<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">&quot;Baz implementation of Ibaz interface Action: 0x%x.</span><span class="gtkdoc esc">\n</span><span class="string">&quot;</span><span class="gtkdoc opt">,</span>
           self<span class="gtkdoc opt">-&gt;</span>instance_member<span class="gtkdoc opt">);</span>
<span class="gtkdoc opt">}</span>

<span class="gtkdoc kwb">static void</span>
<span class="function">maman_baz_do_something</span> <span class="gtkdoc opt">(</span>MamanBaz <span class="gtkdoc opt">*</span>self<span class="gtkdoc opt">)</span>
<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">&quot;Baz implementation of Ibaz interface Something: 0x%x.</span><span class="gtkdoc esc">\n</span><span class="string">&quot;</span><span class="gtkdoc opt">,</span>
           self<span class="gtkdoc opt">-&gt;</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">-&gt;</span>do_action <span class="gtkdoc opt">=</span> maman_baz_do_action<span class="gtkdoc opt">;</span>
  iface<span class="gtkdoc opt">-&gt;</span>do_something <span class="gtkdoc opt">=</span> maman_baz_do_something<span class="gtkdoc opt">;</span>
<span class="gtkdoc opt">}</span>

<span class="gtkdoc kwb">static void</span>
<span class="function">maman_baz_init</span> <span class="gtkdoc opt">(</span>MamanBaz <span class="gtkdoc opt">*</span>self<span class="gtkdoc opt">)</span>
<span class="gtkdoc opt">{</span>
  self<span class="gtkdoc opt">-&gt;</span>instance_member <span class="gtkdoc opt">=</span> <span class="number">0xdeadbeef</span><span class="gtkdoc opt">;</span>
<span class="gtkdoc opt">}</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
    </p>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.24</div>
</body>
</html>