KGRKJGETMRETU895U-589TY5MIGM5JGB5SDFESFREWTGR54TY
Server : Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/0.9.8e-fips-rhel5 DAV/2 PHP/5.2.17
System : Linux localhost 2.6.18-419.el5 #1 SMP Fri Feb 24 22:47:42 UTC 2017 x86_64
User : nobody ( 99)
PHP Version : 5.2.17
Disable Function : NONE
Directory :  /usr/share/gtk-doc/html/gobject/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //usr/share/gtk-doc/html/gobject/howto-gobject-chainup.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Chaining up</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.69.1">
<link rel="start" 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.6 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="preface" href="pr01.html" title="Introduction">
<link rel="part" href="pt01.html" title="Part&#160;I.&#160;Concepts">
<link rel="chapter" href="ch01.html" title="Background">
<link rel="chapter" href="ch02.html" title="The Glib Dynamic Type System">
<link rel="chapter" href="chapter-gobject.html" title="The GObject base class">
<link rel="chapter" href="chapter-signal.html" title="The GObject messaging system">
<link rel="reference" href="rn01.html" title="API Reference">
<link rel="reference" href="rn02.html" title="Tools Reference">
<link rel="part" href="pt02.html" title="Part&#160;IV.&#160;Tutorial">
<link rel="chapter" href="howto-gobject.html" title="How To define and implement a new GObject?">
<link rel="chapter" href="howto-interface.html" title="How To define and implement Interfaces?">
<link rel="chapter" href="howto-signals.html" title="Howto create and use signals">
<link rel="part" href="pt03.html" title="Part&#160;V.&#160;Related Tools">
<link rel="chapter" href="tools-gob.html" title="GObject builder">
<link rel="chapter" href="tools-ginspector.html" title="Graphical inspection of Gobjects">
<link rel="chapter" href="tools-refdb.html" title="Debugging reference count problems">
<link rel="chapter" href="tools-gtkdoc.html" title="Writing API docs">
<link rel="index" href="ix01.html" title="Index">
<link rel="index" href="ix02.html" title="Index of deprecated symbols">
<link rel="index" href="ix03.html" title="Index of new symbols in 2.2">
<link rel="index" href="ix04.html" title="Index of new symbols in 2.4">
<link rel="index" href="ix05.html" title="Index of new symbols in 2.6">
<link rel="index" href="ix06.html" title="Index of new symbols in 2.8">
<link rel="index" href="ix07.html" title="Index of new symbols in 2.10">
<link rel="index" href="ix08.html" title="Index of new symbols in 2.12">
</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="2"><tr valign="middle">
<td><a accesskey="p" href="howto-gobject-methods.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
<td><a accesskey="u" href="howto-gobject.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
<th width="100%" align="center">GObject Reference Manual</th>
<td><a accesskey="n" href="howto-interface.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
</tr></table>
<div class="sect1" lang="en">
<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 type="disc">
<li><p>Parent class A defines a public virtual method named <code class="function">foo</code> and 
         provides a default implementation.</p></li>
<li><p>Child class B re-implements method <code class="function">foo</code>.</p></li>
<li><p>In the method B::foo, the child class B calls its parent class method A::foo.</p></li>
</ul></div>
<p>
       There are many uses to this idiom:
       </p>
<div class="itemizedlist"><ul type="disc">
<li><p>You need to change 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
           slightly and chain up to ensure that the previous behaviour is not really modifed, just extended.
           </p></li>
<li><p>You are lazy, you have access to the source code of the parent class but you don't want 
           to modify it to add method calls to new specialized method calls: it is faster to hack the child class
           to chain up than to modify the parent to call down.</p></li>
<li><p>You need to implement the Chain Of Responsability pattern: each object of the inheritance
           tree chains up to its parent (typically, at the begining or the end of the method) to ensure that
           they each handler is run in turn.</p></li>
</ul></div>
<p>
       I am personally not really convinced any of the last two uses are really a good idea but since this
       programming idiom is often used, this section attemps to explain how to implement it.
     </p>
<p>To explicitely 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 class function pointer and invoke it directly.
        <sup>[<a name="id2805766" href="#ftn.id2805766">12</a>]</sup>
     </p>
<p>The function <code class="function"><a href="gobject-Type-Information.html#g-type-class-peek-parent">g_type_class_peek_parent</a></code> is used to access the original parent 
      class structure. Its input is a pointer to the class of the derived object and it returns a pointer
      to the original parent class structure. The code below shows how you could use it:
</p>
<pre class="programlisting">
static void
b_method_to_call (B *obj, int a)
{
  BClass *klass;
  AClass *parent_class;
  klass = B_GET_CLASS (obj);
  parent_class = g_type_class_peek_parent (klass);

  /* do stuff before chain up */
  parent_class-&gt;method_to_call (obj, a);
  /* do stuff after chain up */
}
</pre>
<p>
     A lot of people who use this idiom in GTK+ store the parent class structure pointer in a global static 
     variable to avoid the costly call to <code class="function"><a href="gobject-Type-Information.html#g-type-class-peek-parent">g_type_class_peek_parent</a></code> for each function call.
     Typically, the class_init callback initializes the global static variable. <code class="filename">gtk/gtkhscale.c</code>
     does this.
    </p>
<div class="footnotes">
<br><hr width="100" align="left">
<div class="footnote"><p><sup>[<a name="ftn.id2805766" href="#id2805766">12</a>] </sup>The <span class="emphasis"><em>original</em></span> adjective used in this sentence is not innocuous. To fully 
           understand its meaning, you need to recall how class structures are initialized: for each object type,
           the class structure associated to 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 class_init callback on 
           the resulting class structure. Since the class_init callback is responsible for overwriting the class structure
           with the user re-implementations of the class methods, we cannot merely use the modified copy of the parent class
           structure stored in our derived instance. We want to get a copy of the class structure of an instance of the parent 
           class.
          </p></div>
</div>
</div>
</body>
</html>

Anon7 - 2021