1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <title>How to define and implement interfaces</title>
6 <meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
7 <link rel="home" href="index.html" title="GObject Reference Manual">
8 <link rel="up" href="pt02.html" title="Part IV. Tutorial">
9 <link rel="prev" href="howto-gobject-chainup.html" title="Chaining up">
10 <link rel="next" href="howto-interface-implement.html" title="How To define implement an Interface?">
11 <meta name="generator" content="GTK-Doc V1.13 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
13 <link rel="preface" href="pr01.html" title="Introduction">
14 <link rel="part" href="pt01.html" title="Part I. Concepts">
15 <link rel="chapter" href="chapter-intro.html" title="Background">
16 <link rel="chapter" href="chapter-gtype.html" title="The GLib Dynamic Type System">
17 <link rel="chapter" href="chapter-gobject.html" title="The GObject base class">
18 <link rel="chapter" href="chapter-signal.html" title="The GObject messaging system">
19 <link rel="reference" href="rn01.html" title="API Reference">
20 <link rel="reference" href="rn02.html" title="Tools Reference">
21 <link rel="part" href="pt02.html" title="Part IV. Tutorial">
22 <link rel="chapter" href="howto-gobject.html" title="How to define and implement a new GObject">
23 <link rel="chapter" href="howto-interface.html" title="How to define and implement interfaces">
24 <link rel="chapter" href="howto-signals.html" title="How to create and use signals">
25 <link rel="part" href="pt03.html" title="Part V. Related Tools">
26 <link rel="chapter" href="tools-vala.html" title="Vala">
27 <link rel="chapter" href="tools-gob.html" title="GObject builder">
28 <link rel="chapter" href="tools-ginspector.html" title="Graphical inspection of GObjects">
29 <link rel="chapter" href="tools-refdb.html" title="Debugging reference count problems">
30 <link rel="chapter" href="tools-gtkdoc.html" title="Writing API docs">
31 <link rel="index" href="api-index-full.html" title="Index">
32 <link rel="index" href="api-index-deprecated.html" title="Index of deprecated symbols">
33 <link rel="index" href="api-index-2-2.html" title="Index of new symbols in 2.2">
34 <link rel="index" href="api-index-2-4.html" title="Index of new symbols in 2.4">
35 <link rel="index" href="api-index-2-6.html" title="Index of new symbols in 2.6">
36 <link rel="index" href="api-index-2-8.html" title="Index of new symbols in 2.8">
37 <link rel="index" href="api-index-2-10.html" title="Index of new symbols in 2.10">
38 <link rel="index" href="api-index-2-12.html" title="Index of new symbols in 2.12">
39 <link rel="index" href="api-index-2-14.html" title="Index of new symbols in 2.14">
40 <link rel="index" href="api-index-2-18.html" title="Index of new symbols in 2.18">
41 <link rel="index" href="api-index-2-22.html" title="Index of new symbols in 2.22">
42 <link rel="index" href="api-index-2-24.html" title="Index of new symbols in 2.24">
44 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
45 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
46 <td><a accesskey="p" href="howto-gobject-chainup.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
47 <td><a accesskey="u" href="pt02.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
48 <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
49 <th width="100%" align="center">GObject Reference Manual</th>
50 <td><a accesskey="n" href="howto-interface-implement.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
52 <div class="chapter" title="How to define and implement interfaces">
53 <div class="titlepage"><div><div><h2 class="title">
54 <a name="howto-interface"></a>How to define and implement interfaces</h2></div></div></div>
56 <dt><span class="sect1"><a href="howto-interface.html#howto-interface-define">How to define interfaces</a></span></dt>
57 <dt><span class="sect1"><a href="howto-interface-implement.html">How To define implement an Interface?</a></span></dt>
58 <dt><span class="sect1"><a href="ch06s03.html">Interface definition prerequisites</a></span></dt>
59 <dt><span class="sect1"><a href="howto-interface-properties.html">Interface Properties</a></span></dt>
61 <div class="sect1" title="How to define interfaces">
62 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
63 <a name="howto-interface-define"></a>How to define interfaces</h2></div></div></div>
65 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>
66 but I feel it is needed to show exactly how to create an interface.
69 As above, the first step is to get the header right:
71 <pre class="programlisting">
72 #ifndef __MAMAN_IBAZ_H__
73 #define __MAMAN_IBAZ_H__
75 #include <glib-object.h>
77 #define MAMAN_TYPE_IBAZ (maman_ibaz_get_type ())
78 #define MAMAN_IBAZ(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MAMAN_TYPE_IBAZ, MamanIbaz))
79 #define MAMAN_IS_IBAZ(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MAMAN_TYPE_IBAZ))
80 #define MAMAN_IBAZ_GET_INTERFACE(inst) (G_TYPE_INSTANCE_GET_INTERFACE ((inst), MAMAN_TYPE_IBAZ, MamanIbazInterface))
83 typedef struct _MamanIbaz MamanIbaz; /* dummy object */
84 typedef struct _MamanIbazInterface MamanIbazInterface;
86 struct _MamanIbazInterface
88 GTypeInterface parent_iface;
90 void (*do_action) (MamanIbaz *self);
93 GType maman_ibaz_get_type (void);
95 void maman_ibaz_do_action (MamanIbaz *self);
97 #endif /* __MAMAN_IBAZ_H__ */
100 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>
101 which derives from a <span class="type"><a class="link" href="gobject-The-Base-Object-Type.html#GObject">GObject</a></span> except for a few details:
103 <div class="itemizedlist"><ul class="itemizedlist" type="disc">
104 <li class="listitem"><p>
105 The <code class="function">_GET_CLASS</code> macro is called <code class="function">_GET_INTERFACE</code>
106 and not implemented with <code class="function">G_TYPE_INSTANCE_GET_CLASS</code>
107 but with <code class="function">G_TYPE_INSTANCE_GET_INTERFACE</code>.
109 <li class="listitem"><p>
110 The instance type, <span class="type">MamanIbaz</span> is not fully defined: it is
111 used merely as an abstract type which represents an instance of
112 whatever object which implements the interface.
114 <li class="listitem"><p>
115 The parent of the <span class="type">MamanIbazInterface</span> is not
116 <span class="type">GObjectClass</span> but <span class="type">GTypeInterface</span>.
122 The implementation of the <span class="type">MamanIbaz</span> type itself is trivial:
124 <div class="itemizedlist"><ul class="itemizedlist" type="disc">
125 <li class="listitem"><p><code class="function">maman_ibaz_get_type</code> registers the
126 type in the type system.
128 <li class="listitem"><p><code class="function">maman_ibaz_base_init</code> is expected
129 to register the interface's signals if there are any (we will see a bit
130 (later how to use them). Make sure to use a static local boolean variable
131 to make sure not to run the initialization code twice (as described in
132 <a class="xref" href="gtype-non-instantiable-classed.html#gtype-non-instantiable-classed-init" title="Interface Initialization">the section called “Interface Initialization”</a>,
133 <code class="function">base_init</code> is run once for each interface implementation
134 instantiation)</p></li>
135 <li class="listitem"><p><code class="function">maman_ibaz_do_action</code> dereferences
136 the class structure to access its associated class function and calls it.
141 <pre class="programlisting">
143 maman_ibaz_base_init (gpointer g_class)
145 static gboolean is_initialized = FALSE;
149 /* add properties and signals to the interface here */
151 is_initialized = TRUE;
156 maman_ibaz_get_type (void)
158 static GType iface_type = 0;
161 static const GTypeInfo info = {
162 sizeof (MamanIbazInterface),
163 maman_ibaz_base_init, /* base_init */
164 NULL, /* base_finalize */
167 iface_type = g_type_register_static (G_TYPE_INTERFACE, "MamanIbaz",
175 maman_ibaz_do_action (MamanIbaz *self)
177 g_return_if_fail (MAMAN_IS_IBAZ (self));
179 MAMAN_IBAZ_GET_INTERFACE (self)->do_action (self);
188 Generated by GTK-Doc V1.13</div>