| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | |
| <html> | |
| <head> | |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
| <title>Chaining up: GObject Reference Manual</title> | |
| <meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> | |
| <link rel="home" href="index.html" title="GObject Reference Manual"> | |
| <link rel="up" href="howto-gobject.html" title="How to define and implement a new GObject"> | |
| <link rel="prev" href="howto-gobject-methods.html" title="Object methods"> | |
| <link rel="next" href="howto-interface.html" title="How to define and implement interfaces"> | |
| <meta name="generator" content="GTK-Doc V1.25.1 (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-gobject.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td> | |
| <td><a accesskey="p" href="howto-gobject-methods.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td> | |
| <td><a accesskey="n" href="howto-interface.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-gobject-chainup"></a>Chaining up</h2></div></div></div> | |
| <p>Chaining up is often loosely defined by the following set of | |
| conditions: | |
| </p> | |
| <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> | |
| <li class="listitem"><p>Parent class A defines a public virtual method named <code class="function">foo</code> and | |
| provides a default implementation.</p></li> | |
| <li class="listitem"><p>Child class B re-implements method <code class="function">foo</code>.</p></li> | |
| <li class="listitem"><p>B’s implementation of <code class="function">foo</code> calls (‘chains up to’) its parent class A’s implementation of <code class="function">foo</code>.</p></li> | |
| </ul></div> | |
| <p> | |
| There are various uses of this idiom: | |
| </p> | |
| <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> | |
| <li class="listitem"><p>You need to extend the behaviour of a class without modifying its code. You create | |
| a subclass to inherit its implementation, re-implement a public virtual method to modify the behaviour | |
| and chain up to ensure that the previous behaviour is not really modified, just extended. | |
| </p></li> | |
| <li class="listitem"><p>You need to implement the | |
| <a class="ulink" href="http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern" target="_top">Chain | |
| Of Responsibility pattern</a>: each object of the inheritance | |
| tree chains up to its parent (typically, at the beginning or the end of the method) to ensure that | |
| each handler is run in turn.</p></li> | |
| </ul></div> | |
| <p> | |
| </p> | |
| <p> | |
| To explicitly chain up to the implementation of the virtual method in the parent class, | |
| you first need a handle to the original parent class structure. This pointer can then be used to | |
| access the original virtual function pointer and invoke it directly. | |
| <a href="#ftn.id-1.6.3.9.3.1" class="footnote" name="id-1.6.3.9.3.1"><sup class="footnote">[7]</sup></a> | |
| </p> | |
| <p> | |
| Use the <code class="function">parent_class</code> pointer created and initialized | |
| by the | |
| <a class="link" href="gobject-Type-Information.html#G-DEFINE-TYPE:CAPS" title="G_DEFINE_TYPE()"><code class="function">G_DEFINE_TYPE</code></a> | |
| family of macros, for instance: | |
| </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</pre></td> | |
| <td class="listing_code"><pre class="programlisting"><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span> | |
| <span class="function">b_method_to_call</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">B</span><span class="normal"> </span><span class="symbol">*</span><span class="normal">obj</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">gint</span><span class="normal"> some_param</span><span class="symbol">)</span> | |
| <span class="cbracket">{</span> | |
| <span class="normal"> </span><span class="comment">/* do stuff before chain up */</span> | |
| <span class="normal"> </span><span class="comment">/* call the method_to_call() virtual function on the</span> | |
| <span class="comment"> * parent of BClass, AClass.</span> | |
| <span class="comment"> *</span> | |
| <span class="comment"> * remember the explicit cast to AClass*</span> | |
| <span class="comment"> */</span> | |
| <span class="normal"> </span><span class="function">A_CLASS</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">b_parent_class</span><span class="symbol">)-></span><span class="function">method_to_call</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">obj</span><span class="symbol">,</span><span class="normal"> some_param</span><span class="symbol">);</span> | |
| <span class="normal"> </span><span class="comment">/* do stuff after chain up */</span> | |
| <span class="cbracket">}</span></pre></td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| </div> | |
| <p> | |
| </p> | |
| <div class="footnotes"> | |
| <br><hr style="width:100; text-align:left;margin-left: 0"> | |
| <div id="ftn.id-1.6.3.9.3.1" class="footnote"><p><a href="#id-1.6.3.9.3.1" class="para"><sup class="para">[7] </sup></a> | |
| The <span class="emphasis"><em>original</em></span> adjective used in this sentence is not innocuous. To fully | |
| understand its meaning, recall how class structures are initialized: for each object type, | |
| the class structure associated with this object is created by first copying the class structure of its | |
| parent type (a simple <code class="function">memcpy</code>) and then by invoking the <code class="function">class_init</code> callback on | |
| the resulting class structure. Since the <code class="function">class_init</code> callback is responsible for overwriting the class structure | |
| with the user re-implementations of the class methods, the modified copy of the parent class | |
| structure stored in the derived instance cannot be used. A copy of the class structure of an instance of the parent | |
| class is needed. | |
| </p></div> | |
| </div> | |
| </div> | |
| <div class="footer"> | |
| <hr>Generated by GTK-Doc V1.25.1</div> | |
| </body> | |
| </html> |