~ubuntu-branches/debian/squeeze/glib2.0/squeeze

« back to all changes in this revision

Viewing changes to docs/reference/gobject/html/howto-interface-implement.html

  • Committer: Bazaar Package Importer
  • Author(s): Gustavo Noronha Silva
  • Date: 2009-02-15 13:00:43 UTC
  • mfrom: (1.3.1 upstream) (69.1.10 intrepid)
  • Revision ID: james.westby@ubuntu.com-20090215130043-q47fbt3owmt42m2f
Tags: 2.18.4-2
* Release to unstable
* debian/rules:
- bump SHVER, since we are already forcing a 2.18.0 dependecy on the
  symbols introduced in the development versions
* debian/control.in:
- added Homepage and Vcs-* control fields

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2
2
<html>
3
3
<head>
4
 
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
 
4
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5
5
<title>How To define implement an Interface?</title>
6
 
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2">
7
 
<link rel="start" href="index.html" title="GObject Reference Manual">
 
6
<meta name="generator" content="DocBook XSL Stylesheets V1.74.0">
 
7
<link rel="home" href="index.html" title="GObject Reference Manual">
8
8
<link rel="up" href="howto-interface.html" title="How to define and implement interfaces">
9
9
<link rel="prev" href="howto-interface.html" title="How to define and implement interfaces">
10
10
<link rel="next" href="ch06s03.html" title="Interface definition prerequisites">
11
 
<meta name="generator" content="GTK-Doc V1.9 (XML mode)">
 
11
<meta name="generator" content="GTK-Doc V1.11 (XML mode)">
12
12
<link rel="stylesheet" href="style.css" type="text/css">
13
13
<link rel="preface" href="pr01.html" title="Introduction">
14
 
<link rel="part" href="pt01.html" title="Part&#160;I.&#160;Concepts">
 
14
<link rel="part" href="pt01.html" title="Part I. Concepts">
15
15
<link rel="chapter" href="chapter-intro.html" title="Background">
16
16
<link rel="chapter" href="chapter-gtype.html" title="The GLib Dynamic Type System">
17
17
<link rel="chapter" href="chapter-gobject.html" title="The GObject base class">
18
18
<link rel="chapter" href="chapter-signal.html" title="The GObject messaging system">
19
19
<link rel="reference" href="rn01.html" title="API Reference">
20
20
<link rel="reference" href="rn02.html" title="Tools Reference">
21
 
<link rel="part" href="pt02.html" title="Part&#160;IV.&#160;Tutorial">
 
21
<link rel="part" href="pt02.html" title="Part IV. Tutorial">
22
22
<link rel="chapter" href="howto-gobject.html" title="How to define and implement a new GObject">
23
23
<link rel="chapter" href="howto-interface.html" title="How to define and implement interfaces">
24
24
<link rel="chapter" href="howto-signals.html" title="How to create and use signals">
25
 
<link rel="part" href="pt03.html" title="Part&#160;V.&#160;Related Tools">
 
25
<link rel="part" href="pt03.html" title="Part V. Related Tools">
 
26
<link rel="chapter" href="tools-vala.html" title="Vala">
26
27
<link rel="chapter" href="tools-gob.html" title="GObject builder">
27
28
<link rel="chapter" href="tools-ginspector.html" title="Graphical inspection of GObjects">
28
29
<link rel="chapter" href="tools-refdb.html" title="Debugging reference count problems">
36
37
<link rel="index" href="ix07.html" title="Index of new symbols in 2.10">
37
38
<link rel="index" href="ix08.html" title="Index of new symbols in 2.12">
38
39
<link rel="index" href="ix09.html" title="Index of new symbols in 2.14">
 
40
<link rel="index" href="ix10.html" title="Index of new symbols in 2.14">
39
41
</head>
40
42
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
41
43
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
49
51
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
50
52
<a name="howto-interface-implement"></a>How To define implement an Interface?</h2></div></div></div>
51
53
<p>
52
 
      Once the interface is defined, implementing it is rather trivial. Source code showing how to do this
53
 
      for the <span class="type">IBaz</span> interface defined in the previous section is located in 
54
 
      <code class="filename">sample/interface/maman-baz.{h|c}</code>.
 
54
      Once the interface is defined, implementing it is rather trivial.
55
55
    </p>
56
56
<p>
57
 
      The first step is to define a normal GType. Here, we have decided to use a GType which derives from
