~ubuntu-branches/ubuntu/saucy/glib2.0/saucy-proposed

« back to all changes in this revision

Viewing changes to docs/reference/gobject/html/howto-gobject-chainup.html

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2013-06-25 12:43:56 UTC
  • mfrom: (1.63.22)
  • Revision ID: package-import@ubuntu.com-20130625124356-597xf4lpikauchiy
Tags: 2.37.3-1ubuntu1
* Resynchronise on unrelease Debian SVN (glib is in NEW there). Remaining
  change:
  - Build-Depend on python:any for cross building.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
<link rel="up" href="howto-gobject.html" title="How to define and implement a new GObject">
9
9
<link rel="prev" href="howto-gobject-methods.html" title="Object methods">
10
10
<link rel="next" href="howto-interface.html" title="How to define and implement interfaces">
11
 
<meta name="generator" content="GTK-Doc V1.18.1 (XML mode)">
 
11
<meta name="generator" content="GTK-Doc V1.19 (XML mode)">
12
12
<link rel="stylesheet" href="style.css" type="text/css">
13
13
</head>
14
14
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
32
32
<li class="listitem"><p>In the method B::foo, the child class B calls its parent class method A::foo.</p></li>
33
33
</ul></div>
34
34
<p>
35
 
      There are many uses to this idiom:
 
35
      There are various uses to this idiom:
36
36
      </p>
37
37
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
38
 
<li class="listitem"><p>You need to change the behaviour of a class without modifying its code. You create
 
38
<li class="listitem"><p>You need to extend the behaviour of a class without modifying its code. You create
39
39
          a subclass to inherit its implementation, re-implement a public virtual method to modify the behaviour
40
 
          slightly and chain up to ensure that the previous behaviour is not really modified, just extended.
 
40
          and chain up to ensure that the previous behaviour is not really modified, just extended.
41
41
          </p></li>
42
 
<li class="listitem"><p>You are lazy, you have access to the source code of the parent class but you don't want 
43
 
          to modify it to add method calls to new specialized method calls: it is faster to hack the child class
44
 
          to chain up than to modify the parent to call down.</p></li>
45
42
<li class="listitem"><p>You need to implement the Chain Of Responsibility pattern: each object of the inheritance
46
43
          tree chains up to its parent (typically, at the beginning or the end of the method) to ensure that
47
44
          they each handler is run in turn.</p></li>
48
45
</ul></div>
49
46
<p>
50
 
      I am personally not really convinced any of the last two uses are really a good idea but since this
51
 
      programming idiom is often used, this section attempts to explain how to implement it.
52
47
    </p>
53
48
<p>
54
49
      To explicitly chain up to the implementation of the virtual method in the parent class, 
56
51
      access the original class function pointer and invoke it directly.
57
52
      <a href="#ftn.id-1.6.3.8.3.1" class="footnote" name="id-1.6.3.8.3.1"><sup class="footnote">[11]</sup></a>
58
53
    </p>
59
 
<p>The function <code class="function"><a class="link" href="gobject-Type-Information.html#g-type-class-peek-parent" title="g_type_class_peek_parent ()">g_type_class_peek_parent</a></code> is used to access the original parent 
60
 
    class structure. Its input is a pointer to the class of the derived object and it returns a pointer
61
 
    to the original parent class structure. The code below shows how you could use it:
 
54
<p>The function <code class="function"><a class="link" href="gobject-Type-Information.html#g-type-class-peek-parent" title="g_type_class_peek_parent ()">g_type_class_peek_parent</a></code>
 
55
    is used to access the original parent class structure. Its input is a
 
56
    pointer to the class of the derived object and it returns a pointer to
 
57
    the original parent class structure. Instead of using this function
 
58
    directly, though, you should use the <code class="function">parent_class</code>
 
59
    pointer created and initialized for us by the G_DEFINE_TYPE_* family of
 
60
    macros, for instance:
62
61
</p>
63
62
<pre class="programlisting">
64
63
static void
65
64
b_method_to_call (B *obj, int a)
66
65
{
67
 
  BClass *klass;
68
 
  AClass *parent_class;
69
 
 
70
 
  klass = B_GET_CLASS (obj);
71
 
  parent_class = g_type_class_peek_parent (klass);
72
 
 
73
66
  /* do stuff before chain up */
74
67
 
75
 
  parent_class-&gt;method_to_call (obj, a);
 
68
  /* call the method_to_call() virtual function on the
 
69
   * parent of BClass, AClass.
 
70
   *
 
71
   * remember the explicit cast to AClass*
 
72
   */
 
73
  A_CLASS (b_parent_class)-&gt;method_to_call (obj, a);
76
74
 
77
75
  /* do stuff after chain up */
78
76
}
95
93
</div>
96
94
<div class="footer">
97
95
<hr>
98
 
          Generated by GTK-Doc V1.18.1</div>
 
96
          Generated by GTK-Doc V1.19</div>
99
97
</body>
100
98
</html>
 
 
b'\\ No newline at end of file'