~ubuntu-branches/ubuntu/intrepid/glib2.0/intrepid-updates

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge, Loic Minier, Sebastian Dröge
  • Date: 2008-07-22 11:17:05 UTC
  • mfrom: (1.2.44 upstream)
  • Revision ID: james.westby@ubuntu.com-20080722111705-0ejknwv7o1dyxmjj
Tags: 2.17.4-1
[ Loic Minier ]
* List back m68k in arches where we could make the testsuite fatal,
  following the update on GNOME #481575.
* Document why testsuite is currently completely disabled (fails when
  there's no writable $HOME).

[ Sebastian Dröge ]
* New upstream development release, the new API might still change:
  + debian/rules,
    debian/libglib2.0-0.symbols:
    - Updated for the new symbols.
  + debian/patches/90_gio-nautilus-crash.patch:
    - Dropped, merged upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
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">
59
60
<a name="howto-interface-define"></a>How to define interfaces</h2></div></div></div>
60
61
<p>
61
62
    The bulk of interface definition has already been shown in <a class="xref" href="gtype-non-instantiable-classed.html" title="Non-instantiable classed types: interfaces">the section called “Non-instantiable classed types: interfaces”</a>
62
 
    but I feel it is needed to show exactly how to create an interface. The sample source code
63
 
    associated to this section can be found in the documentation's source tarball, in the 
64
 
    <code class="filename">sample/interface/maman-ibaz.{h|c}</code> file.
 
63
    but I feel it is needed to show exactly how to create an interface.
65
64
  </p>
66
65
<p>
67
66
    As above, the first step is to get the header right:
68
67
</p>
69
68
<pre class="programlisting">
70
 
#ifndef MAMAN_IBAZ_H
71
 
#define MAMAN_IBAZ_H
 
69
#ifndef __MAMAN_IBAZ_H__
 
70
#define __MAMAN_IBAZ_H__
72
71
 
73
72
#include &lt;glib-object.h&gt;
74
73
 
75
 
#define MAMAN_TYPE_IBAZ                (maman_ibaz_get_type ())
76
 
#define MAMAN_IBAZ(obj)                (G_TYPE_CHECK_INSTANCE_CAST ((obj), MAMAN_TYPE_IBAZ, MamanIbaz))
77
 
#define MAMAN_IS_IBAZ(obj)             (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MAMAN_TYPE_IBAZ))
78
 
#define MAMAN_IBAZ_GET_INTERFACE(inst) (G_TYPE_INSTANCE_GET_INTERFACE ((inst), MAMAN_TYPE_IBAZ, MamanIbazInterface))
79
 
 
80
 
 
81
 
typedef struct _MamanIbaz MamanIbaz; /* dummy object */
82
 
typedef struct _MamanIbazInterface MamanIbazInterface;
83
 
 
84
 
struct _MamanIbazInterface {
85
 
  GTypeInterface parent;
 
74
#define MAMAN_TYPE_IBAZ                 (maman_ibaz_get_type ())
 
75
#define MAMAN_IBAZ(obj)                 (G_TYPE_CHECK_INSTANCE_CAST ((obj), MAMAN_TYPE_IBAZ, MamanIbaz))
 
76
#define MAMAN_IS_IBAZ(obj)              (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MAMAN_TYPE_IBAZ))
 
77
#define MAMAN_IBAZ_GET_INTERFACE(inst)  (G_TYPE_INSTANCE_GET_INTERFACE ((inst), MAMAN_TYPE_IBAZ, MamanIbazInterface))
 
78
 
 
79
 
 
80
typedef struct _MamanIbaz               MamanIbaz; /* dummy object */
 
81
typedef struct _MamanIbazInterface      MamanIbazInterface;
 
82
 
 
83
struct _MamanIbazInterface
 
84
{
 
85
  GTypeInterface parent_iface;
86
86
 
87
87
  void (*do_action) (MamanIbaz *self);
88
88
};
91
91
 
92
92
void maman_ibaz_do_action (MamanIbaz *self);
93
93
 
94
 
#endif /*MAMAN_IBAZ_H*/
 
94
#endif /* __MAMAN_IBAZ_H__ */
95
95
</pre>
96
96
<p>
97
97
    This code is the same as the code for a normal <span class="type"><a class="link" href="gobject-Type-Information.html#GType" title="GType">GType</a></span>
104
104
                  but with <code class="function">G_TYPE_INSTANCE_GET_INTERFACE</code>.
105
105
      </p></li>
106
106
<li><p>
107
 
        The instance type, <span class="type">MamanIbaz</span> is not fully defined: it is used merely as an abstract 
108
 
        type which represents an instance of whatever object which implements the interface.
 
107
        The instance type, <span class="type">MamanIbaz</span> is not fully defined: it is
 
108
        used merely as an abstract type which represents an instance of
 
109
        whatever object which implements the interface.
 
110
      </p></li>
 
111
<li><p>
 
112
        The parent of the <span class="type">MamanIbazInterface</span> is not
 
113
        <span class="type">GObjectClass</span> but <span class="type">GTypeInterface</span>.
109
114
      </p></li>
110
115
</ul></div>
111
116
<p>
124
129
      <a class="xref" href="gtype-non-instantiable-classed.html#gtype-non-instantiable-classed-init" title="Interface Initialization">the section called “Interface Initialization”</a>, 
125
130
      <code class="function">base_init</code> is run once for each interface implementation 
126
131
      instantiation)</p></li>
127
 
<li><p><code class="function">maman_ibaz_do_action</code> dereferences the class
128
 
      structure to access its associated class function and calls it.
 
132
<li><p><code class="function">maman_ibaz_do_action</code> dereferences
 
133
      the class structure to access its associated class function and calls it.
129
134
      </p></li>
130
135
</ul></div>
131
136
<p>
134
139
static void
135
140
maman_ibaz_base_init (gpointer g_class)
136
141
{
137
 
  static gboolean initialized = FALSE;
138
 
 
139
 
  if (!initialized) {
140
 
    /* create interface signals here. */
141
 
    initialized = TRUE;
142
 
  }
 
142
  static gboolean is_initialized = FALSE;
 
143
 
 
144
  if (!is_initialized)
 
145
    {
 
146
      /* add properties and signals to the interface here */
 
147
 
 
148
      is_initialized = TRUE;
 
149
    }
143
150
}
144
151
 
145
152
GType
146
153
maman_ibaz_get_type (void)
147
154
{
148
 
  static GType type = 0;
149
 
  if (type == 0) {
150
 
    static const GTypeInfo info = {
151
 
      sizeof (MamanIbazInterface),
152
 
      maman_ibaz_base_init,   /* base_init */
153
 
      NULL,   /* base_finalize */
154
 
      NULL,   /* class_init */
155
 
      NULL,   /* class_finalize */
156
 
      NULL,   /* class_data */
157
 
      0,
158
 
      0,      /* n_preallocs */
159
 
      NULL    /* instance_init */
160
 
    };
161
 
    type = g_type_register_static (G_TYPE_INTERFACE, "MamanIbaz", &amp;info, 0);
162
 
  }
 
155
  static GType iface_type = 0;
 
156
  if (iface_type == 0)
 
157
    {
 
158
      static const GTypeInfo info = {
 
159
        sizeof (MamanIbazInterface),
 
160
        maman_ibaz_base_init,   /* base_init */
 
161
        NULL,   /* base_finalize */
 
162
      };
 
163
 
 
164
      iface_type = g_type_register_static (G_TYPE_INTERFACE, "MamanIbaz",
 
165
                                           &amp;info, 0);
 
166
    }
 
167
 
163
168
  return type;
164
169
}
165
170
 
166
 
void maman_ibaz_do_action (MamanIbaz *self)
 
171
void
 
172
maman_ibaz_do_action (MamanIbaz *self)
167
173
{
 
174
  g_return_if_fail (MAMAN_IS_IBAZ (self));
 
175
 
168
176
  MAMAN_IBAZ_GET_INTERFACE (self)-&gt;do_action (self);
169
177
}
170
178
</pre>