58
 
      GObject. Its name is <span class="type">MamanBaz</span>:
 
57
      The first step is to define a normal GObject class, like:
59
58
</p>
60
59
<pre class="programlisting">
61
 
#ifndef MAMAN_BAZ_H
62
 
#define MAMAN_BAZ_H
 
60
#ifndef __MAMAN_BAZ_H__
 
61
#define __MAMAN_BAZ_H__
63
62
 
64
63
#include &lt;glib-object.h&gt;
65
64
 
66
65
#define MAMAN_TYPE_BAZ             (maman_baz_get_type ())
67
66
#define MAMAN_BAZ(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), MAMAN_TYPE_BAZ, Mamanbaz))
68
 
#define MAMAN_BAZ_CLASS(vtable)    (G_TYPE_CHECK_CLASS_CAST ((vtable), MAMAN_TYPE_BAZ, MamanbazClass))
69
67
#define MAMAN_IS_BAZ(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MAMAN_TYPE_BAZ))
70
 
#define MAMAN_IS_BAZ_CLASS(vtable) (G_TYPE_CHECK_CLASS_TYPE ((vtable), MAMAN_TYPE_BAZ))
71
 
#define MAMAN_BAZ_GET_CLASS(inst)  (G_TYPE_INSTANCE_GET_CLASS ((inst), MAMAN_TYPE_BAZ, MamanbazClass))
72
 
 
73
 
 
74
 
typedef struct _MamanBaz MamanBaz;
75
 
typedef struct _MamanBazClass MamanBazClass;
76
 
 
77
 
struct _MamanBaz {
78
 
  GObject parent;
 
68
#define MAMAN_BAZ_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), MAMAN_TYPE_BAZ, MamanbazClass))
 
69
#define MAMAN_IS_BAZ_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), MAMAN_TYPE_BAZ))
 
70
#define MAMAN_BAZ_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), MAMAN_TYPE_BAZ, MamanbazClass))
 
71
 
 
72
 
 
73
typedef struct _MamanBaz        MamanBaz;
 
74
typedef struct _MamanBazClass   MamanBazClass;
 
75
 
 
76
struct _MamanBaz
 
77
{
 
78
  GObject parent_instance;
 
79
 
79
80
  int instance_member;
80
81
};
81
82
 
82
 
struct _MamanBazClass {
83
 
  GObjectClass parent;
 
83
struct _MamanBazClass
 
84
{
 
85
  GObjectClass parent_class;
84
86
};
85
87
 
86
88
GType maman_baz_get_type (void);
87
89
 
88
 
 
89
 
#endif //MAMAN_BAZ_H
90
 
</pre>
91
 
<p>
92
 
      There is clearly nothing specifically weird or scary about this header: it does not define any weird API
93
 
      or derives from a weird type.
94
 
    </p>
95
 
<p>
96
 
      The second step is to implement <code class="function">maman_baz_get_type</code>:
97
 
</p>
98
 
<pre class="programlisting">
99
 
GType 
100
 
maman_baz_get_type (void)
101
 
{
102
 
  static GType type = 0;
103
 
  if (type == 0) {
104
 
    static const GTypeInfo info = {
105
 
      sizeof (MamanBazClass),
106
 
      NULL,   /* base_init */
107
 
      NULL,   /* base_finalize */
108
 
      NULL,   /* class_init */
109
 
      NULL,   /* class_finalize */
110
 
      NULL,   /* class_data */
111
 
      sizeof (MamanBaz),
112
 
      0,      /* n_preallocs */
113
 
      baz_instance_init    /* instance_init */
114
 
    };
115
 
    static const GInterfaceInfo ibaz_info = {
116
 
      (GInterfaceInitFunc) baz_interface_init,    /* interface_init */
117
 
      NULL,               /* interface_finalize */
118
 
      NULL                /* interface_data */
119
 
    };
120
 
    type = g_type_register_static (G_TYPE_OBJECT,
121
 
                                   "MamanBazType",
122
 
                                   &amp;info, 0);
123
 
    g_type_add_interface_static (type,
124
 
                                 MAMAN_TYPE_IBAZ,
125
 
                                 &amp;ibaz_info);
126
 
  }
127
 
  return type;
128
 
}
129
 
</pre>
130
 
<p>
131
 
      This function is very much like all the similar functions we looked at previously. The only interface-specific
132
 
      code present here is the call to <code class="function"><a class="link" href="gobject-Type-Information.html#g-type-add-interface-static">g_type_add_interface_static</a></code> which is used to inform
133
 
      the type system that this just-registered <span class="type"><a class="link" href="gobject-Type-Information.html#GType">GType</a></span> also implements the interface 
134
 
      <code class="function">MAMAN_TYPE_IBAZ</code>.
135
 
    </p>
136
 
<p>
137
 
      <code class="function">baz_interface_init</code>, the interface initialization function, is also pretty simple:
138
 
</p>
139
 
<pre class="programlisting">
140
 
static void baz_do_action (MamanBaz *self)
141
 
{
142
 
  g_print ("Baz implementation of IBaz interface Action: 0x%x.\n", self-&gt;instance_member);
143
 
}
144
 
static void
145
 
baz_interface_init (gpointer   g_iface,
146
 
                    gpointer   iface_data)
147
 
{
148
 
  MamanIbazInteface *iface = (MamanIbazInteface *)g_iface;
149
 
  iface-&gt;do_action = (void (*) (MamanIbaz *self))baz_do_action;
150
 
}
151
 
static void
152
 
baz_instance_init (GTypeInstance   *instance,
153
 
                   gpointer         g_class)
154
 
{
155
 
  MamanBaz *self = MAMAN_BAZ(instance);
 
90
#endif /* __MAMAN_BAZ_H__ */
 
91
</pre>
 
92
<p>
 
93
      There is clearly nothing specifically weird or scary about this header:
 
94
      it does not define any weird API or derives from a weird type.
 
95
    </p>
 
96
<p>
 
97
      The second step is to implement <span class="type">MamanBaz</span> by defining
 
98
      its GType. Instead of using <code class="function">G_DEFINE_TYPE</code> we
 
99
      use <code class="function">G_DEFINE_TYPE_WITH_CODE</code> and the
 
100
      <code class="function">G_IMPLEMENT_INTERFACE</code> macros.
 
101
</p>
 
102
<pre class="programlisting">
 
103
static void maman_ibaz_interface_init (MamanIbazInterface *iface);
 
104
 
 
105
G_DEFINE_TYPE_WITH_CODE (MamanBar, maman_bar, G_TYPE_OBJECT,
 
106
                         G_IMPLEMENT_INTERFACE (MAMAN_TYPE_IBAZ,
 
107
                                                maman_ibaz_interface_init));
 
108
</pre>
 
109
<p>
 
110
      This definition is very much like all the similar functions we looked
 
111
      at previously. The only interface-specific code present here is the call
 
112
      to <code class="function">G_IMPLEMENT_INTERFACE</code>. 
 
113
    </p>
 
114
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
 
115
<h3 class="title">Note</h3>
 
116
<p>Classes can implement multiple interfaces by using multiple
 
117
    calls to <code class="function">G_IMPLEMENT_INTERFACE</code> inside the call
 
118
    to <code class="function">G_DEFINE_TYPE_WITH_CODE</code>.</p>
 
119
</div>
 
120
<p>
 
121
      <code class="function">maman_baz_interface_init</code>, the interface
 
122
      initialization function: inside it every virtual method of the interface
 
123
      must be assigned to its implementation:
 
124
</p>
 
125
<pre class="programlisting">
 
126
static void
 
127
maman_baz_do_action (MamanBaz *self)
 
128
{
 
129
  g_print ("Baz implementation of IBaz interface Action: 0x%x.\n",
 
130
           self-&gt;instance_member);
 
131
}
 
132
 
 
133
static void
 
134
maman_ibaz_interface_init (MamanIbazInterface *iface)
 
135
{
 
136
  iface-&gt;do_action = baz_do_action;
 
137
}
 
138
 
 
139
static void
 
140
maman_baz_init (MamanBaz *self)
 
141
{
 
142
  MamanBaz *self = MAMAN_BAZ (instance);
156
143
  self-&gt;instance_member = 0xdeadbeaf;
157
144
}
158
145
</pre>
159
146
<p>
160
 
      <code class="function">baz_interface_init</code> merely initializes the interface methods to the implementations
161
 
      defined by <span class="type">MamanBaz</span>: <code class="function">maman_baz_do_action</code> does nothing very useful 
162
 
      but it could :)
163
147
    </p>
164
148
</div>
 
149
<div class="footer">
 
150
<hr>
 
151
          Generated by GTK-Doc V1.11</div>
165
152
</body>
166
153
</html>